Fix vanilla and torch attention cu_seqlens handling#310
Open
Mr-Neutr0n wants to merge 1 commit intoTencent-Hunyuan:mainfrom
Open
Fix vanilla and torch attention cu_seqlens handling#310Mr-Neutr0n wants to merge 1 commit intoTencent-Hunyuan:mainfrom
Mr-Neutr0n wants to merge 1 commit intoTencent-Hunyuan:mainfrom
Conversation
The vanilla attention mode completely ignored cu_seqlens_q/cu_seqlens_kv when they were provided, computing attention over the entire concatenated sequence including padding tokens. This caused valid image/text tokens to attend to padding tokens and vice versa, leading to severe quality degradation compared to flash attention (see Tencent-Hunyuan#296). The torch attention mode hardcoded cu_seqlens_q[1] as a single split point, which only worked correctly for batch_size=1. For batch_size > 1 the cu_seqlens array contains multiple segment boundaries that were silently ignored, producing incorrect attention outputs. Changes: - vanilla mode: build a block-diagonal attention mask from cu_seqlens that prevents cross-segment attention between valid and padding tokens - torch mode: iterate over all batch items using the full cu_seqlens array to correctly split valid/padding segments per sample
|
|
Author
|
I have read the CLA Document and I hereby sign the CLA |
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
mode="vanilla") completely ignorescu_seqlens_q/cu_seqlens_kvwhen provided, allowing attention to bleed across segment boundaries (valid tokens attend to padding tokens and vice versa). This causes the quality degradation reported in [BUG] attention 使用 vanilla 版本质量差上许多 #296. Fixed by building a block-diagonal attention mask fromcu_seqlensthat prevents cross-segment attention.mode="torch") hardcodescu_seqlens_q[1]as a single split point, which only works forbatch_size=1. Forbatch_size > 1, the remaining segment boundaries incu_seqlensare silently ignored. Fixed by iterating over all batch items using the fullcu_seqlensarray.Details
flash_attn_varlen_funcin theflashmode correctly handles variable-length sequences viacu_seqlens, separating valid (image + text) tokens from padding tokens. Thevanillaandtorchcode paths did not replicate this behavior:Vanilla mode computed attention over the entire concatenated sequence with no masking at all. When
cu_seqlensencodes a valid segment[0, s)and a padding segment[s, max_len), all tokens could freely attend to each other, corrupting the output.Torch mode used
cu_seqlens_q[1]to split into exactly two segments. Thecu_seqlensarray has2 * batch_size + 1entries (a valid/padding pair per batch item), so the two-segment split is only correct whenbatch_size == 1.Test plan
batch_size=1with padding tokensbatch_size > 1mode="vanilla"is comparable tomode="flash"(addresses [BUG] attention 使用 vanilla 版本质量差上许多 #296)