fix: correct cross-frame attention repeat factor in MemoryEfficientCrossAttention#473
Open
Mr-Neutr0n wants to merge 1 commit intoStability-AI:mainfrom
Open
Conversation
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.
Summary
MemoryEfficientCrossAttentionhas a bug in its cross-frame attention implementation (based on Text2Video-Zero). The repeat factornused when expandingkandvtensors is incorrect.The Bug
The
n_cpcalculation was commented out, andn_times_crossframe_attn_in_selfwas used as the repeat count instead:The slicing
k[::n_times_crossframe_attn_in_self]selects every N-th frame, producingbatch_size / n_times_crossframe_attn_in_selfentries. To restore the original batch dimension, the repeat factor must ben_cp = batch_size // n_times_crossframe_attn_in_self, notn_times_crossframe_attn_in_selfitself.Using
n_times_crossframe_attn_in_selfas the repeat factor only produces the correct result whenbatch_size == n_times_crossframe_attn_in_self^2, which is not generally the case. For all other batch sizes, the output tensor has the wrong batch dimension, leading to shape mismatches or silently incorrect attention.The Fix
Uncomment
n_cpand use it as the repeat factor, consistent with the existing correct implementation inCrossAttention: