CLS pooling vs mean: what each does and which to use
A search model reads your text in small pieces and gives each piece its own list of numbers. Pooling is the last step. It squeezes all those lists into one list for the whole text. Mean vs CLS pooling is the choice you will meet most often. This note shows how each one works, when each one wins, and what the choice did to a real search I built.
In short
Think of a row of students. Each one reads one word of a sentence. Now you need one answer for the whole sentence.
Mean pooling asks every student and takes the average. Every word gets a small vote.
CLS pooling asks only one student, who sits at the front. That student reads no word of their own. Their job is to listen to all the others and sum them up. This works best when the model was trained to fill that seat.
Max pooling keeps the strongest signal from any word. Last token pooling asks the student at the end of the row, who has heard everyone before them.
The safe rule is to use the pooling the model was trained with. It is written in the model’s own files. But test it on your own text, because the files can still lose.
That happened to me. The files for my model said CLS. In one test of 50 questions, mean found the right answer 27 times and CLS 19 times.
The technical details
What you have before pooling
A model like BERT splits text into tokens and adds one special token, [CLS], at the start. After the last layer, every token has its own vector:
tokens: [CLS] how to stay patient (end token left out)
vectors: v0 v1 v2 v3 v4 one vector per token
| | | | |
+------+-----+-----+------+
|
pooling
|
one vector for the whole text
Pooling turns that set into one vector, so two texts can be compared with one cosine. Sentence embeddings are just this pooled vector, usually scaled to length 1.
CLS pooling
[CLS] how to stay patient
v0 v1 v2 v3 v4
^
keep v0, drop the rest
The [CLS] token reads no word of its own. Through attention it can look at every other token, so training can teach it to hold a summary. That is the catch: it only holds a good summary if the training asked it to. A model trained with CLS pooling learns to use that slot. A model trained with mean pooling may leave it weak.
CLS adds no work of its own. But one slot must carry everything, so on long text some detail can be lost.
Mean pooling
[CLS] how to stay patient [PAD] [PAD]
v0 v1 v2 v3 v4 -- -- padding is left out
(v0 + v1 + v2 + v3 + v4) / 5
Mean pooling adds up the real token vectors and divides by how many there are. The padding added to fill a batch must be left out, which is what the mask does. Every word counts the same, so the result leans on the whole text. The weak side: in a long text, one key word is one vote among hundreds.
Max pooling and last token pooling
Max pooling looks at each number in the vector and keeps the biggest value any token gave. Last token pooling keeps the vector of the final token. Models like Qwen3 read left to right, so only the last token has seen the whole text. For them, the last token is the natural choice.
What the research says
The Sentence-BERT paper tested the three classic choices on the same model, trained two ways. NLI and STSb are two standard sentence tests, and higher is better:
| Pooling | NLI score | STSb score |
|---|---|---|
| Mean | 80.78 | 87.44 |
| CLS | 79.80 | 86.62 |
| Max | 79.07 | 69.92 |
Its authors found that pooling had “a rather minor impact” in one training setup and “a large impact” in the other. Max lost clearly on STSb and slightly on NLI. So mean and CLS are both fair picks, and the gap depends on the model and the task.
How to find a model’s pooling
Do not guess. A Sentence Transformers model keeps it in 1_Pooling/config.json. For mxbai embed large that file says "pooling_mode_cls_token": true. For all-MiniLM-L6-v2 it says mean. In Ollama, check the model file’s metadata. For bge-m3 it says bert.pooling_type: 2, which is CLS. Then check what your runtime really does, because some ignore the setting. Run both poolings on a few real texts and compare.
Pick the wrong one and the vectors change a lot. When I mean pooled the small Qwen3 0.6B instead of using its last token, its cosine to the official vectors was only 0.55, on 12 real chunks and 8 questions.
Both in code
This is how my search project gets both from one model run:
mask = enc["attention_mask"].unsqueeze(-1).expand(h.size()).float()
mean = F.normalize(torch.sum(h * mask, 1) / torch.clamp(mask.sum(1), min=1e-9), p=2, dim=1)[0]
cls = F.normalize(h[:, 0], p=2, dim=1)[0]
h holds the last layer for every token. The first line builds the padding mask. The second is mean pooling. The third takes token 0, which is CLS. Both are scaled to length 1.
What happened in Nooor
Nooor finds Quran commentary for a problem you describe in your own words. It uses mxbai-embed-large-v1, an int8 ONNX file that reads at most 512 tokens. Its model card points to CLS. I picked mean anyway, and later tested both.
The test had 50 questions, 30 I tuned on and 20 held out. Each had its right verses written down first. A question counts as found when one of them is in the top 10. Only Maududi’s commentary was searched. Each question ran as typed, and again with terms: extra search words a language model writes for each question before Nooor searches. MRR rewards a right answer near the top.
| Model and pooling | As typed: found, top 3, MRR | With terms: found, top 3, MRR |
|---|---|---|
| mxbai, mean | 27, 12, 0.21 | 25, 17, 0.26 |
| mxbai, CLS (model card) | 19, 12, 0.19 | 23, 16, 0.23 |
| bge-m3, CLS (how it was trained) | 16, 6, 0.10 | 21, 13, 0.20 |
| bge-m3, mean | 11, 5, 0.10 | 14, 10, 0.14 |
bge-m3 ran on one chunk per footnote, so compare inside a model, not across. Mean vs CLS pooling went opposite ways for the two models. For mxbai, mean found 8 more questions as typed, with a tie on top 3. For bge-m3, the pooling it was trained with won, 16 to 11.
One trap is worth knowing. The ready made container image for this model ignores the pooling setting in its ONNX path and always mean pools. So setting CLS there changes nothing, and I needed my own script to test it. More is on the Nooor project page.
What went wrong
I picked mean pooling on 11 September from a reason, not a test. The test came on 27 September and agreed, but for two weeks the choice was a guess.
The win is also smaller than it looks. With the added terms, which is how Nooor really searches, mean beat CLS by only 2 of 50. The terms were tuned on Nooor’s own int8 setup, which may favour it. The test also covered Maududi only. Ibn Kathir’s chunks are much longer, a median of 404 tokens against 109, and CLS never ran on them.
What I will try next
Run CLS on Ibn Kathir’s longer chunks, and test every pooling a new model allows before I load it.