Repository navigation
[GPT2] Fix encoder_attention_mask being silently discarded in cross-attention - #47946
Merged
Rocketknight1 merged 2 commits intoAug 18, 2026
Conversation
Rocketknight1
approved these changes
Aug 14, 2026
Member
|
run-slow: decision_transformer, gpt2 |
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: decision_transformer, gpt2 |
Contributor
|
This comment contains models: ["models/decision_transformer", "models/gpt2"] |
…ss-attention The new attn mask interface (huggingface#42848) rewired GPT2Model and its decision_transformer copy to build the cross-attention mask via create_bidirectional_mask, but added an encoder_attention_mask = None line directly above the call that overwrote the incoming mask before it was passed in. As a result, encoder padding was ignored in cross-attention. Remove the line and add a regression test.
Rocketknight1
force-pushed
the
gpt2_cross_attention_discarding_encoder_attention_mask
branch
from
August 14, 2026 11:50
567ca8f to
72a9509
Compare
Contributor
CI recapDashboard: View test results in Grafana |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Collaborator
|
Oh wow, thanks for fixing @DavidJohnQuinlan! That one is probably on me 😬 |
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.
What does this PR do?
On line 591 (
encoder_attention_mask = None) inGPT2Modelinsrc/transformers/models/gpt2/modeling_gpt2.pyappears to be a bug. The user definedencoder_attention_maskis overwritten and set to None before it is passed tocreate_bidirectional_mask. This has a knock on effect on the creation of anencoder_attention_maskin the case that there are padding tokens present in the input. This causes padding tokens not be ignored during the cross attention calculation withattention_mask=None(since encoder_attention_mask=None) when callingtorch.nn.functional.scaled_dot_product_attention.This bug was introduced in the recent masking refactor
[Attn] New attn mask interface everywhere (#42848)(4b8ba25). I appreciate that this is not the default use case for GPT2 but seemed wrong to not highlight/fix when reviewing GPT2 model code.What's implemented
The fix involves simply removing this line of code from the GPT2Model in addition to removing the same bug from the
DecisionTransformermodel which copies code from the GPT2 model. Additionally, to ensure that this type of error does not re-occur I have added a regression test to the GPT2 tests. This test assigns some of the input tokens as padding tokens which should be removed, a copy of the encoder hidden states is also generated with their padded tokens assigned corrupted values. A comparison is made between the last hidden state of the clean and corrupted encoder hidden states. The idea being that these tokens will never be output due to the mask fix, however, when the mask was set to None these output values would be different due to the addition of the corrupted data.AI assistance
I identified the bug, wrote the fix and created the test. Claude Code was used as a review and feedback tool during development.
Before submitting
Pull Request checks?
Who can review?
@Rocketknight1 @qgallouedec @vasqu