Repository navigation
Conversation
| source: flashinfer | ||
| destination: tensorrt_llm/_torch/moe/flashinfer | ||
| include: | ||
| - prims_ts/batched_gemm/__init__.py |
There was a problem hiding this comment.
Can use wildcard instead of listing all files.
There was a problem hiding this comment.
Done where it fits: batched_gemm/*.py and moe/*.json are wildcards now. The moe/ modules are still listed one by one because a wildcard would also pull in FlashInfer's moe/runner.py and moe/tools/, which this PR does not vendor: TensorRT-LLM's trtllm::prims_ts_moe op takes the runner's place.
There was a problem hiding this comment.
huge patch file usually indicates misuse of patch. Maybe these changes should be maintained as a fork branch and create a PR to merge it back to flashinfer.
There was a problem hiding this comment.
Agreed, and the PR now works that way. The kernels are FlashInfer main as-is (637f9c53), and the patch is down to 293 lines of TensorRT-LLM-only adaptations: relative imports, leaving out the dense GEMM package, a TensorRT-LLM compile-cache module name, and an in-process compile fallback for flashinfer-python==0.7.3. The locality-domain kernel changes are gone; kernel fixes (such as the MXFP8 scale rounding noted in the description) go to FlashInfer first and come back through a new pin.
07b6d33 to
d011547
Compare
Integrate FlashInfer's PrimsTS batched-GEMM MoE kernels as a new MoE implementation family on the refactored MoE stack. - Vendor flashinfer/prims_ts (batched GEMM and MoE adapter modules) from FlashInfer main 855eece4 with scripts/vendor/manage.py, plus an integration patch: relative imports, no dense GEMM module, a TRT-LLM compile-cache module name, and in-process compilation when the pinned flashinfer cannot persist kernels compiled without TVM-FFI. - Add fused_moe/prims_ts with NVFP4, W4A8_MXFP4_MXFP8 and BF16 leaves (flashinfer.prims_ts.batched_gemm.*) for SM100/SM103/SM107, selected by moe_config.backend PRIMS_TS or the tactic prims_ts. The weights reuse the TRTLLM-Gen quantization methods and routing runs on the host; EPLB, MoE LoRA and expert bias are declined. - Add trtllm::prims_ts_moe: moe_sort with a new opt-in return_token_map output, the FC1/FC2 PrimsTS GEMMs tuned by the TRT-LLM AutoTuner, and the moe_unpermute_inplace finalize. The op is CUDA-graph capturable. - Allow PRIMS_TS for the Kimi K3 routed experts (SiTU on the quantized leaves). - Tests: selection and eligibility, the moe_sort token map, CUDA graph replay, K3 SiTU numerics including a saturating linear soft cap, serve schema and precompiled-install coverage; CI entries on B200, B300 and GR100. Signed-off-by: qgai <[email protected]>
Re-pin flashinfer-prims-ts-moe from FlashInfer main 855eece4 to 637f9c53, which adds FlashInfer NVIDIA#8497 (faster MXFP4 x MXFP8 MoE GEMMs: deeper-pipelined tile configs, 64-byte K-block weights and an opt-in fused FC2 finalize) and NVIDIA#8498. The integration patch applies unchanged, and the new launch arguments are optional, so the op keeps the unfused finalize. NVIDIA#8497 renumbers the tactics. Key the autotuner cache on a digest of the vendored config mapper and tables instead of a hand-kept version string, so tuned tactics from other config tables are not replayed. Signed-off-by: qgai <[email protected]>
d011547 to
3fe9999
Compare
@coderabbitai summary
Description
Adds
PRIMS_TS, a MoE implementation family that runs FlashInfer's PrimsTS(CuTe DSL primitives + task scheduling) batched-GEMM kernels, on the refactored
MoE stack (
MoEImplBaseleaves,can_implement,moe_resolution). Based onmain
ab8f4ea03c.This PR used to carry the PrimsTS MoE prototype with Rubin locality domains
(9 commits, head
c4b7715903, kept on the fork as tagprimsTSforMOE-locality-c4b7715903). The scope is now only the integration;locality domains and the kernel changes made for them are dropped, and the
branch is rebuilt on current main.
Kernels.
flashinfer/prims_ts(batched GEMM and MoE adapter modules) isvendored from FlashInfer main
637f9c53undertensorrt_llm/_torch/moe/kernels/prims_ts, throughscripts/vendor/manage.py(lock entry
flashinfer-prims-ts-moe), as the PrimsTS attention kernels are.637f9c53includes FlashInfer #8497 (faster MXFP4 x MXFP8 GEMMs) and #8498.The integration patch (
3rdparty/vendor_patches/flashinfer-prims-ts-moe.patch)holds TRT-LLM adaptations only:
compiled without TVM-FFI.
flashinfer-python==0.7.3predates FlashInfer[None][ci] Waive qwen3 test for accuracy bug in https://nvbugs/5505402 #7585, which added that.
Family.
fused_moe/prims_ts/holds three leaves on SM100/SM103/SM107:flashinfer.prims_ts.batched_gemm.nvfp4flashinfer.prims_ts.batched_gemm.w4a8_mxfp4_mxfp8flashinfer.prims_ts.batched_gemm.none(BF16)They are selected with
moe_config.backend: PRIMS_TSortactic: prims_ts; AUTO never picks them.is_situ_activationcontract those methods read. For SiTU,fc31_scale_comits the FC1 dequant, and the soft caps are restored after the NVFP4 method dividesact_betabyfc31_alpha.Op.
trtllm::prims_ts_moeruns three steps:moe_sortwith a new opt-inreturn_token_map. Output 3 then becomes the permuted-row to token map that the kernels gather with, sizedgetRouteMapAllocCount.moe_unpermute_inplace.The op has no host syncs and can be captured in a CUDA graph.
Models. Kimi K3 accepts
PRIMS_TSfor its routed experts, with SiTU on the NVFP4 and MXFP4×MXFP8 leaves.Behavior changes
flashinfer.nvfp4, the documented shortest tactic of the B12x NVFP4 leaf, now also matches the PrimsTS NVFP4 leaf. Asmoe_config.tacticit is now refused as spanning two families. Useb12x.nvfp4or the full id. The catalog's own note ("an implementation added in a later release can match a shortest tactic too") covers this.VANILLA,PRIMS_TSdegrades to CUTLASS with a warning when no leaf can serve the layer. A tactic or id pin never degrades.Known limitations / follow-ups
MXFP4×MXFP8 accuracy. On identical weights and inputs, PrimsTS NVFP4 and BF16 outputs are bitwise identical to TRTLLM-Gen. MXFP4×MXFP8 differs:
Cause: the FlashInfer FC1 epilogue quantizes the MXFP8 intermediate with the OCP floor scale,
2^floor(log2 amax) / 2^8. That clips about 25% of 32-value blocks at 448. TRT-LLM'smxfp8_quantizeand TRTLLM-Gen round the scale up instead, to2^ceil(log2(amax / 448)). An fp32 simulation of each rule matches its backend to within 0.28%.Fix: rounding the scale up in the epilogue makes PRIMS_TS bitwise identical to TRTLLM-Gen. That change belongs upstream in FlashInfer and will come in a follow-up. No MXFP4×MXFP8 accuracy test is in the CI lists until then.
Autotuning cost. Without [None][ci] Waive qwen3 test for accuracy bug in https://nvbugs/5505402 #7585 the compiled GEMMs live only in the process. A full tune compiles 64 (BF16), 70 (NVFP4) and 137 (MXFP4×MXFP8) FC1/FC2 configs per process, about 5 s each. For that reason, the
test_moe_backendPRIMS_TS matrix is not in CI.B300 and GR100 entries were not run locally; B200 was.
Test Coverage
Run locally on B200 (SM100) with
flashinfer-python==0.7.3andnvidia-cutlass-dsl==4.8.0, at this head:tests/unittest/_torch/moe/test_moe_impl.py(selection, eligibility, declarations, tactic pin);test_moe_impl_catalog.py;test_trtllm_serve_config_schemas.py(newPRIMS_TS/prims_tsrows);test_llmapi_config_telemetry_docs.py;test_precompiled_link_mode.py(vendored JSON kept); tactic tests intest_modeling_speculative.py,test_modeling_v2_routing.py,test_model_config.pytests/unittest/_torch/moe/test_prims_ts_moe.py: vendored import guard,moe_sorttoken map (all local / EP slice), CUDA-graph replay following changing routes (BF16, NVFP4, MXFP4×MXFP8)test_kimi_k3_situ_moe.py:[PRIMS_TS]explicit backend,test_nvfp4_kernel_actually_applies_situ[PRIMS_TS], newtest_nvfp4_situ_linear_softcap_binds[TRTLLM/PRIMS_TS]test_moe_module.py::test_configurable_moe_single_gpu[...PRIMS_TS-quant={None,NVFP4}-routing=Renormalize], with autotuneNew SiTU test:
test_nvfp4_situ_linear_softcap_binds. It uses a linear soft cap of 1.0, which saturates the activations. With the cap restored, rel L2 is 0.175. With the cap left divided byfc31_alpha, it is 1.17. Before the fix, PRIMS_TS NVFP4 + SiTU returned all zeros.CI entries:
l0_b200pre-merge:test_prims_ts_moe.py(about 40 s; the op, the token map and graph replay for all three formats).l0_b200post-merge:test_configurable_moe_single_gpu[...PRIMS_TS-quant=None/NVFP4-routing=Renormalize]. These run in post-merge because each autotunes and JIT-compiles about 70 configs.l0_b300: the K3 PRIMS_TS SiTU tests, next to the TRTLLM ones (about 40 s).l0_gr100:test_prims_ts_moe.py.Generated files were regenerated:
trtllm-serveschemas;llm_args_golden_manifest.json, which only adds thePRIMS_TSliteral value. This needs telemetry/privacy code-owner approval.PR Checklist
Please review the following before submitting your PR:
PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.
PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.
Test cases are provided for new code paths (see test instructions)
If PR introduces API changes, an appropriate PR label is added - either
api-compatibleorapi-breaking. Forapi-breaking, includeBREAKINGin the PR title.Any new dependencies have been scanned for license and vulnerabilities
CODEOWNERS updated if ownership changes
Documentation updated as needed
Update tava architecture diagram if there is a significant design change in PR.
The reviewers assigned automatically/manually are appropriate for the PR.
Please check this after reviewing the above items as appropriate for this PR.
GitHub Bot Help
To see a list of available CI bot commands, please comment
/bot help.