Repository navigation
[Triton/Gluon] Add gemm_a16w32: bf16 x fp32-weight GEMM (fp32 FMA at small M, bf16x3 MFMA above) - #7770
Conversation
…ne launch Some MoE routers keep the gate weight in fp32 (MiniMax-M3.x: 128 x 6144). This kernel computes Y = X @ W^T in fp32 from a bf16 X and a W that split_bf16x3() splits into three bf16 terms. The sum of the terms is exactly W. Each K step loads the X tile once and adds three bf16 MFMA dots into one fp32 accumulator. With split-K, the last split of each tile adds the partials in split order. Thus the result does not depend on the workgroup order. - Kernel, wrapper, unit test (fp64 reference) and benchmark. - gfx950 configs: DEFAULT.json and the tuned N=128 K=6144 file (M <= 256). - Registration in the tuning scripts (tuning/kernels.py). Signed-off-by: Vinayak Gokhale <[email protected]>
Get the split-K arrival counters from capture_scoped_cache and persistent_alloc in aiter.utility.graph_alloc, as gemm_afp8wfp8 does. Thus a first split-K call inside a CUDA graph capture gets counters for that graph only. Eager calls and other graphs do not use them. - Pass None for the workspace and the counters when NUM_KSPLIT is 1. - Add a test without split-K (small K). - Add a test where the first split-K call is inside a capture. The graph then replays with new inputs, and an eager call follows. - Skip the tests on archs other than gfx950, which have no configs. - Add a docstring to the public wrapper. Signed-off-by: Vinayak Gokhale <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
The config of each M bucket now selects the kernel with kernel_type: - "bandwidth_bound": a new fp32 FMA kernel that reads the fp32 weight. Each program computes BLOCK_SIZE_M rows of the output for one row of W. K is a compile-time value, so the trip count of the K loop is known. - "compute_bound": the bf16x3 MFMA kernel. gemm_a16w16x3 now takes the fp32 weight and its bf16x3 terms: gemm_a16w16x3(x, w, w_bf16x3). One call covers every M. - gfx950 configs: the FMA kernel for M <= 32, in DEFAULT.json and in the N=128 K=6144 file. The other buckets do not change. - Tests: every M from 1 to 33, and the FMA kernel in the repeat, CUDA graph and small-K tests. - Tuning: the sweep times the bf16x3 tiles against the installed FMA config, because BLOCK_SIZE_N 1 is not in SEARCH_SPACE. Signed-off-by: Vinayak Gokhale <[email protected]>
Only git mv. The next commit changes the contents. Signed-off-by: Vinayak Gokhale <[email protected]>
The op takes bf16 activations and fp32 weights, as the a{act}w{weight}
names of the other GEMMs tell (gemm_a16w16, gemm_a16wfp4). The bf16x3
split is a detail of the MFMA kernel.
- Public op gemm_a16w32, config name GEMM-A16W32 and the tuning entry
gemm_a16w32. Kernels _gemm_a16w32_fma_kernel and
_gemm_a16w32_bf16x3_kernel.
- split_bf16x3() and the kernel_type values do not change.
Signed-off-by: Vinayak Gokhale <[email protected]>
ROCm/aiter#7770 renamed gemm_a16w16x3 to gemm_a16w32 (bf16 activations, fp32 weights, fp32 output). Import the new name. No other change. Signed-off-by: Vinayak Gokhale <[email protected]>
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
On the ROCm bf16x3 router tier (gfx950, bf16 input, fp32 weight, fp32 output), below MIN_TOKENS (2048) call AITER's gemm_a16w32: one launch for every M, fp32 FMA at small M and the bf16x3 MFMA kernel above. 2048 tokens and up keep the existing bf16x3 GEMM. When the installed AITER does not have the kernel (import fails), the previous dispatch runs: the fp32 Triton kernel up to 32 tokens, F.linear in fp32 above. MiniMax-M3.1 router (N 128, K 6144), us a call, gemm_a16w32 vs today: M 1-32: 2.5-5.8 vs 2.4-6.2 (fp32 Triton kernel; within -17%..+6%) M 48 7.9 vs 11.8, 128 9.5 vs 14.6, 512 13.2 vs 22.4, 1024 18.5 vs 34.4, 2047 24.7 vs 47.3 (F.linear fp32 path) Needs ROCm/aiter#7770 (gemm_a16w32); without it nothing changes. Router logits inside GateLinear match fp64 to 5e-9..7e-8 (relative to sum |x||W|) at M = 1, 8, 32, 33, 512 and 2047, eager and in a replayed CUDA graph. New device-free test of the dispatch. Signed-off-by: Vinayak Gokhale <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
Shared arrival counters across captured graphs and missing synchronization before split-K arrival publication can corrupt results during concurrent replay and multi-wave execution.
5 open issues
What changed in this PR
Adds gemm_a16w32 to AITER’s Triton GEMM support for BF16 activations and FP32 weights, including MoE router workloads.
Changes:
- Adds FP32 FMA for small M and BF16x3 MFMA with deterministic split-K for larger M.
- Adds the public wrapper, weight decomposition, gfx950 configs, and tuning registration.
- Adds accuracy, repeatability, graph-capture tests, and a rotating-buffer benchmark.
| File | Description |
|---|---|
op_tests/triton_tests/gemm/basic/test_gemm_a16w32.py |
Tests accuracy, shapes, strides, repeatability, and graph capture. |
op_tests/op_benchmarks/triton/bench_gemm_a16w32.py |
Adds latency, throughput, and bandwidth benchmarks. |
aiter/ops/triton/utils/_triton/tuning/kernels.py |
Registers the GEMM family and candidate filter. |
aiter/ops/triton/gemm/basic/gemm_a16w32.py |
Adds weight decomposition, dispatch, and split-K workspace management. |
aiter/ops/triton/configs/gfx950/triton/gemm/gemm_a16w32/GEMM-A16W32-N=128-K=6144.json |
Adds shape-specific gfx950 tuning values. |
aiter/ops/triton/configs/gfx950/triton/gemm/gemm_a16w32/DEFAULT.json |
Provides default gfx950 dispatch and tuning values. |
aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a16w32.py |
Implements the FMA and BF16x3 MFMA kernels. |
Review effort: Balanced
…ival Split-K: one thread adds the arrival of a tile, and the release of that atomic covers only its own wave. The other waves of the tile could still have partial stores in flight, so the last split could read old partials. Now each wave drains its stores (s_waitcnt vmcnt(0)) before a barrier, and then the arrival is added, as _sum_splits_on_xcd in gemm_afp8wfp8 does. The sum order does not change, so the results are bitwise the same. The wait costs 0.1 to 0.7 us (1 to 5 %) at M = 64 to 2048 for N = 128, K = 6144. Also: - split_bf16x3 raises ValueError for values that round to inf in bf16 (|w| >= 2**128 - 2**119). The docstring gives the range where the split is exact (2**-110 to that bound), and a test checks the limits. - Benchmark: a call reads only w (FMA kernel) or only w_bf16x3 (bf16x3 kernel). Only that weight rotates, and the other one is fixed, so the pool size counts only the bytes that the kernel reads. - Docstring: graphs captured on one stream can share its split-K counters. Signed-off-by: Vinayak Gokhale <[email protected]>
tl.dot in the bf16x3 kernel needs blocks of 16 or more in each dimension. At M=1 every candidate had BLOCK_SIZE_M 8, so the sweep could not use the oversized-tile retry. Now the plan at M=1 has only BLOCK_SIZE_M >= 16. Signed-off-by: Vinayak Gokhale <[email protected]>
Boss2002n
left a comment
There was a problem hiding this comment.
Must fix before merge
- The split-K code is a copy (kernel, wrapper): it is the same code as in
gemm_afp8wfp8, but without its fallback for torch older than 2.10. Fix: share one helper in_triton_kernels/common/splitk_reduce.py. - The kernel compiles too many times (kernel):
GRID_MNis a compile-time value that changes with M. For N=128, K=6144, M from 33 to 8192 gives 72 compiles instead of 5. Fix: computeGRID_MNinside the kernel. - Large weights read the wrong memory (kernel):
2 * stride_btandpid_k * M * Nare 32-bit and overflow when N × K ≥ 2^30 (for example N=131072, K=8192). Fix: cast to int64, as #6758 did. - The tuner cannot tune the small-M kernel (tuning/kernels.py):
SEARCH_SPACEhas noBLOCK_SIZE_Nof 1 and noBLOCK_SIZE_Mof 1 or 4, so the tuner skips every FMA candidate. Fix: add these values.
Nice to fix
- Make the test tolerance tighter: today a kernel that drops the third bf16 part still passes. Also add one split-K shape where K is not a multiple of
BLOCK_SIZE_K(test). - Remove about 33 of the 89 test cases; they repeat other cases (test).
- Split the fp32 weight inside the kernel, as #7832 does, so callers do not keep a second copy (
w_bf16x3) (wrapper). - Benchmark: count the bytes that the chosen kernel really reads, support
--model, and do not import from_triton_kernels/(bench). Also keepcache_modifieron the masked K loads, and show the bad values in the assert messages.
…_afp8wfp8 The bf16x3 kernel had its own copy of the fused split-K code of gemm_afp8wfp8, without the fallback for torch < 2.10. Now both ops use one copy: - _tile_on_xcd and _sum_splits_on_xcd move from the gemm_afp8wfp8 kernel file to _triton_kernels/common/splitk_reduce.py, without change. - The counters and the partials buffer move from the gemm_afp8wfp8 wrapper to utils/fused_splitk.py. fused_splitk_workspace() returns None when the counters cannot be made (first use inside a capture with torch < 2.10) or when the arch is not gfx950. Then gemm_a16w32 writes the partials and adds them with _gemm_splitk_reduce_kernel. - The bf16x3 kernel computes the tile count in the kernel. GRID_MN was a compile-time value that changed with M, so each M gave a new compile. - The partial and term offsets are int64: 2 * stride_bt and the partial slot offsets overflowed int32 when N * K >= 2**30 (as #6758 did). - The masked K loads keep cache_modifier. gemm_afp8wfp8 does not change: the instruction streams of its tile and packed kernels (fused and unfused split-K) are identical before and after. README.md and the review instructions name the new shared helpers. Signed-off-by: Vinayak Gokhale <[email protected]>
SEARCH_SPACE had no BLOCK_SIZE_N of 1 and no BLOCK_SIZE_M of 1 or 4, so should_skip rejected every candidate of the fp32 FMA kernel and the sweep could not tune the small-M buckets. - SEARCH_SPACE gets BLOCK_SIZE_M 1 and 4 and BLOCK_SIZE_N 1. - These values are vector tiles (below MIN_DOT_TILE, 8). Only a kernel with vector_tiles=True gets them. gemm_a16w32 sets it. So the other families keep the candidate lists that they had. - should_skip of gemm_a16w32 admits FMA candidates with BLOCK_SIZE_N 1, no split-K and no tile swizzle. It rejects an FMA candidate whose BLOCK_SIZE_M x BLOCK_SIZE_K fp32 sum is more than 256 values per thread. The plans of the other families do not change: build_space gives the same candidate values and the same configs for the other 24 (family, backend) pairs on gfx950 and 25 pairs on gfx1250, at 14 values of M from 1 to 8192. AGENTS.md and README.md tell the new rule. Signed-off-by: Vinayak Gokhale <[email protected]>
Triton made a second variant of each config for the values of M that are a multiple of 16. M only masks the rows of the last tile, so the bf16x3 kernel does not specialize on it now. For N=128, K=6144 and each M from 33 to 8192, the bf16x3 kernel compiled 144 times before this series (72 GRID_MN values, each twice), 10 times with GRID_MN in the kernel, and 5 times now: one compile for each config. Signed-off-by: Vinayak Gokhale <[email protected]>
…16x3 optional The bf16x3 kernel needed the terms (3, N, K) from split_bf16x3, so callers kept a second copy of the weight, 1.5 times its size. Now the kernel can read the fp32 weight and split each loaded tile into the three bf16 terms, with the rounding of split_bf16x3 (as the router of #7832 splits its fp32 weight). The kernel picks the path from the dtype of the weight pointer. w_bf16x3 is now optional, so gemm_a16w32(x, w) works. vLLM calls gemm_a16w32(x, weight, weight_split): that call still works, and the kernel reads the given terms, because that is faster. Both paths give the same bits: same terms, same order of the dots. A test checks it at M 1, 33, 256, 2048 and 4096. Bench (graph timing, cold weights, N=128, K=6144, gfx950, median of 3 runs in one session, us): M before given terms split in kernel 1-32 fp32 FMA kernel, not changed: within 1% (2.6% at M=8) 33 7.66 6.95 7.79 64 8.19 7.20 8.08 128 9.23 8.13 9.03 256 11.18 10.22 12.08 512 13.02 11.11 12.69 1024 17.97 16.13 19.71 2048 23.92 22.95 23.94 4096 48.05 44.41 45.69 8192 58.54 55.90 74.15 "before" is the head of the PR, which needs the terms. The faster column of the given terms comes from the shared split-K of the earlier commit. The split in the kernel loads 4 bytes for each weight value, not 6, but the warps that share a weight tile all split it, and the configs are tuned for the given terms. So the kernel reads the terms when it gets them. Without w_bf16x3, the bf16x3 kernel has the limits of split_bf16x3: exact for 2**-110 <= |w| < 2**128 - 2**119, inf or NaN at or above that limit. A probe with one-hot rows showed the same exact range for both paths. Signed-off-by: Vinayak Gokhale <[email protected]>
…peated cases
- _check bounds each output by 2**-21 sum_k |x w|, not a global bound of
4e-6 max sum_k |x w|: about 8 times tighter. The kernels stay below
2**-21.9 of it at K = 96 and below 2**-23.6 at K >= 3072.
- A new exact test gives each row of x one value, a power of two. Then each
output is that value times one weight, exact in fp32, and the test asks
for the same bits. A kernel that drops the third bf16 term fails it (and
_check fails at K = 96). A lost third term gives errors of 2**-22.7 sum
|x w| or more, which a tolerance cannot tell from the fp32 rounding at
large K.
- A split-K shape with a K tail: K = 6112 is not a multiple of BLOCK_SIZE_K.
- A graph capture test without counters: the partials go to the reduce
kernel, as with torch < 2.10.
- The assert messages show the bad values.
- Removed 32 cases that repeat other cases:
- test_gemm_a16w32_small_m ran each M from 1 to 33 (33 cases). M 1, 3,
16, 32 and 33 are in test_gemm_a16w32. M 2 and 4 run the same compile
and code path as M 3 (BLOCK_SIZE_M 1). M 13 to 31 repeat the
remainders M % 4 and the compile of M 9 to 12 (the M_LEQ_32 config is
the M_LEQ_16 config). It keeps M 5 to 12 (8 cases).
- test_gemm_a16w32_repeats_bitwise: M 1, 8 and 32 run the FMA kernel,
which has no split-K state to go stale. M 64 has the config and the
split count of M 33. It keeps M 33 and 128.
- test_gemm_a16w32_in_graph_capture: M 32 is the FMA kernel as M 1, and
M 64 has the config of M 33. It keeps M 1 and 33.
- test_gemm_a16w32_allocates_y_and_takes_strided_x: M 31 is the FMA
kernel as M 1. It keeps M 1 and 512.
Signed-off-by: Vinayak Gokhale <[email protected]>
… kernel import - The bandwidth counts the weight that the chosen kernel reads: the fp32 weight for the FMA kernel and for the split in the bf16x3 kernel, the bf16 terms when the call gives them (--w-split, as vLLM calls the op). That weight rotates. A weight that the kernel does not read is fixed. - --model benchmarks the MoE router GEMM of the models with num_expert in model_configs.json: N is num_expert, K is hidden_size. -M selects one M. - The bench gets the kernel choice from get_gemm_config in utils/, not from _triton_kernels/. Signed-off-by: Vinayak Gokhale <[email protected]>
should_skip_generic rejected num_stages 1 when a candidate has several K steps, because Triton pipelines a dot kernel then. The fp32 FMA kernel has no tl.dot, and its installed configs use num_stages 1, so the sweep could not find them again. A vector BLOCK_SIZE_N (below MIN_DOT_TILE) now keeps both num_stages values. Only gemm_a16w32 has vector tiles, so the other plans do not change: build_space gives the same candidates and configs for the other 24 (family, backend) pairs on gfx950 and 25 on gfx1250 at 14 values of M, and harness --plan is the same for gemm_afp4wfp4_preshuffle and gemm_a16w16. At M 1, 8 and 32, the installed FMA config is in the plan now. Signed-off-by: Vinayak Gokhale <[email protected]>
The README's shared-helpers table names the new helpers. Leave .github out of this PR. Signed-off-by: Vinayak Gokhale <[email protected]>
|
Thank you for the review. The changes are in a19b318 to e8cb39b. Must fix
Nice to fix
With the shared split-K, the call with the split given is 4% to 15% faster than before for M of 33 and more. |
Bring in main 008d53c. It has #8313, the fix of tests/test_ar_rmsnorm_routing.py that failed in the 1-GPU CI job. There are no conflicts. Signed-off-by: Vinayak Gokhale <[email protected]>
…small M, bf16x3 MFMA above) (ROCm#7770)


Summary
This PR adds
gemm_a16w32to support f32 weights. This is used in some models like Minimax for the moe router. It takes bf16 activations and fp32 weights and returns fp32. At small M it uses fp32 FMA on the fp32 weight, and at larger M it uses bf16 MFMA on three exact bf16 terms of the weight (split_bf16x3()).Changes
gemm_a16w32(x, w, w_bf16x3=None).kernel_typein the config of each M bucket selects the kernel. Withw_bf16x3=None, the bf16x3 kernel splits the fp32 weight after each load; with the split given, it reads the split (faster, same bits).bandwidth_bound): each program computesBLOCK_SIZE_Mrows of the output for one row of W.compute_bound): deterministic split-K in the kernel. It uses the fused split-K helpers that it now shares withgemm_afp8wfp8(_triton_kernels/common/splitk_reduce.py,utils/fused_splitk.py). It compiles once for each config (Mis not specialized).--model), gfx950 configs and registration intuning/kernels.py. The tuner can now sweep the FMA kernel (BLOCK_SIZE_M1 and 4,BLOCK_SIZE_N1, only for kernels withvector_tiles=True).Results (MI355X, N=128, K=6144, us per call, CUDA graph, cold weights)
With the shared split-K (head e8cb39b), the call with the split given is 4% to 15% faster for M of 33 and more: M=64 7.20 us, 256 10.22, 1024 16.13, 2048 22.95, 8192 55.90. M of 32 and less (FMA kernel) does not change.
Testing
pytest op_tests/triton_tests/gemm/basic/test_gemm_a16w32.py: 78 passed.test_gemm_afp8wfp8.py: 453 passed, 274 skipped (MI355X, vLLM nightly image, Triton 3.8). It collects 727 cases, the same as the file on main. Main's [Triton/Gluon] Unify get_shapes for GEMMs #8214 made its shape list shorter.bench_gemm_a16w32.py, andverify_configs.py gemm_a16w32 --expect-tunedat M = 1, 32 and 64: pass.Limitations
w_bf16x3=None), the bf16x3 kernel is up to 27% slower than with it at some M (for example M=8192), because the configs are tuned for the given split. Callers that can keep the split should pass it.PR Self-Review
Reviewed revision
a98b1b9
Behavior and intent
This PR adds a new op,
gemm_a16w32: bf16 activations times fp32 weights, with an fp32 output. MoE routers such as the MiniMax-M3.1 router (128 x 6144) keep fp32 weights. Before, a caller had to cast the activations to fp32 and run an fp32 GEMM, or write a model-specific kernel. At small M the op uses fp32 FMA. At larger M it uses bf16 MFMA on three exact bf16 parts of each weight. No existing op changes. This revision also moves the fused split-K helpers ofgemm_afp8wfp8into shared modules (_triton_kernels/common/splitk_reduce.pyandutils/fused_splitk.py);gemm_afp8wfp8compiles to the same instructions.Validation and unverified scope
pytest op_tests/triton_tests/gemm/basic/test_gemm_a16w32.pyat head a98b1b9 (main 008d53c merged; MI355X): 78 passed. At e8cb39b, with the third bf16 term removed from the kernel (a temporary mutation), the old tests pass but 8 new cases fail.test_gemm_afp8wfp8.py: 509 passed and 330 skipped, before and after. The 229 kernels that it compiles have the same instruction streams before and after.verify_configs.py gemm_a16w32 --M 1 --N 128 --K 6144 --expect-tunedpasses.sweep_configs.pyruns of the tuning checks.PR-specific question
Only the new op
gemm_a16w32reads the new config filesconfigs/gfx950/triton/gemm/gemm_a16w32/DEFAULT.jsonandGEMM-A16W32-N=128-K=6144.json. Calls with N=128 and K=6144 use the tuned file and select one config for each M bucket; other shapes use DEFAULT.json. The newSEARCH_SPACEvalues go only to kernels withvector_tiles=True(onlygemm_a16w32), and the plans of all other families are the same as before, so the tuning of other ops keeps its previous selection.Changed op tests
op_tests/triton_tests/gemm/basic/test_gemm_a16w32.py: new. It tests both kernels against an fp64 reference with a bound of 2^-21 x sum |x w|, an exact test with one power-of-two value in each row of x (it finds a lost third bf16 term), a split-K shape with a K tail (K=6112), the split in the kernel against the given split, and thesplit_bf16x3()limits. No other test covers this op.op_tests/op_benchmarks/triton/bench_gemm_a16w32.py: new. It counts the bytes that the chosen kernel reads, supports--model, and rotates only the weight that the kernel reads.op_tests/triton_tests/gemm/basic/test_gemm_afp8wfp8.py: only the monkeypatch target changes, to the shared helper inutils/fused_splitk.py. No test case changes.