Repository navigation
Conversation
* 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]>
Collaborator
Author
|
/rerun-test test_prepared_nvfp4.py test_prepared_mxfp4.py test_route_quant_prerequisites.py |
kpham-sgl
requested review from
BBuf,
DarkSharpness,
Edwardf0t1,
Fridge003,
HaiShaw,
HydraQYH,
Ying1123,
celve,
ch-wan,
ispobock,
merrymercy and
yuan-luo
as code owners
October 26, 2026 01:41
3 of 4 tasks
Contributor
|
🚀 🚀 |
Collaborator
Author
|
/rerun-test test_prepared_mxfp4.py test_prepared_nvfp4.py |
Contributor
|
🚀 |
… 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]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
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]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
kpham-sgl
force-pushed
the
kpham/k3-fused-nvfp4-route-prep
branch
from
October 26, 2026 02:46
88dd6b9 to
039539d
Compare
Collaborator
Author
|
/rerun-test test_prepared_mxfp4.py test_prepared_nvfp4.py test_route_quant_prerequisites.py |
Contributor
|
🚀 🚀 |
Closed
3 of 5 tasks
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. |
2 of 5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 <= 8on both NVFP4 and MXFP4.Modifications
route_quant_metadata.cuh: templated on a row quantizer (Mxfp8RowQuant, andNvfp4RowQuantunder-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_metadatatakes the weight dtype; the Tritonprepare_expert_tile_metadatais 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_metadataserve both formats.trtllm_prepared_fp4.py:try_prepared_k3_nvfp4; MXFP4 and NVFP4 gates move from M in {1, 8} toM <= 8, with in-op finalize for M > 1.flashinfer_trtllm.py,kimi_k3.py: the NVFP4 runner takes the prepared input (gated onhidden_pad == 0, keeping main's padding and deferred-finalize checks) and tries the prepared body for deferred finalize or1 < M <= 8; the model stages the fused preparation at every batch size through a shared_route_stagedhelper (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
route_radix+fp4_quantizefor 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 matchestrtllm_fp4_block_scale_routed_moeto 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.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: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:
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
test_prepared_nvfp4.py;test_route_quant_prerequisites.pyandtest_prepared_mxfp4.pyfrom [Perf] [NVIDIA] kimi-k3: Device-prepared expert-tile routing metadata for the FlashInfer FP4 MoE #46475 also cover it).🤖 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