Skip to content

[AMD] perf: Integrate Kimi-K3 ROCm fused kernels into the model - #43130

Closed
yuychang wants to merge 8 commits into
sgl-project:mainfrom
yuychang:up/b6-k3-rocm-integration
Closed

yuychang wants to merge 8 commits into
sgl-project:mainfrom
yuychang:up/b6-k3-rocm-integration

Conversation

@yuychang

@yuychang yuychang commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Stack: part 6 of 8. Based on #41794, #43125, #43126, #43127, #43128, #43129. Until those merge, this diff also shows their changes. Review only the last commit, 867d6953da.

Uses ROCm/aiter#6270 for the fused AR+RMSNorm (hip_fused_ar_rmsnorm). Without it that path falls back to plain all-reduce. See AITER dependency.

Motivation

This PR connects the kernels from the previous PRs to the Kimi-K3 model on ROCm, and adds the all-reduce fusions and FP8 decode projections that use them.

Modifications

  • Call the MLA, KDA and MoE fused kernels from new kimi_k3_rocm_*.py modules. Each path has its own env switch and falls back to the existing code when the shape or hardware does not match.
  • kimi_k3.py only gains short _is_hip hooks. The CUDA paths do not change.
  • ROCm decode reads the attention input already quantized for the in-projection. The decoder asks the stage boundary for quant_format="fp8" or "fp8_per_token", the existing boundary option, so the input norm runs fused with the FP8 quant in upstream's own kernels.
  • AttnBankState (shared) gains quantized attention reads: the bank aggregates without the out-norm, then runs that format's norm+quant kernel. Where the aggregation cannot skip the norm, or the read is gathered afterwards, it reads plainly. AttnResidual.aggregates_unnormed reports which applies.
  • Add hip_fused_ar_rmsnorm on AITER custom all-reduce, with a token cap from measurements.
  • Split the MoE [latent | shared] all-reduce into two when it is larger than the quick-reduce buffer.
  • Add PTPC FP8 decode projections, online FP8 loading and shared-expert weight caches.
  • Use all-zero positions for the Triton MLA identity-RoPE fallback, so it never reads past the one-row cos/sin tables.
  • Keep the MORI combine output when the zero-copy MoE buffer is published (moe_runner/aiter.py).
  • Add an opt-in SGLang BF16 GEMM tuning profile for M=16384. It is set while the Kimi-K3 model is built, before AITER loads its BF16 GEMM table, so it also applies to single-process tools such as bench_one_batch.
  • Let AttnResidual.forward take out_norm=None.
  • Turn off symmetric memory under graph capture from an argument hook that only runs on HIP.
  • Rename SGLANG_AITER_K3_OPT to SGLANG_ROCM_K3_AITER_OPT; the old name still works with a deprecation warning.
  • Add runtime tests, all-reduce benchmarks and entries in the env variable reference.
New env flag Default
SGLANG_ROCM_FUSED_AR_RMSNORM, _MAX_TOKENS on, 24
SGLANG_ROCM_K3_SPLIT_OVERSIZED_MOE_AR on
SGLANG_ROCM_K3_ATTN_RES_TUNED_LAUNCH on
SGLANG_ROCM_K3_AITER_OPT (was SGLANG_AITER_K3_OPT) off
SGLANG_ROCM_K3_AITER_TUNED_MOE_FRONT, SGLANG_ROCM_K3_MOE_LATENT_MXFP4, SGLANG_ROCM_K3_MOE_LATENT_FP8, SGLANG_ROCM_K3_PTPC_FP8, SGLANG_ROCM_K3_PTPC_FP8_SHARED_DOWN, SGLANG_ROCM_K3_ONLINE_FP8_*, SGLANG_ROCM_K3_AITER_M16384_PROFILE off

The three flags that default to on only apply on ROCm.

Shared (non-K3) files: arg_groups/pipeline.py, arg_groups/resolution_hooks.py, attn_residual.py, attn_res_hip.py, layer_boundary/residual/attn_bank.py, aiter_mla_gluon.py, moe_runner/aiter.py, mxfp4.py, quark_w4a4_mxfp4_moe.py, forward_mla_rocm.py, layers/communication/hip_*.py.

Changes since the last revision

AITER dependency

hip_fused_ar_rmsnorm calls custom_fused_ar_rms(..., residual_out=, out=, num_norm_rows=, skip_residual=), which ROCm/aiter#6270 adds. Without it the module returns None and the model uses plain all-reduce; nothing fails, so check the AITER version when you measure that path.

It does not use --enable-aiter-allreduce-fusion. That flag fuses all-reduce + residual add + RMSNorm at the shared layer boundary. Kimi-K3's MoE all-reduce is AR + RMSNorm on the latent rows only, with no residual, which the flag's kernel cannot do until ROCm/aiter#6270.

Accuracy Tests

GSM8K, 5-shot, 1319 questions. Base is #43129 (kernels present, not called). Each run used a fresh server.

Base This PR
0.929, 0.942 (mean 0.936) 0.923, 0.937 (mean 0.930)

GSM8K varies by about 0.013 between runs of the same code on this setup, so the PR is at parity. Both arms occasionally emit garbled text for a few adjacent questions that ran in the same batch (4 to 8 answers on base, 13 to 17 on this PR, out of 1319). The shared-expert bug fixed in this revision garbled 62 to 67 answers throughout a run.

Speed Tests and Profiling

Result: TPOT is 4.8% to 8.4% lower, TTFT about 7% lower, and throughput 5.6% to 9.1% higher at every concurrency. Base is #43129 (kernels present, not called). This PR uses the recipe's env switches.

MI355X x8, TP8, /data/models/Kimi-K3-Quark-MXFP4-AttnFP8, AITER e8f486b55 (upstream AITER main plus the Kimi-K3 changes in the draft AITER PRs, including ROCm/aiter#6270). Kimi-K3 8K/1K recipe: random 8192 in / 1024 out, 4 waves per concurrency, --disable-radix-cache, 16K chunked prefill, FP8 KV cache, AITER prefill and decode attention, TP8 without DCP8. Each arm used a fresh server. Base TPOT varied by at most 0.7% over three runs.

Conc TPOT (ms) base TPOT (ms) PR Δ TTFT (ms) base TTFT (ms) PR Δ Throughput (tok/s/GPU) base Throughput (tok/s/GPU) PR Δ
2 17.24 15.80 -8.4% 856 791 -7.6% 124.7 136.0 +9.1%
4 18.95 17.91 -5.5% 1560 1453 -6.9% 219.2 232.1 +5.9%
8 22.28 20.88 -6.3% 2441 2267 -7.1% 365.1 390.2 +6.9%
16 27.82 26.28 -5.5% 4194 3888 -7.3% 564.5 598.8 +6.1%
32 37.10 35.26 -5.0% 7699 7130 -7.4% 807.7 853.4 +5.7%
64 53.92 51.36 -4.8% 14713 13618 -7.4% 1055.0 1114.3 +5.6%

The previous revision of this description reported up to -19% TPOT against an older base, and was measured before the shared-expert fix above. These numbers replace it.

Tests (MI355X): test_attn_bank_quant_read.py (6, new), test_kimi_k3_kda_inproj_merge.py, test_mla_bf16_absorb_weight.py, test_hip_k3_moe_pair_ar.py, test_kimi_k3_rocm_bootstrap.py, test_latent_mxfp4_aiter_hip.py, test_hip_fused_ar_rmsnorm.py: 36 passed. test/registered/unit/layer_boundary/ has the same 17 failures on this ROCm machine with and without this PR (CUDA-only kernels, and stubs without .weight reaching the ROCm attention-residual kernel).

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ⏳ Run #38501310209
Latest PR Test (Extra): ⏳ Run #38501310140
Latest PR Test (AMD ROCm 10): ⏳ Run #38501310249

@hubertlu-tw

Copy link
Copy Markdown
Collaborator

@yuychang FYI: we have --enable-aiter-allreduce-fusion for fused custom all-reduce kernels

…rb on FP8 kernels on ROCm

- Merge the Quark per-channel FP8 [q,k,v,g | f_a | b] KDA in-projection into one FP8 linear for decode
- Pad the merged output to a multiple of 64 so it keeps AITER's preshuffled FP8 GEMM
- Serve the Quark FP8 f_b from a BF16 copy so it uses the tiny GEMM and the fused KDA decode
- Take the single in-proj GEMM path whenever the merged layer exists, not only when _bfa_w is set
- Requantize the Quark per-channel FP8 kv_b to per-tensor FP8 so the MLA absorb BMMs run AITER a8w8
- Gate the absorb requantization behind SGLANG_ROCM_K3_MLA_ABSORB_FP8, on by default
- Read SGLANG_ROCM_K3_KDA_FUSED_BACKEND through envs instead of os.environ
@yuychang

yuychang commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor Author

@HubertLu Thanks for the information. I checked whether --enable-aiter-allreduce-fusion can cover Kimi-K3. It can't today, because K3's all-reduces don't match the pattern it fuses:

Kimi-K3 all-reduce Module in this PR Covered by the flag?
Attention-residual prefix: AR + residual add, no norm hip_ar_residual.py No. The flag always applies RMSNorm after the add.
MoE [latent | shared]: AR + RMSNorm on the latent rows only, no residual hip_fused_ar_rmsnorm.py No. fused_allreduce_rmsnorm has no num_norm_rows / skip_residual.
MoE pair larger than the quick-reduce buffer: split into two all-reduces hip_k3_moe_pair_ar.py No. This isn't a fusion.

The flag also works only at the shared layer boundary (fused_attn_input / forward_with_allreduce_fusion). K3's attention-residual layers never reach that code, so turning it on changes nothing for K3.

The AITER options K3 needs are in ROCm/aiter#6270. I added that dependency to the description.

You're right that hip_fused_ar_rmsnorm._aiter_fused_ar_rms duplicates the dispatch in GroupCoordinator.fused_allreduce_rmsnorm. Once ROCm/aiter#6270 lands, I'd like to add num_norm_rows, skip_residual and out to GroupCoordinator.fused_allreduce_rmsnorm. Then the K3 module could call it instead of copying the dispatch.

- Let Quark W8A8 FP8 take a per-token (fp8, scale) tuple from any AITER producer, not only small-M FP8.
- Unpack an (fp8, per-token scale) tuple in apply_fp8_linear on ROCm for linears that pass it straight through.
- Add ROCm probes for linears that take group-128 or PTPC FP8 tuples without re-quantization.
- Add the PTPC FP8 AITER GEMM wrapper with packing, per-channel detection and numerical tests.
- Write PTPC GEMM out= via aiter's gemm_a8w8_bpreshuffle_out (ROCm/aiter#6437), or copy on builds lacking it.
- Add BM=16 kernels that fuse radix-4 routing, sorting and MXFP8 quantization.
- Include correctness tests and the decode sort toggle.
- Add the gfx950 FlyDSL MLA output-gate epilogue and its AITER adapter.
- Add a JIT kernel that applies the MLA output gate and quantizes the result to FP8.
- Add an AITER adapter that writes the MLA query and latent KV cache in one launch.
- Register SGLANG_ROCM_K3_FLYDSL_SOURCE in Envs and keep SGLANG_K3_FLYDSL_SOURCE as a deprecated alias.
- Add EnvStrWithAlias for string variables that are renamed.
- Add numerical tests against the unfused reference.
- Vendor the FlyDSL vector shim in kernels/jit/utils, since AITER no longer ships aiter.ops.flydsl.kernels.vector.
- Add the gfx950 FlyDSL group-64 KDA input projection and its AITER adapter.
- Add an AITER adapter for packed MXFP4 latent down and up projections.
- Add numerical tests for the group-64 projection against the BF16 reference.
- Add gfx950 FlyDSL tri and dual projection kernels for the MoE front before routing.
- Add a cooperative kernel that returns the shared expert already activated.
- Add an FP8 shared-expert down projection kernel.
- Add a latent tail kernel that fuses residual add, RMSNorm and FP8 quantization.
- Add AITER adapters, a row-wise FP8 weight helper, numerical tests and a benchmark.
- Check router top-k against FP32 within BF16 rounding, since near-tied logits can swap.
@hubertlu-tw

Copy link
Copy Markdown
Collaborator

@HubertLu Thanks for the information. I checked whether --enable-aiter-allreduce-fusion can cover Kimi-K3. It can't today, because K3's all-reduces don't match the pattern it fuses:

Kimi-K3 all-reduce Module in this PR Covered by the flag?
Attention-residual prefix: AR + residual add, no norm hip_ar_residual.py No. The flag always applies RMSNorm after the add.
MoE [latent | shared]: AR + RMSNorm on the latent rows only, no residual hip_fused_ar_rmsnorm.py No. fused_allreduce_rmsnorm has no num_norm_rows / skip_residual.
MoE pair larger than the quick-reduce buffer: split into two all-reduces hip_k3_moe_pair_ar.py No. This isn't a fusion.
The flag also works only at the shared layer boundary (fused_attn_input / forward_with_allreduce_fusion). K3's attention-residual layers never reach that code, so turning it on changes nothing for K3.

The AITER options K3 needs are in ROCm/aiter#6270. I added that dependency to the description.

You're right that hip_fused_ar_rmsnorm._aiter_fused_ar_rms duplicates the dispatch in GroupCoordinator.fused_allreduce_rmsnorm. Once ROCm/aiter#6270 lands, I'd like to add num_norm_rows, skip_residual and out to GroupCoordinator.fused_allreduce_rmsnorm. Then the K3 module could call it instead of copying the dispatch.

@yuychang Thank you for looking into the difference. Yes, let's keep the maintainability of all the variants reasonable.

- Call the MLA, KDA and MoE fused kernels from kimi_k3_rocm_*.py behind per-operator env switches.
- Keep the ROCm bodies in kimi_k3_rocm_*.py; kimi_k3.py only gains short _is_hip hooks.
- Run the ROCm fused MoE front through forward_fused_rocm so the CUDA path is unchanged.
- Read the ROCm decode attention input as FP8 through the stage boundary's quant_format.
- Let an attention-residual bank read fuse its norm with the requested quantization where it can skip the norm.
- Add hip_fused_ar_rmsnorm on AITER custom all-reduce with a measured token cap.
- Split oversized [latent | shared] MoE all-reduces so each slice fits quick reduce.
- Add PTPC FP8 decode projections, online FP8 loading and shared-expert caches.
- Use zero positions for the Triton MLA identity-RoPE fallback so it never reads past the one-row tables.
- Keep the MORI combine output when the zero-copy MoE buffer is published.
- Set an opt-in SGLang M16384 BF16 GEMM profile for AITER while the Kimi-K3 model is built.
- Write Quark MXFP4 shared-expert projections into caller buffers from kimi_k3_rocm_moe.py.
- Disable symmetric memory under graph capture from an arg hook that only runs on HIP.
- Rename SGLANG_AITER_K3_OPT to SGLANG_ROCM_K3_AITER_OPT and keep the old name as a deprecated alias.
- Add runtime tests, all-reduce benchmarks and the env variable reference.
@yuychang
yuychang force-pushed the up/b6-k3-rocm-integration branch from 0acafe4 to 867d695 Compare October 14, 2026 02:29
@yuychang
yuychang marked this pull request as ready for review October 14, 2026 02:48
@yuychang

Copy link
Copy Markdown
Contributor Author

@HubertLu Follow-up after rebasing onto the stage-boundary refactor (#42894, #42898, #42936):

  • hip_ar_residual is gone. Its call site no longer exists upstream, and it gave no gain because K3 already folds the add into attn_res_hip.
  • The ROCm FP8 attention-input fusion now uses the boundary's existing quant_format option and upstream's norm+quant kernels instead of K3-local copies. The only shared change is a quantized read in AttnBankState, covered by test_attn_bank_quant_read.py.
  • hip_fused_ar_rmsnorm still needs [HIP] [JIT] [Feature] Custom allreduce: fused residual add and row-selective RMSNorm ROCm/aiter#6270. Once that lands, I'd still like to move its dispatch into GroupCoordinator.fused_allreduce_rmsnorm, as discussed above.

Updated A/B and GSM8K results are in the description. The PR is ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

amd documentation Improvements or additions to documentation jit-kernel npu quant LLM Quantization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants