Repository navigation
[Config] [Perf] Use non-temporal B loads in twelve Kimi-K3 decode BF16 GEMM rows on gfx950 - #8915
Conversation
…ws on gfx950 The rows of 6016 x 7168 and of 6288 x 7168 at 1, 2, 4, 8, 16 and 32 rows name the same tile with _bnt. They are older than the option (#5145, #6019 and #5903; the option is #7546), so the tuner never raced it for them. A decode step reads the weights of every layer once, and the tuner measures that: run_perftest gives every timed launch its own copy of the tensors. MI355X, one GPU, FlyDSL 0.5.3, the tuner's method (10 warm-up and 101 timed eager launches), 3 runs per kernel, the two kernels alternate, median, microseconds, row today -> _bnt: 1 x 6016 x 7168 16.31 -> 15.40 2 x 6016 x 7168 16.39 -> 15.53 4 x 6016 x 7168 16.46 -> 15.70 8 x 6016 x 7168 16.62 -> 16.02 16 x 6016 x 7168 17.93 -> 17.20 32 x 6016 x 7168 18.30 -> 16.93 1 x 6288 x 7168 16.78 -> 15.63 2 x 6288 x 7168 16.96 -> 15.57 4 x 6288 x 7168 17.00 -> 15.68 8 x 6288 x 7168 17.33 -> 16.27 16 x 6288 x 7168 18.34 -> 17.46 32 x 6288 x 7168 20.41 -> 19.68 That is 3.6 % to 8.2 % less time per launch. The six rows of 6016 x 7168 and the row of 32 x 6288 x 7168 were measured on main b4d0133. The other five rows were measured in a second session, on main 8b6b4b4 with those seven rows changed. solidx is the index of the new name in the tuner catalog; us, err_ratio, tflops and bw are the _bnt measurements. tgemm.mm selects these rows for 1 to 32 rows of both shapes. A caller that launches one weight tensor back to back while it fits the last-level cache can get slower. Measured with one weight tensor: +7.7 % for 32 x 6288 x 7168, +11.5 % and +12.8 % for 16 x 6016 x 7168, -3.9 % and -2.8 % for 32 x 6016 x 7168. The other nine rows were not measured in that state. Signed-off-by: Jin Pan <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
Conflict: aiter/configs/model_configs/kimik3_bf16_tuned_gemm.csv, MoE front 6016x7168 at M = 1..32, where main (ROCm#8915) retuned all six rows with non-temporal B loads and this branch had replaced M = 8 and M = 32. Main's rows kept for that block (its own table times them at 16.02 / 16.93 us against this branch's 16.04 / 18.03 us); every other row of this branch kept. No duplicate (gfx, cu_num, M, N, K, bias, dtype, outdtype, scaleAB, bpreshuffle) key in the file. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Conflicts: - gemm_a16w16_gfx950.py vs ROCm#8929: keep this branch's 4-argument write_cshuffle_vec_to_global (main's out_buf/is_split_k only serve the atomic split-K store this branch removes) and use main's int64 C offset at all three C stores, including store_c_vec of the slice-K combine and split-K reducers, which main does not have. - test_flydsl_hgemm.py: add 31777x67712x512 ft k_waves=2, a C past 2^31 elements through store_c_vec. It fails with an illegal memory access when store_c_vec uses an int32 offset. - kimik3 table vs ROCm#8915: 11 of the 12 _bnt rows take main's row; the 8 without split-K/slice-K as is, 1x6016, 8x6016 and 32x6016 after re-timing under the fp32 reduction. 32x6288x7168 keeps this branch's ks4 row (tuner time tied, faster with warm weights). Co-authored-by: Claude <[email protected]> Signed-off-by: Andreas Karatzas <[email protected]>
…4098) (cherry picked from commit b34b6b8) Rebase onto main 12-03: kimik3_bf16_tuned_gemm.csv conflicted with main's gfx950 row updates (#8317 / #8245 / #8915). Replayed by row key: the 32 gfx1250 rows this commit adds are inserted; its 15 gfx950 edits were number formatting only (a CSV round-trip) and are left as main has them. Same rows as the PR head 3b64098.
Summary
Twelve rows of
aiter/configs/model_configs/kimik3_bf16_tuned_gemm.csvname the same tile with_bnt, the non-temporal B loads of #7546: 6016 x 7168 and 6288 x 7168 at 1, 2, 4, 8, 16 and 32 rows (gfx950, 256 compute units, BF16 to BF16, no bias).tgemm.mmcallers of these shapes with 1 to 32 rows. In SGLangmainthat is the merged Kimi-K3 input projection (6288 x 7168). SGLang's call for 6016 x 7168 copies only the tile from the row, so those six rows change nothing there yet.Motivation
_bntfor them.run_perftestgives every timed launch its own copy of the tensors._bntonly. For seven rows I also measured the four K-stagger variants. Without_bntthey are 2.8 to 11.0 % slower than_bnt. With_bntthey read 0.989 to 1.032 of it. One beats_bntin all three runs:_stg3_bntat 32 x 6288, by 1.1 %. One session does not settle 1 %, so it is not applied. The other five rows have no stagger measurement.main8b6b4b4 ([HIP] [FlyDSL] [CI] [Perf] gfx950 small-M BF16 ring GEMM policies (rg lm_head, rk DSpark FC) in the hgemm catalog, tuner and tgemm path #8675 has since added 426 ring candidates). I raced one per key: the same tile with_bnt. A full race is about 7 to 21 GPU-hours at the run times of my sessions, compile not counted. I did not run it.Changes
One file, 12 lines, no kernel, policy or test file:
kernelNamegets_bnt;solidx(its index in the tuner catalog ofmain8b6b4b4),us,err_ratio,tflopsandbware those of the_bntkernel.tgemm.mm, either shape_bnttgemm.mm, either shapegemm_a16w16()call with the ten tile fields of a rowAn AOT cache from before this change lacks the seven new kernels.
tgemm.mmcompiles them at first use. A run-only process (FLYDSL_RUNTIME_RUN_ONLY=1) raisesRuntimeErrorfor the 12 rows until the prebuild runs again.The SGLang path, with file and line in SGLang
main884b0b9eaa9eAll lines are in
python/sglang/srt/models/kimi_k3.pyunless a file is named._merge_kda_inproj_weights_hip(line 2401) concatenatesfused_qkvg_proj,f_a_projandb_projand pads the rows to a multiple of 8 (lines 2416 to 2419; the pad is line 375).fused_qkvg_proj: 6144 rows at TP 8 (line 194, "(6144, 7168) ... KDA fused qkvg"; it is 4 xhead_dimxnum_heads/ TP by lines 2034 and 2082 to 2098).f_a_proj: 128 rows (lines 2078 and 2109 to 2111: "f_a (128, replicated)").b_proj: 12 rows per rank (line 2077: "b (12/rank)"; line 2106: "TP8 shards K3's 96 beta rows").SGLANG_ROCM_K3_FUSE_KDA_INPROJ_MAX_TOKENS,python/sglang/srt/environ.pyline 1288) the model callsself.fused_qkvg_proj.quant_method.apply(self._qkvgbfa_layer, hidden_states, None)(lines 2641 to 2643). That isUnquantizedLinearMethod.apply, which ends intgemm.mm(x, layer.weight, bias, otype=x.dtype)(python/sglang/srt/layers/quantization/unquant.pyline 1157).environ.pyline 1131); the hipBLASLt list has no (6288, 7168) (unquant_hip.pylines 19 to 21); the Triton path needs K of at most 2048 or a registered shape (unquant.pylines 147 to 150 and 181 to 205).SGLANG_ROCM_K3_FUSE_KDA_INPROJ,environ.pyline 1287) when the conditions of_may_fuse_kda_inprojhold (line 2437).mainthat sends it throughtgemm.mm. Under AITER it asks for fp32 output and takesa16w16_into(unquant_hip.pyline 81), which copies ten fields:block_m,block_n,block_k,stages,split_k,m_waves,n_waves,k_waves,group_m,use_half_tile_interleaved(lines 47 and 48). It does not copyb_non_temporal. So for SGLangmainthe six rows of 6016 x 7168 change nothing until that call also copiesb_non_temporal.AOT pre-compilation: what the replay builds, and the run-only check
python -m aiter.aot.flydsl.gemmreplays every row throughaiter.tuned_gemm.gemm_a16w16. The kernel binary depends on the tile and its options, not on M, N or K.main8b6b4b4 and on this branch, and again onmain4122ef5 and its merge with this commit: 26 and 26 entries each time, with the same binaries. 19 are common and byte-identical. 7 are only onmain(the seven tiles without_bnt), 7 only on this branch (the same tiles with_bnt).t32x64x128x5_ks2_w1x4x1. I did not run the replay of the full tables.python -m aiter.aot.flydsl.gemm --checkwith the rows of this branch on a cache that was built from the rows ofmain: 22 ok, 12 failed, at 8b6b4b4 and at 4122ef5. The 12 are the edited rows:RuntimeError: FLYDSL_RUNTIME_RUN_ONLY=1 but no usable AOT cache.tgemm.mm: nob_non_temporal,k_stagger,bpreshuffle). It matches no entry of the replay, onmainand on this branch alike.Performance (if applicable)
One idle MI355X. The tuner's method (
run_perftest(run_flydsl_gemm_bf16, ...), 10 warm-up and 101 timed eager launches), median of 3 runs. Microseconds; K = 7168.main8b6b4b4 with the other seven rows changed. Those seven:mainb4d0133, repeated in the second session (3.2 to 9.4 %; per row up to 2.8 points from the first).Cost with warm weights (one weight tensor, launched again while in the 256 MiB last-level cache):
32 x 6288: through
tgemm.mm, back to back in a graph. 6016: direct call with the row's tile, eager, two sessions. Not measured in this state: 1, 2, 4, 8 x 6016 and 1, 2, 4, 8, 16 x 6288.The runs of the 12 rows (microseconds), software, sessions
MI355X (gfx950, 256 compute units), PyTorch 2.11.0+rocm10.0.0, HIP runtime 7.15.26333, FlyDSL 0.5.3. Every timed launch has its own copy of input, weight and output. Three runs per kernel, the two kernels alternate. Session A is the first session:
mainb4d0133. Session B is the second:main8b6b4b4 with the seven rows of session A changed._bntusin the table beforet16x32x256x4_ks1_w1x1x2t16x32x128x5_ks1_w1x2x1t16x32x128x7_ks1_w1x2x1t16x32x256x4_ks1_w1x1x2t16x32x256x4_ks1_w1x2x1t32x96x64x5_ks4_w2x2x1t16x32x128x5_ks1_w1x2x1t16x32x128x6_ks1_w1x2x1t16x32x128x6_ks1_w1x2x1t16x32x128x5_ks1_w1x2x1t16x32x128x5_ks1_w1x2x1t32x64x128x5_ks2_w1x4x1_bntrun is below the fastest run of the old kernel: by at least 3.1 % in session A, by 4.4 to 7.6 % in session B._bntruns are all below the three runs of the row's kernel, the_bntmedian is at most 0.98 of that kernel's median, and theerr_ratioof_bntis at most 0.05. All five met it.K-stagger variants and the repeat of the seven rows, tuner's method (second session)
Microseconds, median of 3 runs, the six kernels of a tile alternate in one process. In brackets: the ratio to
_bnt._bnt_stg1_stg1_bnt_stg3_stg3_bntt32x64x128x5_ks2_w1x4x1t32x96x64x5_ks4_w2x2x1t16x32x256x4_ks1_w1x2x1t16x32x256x4_ks1_w1x1x2t16x32x128x7_ks1_w1x2x1t16x32x128x5_ks1_w1x2x1t16x32x256x4_ks1_w1x1x2_bntin this table is the repeat of the seven rows of the first session:_bntis 3.2 to 9.4 % faster, and its three runs are below the three plain runs in all seven tiles._bnt(0.7 to 11.0 %)._stg3_bntis 1.1 % faster than_bnt, and its three runs (19.77, 19.57, 19.66) are all below the three_bntruns (19.95, 19.78, 19.89). The closest two runs are 0.06 % apart._stg1_bntis 0.7 % faster by the median, and its runs overlap those of_bnt. The three runs of each of these kernels differ by 0.8 to 1.3 % (up to 3.0 % elsewhere in this session), so one session does not settle a difference of 1 %. No row gets a stagger variant; a second session can settle it.Method 2: back-to-back launches in a graph (the seven rows of the first session)
One process per tree and round, 3 rounds, the two trees alternate. Per case a captured HIP graph of 184 back-to-back launches, 30 replays; the launches cycle over cold weight copies (up to 8 GB). Microseconds per launch, median over all replays. Every output is checked against an fp32 reference before it is timed.
mainb4d0133.The cases build the kernels that
tgemm.mmbuilds from the old and from the new row (the same kernel names). The path differs per shape:gemm_a16w16()directly with the ten tile fields of the row and BF16 output; the second side built the tile withb_non_temporalon. Not throughtgemm.mm.tgemm.mm; the row on the second side named the_bntkernel of this pull request.tgemm.mmtgemm.mm. The two rows of 6016 x 7168: eager, direct call with the row's tile and BF16 output, one weight tensor, two sessions.Testing
MI355X only. No test changes: only table rows change.
op_tests/test_gemm_a16w16.py -t normal -d bf16 -o bf16 -mnk <shapes>launches these rows throughtgemm.mm. It logstgemm errabove its 0.05 mark for six of nine shapes: 0.053 to 0.108 onmain, 0.051 to 0.108 with this change; the other three read 0. Per shape this change reads from 0.006 belowmainto 0.0006 above. The inputs are not seeded: two runs of one kernel differ by up to 0.003. So this change does not cause it. The mark is a log line; every run ended with exit 0. Cause and all numbers: the block below.On a GPU:
main8b6b4b4 and a tree with seven of the rows.main.test_flydsl_hgemm.py,test_gemm_a16w16.py,test_graph_alloc.py(defaults): pass on the tree with seven rows, not run on the tree of this commit. They select none of the 12 rows.Why
tgemm errreads 0.05 to 0.11 for six shapes, and all three columnstgemm erris the share of output elements for whichtorch.isclose(torch, tgemm.mm, rtol=1e-2, atol=1e-2)is false (checkAllcloseinaiter/test_common.py). Above 0.05 the function logsfailed!and returns the share. The test stores it in its table and goes on. It stops only at a NaN, an Inf, or a difference above half of the largest output.ks2,ks4) or has two K waves (w1x1x2). On gfx950 with BF16 output each K part converts its sum to BF16 before the parts are added (aiter/ops/flydsl/kernels/gemm_a16w16_gfx950.py: lines 847, 893 and 931, and the packed atomic add of line 531). The test compares that with a result that was rounded once.err_ratio0.0053 to 0.0134, onmaintoo. The tiles withks1and one K wave read 0 by both...._splitk_clean) read 0.052 to 0.055 against torch on every shape.main_bnt)_bnt)t16x32x256x4_ks1_w1x1x2t16x32x128x5_ks1_w1x2x1t16x32x128x7_ks1_w1x2x1t16x32x256x4_ks1_w1x1x2t16x32x256x4_ks1_w1x2x1t32x96x64x5_ks4_w2x2x1t32x96x64x5_ks4_w2x2x1t32x64x128x5_ks2_w1x4x1t32x64x128x5_ks2_w1x4x1t16x32x128x5orx6,ks1_w1x2x1t48x64x128x3_ks2_w1x4x1t64x96x64x7_ks4_w2x2x1Two runs of the same
_bntkernel (the two trees) differ by up to 0.0030. On the tree with 12 rows the share is at most 0.0006 abovemain(four shapes) and up to 0.0060 below it (two shapes). On the tree with seven rows it is belowmainin all six.Checks on a CPU (no GPU), on
main8b6b4b4 and on this branch; repeated onmain4122ef5op_tests/tuning_tests(test_a16w16_tuned_config_lookup,test_csv_validation,test_config_shape_collision,test_dispatch_verification,test_asm_splitk_guard,test_tuner_infra,test_compare_logic,test_mp_tuner_logic,test_a6w6_tuning,test_mixed_mxfp_tuning,test_arch_resolution): 250 tests pass, 8 skipped, the same on both trees. Onmain4122ef5 and on its merge with this commit these modules hold 264 tests ([HIP] [FlyDSL] [CI] [Perf] gfx950 small-M BF16 ring GEMM policies (rg lm_head, rk DSpark FC) in the hgemm catalog, tuner and tgemm path #8675 added 14): 254 pass, 8 are skipped, and 2 of the new ones need a device. The same on both trees.get_GEMM_A16W16_tuned_configfor M = 1 to 48 of both shapes: on this branch M = 1 to 32 select the edited rows; M = 33 to 48 select the rows ofmain.tgemm.mmon CPU tensors up to the launch (the launch is a stand-in), M = 1 to 48, both shapes: it builds the_bntkernel for M = 1 to 32 and the kernel ofmainfor M = 33 to 48._a16w16_flydsl_launch, as written in SGLangmain884b0b9eaa9e, against both trees for M = 1 to 48 of both shapes: the ten fields are identical in all 96 cases, and so is the kernel thatgemm_a16w16()builds from them, for BF16 and for fp32 output.solidxandsplitKof the 12 rows equal the tuner catalog (get_flydsl_bf16_catalog) ofmain8b6b4b4, the catalog of the sessions: 12 of 12. Onmain4122ef5 a process with a GPU also gets the ring candidates of [HIP] [FlyDSL] [CI] [Perf] gfx950 small-M BF16 ring GEMM policies (rg lm_head, rk DSpark FC) in the hgemm catalog, tuner and tgemm path #8675, 33 to 36 per key. They sort before these names, so the index there is 12 higher (M = 1 to 16) or 15 higher (M = 32); I computed that without a GPU.splitKdoes not change. No launch readssolidxof a FlyDSL row.--checkrun: the block under "Changes".blackandruffdo not apply: no Python file changes.main4122ef5 and on its merge with this commit: the lookup, thetgemm.mmstand-in and the ten-field check give the same 96 results; the 26 kernel binaries of the replay are byte-identical to those of 8b6b4b4;tests/test_flydsl_aot_replay.pyand the other CPU tests that read the tables pass or fail alike on both trees (the failures need a device). The merge has no conflict.Documentation
The change is tuned-table rows.
Dependencies
Breaking Changes
No interface change. Two risks (above): the cost with warm weights (such a caller needs its own table,
AITER_CONFIG_GEMM_BF16) and a run-only process with an old AOT cache.PR Self-Review
Reviewed revision
27c1801
Behavior and intent
For 1 to 32 rows of the two shapes,
tgemm.mmon gfx950 builds the tuned tile with non-temporal B loads. The rows were tuned before the option existed.Validation and unverified scope
op_tests/test_gemm_a16w16.py -t normal -d bf16 -o bf16on 16 shapes ran on the tree of this commit (exit 0); the rest is under "Performance" and "Testing". Unverified: any GPU run onmain4122ef5 (the GPU runs are on 8b6b4b4); other gfx950 hardware with 256 compute units; serving; a re-tune; the three test files on the tree of this commit; the full-table AOT prebuild; nine rows with warm weights.PR-specific question
The 12 keys of the diff; they serve M = 1 to 32 of the two shapes. Unrelated shapes: the diff is these 12 lines, no other BF16 table holds these keys, the lookup returns the rows of
mainfor M = 33 to 48, and the table tests pass.Changed op tests
None.