Repository navigation
[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
Conversation
…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]>
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
Bot
force-pushed
the
cursor/kda-gemm-epilogue-tune-7cb7
branch
from
November 26, 2026 10:56
e37dd89 to
e6c355d
Compare
…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]>
12 of 19 tasks
… 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
5 of 11 tasks
… 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stacked on #8739 (up to 89bf56b: the
_proj_gemmhook, the bf16 hi+lo split of the gate's lr, no HTI in KDA dispatch). This PR optimizes the gfx950 FlyDSLgemm_a16w16epilogue fusions of the Kimi-K3 KDA layer:KDA_GATE,KDA_GATE_BWD, and their safe-gate versions)SIGMOIDandSIGMOID_MULdx += dgp·g_proj(nn layout) anddW_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.
split_k=1and the 128x128 full-tile kernel.A_log/lower_bound/dt_biason every call withtorch.tensorandtorch.cat.[M, N]fp32dtermonly to sum it per head.Changes
Kernel (
aiter/ops/flydsl/kernels/gemm_a16w16_gfx950.py)split_reduce, full-tile kernel). It is used by every split-K epilogue and by every KDA split.[split_k, M, N]workspace with plain stores: no atomics, no zeroed output, no signal wait.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.split_kper launch and are never reset.block_threads * vec == 32 * block_n / 2). fp32 C and some bf16 tiles break this.b_k_repeat. B holds K / repeat rows and is read once per K slice. The hi/lo gate GEMMs (K = 2R) readf_btwice instead of building a 6 MB[f_b | f_b]on every call.dtermanddzinstead of the[M, N]dterm.exp2/rcp.A_log,dt_biasand a constexpr lower bound are passed as they are.Wrappers (
aiter/ops/flydsl/kda_layer_bwd.py,aiter/ops/kda_fused_layer.py)config=overrides both.kda_gate_proj_bwdreturns(dz, dA_log, d dt_bias), and the_proj_gemmkda_gate_bwdbranch passes that through.sigmoid_gate_projstores 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, orall: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_HTIstill 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)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._proj_gemmcontract).usblank, no HTI) untilcsrc/flydsl_gemm_epilogue/gemm_epilogue_tune.pyruns on gfx950.HTI findings (e37dd89, e6c355d, b2545c1)
AITER_KDA_GEMM_NO_HTI=alleverything passed.block_threads * vec / (block_n / 2)= 16).test_hgemm_hti_store_exactchecks main's behavior directly. On b4c7317 all 12 combinations are bit-exact on MI350X.Split-K finding (b4c7317)
test_kda_wgrad_split'sft_sk2row (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.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.
Testing
Documentation
Dependencies
Breaking Changes
Within the #8739 stack only:
kda_gate_proj_bwdand_proj_gemm(epilogue="kda_gate_bwd")return(dz, dA_log, ddt).gemm_a16w16takesa_log,dt_biasandlower_boundseparately, pluscolsumandb_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: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.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_projcompares each fused GEMM withunfused_torch(the layer's own non-fused path) andunfused_tgemm(aiter's tuned bf16tgemm.mmplus elementwise), over-mtoken counts. The gate GEMMs get the same hi/lo split.test_kda_wgrad_splitis 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 withtorch.equal. The configs are the dispatcher's pick, split_k 1/2/4 (deterministicsplit_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.gate_proj_bwdstage compares(dz, dA_log, d dt_bias).op_tests/test_flydsl_hgemm.py:test_hgemm_hti_store_exactis new. It runs plaingemm_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.