**Update: the DPP finding now has real-kernel validation — Flash Attention + DME on MI300X, and a real LLM attention layer on top**
When I posted the original wave-reduce result, the fair open question was: *does the
microbenchmark speedup survive in a real kernel, or does memory traffic wash it out?*
I built a Flash Attention (FA2) kernel on MI300X to answer that, then took it one step
further and validated it against an actual transformer layer. Short version: the
advantage is real but domain-specific, a second CDNA3 feature (the Data Movement
Engine) carried as much of the speedup as the DPP reductions, and the kernel
reproduces a real LLM attention layer within fp16 tolerance.
**1) Flash Attention, four build-order steps** (Br=16, Bc=64, D=64, seqLen=512, 8 heads,
gfx942 / ROCm 7.2). All PASS correctness vs fp32 CPU ref.
| Step | Change | Median | Speedup |
|------|--------|--------|---------|
| 1 | naive (`__shfl` softmax, dot product) | 97.2 µs | 1.00× |
| 2 | + full-DPP softmax | 90.5 µs | 1.074× |
| 3 | + DME async double-buffer K/V | **82.4 µs** | **1.18×** |
| 4 | + MFMA `16x16x16` for Q·Kᵀ and P·V | 87.0 µs | 1.118× |
**18% end-to-end** from two primitive substitutions. rocprofv3 isolates the cause:
- DPP softmax: **−8.6% SQ_INSTS_LDS** (the `ds_bpermute` from `__shfl` is gone)
- DME prefetch: **−44.7% SQ_INSTS_VMEM_RD** — the hardware signature of load/compute overlap
**What I’m reporting honestly, including the negatives:**
- **The big microbenchmark number is reduction-domain-specific.** The 1.79× reduce
result holds for short, register-bound rows. In memory-bound kernels (LayerNorm /
Softmax at 512 cols) it collapses to 1–3% — DPP is still fastest and still zero-LDS,
but the headline number doesn’t transfer. Worth stating plainly.
- **The zero-LDS property is wave-scoped, not block-scoped.** Block-level reduce ties
hipCUB BlockReduce *exactly* (1.000×) at every block size.
- **MFMA did not win at head dim 64** (Step 4 is 0.95× vs Step 3) — `16x16x16` tiles
are too small to saturate the matrix cores at small D. But at **D=128** the MFMA path
reaches **10.45 TFLOPS vs 6.19 at D=64 (+69%)** — it wins once the head dim is large
enough, exactly as expected.
- A Bc tile-size sweep refuted the “freeing LDS lets you grow the tile” idea: Bc=64
(= wave width) is the sweet spot; Bc=96 fits in LDS but is slower.
**Reusable by-product:** getting Step 4 correct required pinning down the
`v_mfma_f32_16x16x16f16` wave64 register-to-matrix lane mapping, which isn’t obvious
from the ISA docs. Verified empirically with a full 16×16×16 GEMM-vs-reference probe
(only one layout combination matched, err 0.001):
```
A in : lane L, frag f → A[row = L%16 ][k = (L/16)*4 + f]
B in : lane L, frag f → B[k = (L/16)*4+f][col = L%16 ]
C/out : lane L, frag f → C[row = (L/16)*4+f][col = L%16 ]
```
**2) Generalized to LLM-realistic shapes, then validated against a real layer.**
I extended the kernel to runtime seqLen, GQA (grouped-query attention), and causal
masking, then ran two checks:
- **seqLen sweep 512→4096 at D=128** (MHA, full): the attention cost fits a clean
power law, **n^1.90 (R²=0.9964)** — the expected quadratic regime, measured on
hardware rather than assumed.
- **Real-layer validation:** I exported layer 0 of **Qwen2.5-0.5B** (real weights +
RoPE + GQA 14q/2kv + causal) and ran the full pipeline (Q/K/V proj → RoPE → my FA
kernel → O proj) against the fp32 HF reference. Max error through the whole layer was
**4.34% of the signal scale** — well within fp16 tolerance for a kernel + two naive
fp16 GEMMs vs an fp32 reference. The kernel correctly reproduces a real transformer
attention layer, not just synthetic shapes. (One gotcha for anyone doing the same:
Qwen2.5 puts a bias on Q/K/V projections — easy to miss when your GEMM is bias-free.)
**3) Where this is heading (and what’s still an open gate, stated up front).**
These kernels feed a small research collaboration on inference cost over *compressed*
state. The idea: instead of paying O(n²) flat-context attention over every raw
artifact, run inference over a compressed governance state V whose cost should scale
with its structural complexity κ(V), not with the artifact count n. The measured
n^1.90 curve above is the **O(n²) baseline** of that contrast — the side that’s
already nailed down on hardware.
I want to be precise about what is and isn’t done here: the **baseline is measured**,
the **kernel is validated on a real layer**, and the compressed-state side has a
concrete κ from a separate (CPU-side) line of work. What is **not** yet done is the
head-to-head cost contrast at scale on MI300X — that’s the next VM run, and it’s
pre-registered, not hand-waved. I’m reporting the pieces that are solid and flagging
the open gate rather than claiming the efficiency result before it’s earned. (The
same discipline as the negatives above — I’d rather post a clean gate than an
oversold number.)
Everything — kernels, the robustness/GQA/causal sweep, the real-layer validator,
rocprofv3 results, the DME C++ wrapper and its verified semantics — is in the repo.
Happy to compare notes, especially with anyone benchmarking MFMA/GEMM tile sizes on
MI300X — the D=64-vs-D=128 MFMA crossover above might be relevant to your tiling choices.
—