Add forward LSE output to FlyDSL flash attention kernels#844
Open
amd-wsung102 wants to merge 1 commit into
Open
Add forward LSE output to FlyDSL flash attention kernels#844amd-wsung102 wants to merge 1 commit into
amd-wsung102 wants to merge 1 commit into
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.
Motivation
This PR is motivated by the task described here https://amd-hub.atlassian.net/jira/software/c/projects/AIMODELS/boards/2992?assignee=712020%3Acedccbdf-bb1e-4e04-8c3b-edcdd7744eda&selectedIssue=AIMODELS-983, which prepares for a MSLK integration.
Summary
Extends the FlyDSL flash attention forward kernels to optionally emit the per-row log-sum-exp (LSE) alongside the attention output. LSE is the normalization statistic the backward pass needs to recompute the softmax probabilities
Pwithout re-running the online softmax, so this is the enablement step for a pure-FlyDSL forward+backward attention path.Opt-in via
return_lse=True; default behavior is unchanged.Changes
flash_attn_interface.py: addreturn_lsetoflydsl_flash_attn_func; thread it through every build-cache helper; allocate fp32[B, num_heads, Sq]and return(out, lse)when enabled. Cache is keyed onreturn_lseso the existing no-LSE kernels are never perturbed.flash_attn_generic.py:return_lsebuild flag;LSEtensor in the kernel/launch/compile signatures; storesm_scale*m_raw + ln(l)on both the normal store and the zeroed (fully-masked) tile.flash_attn_gfx950.py: same for the dualwave kernel (dense storem_row*ln2 + ln(l_row)), plus the split-K combine kernel finalizingm_max*ln2 + ln(den)from the per-split(m, l). One writer per row via half-wave + OOB-sentinel guards.tests/kernels/test_flash_attn_lse.py: numerical validation vs a float32 PyTorch reference.docs/flash_attn_lse_contract.md: the A1↔A3 interface contract.Output tensor
float32, shape[B, num_heads, Sq](varlen:[B, num_heads, max_seqlen_q], padded rows undefined).Supported / unsupported paths
num_kv_splits > 1)cu_seqlens)NotImplementedErrorblock_table/seqlen_k)NotImplementedErrorfp8 and paged KV are decode-oriented and out of scope for the training backward path; they fail loudly rather than silently returning wrong LSE.
Validation
tests/kernels/test_flash_attn_lse.py— 33 cases pass on gfx950, covering dense MHA/GQA/MQA (D=64/128, bf16/f16), split-K, varlen, cross-attention, and fully-masked (-inf) rows. LSE matches the fp32 reference within ~8e-3 (bf16) / ~4e-3 (f16); fp32-accumulating paths (varlen light,D=64) match to ~1e-6.Performance
The LSE store is one fp32 write per query row (single lane per row). Measured on gfx950 (bf16, H=16, D=128, S ∈ {1024, 2048, 4096}), pure kernel overhead is within run-to-run noise (≈0–1.3%). The full-API delta (~3%) is dominated by the extra LSE tensor allocation, not the GPU store.
Compatibility
Fully backwards compatible —
return_lsedefaults toFalseand returns a bare output tensor; LSE build variants are cached separately.Test plan
pytest tests/kernels/test_flash_attn_lse.pyon gfx950 (33 passed)