fix: two bugs in Sampler — missing f-string in _normalize_token and XOR logic error in _remove_eos_token#618
Open
Dhakshin2007 wants to merge 2 commits intogoogle-deepmind:mainfrom
Conversation
The `done` flag was updated using XOR (^): done = state.done ^ (state.last_token == EOS) In a batched setting, this incorrectly flips `done` to True for any batch element whose last_token happens to equal EOS even when `state.done` was already False. The correct operation is AND-NOT (&~), which only ever clears the `done` flag (never sets it): done = state.done & ~(state.last_token == EOS)
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
This PR fixes two independent bugs in the sampling pipeline that can cause incorrect runtime behavior or silently swallow debugging information.
Bug 1 — Missing
fprefix on f-string in_normalize_token(_sampler.py)What's broken
When a user passes a
stop_tokensorforbidden_tokensstring that tokenizes to more than one token, aValueErroris raised. However the error message uses a plain string instead of an f-string:The user sees:
...with no information about which token caused the problem.
Fix
Impact
Affects every user of
SamplerandChatSamplerwho provides multi-tokenstop_tokensorforbidden_tokens. Without the fix, the error message is completely uninformative and makes debugging impossible.Bug 2 — XOR (
^) logic error in_remove_eos_token(_chat_sampler.py)What's broken
_remove_eos_tokenis called in tool-calling flows to undo the EOS token that was generated before a<|tool_response>tag. It needs to cleardone=Trueback todone=Falsefor the affected batch elements. The current code uses XOR:In a batched scenario, XOR can incorrectly set
done=Truefor batch elements wheredone=Falsebutlast_token==EOS. For example:state.donelast_token==EOSTrueTrueFalse✅False✅FalseFalseFalse✅False✅FalseTrueTrue❌False✅TrueFalseTrue✅True✅Row 3 is the problematic case: XOR incorrectly marks a non-done sequence as done.
Fix
Use AND-NOT (
& ~) which only ever clears thedoneflag, never sets it:Impact
Affects batched multi-turn tool-calling inference. In affected batches, sequences can be prematurely marked as done, causing generation to stop early — leading to truncated or missing tool responses.
Files changed
gemma/gm/text/_sampler.py— Bug 1 fix (1-char change: addfprefix)gemma/gm/text/_chat_sampler.py— Bug 2 fix (^→& ~)