Skip to content

[None][feat] Add PrimsTS MoE implementation family - #19115

Draft
sunnyqgg wants to merge 2 commits into
NVIDIA:mainfrom
sunnyqgg:primsTSforMOE
Draft

sunnyqgg wants to merge 2 commits into
NVIDIA:mainfrom
sunnyqgg:primsTSforMOE

Conversation

@sunnyqgg

@sunnyqgg sunnyqgg commented Sep 13, 2026 •

Copy link
Copy Markdown
Collaborator

@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 (MoEImplBase leaves, can_implement, moe_resolution). Based on
main ab8f4ea03c.

This PR used to carry the PrimsTS MoE prototype with Rubin locality domains
(9 commits, head c4b7715903, kept on the fork as tag
primsTSforMOE-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) is
vendored from FlashInfer main 637f9c53 under
tensorrt_llm/_torch/moe/kernels/prims_ts, through scripts/vendor/manage.py
(lock entry flashinfer-prims-ts-moe), as the PrimsTS attention kernels are.
637f9c53 includes 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:

Family. fused_moe/prims_ts/ holds three leaves on SM100/SM103/SM107:

  • flashinfer.prims_ts.batched_gemm.nvfp4
  • flashinfer.prims_ts.batched_gemm.w4a8_mxfp4_mxfp8
  • flashinfer.prims_ts.batched_gemm.none (BF16)

They are selected with moe_config.backend: PRIMS_TS or tactic: prims_ts; AUTO never picks them.

  • Weights use the TRTLLM-Gen quantization methods unchanged.
  • The base provides the is_situ_activation contract those methods read. For SiTU, fc31_scale_c omits the FC1 dequant, and the soft caps are restored after the NVFP4 method divides act_beta by fc31_alpha.
  • Routing runs on the host (EXTERNAL_COMM).
  • The gates decline:
    • EPLB, MoE LoRA, expert bias and gpt-oss SwiGLU;
    • smart router;
    • SM107 without CuTe DSL Rubin support;
    • a hidden size that the weight methods would pad.

Op. trtllm::prims_ts_moe runs three steps:

  1. moe_sort with a new opt-in return_token_map. Output 3 then becomes the permuted-row to token map that the kernels gather with, sized getRouteMapAllocCount.
  2. The FC1 (fused activation) and FC2 GEMMs, with the tactic chosen by the TRT-LLM AutoTuner. The autotuner cache key carries a digest of the vendored config mapper and tables, so tactics tuned against other config tables are not replayed.
  3. The finalize, moe_unpermute_inplace.

The op has no host syncs and can be captured in a CUDA graph.

Models. Kimi K3 accepts PRIMS_TS for its routed experts, with SiTU on the NVFP4 and MXFP4×MXFP8 leaves.

Behavior changes

  • The tactic flashinfer.nvfp4, the documented shortest tactic of the B12x NVFP4 leaf, now also matches the PrimsTS NVFP4 leaf. As moe_config.tactic it is now refused as spanning two families. Use b12x.nvfp4 or the full id. The catalog's own note ("an implementation added in a later release can match a shortest tactic too") covers this.
  • Like every other backend literal except VANILLA, PRIMS_TS degrades to CUTLASS with a warning when no leaf can serve the layer. A tactic or id pin never degrades.

Known limitations / follow-ups

  1. MXFP4×MXFP8 accuracy. On identical weights and inputs, PrimsTS NVFP4 and BF16 outputs are bitwise identical to TRTLLM-Gen. MXFP4×MXFP8 differs:

    • PRIMS_TS vs TRTLLM-Gen: 2.5% relative L2;
    • against the quantize_utils reference: 2.9% for PRIMS_TS vs 1.6% for TRTLLM-Gen.

    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's mxfp8_quantize and TRTLLM-Gen round the scale up instead, to 2^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.

  2. 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_backend PRIMS_TS matrix is not in CI.

  3. B300 and GR100 entries were not run locally; B200 was.

Test Coverage

Run locally on B200 (SM100) with flashinfer-python==0.7.3 and nvidia-cutlass-dsl==4.8.0, at this head:

Tests Result
tests/unittest/_torch/moe/test_moe_impl.py (selection, eligibility, declarations, tactic pin); test_moe_impl_catalog.py; test_trtllm_serve_config_schemas.py (new PRIMS_TS / prims_ts rows); test_llmapi_config_telemetry_docs.py; test_precompiled_link_mode.py (vendored JSON kept); tactic tests in test_modeling_speculative.py, test_modeling_v2_routing.py, test_model_config.py 1241 passed
tests/unittest/_torch/moe/test_prims_ts_moe.py: vendored import guard, moe_sort token map (all local / EP slice), CUDA-graph replay following changing routes (BF16, NVFP4, MXFP4×MXFP8) 6 passed
test_kimi_k3_situ_moe.py: [PRIMS_TS] explicit backend, test_nvfp4_kernel_actually_applies_situ[PRIMS_TS], new test_nvfp4_situ_linear_softcap_binds[TRTLLM/PRIMS_TS] 4 passed; PRIMS_TS matches TRTLLM-Gen to every printed digit
test_moe_module.py::test_configurable_moe_single_gpu[...PRIMS_TS-quant={None,NVFP4}-routing=Renormalize], with autotune passed
PRIMS_TS vs TRTLLM-Gen, same weights (E=8, top-k 2, H=1024, I=512, T=16, three seeds) NVFP4 and BF16 bitwise identical

New 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 by fc31_alpha, it is 1.17. Before the fix, PRIMS_TS NVFP4 + SiTU returned all zeros.

CI entries:

  • l0_b200 pre-merge: test_prims_ts_moe.py (about 40 s; the op, the token map and graph replay for all three formats).
  • l0_b200 post-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:

  • the MoE impl catalog;
  • the trtllm-serve schemas;
  • llm_args_golden_manifest.json, which only adds the PRIMS_TS literal 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-compatible or api-breaking. For api-breaking, include BREAKING in 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.

@sunnyqgg sunnyqgg added the api-compatible Accepted LLM API contract change that is backwards-compatible label Sep 13, 2026 — with ChatGPT Codex Connector
@sunnyqgg sunnyqgg changed the title [None][feat] Add vendored PrimsTS MoE backend for Blackwell [None][feat] Add PrimsTS MoE with Rubin locality domains Sep 13, 2026
Comment thread 3rdparty/vendor_sources.lock.yaml Outdated
source: flashinfer
destination: tensorrt_llm/_torch/moe/flashinfer
include:
- prims_ts/batched_gemm/__init__.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use wildcard instead of listing all files.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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]>
@sunnyqgg sunnyqgg changed the title [None][feat] Add PrimsTS MoE with Rubin locality domains [None][feat] Add PrimsTS MoE implementation family Nov 25, 2026

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

api-compatible Accepted LLM API contract change that is backwards-compatible

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants