Repository navigation
[HIP] [FlyDSL] FlyDSL gemm_decode: small-M dense GEMM kernels (BF16) - #4114
vedenev-amd wants to merge 110 commits into
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
There was a problem hiding this comment.
Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit
ruff
Multiple statements on one line (semicolon)
aiter/aiter/ops/flydsl/kernels/gemm_decode.py
Line 464 in 7f2d377
Multiple statements on one line (semicolon)
aiter/aiter/ops/flydsl/kernels/gemm_decode.py
Line 464 in 7f2d377
Multiple statements on one line (colon)
aiter/aiter/ops/flydsl/kernels/gemm_decode.py
Line 467 in 7f2d377
Do not assign a lambda expression, use a def
aiter/aiter/ops/flydsl/kernels/gemm_decode.py
Line 469 in 7f2d377
Add predicated K tails and partial-column handling, use RNE output conversion, and expose named compile-time defaults. Strengthen gfx950 tests with strict assertions, sentinels, boundary shapes, and conversion checks. Co-authored-by: Cursor <[email protected]>
Benchmark the direct FlyDSL kernel against the fast and fallback wave split-K kernels and explicit hipBLASLt using shared inputs, strict correctness checks, warmed event timing, spread, throughput, and relative speedup. Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Retire duplicate decode paths in favor of unified tuned kernels, while adding architecture-isolated AOT and production small-M tuning coverage. Co-authored-by: Cursor <[email protected]>
Restrict small-M support and remove superseded identities while hardening AOT and optional-backend validation around the production paths. Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Move decode dispatch into the shared GEMM API, complete BF16 tuner integration, and restore the architecture-specific small-M path while retaining focused GPU correctness coverage. Co-authored-by: Cursor <[email protected]>
Use one tensor-derived configuration entry point and perform optional bias directly in Wave and BlockMFMA epilogues, keeping persistence parsing outside the public kernel API. Co-authored-by: Cursor <[email protected]>
…decode Co-authored-by: Cursor <[email protected]> # Conflicts: # aiter/aot/flydsl/gemm.py
Co-authored-by: Cursor <[email protected]>
…o longer sends it to torch. On main an M = 3 call there ran the M = 4 hgemm row through padded M. Decode rows are exact-M, so once the M = 4 row became a decode row M = 3 fell through to the default. The new row names the hgemm kernel main used (t16x32x128x6_ks1, 17.55 us at M = 4). A CSV check under tuning_tests keeps every decode M = 4 row paired with an M = 3 row, except the five cells that had no M = 4 row before the decode rows and so already took the default at M = 3. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n A's device, so a call on a second GPU no longer brings the process down. A handle is bound to the device it was loaded on, and the decode launchers cached one per kernel for every device, launching on the caller's current device. In one process that ran a decode row on cuda:0, the same row on cuda:1 died with a GPU coredump. With specialization_key=A.device and the launch under torch.cuda.device(A.device), as aiter/ops/flydsl/AGENTS.md asks, that call now raises hipErrorInvalidDevice like every other FlyDSL kernel tried (sorted_sum, causal_conv1d_update). Running it there needs FlyDSL to load the module per device: ROCm/FlyDSL#1536. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… of raising. The decode launch fuses the bias and requires it to have A's dtype, but decode_layout_supported, the check tgemm.mm makes before taking a decode row, only looked at the bias layout. A decode row with bias=True would have turned a call the hgemm path accepts (it adds such a bias after the GEMM) into a ValueError. No shipped decode row has a bias, so nothing hit it yet. The fallback log now names the bias dtype too. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… does, not from GPU_ARCHS. get_gfx() follows GPU_ARCHS, while tgemm.mm and the decode tuner candidates use get_gfx_runtime(). With GPU_ARCHS=gfx950 on an MI325X the test compared gfx950 names against the gfx942 kernels the tuner produced and failed in the name round trip; with get_gfx_runtime() it checks the rows the dispatcher actually serves, as test_flydsl_decode_gemm.py already does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ring, so the message is built only when it is logged. This matches _warn_decode_layout_fallback next to it and the review-pr logging rule. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… so the cache_modifier change belonged in another PR. No call site under aiter/ops/flydsl sets cache_modifier on a GTensor, so the change was inert, but it altered shared code every FlyDSL kernel imports. The decode and dispatch op tests pass without it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@xytpai Thanks for the re-review. All six points are addressed at e6d7912, one commit each. 1. M = 3 at gfx950 (6288, 7168) ( Fixed in 338b013. The cell now has an M = 3 row with the hgemm kernel main used through padded M ( Following your correction, the check lives with the CSV validation, in 2. Device in the compiled-handle key ( Fixed in e49d0c0: both decode launchers pass I tested it in one process on two MI325X, with the same decode row first on cuda:0 and then on cuda:1:
So the key alone does not make the second GPU work. Two FlyDSL kernels from main fail the same way on cuda:1 after cuda:0 ( 3. Bias dtype in Fixed in 4048e9f. 4. Fixed in 4ea0a00: the test takes 5. f-string in the logging call ( Fixed in 891c50d: the warning passes 6. Dropped in e6d7912: At e6d7912 on MI325X (gfx942): |
…again; keep main's fused-quant and FlyDSL-only AOT variants alongside the decode strided_a variant. The one conflict was in _variants of aiter/aot/flydsl/gemm.py, where main (#8702) added the batched GEMM fused-quant entry and the rule that non-FlyDSL rows have no variant, and this branch added the strided_a variant for decode rows with M > 1. Decode rows are FlyDSL rows, so both apply: the AOT replay of the Kimi-K3 bf16 CSV still lists all 154 decode rows, with a strided_a job for each M > 1 row. Co-Authored-By: Claude Opus 5.5 <[email protected]>
xytpai
left a comment
There was a problem hiding this comment.
Re-checked at 8d365e9. I read the six fix commits and the merge of main.
Fixed
All six are in, and I have nothing further on the code.
- M = 3 at gfx950 (6288, 7168). The new row is main's M = 4 row for this cell with M set to 3. I ran
test_flydsl_decode_padded_m.pyagainst the CSVs at this head: it passes, and it names the cell when I drop the row. A separate count over the 33 bf16 tuned CSVs finds the same five exceptions and no others. - Device in the handle key. Both decode launchers pass
specialization_key=A.device, andgemm_decode_bf16launches under A's device. I could not check the second-GPU behaviour you describe in ROCm/FlyDSL#1536. - Bias dtype. A bias of another dtype now goes to torch.
get_gfx_runtime()in the dispatch test.%sarguments in the warning.GTensor.loadis back to main's line.
The merge is fine too. _variants keeps main's fused-quant and FlyDSL-only rules next to the strided_a variant, and the wheel build's GEMM replay reports 24976 ok and 0 failed.
One small thing
No workflow runs the new test_flydsl_decode_padded_m.py. .github/scripts/split_tests.sh and the level 0+1 command in tuning-tests.yaml both list the tuning_tests files by name, and neither has it. One line next to test_config_shape_collision.py would wire it in. It does not block.
CI on this head
Aiter Test Gate is red, for two reasons outside this PR:
- Shards 0 and 7 ran no tests.
Download Aiter wheel artifacttimed out after 30 minutes, twice in each. They need a re-run. - Shard 2 fails
tests/test_moe_dispatch_snapshot.py. It fails the same way on main in run 45859420103: #7611 addedintermediate_padto the legacy dispatch and left the golden snapshot as it was. A re-run will not clear it. It needs the fix on main.
The decode tests pass on MI35X at this head: test_flydsl_decode_gemm.py, test_flydsl_gemm_dispatch.py (all 72 decode rows selected) and test_config_shape_collision.py.
I will approve once the gate is green.
xytpai
left a comment
There was a problem hiding this comment.
Two things changed on main since my last comment:
- The snapshot fix is in (#8790), so the shard 2 failure should go once this branch has it.
- This branch now conflicts with main.
A merge of main covers both and gives the gate a fresh run on all eight shards.
…apshot fix (#8790) and clear the conflicts with main. The three conflicts keep both sides: main's MiniMax exports, ring catalog and policy_runs_on next to the decode exports, candidates and launch helpers. Main's ring tuning (#8675) dropped the early return for preshuffled shapes, which had kept decode candidates out of them; the tuner now skips decode candidates when is_shuffle, since decode kernels read the plain weight and the runtime rejects a preshuffled decode row. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…l 0+1 tuning tests, since both list tuning_tests files by name and the new CSV check would otherwise never run in CI. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…og is not flooded with one ResourceWarning per file. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@xytpai Thanks. Main is merged and the test is wired in, at 2d038bb. Merge of main (45e51f7)
The new test in CI (a59eed8, 2d038bb)
Checks at the merged head, MI325X (gfx942), FlyDSL 0.5.3:
|
…eanly; keep main's MINIMAX_M3_MOE_PREFILL_SWIGLU_LIMIT export next to BlockMfmaDecodeConfig in the sorted __all__. Co-Authored-By: Claude Opus 5.5 <[email protected]>
xytpai
left a comment
There was a problem hiding this comment.
LGTM at 11c76ff. This replaces my change request: everything in it and in my later comments is addressed.
- Merges of main. The first had three conflicts (
__init__.py,gemm_a16w16_policy.py,gemm_kernels.py) and the second had one (__init__.py). Each keeps both sides. I redid both three-way merges for the files this PR shares with main; apart from those hunks and the tuner guard, your results match them. - Tuner guard. Skipping decode candidates when
is_shuffleis right, now that #8675 lets preshuffled shapes into the FlyDSL sweep. - Guard test.
test_flydsl_decode_padded_m.pyis wired in and ran in standard shard 4. - CI. The four required checks are green, and all eight MI35X standard shards ran.
test_flydsl_decode_gemm.pyandtest_flydsl_gemm_dispatch.pypass with all 72 decode rows selected. The wheel build's GEMM replay reports 25299 ok and 0 failed.
This rests on reading the code and on this CI run. I did not run anything on gfx950 myself.
… test instead of three and no CI list changes, as review asked. test_flydsl_gemm_dispatch.py repeated the decode test's check that every decode row is dispatched; its decode name round trip now lives in test_flydsl_decode_gemm.py, and its pass over the hgemm rows, which no decode change needs, is dropped. The M = 3 coverage check for decode M = 4 rows moves from its own tuning_tests file into test_csv_validation.py, which already validates the tuned CSVs, so split_tests.sh and tuning-tests.yaml go back to main. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Closing in favour of #9075. As agreed with Yutao Xu and Felix Li, the decode kernels will be integrated as a policy of the existing FlyDSL hgemm dispatch and its tuning space, instead of a separate pipeline. |
…m, so they share the one FlyDSL dispatch path and tuning space instead of a separate decode pipeline, as the review of #4114 asked. The hgemm kernel name keeps its fields and gains five optional ones that only these policies use: _eu (waves per EU), _rbp (ds_bpermute lane reduction), _ct (scalar or packed FP32 contraction), _lds (A staged in LDS) and _pn (persistent over N). A tuned small-M row applies only at its exact M. Outputs are bitwise equal to the #4114 kernels on all 72 gfx950 rows. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude generated (Claude Code). Description version 9, 2026-12-01.
Motivation
LLM autoregressive decode runs linear projections at M=1..5 tokens per step. A standard 16×16 MFMA tile wastes ≥75% of compute slots at these sizes. This PR adds a FlyDSL BF16 decode GEMM optimised for small M, plus the tuned config rows for the shapes where it wins.
Target hardware: gfx942 and gfx950. Measured on MI325X (gfx942, 304 CU) and MI355X (gfx950, 256 CU).
Target shapes: Kimi-K3 TP8 decode GEMMs, N=896…40960, K=128…8448. Generic — no model-specific names in kernel or dispatch code.
Technical Details
Two complementary FlyDSL policies, selected by a runtime tuner:
Wave (
gemm_decode_wave.py) — warp-per-scalar output-centric GEMM. One wavefront owns one tile of (M×N) output scalars; K is split across 64 lanes and reduced either with DPP row shifts or with ads_bpermutebutterfly.v_dot2_f32_bf16on gfx950, scalar/packed FP32 on gfx942.BlockMFMA (
gemm_decode_block_mfma.py) — MFMA block policy with LDS A-staging and software prefetch.The tuner picks the policy per shape; neither wins by M alone (gfx950 rows split roughly evenly, gfx942 rows are mostly BlockMFMA).
Low-level code. The kernels use FlyDSL APIs throughout: no inline assembly. The one remaining direct instruction call is the 4x4x4 MFMA, which the FlyDSL MMA atom does not support yet. Outputs are bitwise identical to the pre-cleanup kernels on every gfx950 row.
Entry point (
gemm_kernels.py):gemm_decode_bf16(A, B, C, config), dispatching to either policy via a tuned config fromiter_gemm_decode_configs. The kernel exists only for M≤5; the dispatcher honours a decode row on exact M match only, never through M padding. The activationAmay have a row stride larger than K (for example a column slice of a fused projection output); it is read in place, without a copy. PackedAkeeps the same machine code as before.Dispatch, tuner and AOT. Decode sits next to the existing FlyDSL hgemm path in each hook, with no new framework:
tuned_gemm.pyhas aflydsl_decode_branch (exact-M check, row check, launch),gemm_a16w16_policy.pyhasget_flydsl_a16w16_decode_configsnext to the hgemm catalog, and the tuner calls both. The GEMM AOT build replays every tuned row throughgemm_a16w16, so it reaches the decode branch with no decode-specific code; for decode rows with M > 1 it also replays a column-sliceA, which selects a kernel of its own. All FlyDSL rows uselibtype = flydsl; the kernel-name prefix tells hgemm and decode apart.aiter.tuned_gemm.is_flydsl_decode_config(row)lets callers such as vLLM ask whether a row selects a decode kernel without matching on labels. Preshuffled-weight shapes get no decode candidates, since the decode kernels read the plain weight. The tuner flag--candidate-policy bounded|deepsets how many decode configurations are timed (documented incsrc/gemm_a16w16/README.md).Tuned configs — decode rows (
libtype = flydsl, kernel nameflydsl_decode_…) inkimik3_bf16_tuned_gemm.csvfor gfx950 and gfx942, M=1..5, covering the Kimi-K3 TP8 decode shapes. Most land where the dispatcher previously fell through totorch. On gfx950 the rest replace an existingflydslortritonrow, and each of those only where graph replay showed decode beats the incumbent for that exact shape and M. On gfx942 nothing is replaced — see below.Duplicate shapes across model configs. A few keys at
(2112,7168)also existed indsv3_andkimik2_bf16_tuned_gemm.csv, and(3584,7168)arrived in main via #5702 while this PR was open. Since the runtime merges every*bf16_tuned_gemm*file into one keyspace, duplicates raise at merge time. All were resolved by the repository's own guard —test_config_shape_collision.py --fix, lowest-uswins — not by hand. That accounts for the deletions in the two other model files.The displaced rows were measured directly against the decode rows that replaced
them — both kernels by name, one process, packed HIP graph, alternating pairs,
null control ≤ 0.13%:
flydsl, µsMax relative error vs FP32 is also lower for decode (2.8–3.5e-03 vs
8.6–8.9e-03), so dsv3 and kimik2 gain accuracy on this shape as well as speed.
Fixes along the way. The tuner's candidate selection discarded valid decode configs before benchmarking them.
Layouts the decode kernels do not take. A decode row is chosen from shape and dtype alone. At call time
decode_layout_supportedchecks the operands; anAwith a non-unit column stride, column-majorB, or a non-packedoutgoes totorch_gemm, as an untuned shape would, so adding a row never breaks a workingtgemm.mmcall. The output-overlap guard measures a stridedArow by row.Performance
Measured against the kernel the config named for the same shape before this PR — the existing row, or
torchwhere none existed. Both kernels invoked by name, dispatcher out of the loop.Packed HIP graph, wall clock over launches, four rotating weights, alternating A/B/B/A pairs scored per pair, incumbent timed against itself as a null control (2% threshold). Both sides checked against an FP32 reference before each timing.
gfx950 (MI355X, 256 CU), every row in this PR, 3 independent passes:
Every row confirmed in every pass, zero regressions. The one tie is
(7168,1536)at M=1, where the kernels sit 0.3% apart; it never lost a pass.After this measurement, 4 gfx950 rows were removed (M=2
12448x7168and6288x7168, M=37168x3072, M=48448x7168). They beat aiter's previous kernel, but through vLLM they lose towvSplitK, so vLLM keepswvSplitKfor them. Two more rows (M=17168x8448, M=36288x7168) were removed because the same shape lost on gfx942 and the untuned CSV has no arch column, so a re-tune would bring them back. That leaves 76. The table above still includes all six.An earlier revision of this description quoted −9%…−25% against
wvSplitK. Those were taken against a September-2025 build of that kernel and are withdrawn.Downstream: the current rows measured through vLLM's
rocm_unquantized_gemm_implgive +5.4% on M=1..4 againstwvSplitK, with no shape slower — a kernel aiter cannot select, as it lives in vLLM's own extension. Wiring and caveats in vllm-project/vllm#55860.gfx942 (MI325X, 304 CU), every row in this PR, 3 independent passes:
Every row confirmed in every pass, zero regressions; no measurement discarded by the null control (255 of 255 kept). Here
previousis always the heuristic fallback, which resolved totorch: at the branch point no bf16 config carried agfx942row withcu_num=304, so all 85 rows are additions and none replaces an existing row. The rows are keyed(gfx942, cu_num=304), which also covers MI300X.Four gfx942 rows that lose to
wvSplitKthrough vLLM (M=17168x8448, M=27168x4224and6288x7168, M=36288x7168) had already been removed before this measurement. After it, 3 more rows were removed on shapes whose gfx950 row lost (M=212448x7168, M=37168x3072, M=48448x7168), so that a re-tune reproduces the CSV. That leaves 82. The table above still includes those 3.Shapes deliberately left without a row. 13 cells at M=3,4,5 where
opusis faster —routerat M=5 by 22%,(7168,8448)at M=5 by 9%,(3072,7168)at M=3 and M=4, and nine other long-K cells at M=3,4,5 by 0.5…6%. Plus 3 cells where the tuner and graph replay disagree reproducibly ((7168,8448)at M=4,attention Oand(4608,1536)at M=5).Full evidence — protocol, benches, raw records, and the measurement errors made and corrected along the way — is attached as
pr4114_evidence.tar.gz.Architecture support
v_dot2_f32_bf16; BlockMFMA uses MFMA with two-stage prefetchTest Plan
op_tests/test_flydsl_decode_gemm.py— a standard op_test, plain script, no pytest.python3 <file>runs the correctness checks first, then a perf sweep oftgemm.mmagainstF.linearunder CUDA graphs:A, directly and throughtgemm.mm;tgemm.mm. The dispatcher drops a row with a malformedkernelNamesilently, so without this a broken row would pass CI;Con a stridedA's second row is rejected,Cin the gap between rows is accepted and correct;Awith column stride 2 and column-majorBthroughtgemm.mm, with and withoutout=, fall back and give the right result;Arun in a freshFLYDSL_RUNTIME_RUN_ONLY=1process.The file sits where the runner already scans.
python3 -m unittest op_tests.tuning_tests.test_csv_validation— newtest_bf16_decode_m4_rows_keep_m3_covered: a cell whose M = 4 row is a decode row also has an M = 3 row, so padded M never sends M = 3 to the default (a short list of cells that had no M = 4 row before is exempt).python3 -m unittest op_tests.tuning_tests.test_config_shape_collision—Ran 33 tests ... OK. The config keyspace stays collision-free across every model file; includes a positive control (a planted duplicate must be caught).Attachment
pr4114_evidence.tar.gz
Submission Checklist
PR Self-Review
Reviewed revision
46c2d37
Behavior and intent
Adds a FlyDSL BF16 decode GEMM for exact M = 1..5 (Wave policy on v_dot2/FP32, BlockMFMA policy on 4x4x4 MFMA) and Kimi-K3 tuned rows that select it through the existing
libtype=flydsldispatch. At these M a 16x16 MFMA tile is >=75% padding, and the existing HGEMM catalog loses to the decode kernel on the tuned shapes. Shapes without a decode row, and layouts the decode kernels do not take, keep the existing path.Validation and unverified scope
python3 op_tests/test_flydsl_decode_gemm.pyexit 0, every check passes; the dispatcher selects all 72 decode rows (then checked intest_flydsl_gemm_dispatch.py, since folded into the decode test);python3 -m unittest op_tests.tuning_tests.test_config_shape_collision33 OK.python3 op_tests/test_flydsl_decode_gemm.pyexit 0, all its checks run, none skipped; the dispatcher selects all 82 gfx942 decode rows, 72 decode candidates round-trip by name.test_csv_validationandtest_config_shape_collisionpass (54 tests); with the gfx950 M = 3 (6288, 7168) row removed, the new M = 3 check fails and names that cell.tests/test_moe_dispatch_snapshot.py: 3 passed at 2d038bb.ci:mi300xrun at this head); end-to-end model accuracy (the Atom Test accuracy jobs fail on main too, missingjsonschemain the ATOM image).PR-specific question
The decode path is taken only when a tuned row names a
flydsl_decode_*kernel whose arch, M, N, K and bias match the call exactly (check_flydsl_decode_row), dtype is BF16 in and out, with no scaling and no preshuffled weights. At call timedecode_layout_supportedrequires A with a unit column stride (any row stride >= K), packed B and out, contiguous bias; anything else goes totorch_gemm.validate_gemm_decode_tensorsapplies the samedecode_layout_supportedrule and rejects arch other than gfx942/gfx950, M outside 1..5, and C overlapping A (row by row), B or bias.Changed op tests
op_tests/test_flydsl_decode_gemm.py: correctness of both policies against torch (bias, odd K tails, LDS padding, persistent N), every shipped decode row dispatched throughtgemm.mm, strided A bit-equal to packed A, overlap guard, layout fallback, and AOT run-only coverage, and decode kernel names parse back to their configs. It is the only test that exercises the decode kernels directly.op_tests/tuning_tests/test_csv_validation.py: addstest_bf16_decode_m4_rows_keep_m3_covered, the CSV check that every cell whose M = 4 row is a decode row also has an M = 3 row, so padded M never sends M = 3 to the default; it sits with the other tuned-CSV checks, reads only the CSVs and needs no GPU.