Skip to content

[Perf] [NVIDIA] kimi-k3: Fused NVFP4 routing preparation and prepared FP4 routing at M <= 8 - #46484

Closed
kpham-sgl wants to merge 12 commits into
sgl-project:mainfrom
kpham-sgl:kpham/k3-fused-nvfp4-route-prep
Closed

kpham-sgl wants to merge 12 commits into
sgl-project:mainfrom
kpham-sgl:kpham/k3-fused-nvfp4-route-prep

Conversation

@kpham-sgl

@kpham-sgl kpham-sgl commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Follow-up to #46475. The NVFP4 checkpoint pays a separate native FP4 quantization launch before routing, and the prepared expert-tile path only covered M = 1 and M = 8. This fuses the radix routing with the NVFP4 quantization on SM107 and runs the prepared path for any 1 <= M <= 8 on both NVFP4 and MXFP4.

Modifications

  • route_quant_metadata.cuh: templated on a row quantizer (Mxfp8RowQuant, and Nvfp4RowQuant under -DENABLE_FP4); one host entry serves both formats and both write the expert-tile metadata in-kernel for M = 1..8.
  • moe_route_quant_fused.py: nvfp4_covered / route_quant_nvfp4 (SM107 only, FlashInfer's default FP4 fast math); allocate_metadata takes the weight dtype; the Triton prepare_expert_tile_metadata is padded to a power-of-two route count so M = 3, 5, 6, 7 compile.
  • route_quant_handoff.py: stage(x, global_scale) selects NVFP4; take, ready, take_metadata serve both formats.
  • trtllm_prepared_fp4.py: try_prepared_k3_nvfp4; MXFP4 and NVFP4 gates move from M in {1, 8} to M <= 8, with in-op finalize for M > 1.
  • flashinfer_trtllm.py, kimi_k3.py: the NVFP4 runner takes the prepared input (gated on hidden_pad == 0, keeping main's padding and deferred-finalize checks) and tries the prepared body for deferred finalize or 1 < M <= 8; the model stages the fused preparation at every batch size through a shared _route_staged helper (metadata only for M <= 8; larger M publishes none and the ordinary routed body consumes the prepared input). Main's 64-token cap on the fused route-quant coverage is kept.

Accuracy Tests

  • From the fork (B200, SM107 guard patched): NVFP4 fused output bit-exact against route_radix + fp4_quantize for M = 1..16, 31, 64, 100, 128, 129, 1000, 4096 (BF16/FP32 activations and scores, strided views, ties, graph replay); in-kernel metadata matches the Triton kernel.
  • test_prepared_nvfp4.py (new): the prepared NVFP4 body matches trtllm_fp4_block_scale_routed_moe to 1e-5 on GB300 at M = 1 and M = 8, deferred and finalized, with normalized and scaled routing weights, with fresh activation/scale/routing/output buffers on every call, and under CUDA-graph replay.
  • GB300 (SM103), TP8/DCP8, Kimi-K3 NVFP4, DSpark real acceptance, fp8 KV, no HiCache, this branch vs main: GSM8K 200 questions, 5-shot, temperature 0: 0.975 / 0.005 invalid vs 0.985 / 0.005; acceptance length 4.53 in both arms.
  • SM107 (two trays), plain TP8, same model and settings, this branch vs main: GSM8K 0.980 / 0.005 vs 0.985 / 0.005; acceptance length 5.29 vs 5.2.

Speed Tests and Profiling

Fork, SM107 TP8, bs=1, 60k/1k, NVFP4: routing body 12.90 -> 10.08 us, metadata prep 3.33 -> 1.57 us, fused preparation 5.61 -> 1.95 us; 144.0 -> 162.8 output tok/s (+13.1%). M8 DSpark verify (block 7, simulated acceptance 5.5): 458 -> 554 tok/s (+20.9%). Fused route preparation at larger M, B200 full layer: M=16 39.5 -> 35.3 us, M=64 63.1 -> 57.2, M=1024 261.0 -> 251.5, M=4096 385 -> 368.

GB300, same servers, one_batch_server --base-url, batch 1, 128k in / 1k out, 95% cache hit, three repeats:

Arm Latency (s) Output tok/s Steady decode tok/s Acc length
main 3.95 / 3.94 / 3.96 294.0 / 294.0 / 294.0 303 4.53
this PR 3.83 / 3.81 / 3.77 308.0 / 307.0 / 308.1 312-318 4.53

Output throughput +4.6% and steady decode +4% on GB300 from the prepared NVFP4 body at the verify batch and the deferred bs=1 path.

SM107, plain TP8, batch 1, 60k in / 1k out, 95% cache hit, three repeats:

Arm Latency (s) Output tok/s Steady decode tok/s Acc length
main 2.33 / 2.33 463.7 / 464.0 449.5 5.2
this PR 2.19 / 2.15 / 2.15 504.7 / 504.7 / 504.4 488.6 5.29

Output throughput +8.8% on SM107, where the fused NVFP4 route+quant kernel is also active.

Stacked on #46475; the first three commits here are that PR.

Checklist

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #40260731260
Latest PR Test (Extra): ❌ Run #40260731289
Latest PR Test (AMD ROCm 10): ❌ Run #40260731252

zcnrex and others added 4 commits October 25, 2026 15:33
* fix(kimi-k3): prepare and fuse Rubin expert routing metadata

Build the BS1 MXFP4 expert routing metadata on device instead of on the
host, then fuse radix routing, per-token-group quantization and the
TRT-LLM id packing into a single launch handed to the MoE runner through
the route-quant handoff.

Measured (BS1, 60k/1024, TP8): metadata prep 12.48 -> 10.97 us and the
fused sequence 2.80-3.12 -> 1.92-2.23 us; end to end 149.56 -> 163.75
output tok/s (+9.5%). GSM8K 127/128.
Co-Authored-By: Claude Opus 5 <[email protected]>

* refactor(kimi-k3): generalize prepared expert-tile routing metadata

Replace k3_prepared_mxfp4.py with trtllm_prepared_fp4.py. The prep
kernel is now prepare_expert_tile_metadata: sort-based, so it handles
repeated experts and multiple tokens up to the tile size, with TOP_K
and PACKED (id<<16 | bf16 weight) as constexprs. Runner setup, tactic
choice and the cached preparation workspace move into a shared helper,
and metadata sizes derive from tokens, top-k, experts and tile.

The prepared path and the fused routing-metadata handoff now run on
SM100/SM103 as well as SM107; the expert body is the same trtllm-gen
SiTU kernel Blackwell already uses.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* refactor(kimi-k3): import prepared routing helpers at module level

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(kimi-k3): fuse eight-token MXFP4 routing metadata

Extend the fused route+quant metadata kernel and the prepared MXFP4 path
from BS1 to M8. With more than one token, the last routing CTA to finish
(a self-resetting arrival counter, safe under graph replay) builds the
one-tile-per-expert layout from per-token expert bitmaps, in the same
(expert, route) order as prepare_expert_tile_metadata. M8 also accepts
finalized output, as the NVFP4 path does.

Measured on B200 (M8, synthetic K3 TP8 MoE shapes, graph replay): fused
route+quant 2.75 -> 4.80 us with metadata, replacing the 2.15 us Triton
prep; route+quant+experts 61.27 -> 59.39 us, and 67.26 us on
FlashInfer's own routing. The new test matches trtllm routed MoE at
M1/M8 (deferred and finalized) and the fused metadata bit-exactly
against the Triton kernel, including under graph replay.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Delete test/registered/kernel/moe/test_prepared_mxfp4.py

---------

Co-authored-by: Claude Opus 5 <[email protected]>
(cherry picked from commit 65406718ab8a1242a28ba10f42c1f8be42d6a681)
(cherry picked from commit 2d98eff)
…oject#68)

FlashInfer 0.7.0.post1 made `moe_op` the first positional argument of
`TrtllmMoERunner`. The K3 prepared-NVFP4 path still constructed it with
keyword arguments only, so a clean dev checkout crashed on the first MoE
forward:

    TypeError: MoERunner.__init__() missing 1 required positional argument: 'moe_op'

dev pins flashinfer_python[cu13]==0.7.0.post1, so pass it directly.

Co-authored-by: Claude Opus 5 <[email protected]>
(cherry picked from commit 92560b213203dbcbaef82926c8684453021f50e5)
(cherry picked from commit ebb74ae)
Checks the device-prepared expert-tile metadata against the Triton reference
and the prepared path against trtllm_fp4_block_scale_routed_moe at M = 1 and
M = 8, deferred and finalized, under CUDA-graph replay.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

/rerun-test test_prepared_nvfp4.py test_prepared_mxfp4.py test_route_quant_prerequisites.py

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (2 tests): ❌ View workflow run

cd test/ && python3 registered/kernels/ops/moe/test_prepared_nvfp4.py
cd test/ && python3 registered/kernels/ops/moe/test_prepared_mxfp4.py

🚀 1-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/moe/test_route_quant_prerequisites.py

@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

/rerun-test test_prepared_mxfp4.py test_prepared_nvfp4.py

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (2 tests): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/moe/test_prepared_mxfp4.py
cd test/ && python3 registered/kernels/ops/moe/test_prepared_nvfp4.py

zcnrex and others added 6 commits October 25, 2026 19:46
… routing to M <= 8 (sgl-project#10)

Fuse the radix routing with the native NVFP4 quantization into one kernel
and run the prepared expert-tile metadata path for any 1 <= M <= 8 on both
NVFP4 and MXFP4. route_quant_metadata is templated on its row quantizer and
built once per format (NVFP4 under -DENABLE_FP4), so both formats write the
metadata in-kernel for M<=8. The Triton metadata kernel, now padded to a
power-of-two route count, serves the unfused callers. The NVFP4 op, handoff and prepared-path checks share
moe_route_quant_fused, route_quant_handoff and one K3 shape check with
MXFP4. Joint finalize and the split8 gather cover the same range.

Measured (BS1, 60k/1024, TP8): routing body 12.90 -> 10.08 us, metadata
prep 3.33 -> 1.57 us, fused preparation 5.61 -> 1.95 us; 143.95 -> 162.77
output tok/s (+13.1%), GSM8K 127/128.
Measured (M8 DSpark verify, block size 7, simulated acceptance 5.5):
458.36 -> 554.25 output tok/s (+20.9%); M8 preparation 7.14 -> 3.93 us,
gather split8 9.99 us vs 10.68 at split4; real acceptance 527.29 tok/s,
GSM8K 126/128.

Co-authored-by: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 9de885fc9902828461ef655cfedb21334c753613)
(cherry picked from commit e2061fc)
Drop the token caps on the fused radix-routing plus quantization kernel:
NVFP4 was limited to M<=8 and MXFP8 to M<=64, but the fused launch beats
the unfused chain at every measured size (M128..32768). Expert-tile
metadata stays M<=8; larger batches run routing and quantization only
and use the ordinary routed body. Stage the NVFP4 handoff on the in-op
finalize path too (M above the push window), and fall back to a
contiguous cast when preparation does not land.

B200 proxy, CUDA graph, us per layer, dev -> this change, FP32 front:
prep M16 5.94 -> 2.46, M128 6.50 -> 3.07, M4096 54.5 -> 38.4;
prep + routed MoE M16 39.45 -> 35.25, M128 89.0 -> 83.4,
M4096 385 -> 368. M1/M8 unchanged.

Co-authored-by: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 503dc2c9838ae0dc8be952966c2094338fdb01a0)
(cherry picked from commit 7d270f7)
…method locally

Co-Authored-By: Claude Fable 5.1 <[email protected]>
(cherry picked from commit f9b59de)
Co-Authored-By: Claude Fable 5.1 <[email protected]>
kpham-sgl and others added 2 commits October 25, 2026 19:46
The prepared body returns the output tensor, not a tuple; result[0] took row
zero and broadcast it across verify-batch tokens (acceptance 4.5 -> 3.9 on
GB300, 5.2 -> 4.4 on SM107).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@kpham-sgl
kpham-sgl force-pushed the kpham/k3-fused-nvfp4-route-prep branch from 88dd6b9 to 039539d Compare October 26, 2026 02:46
@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

/rerun-test test_prepared_mxfp4.py test_prepared_nvfp4.py test_route_quant_prerequisites.py

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (2 tests): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/moe/test_prepared_mxfp4.py
cd test/ && python3 registered/kernels/ops/moe/test_prepared_nvfp4.py

🚀 1-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/moe/test_route_quant_prerequisites.py

@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

Superseded by #48586, which fuses the same routing chain for 1-256 rows on any expert range and took the handoff reset from this PR. Closing.

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

Labels

blackwell SM100/SM120 jit-kernel quant LLM Quantization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants