Skip to content

Fix mllama cross-attention leak for fully masked-out text rows#47103

Closed
MutugiD wants to merge 1 commit into
huggingface:mainfrom
MutugiD:fix-mllama-full-text-row-mask
Closed

Fix mllama cross-attention leak for fully masked-out text rows#47103
MutugiD wants to merge 1 commit into
huggingface:mainfrom
MutugiD:fix-mllama-full-text-row-mask

Conversation

@MutugiD

@MutugiD MutugiD commented Jul 6, 2026

Copy link
Copy Markdown

CI

What does this PR do?

Fixes #39379. In MllamaCrossAttentionDecoderLayer.forward, full_text_row_masked_out_mask was applied only to the MLP output, but not to the cross-attention output before its residual add. As a result, text rows that are fully masked out of cross-attention (tokens that precede their <|image|>) still received image information through the residual connection.

This matches the reference implementation, which applies the mask both after the attention and after the MLP. The added lines mirror the existing post-MLP masking:

if full_text_row_masked_out_mask is not None:
    hidden_states = full_text_row_masked_out_mask[:, 0] * hidden_states  # type: ignore
hidden_states = residual + self.cross_attn_attn_gate.tanh() * hidden_states

As noted in the issue, the discrepancy only appears when the image is not the first token, which is why the existing tests (built around image-first prompts) didn't catch it.

Tests

Adds MllamaCrossAttentionMaskTest (CPU-only): it drives a single MllamaCrossAttentionDecoderLayer with a fully masked-out text row and asserts that row is left unchanged by the layer (no cross-attention leakage), while a row that does attend to the image is modified. The gates are tanh-gated and zero-initialized, so the test opens them to make the residual contributions non-trivial. It fails on main (100% of the masked row's elements change) and passes with the fix.

pytest tests/models/mllama/test_modeling_mllama.py::MllamaCrossAttentionMaskTest

Who can review?

@zucchini-nlp (thanks for the go-ahead on the issue) @ArthurZucker

full_text_row_masked_out_mask was only applied to the MLP output, so the
cross-attention result still leaked into masked-out text rows through the
residual connection. Apply the mask to the cross-attention output as well,
matching the reference implementation, so text tokens that precede their
image correctly receive no image information.

Adds a regression test for the image-not-first-token case.

Fixes huggingface#39379.
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

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

run-slow: mllama

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 28779603918
Result: success | Grafana metrics are not available yet.

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.

Handling of full_text_row_masked_out_mask in mllama is incorrect.

2 participants