Skip to content

initial implementation#757

Draft
kyle-hoffmeyer wants to merge 6 commits into
mainfrom
feat/efficienct_attn_perf_models
Draft

initial implementation#757
kyle-hoffmeyer wants to merge 6 commits into
mainfrom
feat/efficienct_attn_perf_models

Conversation

@kyle-hoffmeyer

Copy link
Copy Markdown
Collaborator

Pull Request Template

Note to AMDers:
This is a public repository. Please do not upload any confidential or customer data. Make sure all such data has been anonymized or removed before making this PR. If you need to attach any private files or links, please insert a Internal OneDrive Link or a Jira Ticket Link instead.

kyle-hoffmeyer and others added 6 commits June 19, 2026 20:32
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant