Repository navigation
fix: sample right-padded prompts from the last attended token - #2050
Merged
jlarson4 merged 2 commits intoNov 25, 2026
Merged
Conversation
jlarson4
reviewed
Nov 24, 2026
| dim=-1 | ||
| ) | ||
| if bool((last_prompt_positions < 0).any()): | ||
| raise ValueError("Each prompt row must have at least one attended token.") |
Collaborator
There was a problem hiding this comment.
Where pad and BOS share an id (GPT-2), padding_side="right" reads a BOS-only row as all padding, so an empty prompt in a right-padded batch, or a lone BOS, now lands here instead of generating. Can that path keep the BOS attended, the way the left-padded side does?
Contributor
Author
There was a problem hiding this comment.
Thanks for catching this! The inferred right-padding mask now keeps the leading BOS attended when the prompt includes BOS and BOS shares the padding ID. Explicit masks still take precedence, and rows that are genuinely all masked still raise.
Collaborator
|
Thanks for that quick fix @emerardd, merging now |
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.
Description
Ordinary decoder-only
generate()andgenerate_stream()used the last physical prompt column to select the first token even when an attention mask marked that column as padding. A mask controls what the model attends to, but it does not change which output column the generation loop reads.With a tiny random GPT-2 on CPU, prompt
[2, 3, 4]generates[4, 4]greedily, while[2, 3, 4, 0, 0]with mask[1, 1, 1, 0, 0]generated[0, 0], with or without KV caching.Select first-step logits at each row's last attended prompt token. Later steps still read the last generated position, using the existing extended mask and position handling. Keep padded prompt tokens in the returned sequence and reject fully masked rows on this path. Encoder-decoder, stateful and multimodal generation paths are unchanged.
Type of change
Validation
Checklist
Only affected tests were run locally.