Skip to content

[WIP] KDA projection GEMM epilogues: split-K/HTI epilogues, K tails, in-kernel gate-bwd sums, Kimi-K3/K3.1 tuned configs - #8754

Draft
huizzhan wants to merge 11 commits into
cursor/kda-fused-layer-bwd-8350from
cursor/kda-gemm-epilogue-tune-7cb7
Draft

huizzhan wants to merge 11 commits into
cursor/kda-fused-layer-bwd-8350from
cursor/kda-gemm-epilogue-tune-7cb7

Conversation

@huizzhan

@huizzhan huizzhan commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stacked on #8739 (up to 89bf56b: the _proj_gemm hook, the bf16 hi+lo split of the gate's lr, no HTI in KDA dispatch). This PR optimizes the gfx950 FlyDSL gemm_a16w16 epilogue fusions of the Kimi-K3 KDA layer:

  • the KDA gate modes (KDA_GATE, KDA_GATE_BWD, and their safe-gate versions)
  • SIGMOID and SIGMOID_MUL
  • the g_proj backward GEMMs: dx += dgp·g_proj (nn layout) and dW_g = dgpᵀ·x (tn layout)

It also adds a CSV-tuned config family (Kimi-K3 and Kimi-K3.1 tables), a tuner, a runtime lookup, and bisection switches.

Motivation

MI350X numbers at m=256 before this PR: gate 11.4 us, gate_bwd 15.9 us, sigmoid_mul 80 us.

  • sigmoid_mul (80 us) loses to an unfused GEMM plus elementwise. Aiter's tuned bf16 hgemm reads a weight of the same 176 MB in about 44 us.
  • Epilogues were limited to split_k=1 and the 128x128 full-tile kernel.
  • The gate wrappers repacked A_log/lower_bound/dt_bias on every call with torch.tensor and torch.cat.
  • The gate backward wrote a full [M, N] fp32 dterm only to sum it per head.
  • dW_g fell back to torch on K tails.

Changes

Kernel (aiter/ops/flydsl/kernels/gemm_a16w16_gfx950.py)

  • Deterministic split-K (split_reduce, full-tile kernel). It is used by every split-K epilogue and by every KDA split.
    • Each split stores its fp32 partial to a [split_k, M, N] workspace with plain stores: no atomics, no zeroed output, no signal wait.
    • After a release fence, each split takes a per-tile ticket atomic_add (AcqRel, agent scope). The split that takes the last ticket does an acquire, sums the partials in split order, applies the epilogue and stores. The same inputs give the same bits on every run.
    • Tickets advance by split_k per launch and are never reset.
    • The atomic split-K path that other callers use keeps [FlyDSL] [gfx950] Kimi-K3 KDA fused layer backward #8739's instruction mix exactly.
  • HTI C store fix.
    • Root cause. The HTI kernel's two wave rows run one barrier apart through the unsplit C store. That store is correct only while each row reads back the C rows it wrote: MMA rows alternate between the two in 16-row blocks, so a store pass must span 2 x 16 rows (block_threads * vec == 32 * block_n / 2). fp32 C and some bf16 tiles break this.
    • Fix. Such stores now let the lagging row catch up with one barrier and then run on fenced barriers. Every fp32-C store does the same; fp32 C reuses two quarters, so it fits at 256x256.
    • Unaffected tiles. Self-owned tiles keep [FlyDSL] [gfx950] Kimi-K3 KDA fused layer backward #8739's store. The 256x256 w2x4 bf16 instruction mix is unchanged except the kernarg loads.
    • The KDA dispatcher still never picks HTI.
  • K tails on the full-tile kernel. The A/B buffers are bounded, and K offsets past K point beyond that bound, so the loads zero-fill.
  • b_k_repeat. B holds K / repeat rows and is read once per K slice. The hi/lo gate GEMMs (K = 2R) read f_b twice instead of building a 6 MB [f_b | f_b] on every call.
  • Gate backward column sums. The backward gate modes write per-M-tile column sums of dterm and dz instead of the [M, N] dterm.
  • Epilogue arithmetic.
    • It runs on the fp32 accumulator for any output dtype.
    • Sigmoid/softplus use exp2/rcp.
    • The per-thread column parameters are loaded once.
    • A_log, dt_bias and a constexpr lower bound are passed as they are.

Wrappers (aiter/ops/flydsl/kda_layer_bwd.py, aiter/ops/kda_fused_layer.py)

  • Tiles come from the tuned table, with a fallback to defaults; config= overrides both.
  • kda_gate_proj_bwd returns (dz, dA_log, d dt_bias), and the _proj_gemm kda_gate_bwd branch passes that through.
  • The FlyDSL sigmoid_gate_proj stores in the weight dtype.
  • proj_gemm (from [FlyDSL] [gfx950] Kimi-K3 KDA fused layer backward #8739) uses the tuned tiles and the K-tail path.

Bisection switches. Each takes a comma list of gate, gate_bwd, sigmoid, fwd, dgrad, wgrad, or all:

  • AITER_KDA_GEMM_NO_SPLITK: forces split_k = 1.
  • AITER_KDA_GEMM_ALLOW_HTI: lets these paths run a tuned HTI row. HTI is off by default for every KDA path, and HTI rows run on the full-tile 128x256 kernel. AITER_KDA_GEMM_NO_HTI still forces the full-tile kernel.
  • AITER_KDA_GEMM_NO_KTAIL: a K tail falls back to torch.

Tuned configs (new family AITER_CONFIG_GEMM_A16W16_EPILOGUE)

  • The canonical CSVs are header-only.
  • model_configs/kimik3_* covers tokens 256–8192 plus 16384, at TP1.
  • model_configs/kimik3_1_* covers tokens 32768 and 65536, with H·K = 12288/TP for TP 1, 4 and 8.
  • The f_b gate rows have K = 256 (hi/lo); the dW_g rows have fp32 output (the _proj_gemm contract).
  • The rows are seeded with the default tiles (us blank, no HTI) until csrc/flydsl_gemm_epilogue/gemm_epilogue_tune.py runs on gfx950.
  • The config-shape guard covers the family.

HTI findings (e37dd89, e6c355d, b2545c1)

  • Every wrong result was an fp32-C HTI store. That covers the 1x8192 y NaN, the dW_g NaN at m=256, dg_proj's 6.6–7.3%, and 12.4% of the forced HTI dW_g tile. With AITER_KDA_GEMM_NO_HTI=all everything passed.
  • Mechanism. fp32 C stores 4 elements per lane, so a store pass spans 16 rows, and each wave row reads C rows that the other row (one barrier behind) has not written yet. e6c355d's fenced barriers could not order two rows that were out of step.
  • Layout. This does not depend on tn; tn was simply where fp32 C was selected.
  • Exposure on main. main rejects fp32-C HTI at 256x256, and its tuned gfx950 HTI rows are self-owned except one: bf16 64x256 w2x2 (block_threads * vec / (block_n / 2) = 16).
  • Plain-GEMM repro. test_hgemm_hti_store_exact checks main's behavior directly. On b4c7317 all 12 combinations are bit-exact on MI350X.
  • [FlyDSL] [gfx950] Kimi-K3 KDA fused layer backward #8739's m=100 dW_g error was the torch fallback on unrounded dgp (fixed in [FlyDSL] [gfx950] Kimi-K3 KDA fused layer backward #8739 89bf56b), not HTI.

Split-K finding (b4c7317)

  • The failure. test_kda_wgrad_split's ft_sk2 row (atomic split-K, tn, hk=1536) differed from the exact result in a few elements, on two GPUs. Its inputs are integers, so every fp32 partial sum is exact in any order. Accumulation order cannot explain a difference: the atomic protocol lost or doubled a contribution.
  • The response. The KDA dispatch (today only the dgrad at m ≤ 256, split_k=2) and every split-K epilogue now run split_reduce, and the test's split rows grade that path bit-exactly. The atomic protocol itself, used by other callers, is unchanged in this PR.

Performance (if applicable)

These are estimates, not measurements.

Hardware Workload Before Expected after
MI350X gate fwd, m=256 11.4 us ~5–6 us
MI350X gate bwd, m=256 15.9 us ~7–9 us
MI350X sigmoid_mul, m=256 80 us ~40–50 us
MI350X sigmoid_mul / dgrad / wgrad, m ≥ 1024 128x128 FT kernel 128x256 full-tile kernel (HTI stays out of KDA dispatch)
MI350X dx (nn), m=256 112 workgroups ~2x (split-K)

Testing

  • Unit tests added or updated
  • Performance benchmarks run (needs a gfx950 card)
  • Tested on MI250X
  • Tested on MI300X

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

Within the #8739 stack only:

  • kda_gate_proj_bwd and _proj_gemm(epilogue="kda_gate_bwd") return (dz, dA_log, ddt).
  • gemm_a16w16 takes a_log, dt_bias and lower_bound separately, plus colsum and b_k_repeat; a split-K epilogue now requires (and gets) split_reduce.

PR Self-Review

Reviewed revision

7bff7dc

Behavior and intent

The behavior changes are listed above.

Validation and unverified scope

All of this ran offline with FlyDSL 0.5.3 compiling for gfx950 (COMPILE_ONLY) on a CPU VM:

  • Kernel compiles. The targeted kernels build, including b_k_repeat, split_reduce (tn with and without a K tail, nn, epilogues incl. gate backward), and split-K K-tail tn. All 12 HTI store test configs build: nt/tn × bf16/fp32 × 256x256 w2x4, 64x256 w2x2 and 128x128 w2x2. All 16 seed configs (no HTI) build without spills.
  • Dispatch. No KDA path picks HTI for any token count from 256 to 65536.
  • Collision guard. It passes (34 tests), and a duplicate planted across the K3 and K3.1 files is caught.
  • CPU layer. The fused layer backward on CPU matches autograd to about 1e-6.
  • Lint. ruff and black are clean.

Unverified, needs the GPU: the op_tests, and every perf number above.

PR-specific question

N/A.

Changed op tests

  • op_tests/test_kda_fused_layer_bwd.py:
    • test_kda_proj compares each fused GEMM with unfused_torch (the layer's own non-fused path) and unfused_tgemm (aiter's tuned bf16 tgemm.mm plus elementwise), over -m token counts. The gate GEMMs get the same hi/lo split.
    • test_kda_wgrad_split is new. It runs dW_g on integer inputs (exact in fp32) with a NaN-filled output buffer, and compares each config against the exact result with torch.equal. The configs are the dispatcher's pick, split_k 1/2/4 (deterministic split_reduce, as the KDA dispatch runs it), a bf16-output full-tile run, and HTI 256x256 with bf16 output ([FlyDSL] [gfx950] Kimi-K3 KDA fused layer backward #8739's store) and fp32 output (the realigned store). Tokens 256 and 300, TP 1 and 8. All rows are reported before the assert.
    • The gate_proj_bwd stage compares (dz, dA_log, d dt_bias).
  • op_tests/test_flydsl_hgemm.py: test_hgemm_hti_store_exact is new. It runs plain gemm_a16w16 (no KDA) on HTI 256x256 w2x4 (self-owned control), 64x256 w2x2 (a tuned main row) and 128x128 w2x2. It covers nt and tn, bf16 and fp32 out, with integer inputs and 20 NaN-filled launches each, compared bit-exactly. It would fail on the unfixed cross-row store.
  • op_tests/tuning_tests/test_config_shape_collision.py: adds the new family to the guard.
Open in Web Open in Cursor 

cursoragent and others added 5 commits November 26, 2026 10:47
…gate-bwd column sums

- split-K with an epilogue: the last split of a tile (per-tile ticket,
  agent-scope release/acquire) applies the epilogue on the fp32 sums; a
  16-bit output accumulates in an fp32 workspace
- HTI 256x256 takes the forward epilogues; its unsplit store reuses two
  quarter tiles of shared C, so fp32 output / fp32 C fits 128 KB LDS
- K not a multiple of block_k on the full-tile kernel: bounded buffers and
  masked K offsets zero-fill past K (dW_g = dgp^T x at any token count)
- KDA gate bwd writes per-M-tile column sums of dterm and dz instead of an
  [M, N] dterm; dA_log and d dt_bias are a short reduction
- epilogues run on the fp32 accumulator for 16-bit outputs; exp2/rcp math,
  per-thread column parameters hoisted out of the store loop; A_log,
  dt_bias and the lower bound are passed as is (no per-call cat/H2D)
- KDA wrappers: shape-aware tiles, sigmoid gate stored in the weight dtype,
  dW_g in the weight dtype, no torch fallback; op_test compares the fused
  GEMMs with unfused GEMM + element-wise baselines over a token sweep

Co-authored-by: Sealeen Zhang <[email protected]>
…tuner, runtime lookup

New config family AITER_CONFIG_GEMM_A16W16_EPILOGUE (header-only canonical
CSV, key M,N,K,layout,epilogue,dtype,outdtype + cu_num/gfx), merged with
model_configs/kimik3_* (tokens 256..8192 and 16384, TP1) and kimik3_1_*
(tokens 32768/65536, H*K = 12288/TP for TP 1/4/8). Rows are seeded with the
default tiles (us blank, kernelName default_*) until
csrc/flydsl_gemm_epilogue/gemm_epilogue_tune.py runs on gfx950. Lookup takes
the nearest tuned token count at or above the call's. The config-shape guard
covers the family.

Co-authored-by: Sealeen Zhang <[email protected]>
…es; dW_g split_k bit-exact test

An fp32 C quarter of the 256x256 HTI tile is 64 KB and spans both A stages
(and the second both B stages). The interleaved store wrote quarter 0 right
after an unfenced s_barrier while other waves' last A/B fragment reads were
still in flight, so those MMAs read C values: nondeterministic garbage/NaN.
It hit every fp32-C HTI 256x256 launch (sigmoid_mul with its bf16 store,
the dgrad and wgrad GEMMs) at M >= 1024 and the tn wgrad at any token count
whose K splits into whole tile pairs (m = 256).

fp32 C now stores after every MMA and a fenced barrier, with fenced
barriers around the reuse of quarters 0/1 by the second half. 16-bit C
keeps the original four-quarter interleaved store (instruction mix
identical to before).

Bisection switches (comma list of gate, gate_bwd, sigmoid, fwd, dgrad,
wgrad, or all): AITER_KDA_GEMM_NO_SPLITK, AITER_KDA_GEMM_NO_HTI,
AITER_KDA_GEMM_NO_KTAIL.

test_kda_wgrad_split: dW_g on integer inputs (exact in fp32) for the
dispatcher's pick, split_k 1/2/4 and HTI 256x256, NaN-filled output,
torch.equal against the exact result, tokens 256 and 300, TP 1 and 8.

Co-authored-by: Sealeen Zhang <[email protected]>
…lr), dW_g rows with fp32 output

ded3d7a splits the fp32 lr into bf16 hi | lo for the gate GEMMs (K 128 ->
256) and returns every projection GEMM in fp32 (the _proj_gemm contract),
so the f_b gate rows move to K = 256 and the tn rows to outdtype fp32. The
tuner times the gate rows with B = f_b read once per half (b_k_repeat=2).

Co-authored-by: Sealeen Zhang <[email protected]>
@cursor
cursor Bot force-pushed the cursor/kda-gemm-epilogue-tune-7cb7 branch from e37dd89 to e6c355d Compare November 26, 2026 10:56
…t test covers bf16 HTI

e6c355d's fenced fp32-C HTI store did not fix the wrong dW_g / dg_proj
elements on MI350X (6.6-7.3% in the layer table, 12.4% of the forced HTI
256x256 tile in test_kda_wgrad_split, max err ~1.0, nondeterministic),
while AITER_KDA_GEMM_NO_HTI=all passes everything. An error of ~1.0 on O(16)
outputs points at a stale A/B operand in the HTI pipeline rather than at
the C store.

- the HTI C store, shared C size and LDS check are #8739's again, so an
  fp32-C HTI 256x256 is rejected as before
- the KDA dispatcher never runs HTI: defaults and seeds use the full-tile
  128x256 kernel (what NO_HTI=all ran), tuned HTI rows run on it too, and
  AITER_KDA_GEMM_ALLOW_HTI=<paths> opts back in for investigation
- test_kda_wgrad_split adds bf16-output full-tile and HTI 256x256 rows
  (bit-exact too: one rounding of the exact sum); the HTI bf16 row is
  #8739's own HTI path on the tn layout. All rows are reported before the
  assert.

Co-authored-by: Sealeen Zhang <[email protected]>
@cursor cursor Bot mentioned this pull request Nov 26, 2026
12 of 19 tasks
cursoragent and others added 2 commits November 26, 2026 11:19
… reads across them; plain-GEMM bit-exact HTI store test

The HTI kernel's two wave rows (wid // n_waves) run one barrier apart from
the prologue on; only the split-K store realigns them. The unsplit store is
correct out of step only while each row reads back the C rows it wrote: MMA
rows alternate between the two in 16-row blocks, so a store pass must span
2 * 16 rows (block_threads * vec == 32 * block_n / 2). fp32 C (vec 4) and
e.g. 64x256 w2x2 bf16 (a tuned main row) break that: a row reads C the
other has not written yet. The fenced barriers of e6c355d could not help
while the rows were out of step.

Such stores (and every fp32-C store, whose two reused quarters alias all A
and B stages) now let the lagging row catch up with one barrier, then run
on fenced barriers. Self-owned stores (256x256 w2x4 bf16 and the other
tuned HTI rows) keep #8739's code; the 256x256 w2x4 bf16 instruction mix is
unchanged except the kernarg loads.

test_flydsl_hgemm: test_hgemm_hti_store_exact runs plain gemm_a16w16 on
HTI 256x256 w2x4, 64x256 w2x2 and 128x128 w2x2, nt and tn, bf16 and fp32
out, integer inputs, 20 NaN-filled launches each, bit-exact. The KDA
dispatcher still never picks HTI.

Co-authored-by: Sealeen Zhang <[email protected]>
… into cursor/kda-gemm-epilogue-tune-7cb7

Co-authored-by: Sealeen Zhang <[email protected]>

# Conflicts:
#	aiter/ops/flydsl/kda_layer_bwd.py
#	aiter/ops/kda_fused_layer.py
cursoragent and others added 3 commits November 26, 2026 11:44
… and every split-K epilogue

test_kda_wgrad_split's ft_sk2 at hk=1536 differed from the exact result on
two GPUs. Its inputs are integers, so every fp32 sum is exact in any order:
the atomic path (split 0 zeroes C, the other splits add after its signal)
lost or doubled a contribution, which no summation order explains.

split_reduce = split_k makes split-K atomic-free and deterministic: each
split stores its fp32 partial to a [split_k, M, N] workspace, takes the
per-tile ticket after a release fence, and the last split sums the partials
in split order after an acquire (the ticket pattern of the split-K
epilogues). It needs no zeroed output and no signal wait. Every split-K
epilogue uses it (the atomic epilogue path is gone), and the KDA dispatch
sets it on every split_k > 1 (today the m <= 512 dgrad). The atomic path
for other callers keeps #8739's instruction mix exactly.

test_kda_wgrad_split's split rows run split_reduce, still bit-exact.

Co-authored-by: Sealeen Zhang <[email protected]>
… into cursor/kda-gemm-epilogue-tune-7cb7

Co-authored-by: Sealeen Zhang <[email protected]>
… into cursor/kda-gemm-epilogue-tune-7cb7

Co-authored-by: Sealeen Zhang <[email protected]>

# Conflicts:
#	op_tests/test_kda_fused_layer_bwd.py
cursor Bot pushed a commit that referenced this pull request Nov 26, 2026
…raded self-check, non-aborting self-checks; #8744 13275a2) into kda-integration

Conflicts in op_tests/test_kda_fused_layer_bwd.py:
- test_kda_save_fwd_matches_recompute takes #8739's new signature
  (chunk_bwd=); #8754's config_name_of and #8757's MXFP8 tests are kept
- main(): #8739's non-aborting self-checks; #8757's
  test_kda_mxfp8_weight_cache joins them the same way (logged and recorded
  in SELF_CHECK_FAILURES instead of raising)

Co-authored-by: Sealeen Zhang <[email protected]>

This branch has not been deployed

No deployments
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