Repository navigation
fix: keep the causal mask for broadcast 4D padding masks - #1971
Merged
jlarson4 merged 1 commit intoNov 4, 2026
Merged
Conversation
_apply_reconstruct_attention_mask treated every 4D mask as already encoding causal semantics. GPT-1 passes a [batch, 1, 1, seq] padding-only mask to its blocks and leaves the causal mask to the attention module, so any attention mask opened attention to later tokens. Only skip the local causal mask when the 4D mask has its own query rows.
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
AttentionBridge._apply_reconstruct_attention_masktreats every 4D mask as already encoding causal semantics and skips the local causal mask. That holds for the[batch, 1, seq, seq]masks most models build, but HF'sOpenAIGPTModelpasses a padding-only[batch, 1, 1, seq]mask and leaves the causal part to the attention module'sbiasbuffer. So on GPT-1, any attention mask lets every position attend to later tokens.This is reachable by passing
attention_mask=orpadding_side="left"to the bridge. Without a mask, and for right-padded string lists (which pass no mask), GPT-1 was already correct.On
openai-community/openai-gptwith"the capital of france is"and an all-ones mask:all(HF:a)aThe fix only treats a 4D mask as authoritative when it has its own query rows (
shape[-2] != 1). A single query row is also what a causal 4D mask looks like on a cached decode step, but the causal mask allows every key for that row, so applying it there changes nothing; there is a test for that case.I found this with a padding comparison across the supported causal LM architectures, following #1952. GPT-1 was the only one showing it, and it is the only supported architecture I found in transformers that passes this mask shape.
Not related to #1969, which is about
hook_outon the same model.Type of change
Testing
test_mask_slicing.pyandtest_openai_gpt_adapter.py(the GPT-1 ones use a tiny random model, no download): 4 fail before the fix, all 12 pass after.tests/unit -m "not slow"on the locked environment: 8117 passed, 0 failed.test_openai_gpt_adapter,test_forward_padding_side,test_left_padding_positions,test_generate_attention_mask,test_inspect_attention_mask,test_encoder_right_padding_maskandtest_attention_score_sentinel: 118 passed.test_mask_slicing,test_openai_gpt_adapterandtest_forward_padding_sidealso pass on transformers 5.20.2 / torch 2.14.1.mypy .are clean.I did not run the full integration or acceptance suites locally.
Checklist: