fix: per-sequence token counts in batch embedding averaging#3792
Open
alvinttang wants to merge 1 commit intolm-sys:mainfrom
Open
fix: per-sequence token counts in batch embedding averaging#3792alvinttang wants to merge 1 commit intolm-sys:mainfrom
alvinttang wants to merge 1 commit intolm-sys:mainfrom
Conversation
`__process_embed_chunk` previously computed `token_num` as a single scalar summing tokens across the entire batch. When batch_size > 1, each sequence's mean-pooled embedding was divided by the aggregate token count instead of its own, silently producing incorrect results. Replace the scalar sum with per-sequence counts via `attention_mask.sum(dim=1, keepdim=True)` so each embedding is normalized by its own token count. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
__process_embed_chunkcomputedtoken_num = torch.sum(attention_mask).item(), which sums tokens across the entire batch into a single scalar. Whenbatch_size > 1, every sequence's mean-pooled embedding was divided by this aggregate count instead of its own token count, silently producing incorrect embeddings.attention_mask.sum(dim=1, keepdim=True)to get per-sequence token counts (shape(batch, 1)), so each embedding is normalized correctly via broadcasting.ret["token_num"]metadata field (total tokens for billing/logging) is preserved as a scalar via.sum().item().Details
The bug affects both code paths in
get_embeddings:embedding / token_numnow broadcasts correctly per sequence.chunk_embeddings * token_numand the final/ all_token_numboth broadcast correctly with the(batch, 1)shape.When
batch_size == 1, the old and new behavior produce identical results, which is why this went undetected.Test plan
batch_size=1: results unchangedbatch_size>1and variable-length sequences: each embedding should now differ from the old (incorrect) outputret["token_num"]remains a scalar integer🤖 Generated with Claude Code