Skip to content

[ImageGPT] Fix cross-attention crashing, dropping the encoder mask, and doubling its cache - #48694

Open
blipbyte wants to merge 2 commits into
huggingface:mainfrom
blipbyte:fix-imagegpt-cross-attention
Open

blipbyte wants to merge 2 commits into
huggingface:mainfrom
blipbyte:fix-imagegpt-cross-attention

Conversation

@blipbyte

@blipbyte blipbyte commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

CPU CI GPU run-slow

What does this PR do?

Fixes #48693, where @vasqu agreed it is worth fixing for now, while noting it is not high priority
and that this feature may be removed later. No urgency from me — happy for it to sit, or to close
it if the path is going away.

With add_cross_attention=True, ImageGPTModel is broken four ways where GPT-2 is fine: the
default use_cache=True call raises UnboundLocalError on is_updated; the encoder mask is
discarded, so padded encoder positions still reach the output; a padded decoder attention_mask
raises a RuntimeError on the mask shape; and one cached decoding step leaves 22 keys in the
cross-attention cache for 11 encoder positions.

Each of the three fixes is GPT-2's own code, restored:

The test ports #47946's encoder-mask check under a padded decoder mask, adds an assertion that
corrupting visible encoder positions moves the output (without it the check passes for an
encoder-blind model), and compares one cached decoding step against the full forward. Reverting
any one fix reds it.

pytest tests/models/imagegpt/             119 passed, 181 skipped
RUN_SLOW=1 pytest tests/models/imagegpt/  131 passed, 169 skipped
make fix-repo                             17/17, rewrote nothing
make check-repo                           26/26

AI-assisted, per CONTRIBUTING.md. Coordination: #48693, which I opened and @vasqu answered.
Differentiation: #45773 fixes the same crash for whisper, moonshine, pix2struct and
audioflamingo3, not ImageGPT; #44076 refactors this file's output collection, not the cache or the
mask. I reviewed every changed line and ran the suite locally.

Code Agent Policy

  • (First-time contributors only): I confirm that this PR description and code is not written by an LLM or code agent

Before submitting

Who can review?

@vasqu

…nd doubling its cache

Three defects on the `add_cross_attention=True` path, none of them present in GPT-2:

- `ImageGPTModel` built a plain `DynamicCache`, so the cross-attention branch read an
  unbound `is_updated` and raised `UnboundLocalError` on the default `use_cache=True`
  path (huggingface#40811).
- `ImageGPTAttention` never applied `encoder_attention_mask` and masked cross-attention
  with the decoder's own mask instead, so encoder padding was attended and a padded
  decoder batch raised a shape error (huggingface#38635).
- `ImageGPTAttention` wrote the cross-attention cache on every step, so from the second
  step the cache was appended to itself and `generate` failed on the key length (huggingface#38635;
  GPT-2 got the guard in huggingface#39754, decision_transformer through its `# Copied from`).

Each fix is GPT-2's own code: `modeling_gpt2.py:560-566`, `:173` and `:193-195`.

@molbap molbap 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.

thanks, lgtm as long as the tests are simplified, we don't want to add that much code surface.

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: imagegpt

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 34530928940:2
Result: success | Jobs: 16 | Tests: 189,123 | Failures: 0 | Duration: 16h 34m

@blipbyte
blipbyte requested a review from molbap September 11, 2026 14:18
@blipbyte

Copy link
Copy Markdown
Contributor Author

Addressed in 7e6c795: the three cross-attention tests are one test_cross_attention now, no fixed seed, and the cache condition is the simplified form you suggested. Ready for another look.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ImageGPT] Cross-attention crashes, ignores encoder_attention_mask, and doubles its cache

2 participants