Skip to content

fix: sample right-padded prompts from the last attended token - #2050

Merged
jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
emerardd:fix/right-padded-generation
Nov 25, 2026
Merged

jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
emerardd:fix/right-padded-generation

Conversation

@emerardd

Copy link
Copy Markdown
Contributor

Description

Ordinary decoder-only generate() and generate_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

  • Bug fix

Validation

  • Offline CPU regressions cover greedy token and per-step logit parity with unpadded prompts, mixed-length batches, explicit positions, both cache settings, streaming, and fully masked rows.
  • 125 affected unit and integration tests passed, including the existing generation-mask and stream tests.
  • Full mypy and changed-file format checks passed. The full test suite and GPU/MPS were not run locally.

Checklist

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

Only affected tests were run locally.

@jlarson4 jlarson4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one small comment! Let me know if you have any questions @emerardd

dim=-1
)
if bool((last_prompt_positions < 0).any()):
raise ValueError("Each prompt row must have at least one attended token.")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jlarson4

Copy link
Copy Markdown
Collaborator

Thanks for that quick fix @emerardd, merging now

@jlarson4
jlarson4 merged commit 101ae58 into TransformerLensOrg:dev Nov 25, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants