Fix 3D attn_mask head/batch layout in T2V/V2T cross-attention (repeat → repeat_interleave) - #77
Merged
h-munakata merged 1 commit intoAug 30, 2026
Conversation
Contributor
|
lgtm. @h-munakata Could you merge it? |
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.
Problem
nn.MultiheadAttentionexpects a 3Dattn_maskof shape(N * num_heads, L, S)indexed asbatch_idx * num_heads + head_idx. The mask is built with.repeat(nhead, 1, 1), which tiles instead:[M0, M1, M0, M1]rather than the expected[M0, M0, M1, M1]. Heads then attend using the wrong sample's padding mask.Impact
When
num_heads > 1and samples have different valid lengths (the normal case), a sample's output depends on the other samples in the batch, breaking determinism. Atbatch_size == 1the two ops are identical, so single-sample outputs are unchanged.Fix
Use
.repeat_interleave(self.nhead, dim=0)in all affected encoder layers:CIM.py(T2V+V2T),cg_detr_,qd_detr_,taskweave_,tr_detr_transformer.py.