Fix causal+sliding window mask alignment for autoregressive decode#185
Open
juliantorr-es wants to merge 1 commit into
Open
Fix causal+sliding window mask alignment for autoregressive decode#185juliantorr-es wants to merge 1 commit into
juliantorr-es wants to merge 1 commit into
Conversation
The CK wrapper uses bottom-right alignment ('b:' prefix) for causal and local
attention masks. This produces correct coordinates during prefill (seqlen_q == seqlen_k)
but breaks during autoregressive decode (seqlen_q=1, seqlen_k>1).
With bottom-right alignment and a sliding window (e.g., left=128):
y_tmp = seqlen_q - seqlen_k = 1 - 256 = -255
y = 1 + 128 + (-255) = -126
x_start = -(-126) + i_y + 1 = 127 // for single query token
This masks the first ~127 KV positions from every decode step, producing
degenerate repetitive output with models that use sliding window attention
(e.g., GPT-OSS 20B with window=128 alternating every other layer).
Fixes ROCm#158
Collaborator
|
Thanks for the fix! Two options:
Either works; pick whichever gets you unblocked fastest. |
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
Fix for issue #158. The CK (Composable Kernel) FlashAttention wrapper uses bottom-right mask alignment (
"b:"prefix) for causal and local attention masks. This produces correct coordinates during prefill (seqlen_q ≈ seqlen_k) but breaks during autoregressive decode (seqlen_q=1, seqlen_k>1).Root Cause
In
csrc/flash_attn_ck/mha_fwd.cpp(and the other three CK wrapper files), the mask identifier string uses"b:"(bottom-right alignment):When
seqlen_q == 1(decode step) andwindow_size_left >= 0(sliding window), bottom-right alignment computes:Example with window=128, seqlen_q=1, seqlen_k=256:
x_start = 127, masking the first 127 KV tokens from the single query. Every decode step sees a truncated window, producing degenerate repetitive output.Fix
Changed
"b:"to"t:"(top-left alignment) in all 8 mask construction sites across 4 files:csrc/flash_attn_ck/mha_fwd.cppcsrc/flash_attn_ck/mha_bwd.cppcsrc/flash_attn_ck/mha_varlen_fwd.cppcsrc/flash_attn_ck/mha_varlen_bwd.cppThis matches the HuggingFace causal attention convention (top-left aligned).
Verification
With top-left alignment during decode (window=128, seqlen_q=1, seqlen_k=256):
Correct: query at accumulated position N sees KV range [N-128, N].
Related Issues