Fix mllama cross-attention leak for fully masked-out text rows#47103
Closed
MutugiD wants to merge 1 commit into
Closed
Fix mllama cross-attention leak for fully masked-out text rows#47103MutugiD wants to merge 1 commit into
MutugiD wants to merge 1 commit into
Conversation
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.
4 tasks
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: mllama |
Contributor
CI recapDashboard: View test results in Grafana |
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?
Fixes #39379. In
MllamaCrossAttentionDecoderLayer.forward,full_text_row_masked_out_maskwas 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:
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 singleMllamaCrossAttentionDecoderLayerwith 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 onmain(100% of the masked row's elements change) and passes with the fix.Who can review?
@zucchini-nlp (thanks for the go-ahead on the issue) @ArthurZucker