Skip to content

perf(metal): widen four-row flash KV tiles - #89

Merged
mxaddict merged 6 commits into
kryptic-sh:mainfrom
digitaloten:scratch/metal-post-flash-profile
Jul 30, 2026
Merged

perf(metal): widen four-row flash KV tiles#89
mxaddict merged 6 commits into
kryptic-sh:mainfrom
digitaloten:scratch/metal-post-flash-profile

Conversation

@digitaloten

@digitaloten digitaloten commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Summary

  • specialize cooperative four-row, hd128 flash attention for 128 KV positions per tile
  • lazily select the C128 pipeline and retain the existing C64 pipeline as fallback
  • cover causal tail and sliding-window masking at the C128 boundary

Verification

  • cargo fmt --all -- --check
  • cargo test -p infr-metal attention_flash128_policy_is_exact
  • cargo test -p infr-metal --test kernel_names
  • cargo check -p infr-metal --all-targets --locked
  • Metal 32023.883 compiled common.metal + attention.metal; AIR exports attnflash2_c128_f16kv_hd128
  • dispatch test was confirmed to fail when C128 selection was disabled, then pass after restoration
  • all seven PR CI jobs passed, including the expanded causal-tail/sliding-window parity test on a real Metal runner

Performance evidence

Paired same-runner A/B at rows=4, n_head=16, n_kv=8, head_dim=128, kv_len=4096, pos=4092, f16 KV, causal mask. Each process used deterministic inputs, at least 50 warmups and one second of GPU warmup, then 10 calibrated 2–5 second blocks; block 0 was discarded. C128/C64 order alternated across five macos-15 runners. The metric is one-op synchronous MetalBackend::execute wall latency, not isolated kernel time.

Pair C128 median (µs) C64 median (µs) Ratio
1 AB 1911.881 2164.915 0.883121
2 BA 1676.385 1957.752 0.856281
3 AB 1629.594 1951.521 0.835038
4 BA 2045.393 2174.325 0.940703
5 AB 2454.246 2778.088 0.883430

Runner-level geometric-mean ratio: 0.879009 (12.099% faster). Runner-level bootstrap 95% CI: [0.851989, 0.911652]. The predeclared win criterion was an upper bound below 0.95; this passes.

Evidence run: https://github.com/digitaloten/infr/actions/runs/30352605155

Add a 128-position KV specialization for the four-row, hd128 flash
attention shape. Runtime dispatch prefers it when the pipeline fits and
retains the existing 64-position specialization as fallback.
Keep the C128 shape policy inside the runtime-used resolver and update the
softmax ownership comment for both KV chunk widths.
Probe the C128 pipeline only for its four-row target shape and compile the
standard flash pipeline only when C128 cannot run. This avoids paying for
both specializations during pipeline warmup.
Exercise four-row C128 flash attention across a non-aligned second KV
chunk and with a sliding-window lower bound inside the first chunk.
@digitaloten
digitaloten marked this pull request as ready for review July 28, 2026 11:31
@mxaddict
mxaddict merged commit 48af860 into kryptic-sh:main Jul 30, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants