Skip to content

fix: size embeddings by the model's output embedding length - #2479

Open
0xArx wants to merge 1 commit into
abetlen:mainfrom
0xArx:fix/2478-embed-n-embd-out
Open

0xArx wants to merge 1 commit into
abetlen:mainfrom
0xArx:fix/2478-embed-n-embd-out

Conversation

@0xArx

@0xArx 0xArx commented Dec 4, 2026

Copy link
Copy Markdown

Llama.embed() sized its output with n_embd() (the hidden size), but llama.cpp writes embeddings with the model's output size, llama_model_n_embd_out. For EmbeddingGemma 2 (n_embd 512, n_embd_out 768) embed(), create_embedding() and /v1/embeddings returned only the first 512 of the 768 floats.

Changes:

  • LlamaModel.n_embd_out() and Llama.n_embd_out() wrap llama_model_n_embd_out (it falls back to n_embd in llama.cpp, so other models are unchanged).
  • embed() reads both the pooled and the per-token buffers with n_embd_out. I kept the local variable name n_embd so the change stays one line and does not conflict with fix: preserve sequence offsets in unpooled batched embeddings #2395, which fixes the per-sequence offset in the unpooled path.
  • New test_embed_uses_model_output_size drives embed() with a fake context whose n_embd_out differs from n_embd (pooled single and batched, unpooled). It fails on main ([0.0, 1.0] != [0.0, 1.0, 2.0]) and passes here. test_real_llama_embeddings now also checks len(embedding) == n_embd_out().

Testing (macOS arm64, Metal build of the vendored llama.cpp f5e6915):

  • ggml-org/embeddinggemma-2-GGUF embeddinggemma-2-Q8_0.gguf, the issue's repro: main returns 512 floats, this branch 768 (pooled, single and batched; unpooled rows are 768 wide). With fix: preserve sequence offsets in unpooled batched embeddings #2395's one-line offset fix applied on top, batched unpooled output also matches the single-input output.
  • pytest tests: 27 passed.
  • make lint (ruff check + format --check): clean.

Fixes #2478

@0xArx
0xArx force-pushed the fix/2478-embed-n-embd-out branch from 76949fc to 41aabe3 Compare December 4, 2026 21:11

This branch has not been deployed

No deployments
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.

embed() truncates EmbeddingGemma 2 output to n_embd (512) instead of n_embd_out (768)

1 participant