Skip to content

[Perf] kimi-k3: Rank-local expert ids for the EP routed MoE - #45544

Open
Venkat2811 wants to merge 1 commit into
sgl-project:mainfrom
Venkat2811:venkat/k3-compact-routed-expert-ids
Open

Venkat2811 wants to merge 1 commit into
sgl-project:mainfrom
Venkat2811:venkat/k3-compact-routed-expert-ids

Conversation

@Venkat2811

@Venkat2811 Venkat2811 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

  • With expert parallelism each rank runs only its own experts (112 of Kimi-K3's 896 at EP8).
  • FlashInfer picks its routing kernel from the total expert count.
  • At 896 experts, decode steps of 5-16 tokens run the multi-block cluster routing kernel.
  • That costs 11-12.6 us per MoE layer, about 1 ms per decode step.

Modifications

  • The fused route+quant kernel writes rank-local ids: own experts 0..count-1, any other expert count + top-k slot.
  • The runner passes num_experts = round_up(count + top_k, 4) (128 at EP8) and offset 0.
    FlashInfer then uses its single-block routing kernel up to 16 tokens.
  • The same (token, expert, weight) entries reach the expert GEMMs in the same order, so outputs do not change.
  • Startup tuning also tunes the rank-local key (2-3 min more on a first start without an autotune cache).
  • Applies only with EP > 1 on the fused decode path (MXFP4 trtllm-gen MoE, up to 64 tokens per step).
    EP = 1, prefill and other models are unchanged.

Tested configurations

Aggregated Prefill/decode
Hardware 1 x 8x B300 2 x 8x B300 (1 prefill + 1 decode), KV over RDMA
Server --tp-size 8 --dcp-size 8 --ep-size 8 prefill --tp-size 8 --ep-size 8, decode adds --dcp-size 8; sgl-router --pd-disaggregation
Builds main @ 7591118, with and without this change; FlashInfer 0.7.2 same
Speed bench_one_batch_server, input 8192, output 512 bench_serving, random, input 8192, output 512
Launches 3 per build, fresh autotune each 2 per build, fresh autotune each
Accuracy GSM8K 5-shot, 1,319 questions; 16 greedy prompts token by token -

The PR is rebased onto current main; the unit tests pass there.

Results

Routing kernel per MoE layer at 8 / 16 / 24 tokens: 11.1 / 12.2 / 12.6 us on main, 4.3 / 4.8 / 7.1 us with this PR.

Kimi-K3, aggregated (decode output tok/s, mean of 3 launches)

batch 8 batch 16 batch 32
main @ 7591118 558.9 803.6 1271.5
this PR 594.0 (+6.3 %) 873.2 (+8.7 %) 1300.8 (+2.3 %)

Kimi-K3, aggregated, with #45543 (decode output tok/s, mean of 2 launches)

batch 8 batch 16 batch 24 batch 32
main @ 3c56008 554.7 795.4 1077.1 1305.3
#45543 555.2 (+0.1 %) 856.7 (+7.7 %) 1132.1 (+5.1 %) 1372.4 (+5.1 %)
together with this PR 592.4 (+6.8 %) 898.9 (+13.0 %) 1168.0 (+8.4 %) 1397.4 (+7.1 %)

Kimi-K3, prefill/decode (mean of 2 launches)

output tok/s concurrency 16 concurrency 32 concurrency 64
main @ 7591118 761.3 1183.7 1496.1
this PR 793.2 (+4.2 %) 1189.8 (+0.5 %) 1498.9 (+0.2 %)
ITL p99 (ms) concurrency 16 concurrency 32 concurrency 64
main @ 7591118 32.4 38.4 42.3
this PR 28.0 (-13.6 %) 40.2 (+4.7 %) 41.5 (-2.0 %)
  • Decode batch at concurrency 16 / 32 / 64: about 16 / 30 / 40-44 (at 64 the prefill node is the limit).
  • The saving is fixed per decode step, so it is largest at small decode batches.
  • Prefill throughput and TTFT within 3 % (aggregated); memory unchanged.
  • GSM8K: 0.955 (main) vs 0.950 (this PR).
  • Greedy outputs identical when requests are sent one at a time (16/16); batched runs vary on main itself.
  • Unit tests: routed MoE output bitwise equal for global and rank-local ids at EP 2 / 4 / 8 / 16, 1-64 tokens.

Related: #42727 covers EP = 1 for the same model; the two do not overlap.

Reproduce
git clone https://github.com/Venkat2811/sglang && cd sglang
git checkout fc02c949195d520340ff8997e55e2fda0b7c0a82   # this PR; measured as 7591118 + this change
pytest -q test/registered/kernels/ops/moe/test_route_quant_prerequisites.py \
  test/registered/kernels/ops/moe/test_rank_local_routed_moe.py
export SGLANG_FLASHINFER_AUTOTUNE_CACHE=0               # fresh autotune per launch

# Aggregated
python3 -m sglang.launch_server --model-path moonshotai/Kimi-K3 --trust-remote-code \
  --tp-size 8 --dcp-size 8 --ep-size 8 --mem-fraction-static 0.85 \
  --linear-attn-prefill-backend triton --linear-attn-decode-backend triton --port 30000
python3 -m sglang.bench_one_batch_server --model-path moonshotai/Kimi-K3 \
  --base-url http://127.0.0.1:30000 --batch-size 8 16 32 --input-len 8192 --output-len 512
python3 -m sglang.test.few_shot_gsm8k --num-questions 1319 --parallel 64 --num-shots 5 --port 30000

# Prefill/decode (IB = your RDMA devices)
python3 -m sglang.launch_server --model-path moonshotai/Kimi-K3 --trust-remote-code \
  --tp-size 8 --ep-size 8 --mem-fraction-static 0.85 --page-size 64 \
  --linear-attn-prefill-backend triton --linear-attn-decode-backend triton \
  --disaggregation-mode prefill --disaggregation-transfer-backend mooncake \
  --disaggregation-ib-device $IB --disaggregation-bootstrap-port 8998 --host 0.0.0.0 --port 30000
# decode node: same command with --dcp-size 8 and --disaggregation-mode decode
python3 -m sglang_router.launch_router --host 0.0.0.0 --port 8001 --pd-disaggregation \
  --prefill http://$PREFILL_IP:30000 8998 --decode http://$DECODE_IP:30000
for c in 16 32 64; do
  python3 -m sglang.bench_serving --backend sglang --base-url http://$ROUTER_IP:8001 \
    --model moonshotai/Kimi-K3 --dataset-name random --random-input-len 8192 \
    --random-output-len 512 --random-range-ratio 1.0 \
    --num-prompts $((c * 16)) --max-concurrency $c --seed $c
done

P/D launch ranges, output tok/s: at 16, 760.6-761.9 vs 789.0-797.3; at 32, 1182.7-1184.6 vs 1186.7-1192.9;
at 64, 1494.6-1497.6 vs 1498.2-1499.6.

Checklist

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #39562924106
Latest PR Test (Extra): ❌ Run #39562923999
Latest PR Test (AMD ROCm 10): ❌ Run #39562924107

Under expert parallelism each rank's trtllm-gen routed MoE computes only its
own experts (112 of 896 at EP8), but FlashInfer picks its routing kernel from
num_experts. K3's 896 experts select the Tier<896, 16> specialisation, which
is above the dyn-block kernel's 512-expert limit, so decode steps of 5 to 16
tokens run routingIndicesClusterKernel, about 11 to 12.6 us per MoE layer on
8x B300 (once per MoE layer, so about 1 ms per decode step).

The fused route+quant launch (route_quant_fused, decode batches up to 64
tokens) already writes the packed (id << 16 | bf16 weight) ids the routed MoE
consumes. When the model stages the rank's expert range, its pack epilogue now
writes the rank's experts as 0..count-1 and any other expert in top-k slot k
as count + k (unique per row), and the runner passes
num_experts = round_up(count + top_k, 4) with local_expert_offset 0 (128 at
EP8: Tier<256, 16>, dyn-block kernel up to 16 tokens, a smaller cluster kernel
above). The global ids (out_i, which TopKOutput carries on) are unchanged, and
the same (token, local expert, weight) entries reach the expert GEMMs in the
same order, so the MoE output is bitwise unchanged for the same GEMM tactic.

Plumbing:
- route_radix.cuh / route_quant_fused.cuh / moe_route_quant_fused.py: optional
  (local_expert_offset, num_local_experts) for the packed output only; 0
  keeps the global ids. MAX_TOKENS (the fused launch's token cap) is public.
- route_quant_handoff.py: stage(x, local_experts) takes the rank's range;
  rank_local_num_experts() decides whether it applies (a proper sub-range that
  shrinks the routing problem); the range is recorded with the packed tensor
  and read back once with take_local_experts(routing), keyed by that tensor.
- kimi_k3.py: stages the range Mxfp4MoEMethod hands the runner, read per
  forward because bind_full_expert_weights can change it after construction.
- flashinfer_trtllm.py: passes the rank-local num_experts and offset 0 for
  such packed ids, after asserting the range equals quant_info's.

Autotune coverage: FlashInfer's MoE tuner key includes num_experts. When the
decode-shaped autotune forward runs above the fused route's 64-token cap
(decode CUDA graphs past 64 rows), it takes the unfused routing and would only
tune the global key, leaving every rank-local decode call on FlashInfer's
untuned default tactic. During tuning, when the runner sees unfused routing
and rank-local ids apply, it also runs FlashInfer on up to 64 of that
forward's routes, relabelled as the fused route would, so the decode key is
tuned from real routes. Serving paths are unchanged.

Measured on 8x B300, Kimi-K3 TP8 DCP8 EP8, one node: routing kernel per MoE
layer 11.1 / 12.2 / 12.6 us -> 4.3 / 4.8 / 7.1 us at 8 / 16 / 24 decode
rows; decode output throughput (bench_one_batch_server, 8192 in / 512 out)
+6.1 / +4.2 / +1.0 % at batch 8 / 16 / 32 with the expert-GEMM tactics held
at main's fresh picks, and +6.3 / +10.7 / -5.0 % with a fresh autotune per
arm (the tuner picks different GEMM tactics for the new key).

Tests: the kernel's rank-local ids against a torch oracle (EP 2/8/16, first,
middle and last rank, 1 to 64 tokens) with everything else it writes
unchanged; the handoff bookkeeping (keyed, consumed once, global ids when the
range does not apply); and FlashInfer's routed MXFP4 MoE output bitwise equal
between global and rank-local ids (EP 2/4/8/16, 1 to 64 tokens, SM100).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added quant LLM Quantization jit-kernel labels Oct 21, 2026
ihciah pushed a commit to ihciah/sglang that referenced this pull request Nov 25, 2026
…gl-project#45544)

Layer 5 of the Kimi-K3 release line on upstream main 586355a (base branch
kimi-k3-nightly-586355a8, the nightly-dev-cu13-20261028-586355a8 image).
Written as layer 4 of the 27 Oct line on upstream main 5884e4f, where the
checks below ran; the patch applies to this base unchanged.

Under expert parallelism each rank's trtllm-gen routed MoE computes only its
own experts (112 of 896 at EP8), but FlashInfer picks its routing kernel from
num_experts. K3's 896 experts select the Tier<896, 16> specialisation, which
is above the dyn-block kernel's 512-expert limit, so decode steps of 5 to 16
tokens run routingIndicesClusterKernel, about 11 to 12.6 us per MoE layer on
8x B300 (once per MoE layer, so about 1 ms per decode step).

The fused route+quant launch (route_quant_fused, decode batches up to 64
tokens) already writes the packed (id << 16 | bf16 weight) ids the routed MoE
consumes. When the model stages the rank's expert range, its pack epilogue now
writes the rank's experts as 0..count-1 and any other expert in top-k slot k
as count + k (unique per row), and the runner passes
num_experts = round_up(count + top_k, 4) with local_expert_offset 0 (128 at
EP8: Tier<256, 16>, dyn-block kernel up to 16 tokens, a smaller cluster kernel
above). The global ids (out_i, which TopKOutput carries on) are unchanged, and
the same (token, local expert, weight) entries reach the expert GEMMs in the
same order, so the MoE output is bitwise unchanged for the same GEMM tactic.

Plumbing:
- route_radix.cuh / route_quant_fused.cuh / moe_route_quant_fused.py: optional
  (local_expert_offset, num_local_experts) for the packed output only; 0
  keeps the global ids. MAX_TOKENS (the fused launch's token cap) is public.
- route_quant_handoff.py: stage(x, local_experts) takes the rank's range;
  rank_local_num_experts() decides whether it applies (a proper sub-range that
  shrinks the routing problem); the range is recorded with the packed tensor
  and read back once with take_local_experts(routing), keyed by that tensor.
- kimi_k3.py: stages the range Mxfp4MoEMethod hands the runner, read per
  forward because bind_full_expert_weights can change it after construction.
- flashinfer_trtllm.py: passes the rank-local num_experts and offset 0 for
  such packed ids, after asserting the range equals quant_info's.

Autotune coverage: FlashInfer's MoE tuner key includes num_experts. When the
decode-shaped autotune forward runs above the fused route's 64-token cap
(decode CUDA graphs past 64 rows), it takes the unfused routing and would only
tune the global key, leaving every rank-local decode call on FlashInfer's
untuned default tactic. During tuning, when the runner sees unfused routing
and rank-local ids apply, it also runs FlashInfer on up to 64 of that
forward's routes, relabelled as the fused route would, so the decode key is
tuned from real routes. Serving paths are unchanged.

Measured on 8x B300, Kimi-K3 TP8 DCP8 EP8, one node: routing kernel per MoE
layer 11.1 / 12.2 / 12.6 us -> 4.3 / 4.8 / 7.1 us at 8 / 16 / 24 decode
rows; decode output throughput (bench_one_batch_server, 8192 in / 512 out)
+6.1 / +4.2 / +1.0 % at batch 8 / 16 / 32 with the expert-GEMM tactics held
at main's fresh picks, and +6.3 / +10.7 / -5.0 % with a fresh autotune per
arm (the tuner picks different GEMM tactics for the new key).

Tests: the kernel's rank-local ids against a torch oracle (EP 2/8/16, first,
middle and last rank, 1 to 64 tokens) with everything else it writes
unchanged; the handoff bookkeeping (keyed, consumed once, global ids when the
range does not apply); and FlashInfer's routed MXFP4 MoE output bitwise equal
between global and rank-local ids (EP 2/4/8/16, 1 to 64 tokens, SM100).

Source: e251e3d on the kimi-k3-nightly-3c560089 line (fork PR sgl-project#20), a mirror
of upstream sgl-project#45544 (head fc02c94). One conflict, in
moe_route_quant_fused.covered(): upstream sgl-project#44690 dropped the
x.shape[0] == scores.shape[0] check (a mismatch is a caller bug the launcher
reports); kept that, and took the PR's public MAX_TOKENS rename. Checked
against sgl-project#44480 (merged 20 Oct, StandardDispatcher maps expert ids for runners
that need local ids under EP): flashinfer_trtllm and flashinfer_mxfp4 stay in
its keep-global-ids list, so nothing else remaps for this runner and the packed
ids are made rank-local once, in the fused route epilogue. The range the model
stages (moe_ep_rank * num_local_experts, num_local_experts) is the one the MXFP4
quant methods hand the runner at 5884e4f (mxfp4.py,
mxfp4_flashinfer_trtllm_moe.py), and the runner asserts the two are equal. The
runner function this changes is untouched by upstream since 3c56008.

Tests: GPU only (test_rank_local_routed_moe.py, test_route_quant_prerequisites.py
launch the JIT kernels; on a CPU host the new file does not import on the parent
and skips or fails for want of CUDA here, as the existing cases in
test_route_quant_prerequisites.py do on the base). On one B300 GPU on the 3c56008
line: test_rank_local_routed_moe.py 1/1 at this layer (2/2 on 23 Oct counted
layer 6's padded-row case) and test_route_quant_prerequisites.py 5/5. They
rerun on GPU with this branch's image.

Upstream: sgl-project#45544

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 09d9f99)
(cherry picked from commit cb640ad)

This branch has not been deployed

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

Labels

jit-kernel quant LLM Quantization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant