Skip to content

Studio: stop loading EmbeddingGemma in float16 in the RAG embedder - #12864

Merged
danielhanchen merged 3 commits into
unslothai:mainfrom
oobabooga:rag-embed-no-fp16
Oct 6, 2026
Merged

danielhanchen merged 3 commits into
unslothai:mainfrom
oobabooga:rag-embed-no-fp16

Conversation

@oobabooga

@oobabooga oobabooga commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

With RAG_EMBED_DEVICE=gpu, the sentence-transformers RAG embedder loads every model in float16. EmbeddingGemma does not support that. Its model card says:

EmbeddingGemma activations do not support float16. Please use float32 or bfloat16 as appropriate for your hardware.

With unsloth/embeddinggemma-300m on GPU, a document indexes without an error, and then every search of that knowledge base fails with a 500.

Root cause

  • embeddings._get picks the dtype from the device alone: "float32" if device == "cpu" else "float16".
  • In float16, EmbeddingGemma returns NaN for every vector, and nothing checks them before they are written to chunks_vec.
  • vec0 answers a query against those rows with a NULL distance, and store.search_dense computes 1.0 - r["distance"], which raises TypeError. In chat, build_rag_autoinject catches it and logs a warning, so the answer is generated without the documents.

Fix

  • _load_dtype(device, name) keeps float16 on an accelerator, except for models in _FLOAT16_UNSAFE_MODELS, matched as a substring of the lowercased model name. The list holds embeddinggemma. Those models load in bfloat16 where the device has native bf16 (native_bf16_supported, native_bf16_supported_xpu) and in float32 otherwise.
  • store.search_dense skips rows whose distance is NULL or not finite, so knowledge bases indexed in float16 stop failing the whole search.

Behavior that stays as on main:

  • The default RAG_EMBED_DEVICE=auto still loads on CPU in float32, every other model still loads in float16 on GPU, and the llama-server backend is untouched.
  • embedding_identity does not include the dtype, so existing NaN vectors stay in the index and are skipped. Re-uploading the same file does not replace them, because ingestion deduplicates by content hash; delete the document and upload it again.

A fine-tune of EmbeddingGemma whose name does not contain "embeddinggemma" is not matched and still loads in float16.

Testing

Live A/B on an RTX 6000 Ada, with RAG_EMBED_DEVICE=gpu RAG_EMBED_BACKEND=sentence-transformers RAG_EMBEDDING_MODEL=unsloth/embeddinggemma-300m. Each side created a knowledge base through the API, uploaded a three-sentence text file and searched "Where is the Eiffel Tower?".

Step Main This PR
Indexing job completed completed
Dense search 500, TypeError 200, the Eiffel Tower chunk
Hybrid search 500, TypeError 200, the same chunk
Hybrid search of the knowledge base indexed by main 500 200, the same chunk via its lexical hit

The last row ran on the first commit, which had the same search_dense change; the other rows were repeated on the final commit.

  • test_opted_in_accelerator_loads_float16 still passes unchanged. New test_float16_unsafe_model_avoids_float16 covers EmbeddingGemma on CUDA and XPU, with and without native bf16.
  • New test_dense_skips_nan_vectors_instead_of_raising fails on main with the same TypeError.
  • test_rag_store.py, test_rag_embeddings.py, test_rag_embedding_identity.py and test_rag_embed_llama_server.py: 222 passed.

Not tested:

  • ROCm, XPU, or an NVIDIA card without native bf16 (unit tests only).
  • Selecting EmbeddingGemma from Settings, which fails on main and here for a separate reason: its modules.json declares a 4_Normalize folder the repository does not have, so the download is never considered complete.

@oobabooga oobabooga changed the title Studio: stop loading the RAG embedder in float16 on GPU Studio: stop loading EmbeddingGemma in float16 in the RAG embedder Oct 6, 2026
@oobabooga

Copy link
Copy Markdown
Member Author

Reached Codex review convergence at oobabooga#337.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants