initial implementation#757
Draft
kyle-hoffmeyer wants to merge 6 commits into
Draft
Conversation
…ion classes extract_sdpa_cfg unpacks indices as (B, H, N, d) but _efficient_attention_forward/ backward use BNHD layout (B=0, N=1, H=2, d=3). The index tuple was (0,1,2,3) which swapped H and N, causing N_Q=H_Q=32 and N_KV=H_KV=8 to be reversed. Fix to (0,2,1,3) so B_idx=0, H_idx=2, N_idx=1, d_idx=3. Caught during validation: the perf model raised "causal=True but N_Q != N_K: 32 != 8" because it was comparing head counts instead of sequence lengths. Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
… re-reads The previous formula counted each tensor once (single-pass), which was 10.5x under the measured HBM traffic (58 MB predicted vs 616 MB measured on MI300X at seq_len=1024, H=32, d=128). The flash-attention backward algorithm makes two tiled passes: - dK/dV pass: for each KV block, reads all Q/O/dO blocks. -> Q, O, dO are each read N_KV/block_kv times; K, V read once. - dQ pass: for each Q block, reads all KV blocks of K, V. -> K, V each read N_Q/block_q times; Q read once. With block_q=block_kv=64 (default for d=128), this gives ~14x more reads than the naive single-pass model. Validated: 721 MB predicted vs 619 MB measured (0.86x ratio), vs the previous 10.5x error. The remaining ~14% over-prediction is causal masking saving the upper triangle of attention blocks, which the formula conservatively ignores. Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
The flash-style backward recomputes the full forward pass on-chip to avoid storing the attention matrix P. The existing formula already included the QK recompute but was missing the PV matmul (output = P @ V). Adding it brings predicted backward FLOPs from 21.47 G to 25.77 G (vs 40.53 G measured), closing the ratio from 1.89x to 1.57x. The remaining gap is VALU softmax/rescaling ops counted by TOTAL_16_OPS but not in the matmul-only roofline formula. Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
… re-reads The previous formula counted each tensor once (single-pass), which was 10.5x under the measured HBM traffic (58 MB predicted vs 616 MB measured on MI300X at seq_len=1024, H=32, d=128). The flash-attention backward algorithm makes two tiled passes: - dK/dV pass: for each KV block, reads all Q/O/dO blocks. -> Q, O, dO are each read N_KV/block_kv times; K, V read once. - dQ pass: for each Q block, reads all KV blocks of K, V. -> K, V each read N_Q/block_q times; Q read once. With block_q=block_kv=64 (default for d=128), this gives ~14x more reads than the naive single-pass model. Validated: 721 MB predicted vs 616 MB measured (0.85x ratio), vs the previous 10.5x error. The remaining ~15% over-prediction is causal masking saving upper-triangle blocks. Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
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.
Pull Request Template