Embeddings or rerank scores from llama.cpp or Ollama don't match sentence-transformers? Find the cause
A correct GGUF served by llama-server gives the same vectors as sentence-transformers. We measured a cosine of
1.000000 on F32 GGUFs. When the numbers differ, the engine is usually computing something else. It may pool differently, skip the
query: prefix that sentence-transformers adds on its own, or read more tokens than the model's max_seq_length.
For rerankers, the GGUF may have lost part of the model. BERT cross-encoders converted before llama.cpp b12818 have no pooler
(#33109). Qwen3-Reranker GGUFs converted as a plain LLM have no head at all and return meaningless scores.
embedcheck.py sends the same inputs to your server and to the original model, and names the cause.
Corrected on 2026-12-05: this page said that Ollama's legacy /api/embeddings returns unnormalized
vectors. It depends on the model. The legacy endpoint passes on what Ollama's llama-server runner returns, and Ollama asks the runner for raw
vectors (embd_normalize: -1) only when legacyEmbeddingsWereRaw in llm/llama_server.go says so: when the GGUF has a pooling_type, for a
bert GGUF whose tokenizer.ggml.model is not bert or whose normalize_embeddings is false,
and for a nomic-bert GGUF unless normalize_embeddings is true. Otherwise the vector is L2-normalized, as llama-server
does by default. Read from the Ollama v0.40.21 source (the function is the same in v0.40.19). With Ollama v0.40.21 and two small BERT test
models with the BERT tokenizer (CLS and mean pooling), /api/embeddings, /api/embed and /v1/embeddings
all returned vectors of length 1.0; nomic-bert models were not run.
The six causes, and what each engine does
Each row is a setting where an engine can quietly compute something different from SentenceTransformer.encode() or
CrossEncoder.predict(). None of them raises an error. The only exceptions are the long-input refusals and llama-server's
Pooling type 'none' 400.
| Setting | sentence-transformers | llama-server (b12826) | Ollama (v0.40.19) | TEI (v1.10.0) | vLLM (v0.34.0) |
|---|---|---|---|---|---|
| Pooling | 1_Pooling/config.json | GGUF pooling_type, set by the converter from 1_Pooling. --pooling overrides it. No pooling_type: /v1/embeddings answers 400 Pooling type 'none' is not OAI compatible (measured) | GGUF pooling_type | 1_Pooling. If it is missing on a BERT: CLS with a warning. --pooling overrides it | 1_Pooling via modules.json. --pooler-config overrides it |
Prompt prefix (query: , search_query: ) | adds default_prompt_name from config_sentence_transformers.json on every encode() | never | never | only with --default-prompt-name or prompt_name in the request | only on the Cohere-style /v2/embed with input_type. Never on /v1/embeddings |
| Long input | cut at max_seq_length | no cut. Over -ub (default 512): HTTP 500 input (N tokens) is too large to process. increase the physical batch size (measured) | cut at the GGUF context_length (= max_position_embeddings) or num_ctx, not max_seq_length. "truncate": false gives a 400 instead | cut at max_seq_length (--auto-truncate, default true) | refused over max_model_len (= max_seq_length) unless truncate_prompt_tokens is set |
| Normalization | only if the model has a Normalize module | /v1/embeddings: L2 by default (--embd-normalize 2). -1 returns raw vectors (measured) | /api/embed and /v1/embeddings: always L2. Legacy /api/embeddings: depends on the model (raw for some bert and nomic-bert GGUFs, otherwise L2; see the note above) | normalize defaults to true | follows the Normalize module |
| Cross-encoder segment ids | query 0, document 1 | 0 for every token (type_row0 in src/models/bert.cpp). Scores shift slightly (measured) | no rerank endpoint | passes them | passes them |
| Rerank score scale | predict(): sigmoid for 1 label (use activation_fn=Identity() for logits) | raw logit for BERT. P(yes) for Qwen3-Reranker | – | sigmoid unless "raw_scores": true | sigmoid for 1 label unless use_activation: false |
A different scale (raw logit vs sigmoid) does not change the ranking, so it is not a bug. Normalization does not change cosine, but it does change dot-product and L2 scores. Everything else in the table changes the vectors themselves.
What we measured on llama-server
Each row is one run of embedcheck.py against llama-server b12826 on CPU, using F32 GGUFs. We used six short texts (English,
Japanese, code, emoji) and one long text of 1.5× max_seq_length. For rerankers we used 2 queries × 5 documents. The weights are
random, so absolute cosines here are not those of a trained model. A trained model's vectors are more spread out, so the same mistake
usually costs more. Whether a setting matches does not depend on the weights.
| Case | Server flags | Result | Cause found by the checker |
|---|---|---|---|
| CLS + Normalize (bge-style) | --embedding | cos 1.000000 × 6 | match. The long text (785 tokens): HTTP 500, too large for -ub 512 |
| same GGUF | --embedding --pooling mean | cos 0.504–0.612 | server pools with mean (model: cls) |
| mean + Normalize, max_seq_length 128 | --embedding | short 1.000000. Long 0.998473 | server reads 218 tokens, sentence-transformers cuts at 128 |
mean, default prompt query: (E5-style) | --embedding | cos 0.978–0.992 | server sees no prefix (reference: 'query: ') |
| converted from a folder without modules.json | --embedding | HTTP 400 on every input | GGUF has no pooling_type. Pooling type 'none' is not OAI compatible |
| CLS + Normalize | --embedding --embd-normalize -1 | cos 1.000000, |v| = 8.0 | match, with a note that the vectors are not unit length |
| BERT cross-encoder, converted by b12816 | --reranking | Spearman −0.70, −0.60 | scores equal the head applied to raw [CLS] with segment ids 0: the pooler was dropped (#33109) |
| same checkpoint, converted by b12826 | --reranking | Spearman 0.90, 1.00 | scores equal the model run with token_type_ids = 0 (pooler fixed, segment ids still 0) |
Qwen3-Reranker-style, converted with a README saying # Qwen3-Reranker | --reranking | equal to P(yes), Spearman 1.00, 1.00 | match |
same weights, converted from a folder named toy-model with no README | --reranking --pooling rank | Spearman 0.60, −0.40 | no cls.output.weight. Score = softmax(last hidden)[0]. Document 2 scored 0.00486 alone and 0.01073 next to document 1 |
How to reproduce: repro/make_toys.py builds the checkpoints, repro/run_matrix.sh starts llama-server for each case
(a few seconds each), and repro/run_mock.sh runs the checker against a replay server that answers in Ollama, TEI and vLLM format.
The full outputs are in repro/out/.
Check your own server
pip install sentence-transformers requests gguf
# llama-server
llama-server -m bge-small-en-v1.5-f16.gguf --embedding --port 8080
python embedcheck.py embed --model BAAI/bge-small-en-v1.5 --url http://127.0.0.1:8080 --gguf bge-small-en-v1.5-f16.gguf
# Ollama (name the model as Ollama knows it)
python embedcheck.py embed --model nomic-ai/nomic-embed-text-v1.5 --url http://127.0.0.1:11434 --api ollama --served-model nomic-embed-text --trust-remote-code
# TEI / vLLM rerankers
python embedcheck.py rerank --model BAAI/bge-reranker-base --url http://127.0.0.1:8080 --api tei
python embedcheck.py rerank --model Qwen/Qwen3-Reranker-0.6B --kind qwen3 --url http://127.0.0.1:8080 --gguf qwen3-reranker-0.6b-q8_0.gguf
Example: llama-server started with the wrong pooling.
reference: sentence-transformers, pooling=cls, normalize=True, max_seq_length=512, ...
text cosine |server| diagnosis
1 0.504198 1.0000 server pools with mean (model: cls)
...
verdict: MISMATCH
- Pooling differs. llama.cpp: the GGUF's <arch>.pooling_type wins unless you pass --pooling ...
Exit status 0 means it matches (notes such as “not normalized” or “refused the long input” are printed but do not fail).
Exit 1 means a mismatch, and 2 means the server could not be reached. --gguf also reads the GGUF header. It flags a missing
pooling_type, a reranker with no cls.* tensors, a Qwen3 GGUF without rank pooling, and a BERT classifier without
cls.weight (the dropped pooler). How close is close enough: F32/F16 GGUFs land above 0.999. Q8_0 usually lands above 0.99.
Below that, with no pooling, prefix or truncation explanation, suspect the conversion.
Fixes
- Pooling
- Start llama-server with
--pooling cls|mean|lastmatching the model's 1_Pooling/config.json. Or reconvert from the full model folder:convert_hf_to_gguf.pyreads modules.json and 1_Pooling to set pooling_type, and a bare folder (only config.json and weights) gives a GGUF without one. - Prefix
- Add the prefix to the text yourself:
search_query:/search_document:for nomic-embed,query:/passage:for E5, the instruction for Qwen3-Embedding. Or tell sentence-transformers not to add it (prompt="") when you compare. - Truncation
- Cut inputs to
max_seq_lengthtokens before sending (all-MiniLM-L6-v2: 256, although its GGUF says 512). For llama-server, raise-uband-btogether to accept longer inputs up to the model's context. - Cross-encoder pooler
- Reconvert BertForSequenceClassification checkpoints with llama.cpp b12818 or later
(PR #33122, merged 2026-11-27).
--ggufshowscls.weightandclassifier.activation=tanhon a good one. - Qwen3-Reranker head
- Convert from a folder whose README.md starts with
# Qwen3-Reranker, or whose name containsqwen3-reranker(support since b6578, PR #15824). Check that the GGUF listscls.output.weightand pooling_type rank. On vLLM, the original checkpoint needs--hf_overrides '{"architectures": ["Qwen3ForSequenceClassification"], "classifier_from_token": ["no", "yes"], "is_original_qwen3_reranker": true}'and the reranker chat template. - Quantized KV cache
- Don't use
-ctk q4_0 -ctv q4_0for rerankers. In #16407 ggerganov advised against it for reranking, and against q8_0 too.
Known reports
| Report | Model | Cause | Status (2026-11-28) |
|---|---|---|---|
| llama.cpp #33109 | cross-encoder/ms-marco-MiniLM-L-12-v2: transformers +8.50, llama.cpp −0.026 on the same pair | pooler dropped by the converter. Segment ids 0 | closed by #33122 (b12818). Segment ids unchanged |
| llama.cpp #16407 | Qwen3-Reranker, bge, mxbai: near-random order | quantized KV cache, a bad community GGUF, raw vs normalized scores | no single fix recorded. The ggml-org Q8_0 GGUF gave sensible scores |
| llama.cpp #5783 | GritLM embeddings differ from the reference | the pooling default was part of it | no merged fix seen |
| Voodisss/Qwen3-Reranker-0.6B-GGUF | community Qwen3-Reranker GGUFs score ~4.5e-23 | no cls.output.weight | explained on that model card |
FAQ
Why are llama.cpp embeddings different from sentence-transformers?
On an F32 GGUF of the same model, llama-server reproduced sentence-transformers to a cosine of 1.000000 in our runs. When they differ, the cause is almost always one of four settings: a different pooling (the GGUF's pooling_type or a --pooling flag), a prompt prefix that sentence-transformers adds automatically from default_prompt_name, a different truncation length (sentence-transformers cuts at max_seq_length, llama.cpp does not), or quantization. embedcheck.py tells you which.
Why do Ollama embeddings not match Hugging Face cosine similarity?
Ollama's /api/embed and /v1/embeddings L2-normalize and truncate to the GGUF context_length (or num_ctx), not to the model's max_seq_length, and never add the model's query or document prefix. Texts longer than max_seq_length and models with a default prompt (E5-style "query: ") differ from sentence-transformers. The legacy /api/embeddings returns what the runner returns, which depends on the model: raw vectors only for BERT GGUFs whose tokenizer is not BERT's or that set normalize_embeddings to false, and for nomic-bert GGUFs unless they set normalize_embeddings to true; otherwise L2-normalized (Ollama v0.40.21 source).
Why does llama-server /v1/rerank return wrong or tiny scores (like 4.5e-23)?
The GGUF has no classifier head. A Qwen3-Reranker converted as a plain Qwen3 model has no cls.output.weight and no rank pooling; llama-server then returns softmax over the last hidden state, which is tiny on a real model and changes with the other documents in the request. The converter treats Qwen3 as a reranker only if README.md contains '# Qwen3-Reranker', the folder or _name_or_path contains 'qwen3-reranker', or the architecture is *ForSequenceClassification.
Why does a cross-encoder GGUF rank documents differently from CrossEncoder.predict?
Until 2026-11-27 llama.cpp's converter dropped the BERT pooler (dense + tanh), so BertForSequenceClassification models such as cross-encoder/ms-marco-MiniLM-L-12-v2 scored the raw [CLS] vector (llama.cpp #33109). PR #33122 fixed the converter; the first build with it is b12818. Reconvert. Separately, llama.cpp still feeds token_type_ids = 0 for the document, so scores of segment-trained cross-encoders are still slightly off in b12826.
Is llama-server's rerank score a probability?
For Qwen3-Reranker GGUFs, yes: llama.cpp applies a softmax to the yes/no head, which equals P(yes) from the model card recipe (measured equal). For BERT cross-encoders it returns the raw logit; TEI and vLLM apply a sigmoid by default, so the numbers differ but the order is the same.
Related
- How far vLLM and llama.cpp logprobs may differ from Transformers
- llama.cpp pre-tokenizer lookup (chkhsh, unknown pre-tokenizer type)
- Do I need to redownload this GGUF?
- What each engine returns when the input is too long
Sources
- llama.cpp b12826: conversion/bert.py, conversion/qwen.py (Qwen3Model._is_qwen3_reranker), conversion/base.py (_try_set_pooling_type), src/llama-graph.cpp (LLAMA_POOLING_TYPE_RANK), src/models/bert.cpp, tools/server/server-context.cpp, server-common.cpp (format_prompt_rerank). PRs #33122, #15824
- Ollama v0.40.19: server/routes.go (EmbedHandler, EmbeddingsHandler)
- TEI v1.10.0: router/src/lib.rs, router/src/http/types.rs, core/src/infer.rs
- vLLM v0.34.0: transformers_utils/config.py (get_pooling_config), config/model.py, layers/pooler/activations.py, entrypoints/pooling/embed/io_processor.py, examples/pooling/score/qwen3_reranker_online.py
- Reports: llama.cpp #33109, #16407, #5783, Voodisss/Qwen3-Reranker-0.6B-GGUF-llama_cpp