Repository navigation
Add model-defined pooling fallback for embedding models - #156
Merged
davidkoski merged 2 commits intoMar 27, 2026
Merged
Conversation
davidkoski
reviewed
Mar 23, 2026
| tokenCounts.enumerated().map { batchIndex, count in | ||
| let tokenIndex = max(Int(count) - 1, 0) | ||
| return hiddenStates[batchIndex, tokenIndex, 0...] | ||
| }) |
Member
There was a problem hiding this comment.
This requires a read back from the GPU (the asArray) which may not be desired here.
Maybe something like this, but check my math:
// shape: [batch]
let tokenCounts = sum(_mask.asType(.int32), axis: -1)
// shape: [batch]
let tokenIndices = maximum(tokenCounts - 1, 0)
// shape: [batch, 1, 1]
let indices = expandedDimensions(tokenIndices, axes: [1, 2])
// hidden states shape: [batch, sequence, hidden]
// gathered shape: [batch, 1, hidden]
let gathered = takeAlong(hiddenStates, indices, axis: 1)
// squeeze down to [batch, hidden]
pooled = squeezed(gathered, axis: 1)
Contributor
Author
There was a problem hiding this comment.
Good catch. I updated this to use pure MLX tensor ops with takeAlong instead of reading indices back with asArray, so .last pooling now stays on-device and still selects the last non-padding token.
davidkoski
requested changes
Mar 23, 2026
davidkoski
approved these changes
Mar 27, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Changes look good, thank you for this!
TheTom
referenced
this pull request
in TheTom/mlx-swift-lm
Apr 5, 2026
* Add model-defined pooling fallback for embedding models * Use takeAlong for last-token pooling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
This PR fixes embedding pooling for models that do not include sentence-transformers pooling metadata such as
1_Pooling/config.json.In
MLXEmbedders, pooling previously fell back to.nonewhen1_Pooling/config.jsonwas missing. For Qwen3 embedding models, that meant returning raw hidden states instead of a fixed-size sentence embedding.This PR adds a model-defined pooling fallback and uses it for Qwen3 embedding models. It also fixes
.lastpooling so it selects the last non-padding token instead of always taking the final sequence position.Concretely, this change:
poolingStrategytoEmbeddingModelloadPooling(...)prefer1_Pooling/config.json, then fall back to the model-defined pooling strategy.last.lastpooling respect the attention maskThis fixes cases where converted embedding repos are missing
1_Pooling/config.jsonand would otherwise produce variable-length embeddings depending on token count.Validated end-to-end in swama with
mlx-community/Qwen3-Embedding-0.6B-4bit-DWQ:""returns 1024 dims"hello world"returns 1024 dimsChecklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes