← Back to Pull requests

perf(ck_tile): faster FMHA backward on gfx1250 #14470

Closed
unknown closed this pull request without merging on Nov 04, 2026 01:54 UTC
View on GitHub
unknown unknown commented on Oct 27, 2026 15:04 UTC
Author

https://amd-hub.atlassian.net/browse/AICK-2158

Motivation

The gfx1250 FMHA backward (bf16, mainly d128) was well behind the hand-written ASM kernel:

- Training shapes: the d128 TDM pipeline (tdm_kr_ktr) was latency-bound.
- Decode shapes (seqlen_q <= 32): the trload pipeline spent most of each kv step writing dK/dV and waiting on K/V.
- Side kernels: dot_do_o and convert_dq fell back to per-element accesses for padded head dims (d48/d80/d96/d160/d192).

This PR speeds up all three, and fixes a dS LDS write-after-read race in tdm_kr_ktr (#13473).

Technical Details

### d128 training pipeline
(block_fmha_bwd_dq_dk_dv_pipeline_tdm_kr_ktr.hpp, block_gemm_areg_breg_creg_v1.hpp)

- Tuned schedule (kTunedSched) for the unpadded 64x128 d128 tile with no bias/dropout/determinism: reorders sync, TDM issue and atomic ordering to overlap better, and defers dQ
atomics so one buffer_atomic_add_f32 covers a full 128 B line. Padded instances keep the original schedule — the tuned one is 6–17% slower there.
- Block GEMM gained grouped K-chain / operand-reuse helpers to support the new schedule.
- dS race fix (#13473): with ≥2 Q/dO slots, nothing ordered iteration i's dS LDS reads before iteration i+1's dS store. The tuned schedule now double-buffers dS (no barrier
needed on its critical path); other schedules add a block_sync_lds() before the store.

### Decode pipeline
(block_fmha_bwd_dq_dk_dv_pipeline_trload_qr_qtr_dor.hpp, TDM policy)

- K/V prefetch: next K/V tile loads into LDS right after the current one is copied to registers, overlapping the load with the rest of the iteration.
- dQ stores: written as 16-byte vectors via a TransposeC dS·K GEMM (kDQAlongHdim), replacing 128 per-element stores (unpadded hdim).
- dK/dV stores: moved from one 16-byte buffer store per lane/row to a double-buffered LDS stage + tensor_store_from_lds, so tile i's store overlaps tile i+1's compute.
Masked-out kv blocks are zero-filled from a single zeroed stage buffer. This was the dominant cost per kv tile (~5–6k of ~8k cycles) before the change.
- Dispatch (codegen): one d128 decode tile now serves both masked and unmasked shapes once batch nhead_q >= 768.

### Side kernels
(fmha_bwd_kernel.hpp, block_fmha_bwd_convert_dq.hpp)

- With hdim padding, dot_do_o and convert_dq pick a 16-byte vector path at runtime when hdim/strides/pointers are aligned, falling back to per-element otherwise. O stays
non-temporal on the vector path, as in #13465.

### Build
(example/ck_tile/01_fmha/CMakeLists.txt)*

- FMHA_BWD_GFX1250_D128_BF16_MAX_ILP (default ON): gfx1250 bf16 d128 TDM training instances use -amdgpu-sched-strategy=max-ilp (no-mask instances also get expert scheduling),
gated on compiler support.
- Blob regex now accepts both _ntrload and _ntl; CMake reports/warns on match count.

Test Plan

Hardware: gfx1250, ROCm 7.15 container (clang 23). Only gfx1250 was run; other targets rely on CI.

- Correctness: tile_example_fmha_bwd -v=1 against the host reference across general/decode/group-mode/padded-hdim configs (full list below).
- Performance: the 59-case run_all.sh set (bf16), 3 runs each, best TFLOPS, vs ff55582 (develop before this work).
- ASM comparison: d128 b2 h8, s=2048/4096/32768, mask 0/1, total time vs the aiter gfx1250 ASM backward.

<details>
<summary>Full correctness matrix</summary>

- general: d36–d256, d_v != d, bshd/bhsd, batch and group mode, GQA, dropout, sink gradient, fp16, deterministic
- decode: d48/d64/d96/d120/d128, mask t/b, window masks b:16,0 / b:40,2 / t:100,3 / xb:64, GQA, dropout, alibi, group mode
- group-mode d128 training with mask/no-mask/GQA (now get max-ilp)
- padded hdim with sink gradient (d72/d96/d160/d192+d_v128/d256)

</details>

Test Result

Correctness: every case passes -v=1. Same 4 configs print "not supported" as before this PR (3× -deterministic=1: d96, d128 mask t, d80 group; decode with -bias=e).

Throughput (59 cases, geomean vs ff55582): 1.36×, no case slower.

| Group | Cases | Geomean | Range |
|---|---|---|---|
| Decode (s_q <= 32) | 13 | 2.26× | 1.89–2.93× |
| d128 training | 32 | 1.18× | 1.06–1.34× |
| Other hdims (d48/d64/d80/d96/…) | 14 | 1.17× | 0.98–1.36× |

Selected cases (TFLOPS):

| Case | Before | After |
|---|---|---|
| d128 b1280 h2 s8/232 mask=0 | 44.6 | 97.8 |
| d128 b1280 h2 s8/532 mask=t | 73.1 | 181.1 |
| d64 b128 h16 s32/8192 mask=0 | 157.9 | 463.5 |
| d128 b64 h32 s16/4096 mask=2 | 128.0 | 277.0 |
| d128 b2 h8 s4096 mask=0 | 981.2 | 1312.8 |
| d128 b2 h8 s32768 mask=0 | 1130.1 | 1509.2 |
| d128 b2 h8 s32768 mask=1 | 2238.4 | 2808.1 |
| d128 b1 h32/8 s32768 mask=1 | 2229.6 | 2806.9 |
| d48 b8 h16 s2048 mask=1 | 722.0 | 914.3 |
| d96 b4 h32 s4096 mask=1 | 1071.5 | 1209.7 |

ASM gap closed from ~43–61% (before) to ~58–74% (after) of ASM throughput across d128 b2 h8, s2048/4096/32768, mask 0/1.

<details>
<summary>ASM time / CK time detail</summary>

| d128 b2 h8 | s2048 | s4096 | s32768 |
|---|---|---|---|
| mask=0, before | 61.7% | 47.0% | 43.0% |
| mask=0, after | 67.0% | 61.9% | 57.8% |
| mask=1, before | 60.7% | 49.5% | 48.5% |
| mask=1, after | 74.3% | 57.2% | 60.9% |

</details>

Submission Checklist

  • Look over the contributing guidelines at https://github.com/ROCm/TheRock/blob/main/GOVERNANCE.md#pull-requests.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: DDEle Review duration: 10729m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #14470"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%