Repository navigation
fix(feature-extraction): use last attended token for last_token/eos pooling - #1964
Open
definitelynotguru wants to merge 1 commit into
Open
definitelynotguru wants to merge 1 commit into
definitelynotguru wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Why
FeatureExtractionPipelinewithpooling: 'last_token'or'eos'always tookresult.slice(null, -1). In a right-padded batch, every shorter input therefore got the embedding of a padding token instead of its own last token. Batched embeddings from last-token models (e.g. Qwen3-Embedding) then differed from embedding the same texts one at a time. The reference Python implementation for these models uses the attention mask to find the last real token.What changed
src/pipelines/feature-extraction.js: forlast_token/eos, find the last attended position of each row frommodel_inputs.attention_maskand stack those token embeddings.slice(null, -1)path is used. A fully masked row also keeps the old behaviour.poolingnotes thateos/last_tokenselect the last attended token.tests/pipelines/test_pipelines_feature_extraction.js: network-free regression for both aliases covering right-padded, unpadded and left-padded rows.Scope
Only the
last_token/eospooling branch.mean,cls/first_tokenandnoneare unchanged.Blast Radius
Output changes only for right-padded batches using
last_token/eos, where the previous result was a padding-token embedding.Verification
[90, 91]instead of[3, 4]); after: passes.pnpm test: 30/30 suites passed (2249 passed, 54 skipped); structured-output 20/20.pnpm buildanddocs-generate(toctree validation) pass; Prettier clean on changed files.AI assistance was used to prepare this change; I reviewed the diff and test results.