Skip to content

[None][perf] Fold the Mistral Large 4 SM107 decode MoE tail into its all-reduces and runner - #22428

Closed
yt0428 wants to merge 2 commits into
NVIDIA:feat/mistral-large-4from
yt0428:codex/ml4-vr200-pr-moe-tail
Closed

yt0428 wants to merge 2 commits into
NVIDIA:feat/mistral-large-4from
yt0428:codex/ml4-vr200-pr-moe-tail

Conversation

@yt0428

@yt0428 yt0428 commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

@coderabbitai summary

Description

In the Mistral Large 4 SM107 (VR200) TP4/TP8 decode target, each MoE layer's main stream ran five steps in series, while the shared expert ran beside it on a side stream (#22253):

  1. the router GEMM;
  2. renorm_moe_routing_op;
  3. fp4_quantize;
  4. the pre-routed routed-expert runner;
  5. a torch.add of the routed and shared partials, ahead of the closing all-reduce.

This PR removes three of those small kernels from the main stream by folding them into the kernels around them.

What changes

  • NVFP4 quantization folds into the post-attention all-reduce.
    • On MoE layers, the all-reduce + residual add + RMSNorm also writes the routed experts' NVFP4 codes and scales.
    • It uses the Lamport workspace's out-quant variant (fused_quant), which was compiled but never launched before.
    • Its scale arithmetic now follows fp4_quantize: SF = global_scale * (block_max * rcp_approx(6)). The codes and scales are therefore bit-identical to fp4_quantize of the same bf16 norm rows.
    • The router and the shared expert still read the bf16 norm output.
  • Routing folds into the runner.
    • The runner routes the fp32 router logits itself (Renormalize), instead of renorm_moe_routing_op followed by a pre-routed call.
    • The router GEMM and its fp32 output are unchanged.
  • The routed + shared add folds into the closing all-reduce.
    • The split-row one-shot kernel gets an ADD_SECOND_INPUT constexpr, compiled as fused_add2 by build_workspace(..., add_second_input=True).
    • Each rank adds its two bf16 partials in fp32 and rounds once to bf16 before publishing, which gives the same bits as torch.add.
  • New target op module. ops/comm/moe_tail_allreduce.py wraps the two folded collectives.
  • Scope.
    • The folded collectives are one-shot only. A step above ONE_SHOT_ROWS (= TWOSHOT_ABOVE, 3072 rows) keeps the separate fp4_quantize and torch.add.
    • Unchanged: the dense layer, prefill and mixed steps (PrefillTarget), and the other ML4 targets (TEP, DEP, SM103).
    • There are no environment switches; the modeling_v2 router decides which target runs.

Numerics (VR200, the E3 runtime image with this source overlaid)

  • Op level, 4 ranks, bitwise: all 2332 cases are identical:
    • fused quant, 448 cases: norm, updated residual, codes and scales;
    • fused add, 448 cases;
    • CUDA-graph replay of both, 96 cases;
    • runner routing, 1340 cases: finalized rows and the per-slot expert rows, over spread, near-tie, ±100, integer-tie and all-zero logits, at 1–64, 128, 512 and 1024 rows.
  • TP8 (8 ranks over two nodes): the comm checks are identical too.
  • End to end (greedy, natural acceptance):

VR200 results

Step A/B, TP4, one node, configurations interleaved, one shared autotuner cache. The metric is the median device step time (prevDeviceStepTimeMS) while exactly C generation requests run (DSpark K3, about 5.6K context, natural acceptance). Deltas are in ms.

C1 C2 C4 C8
baseline (ms) 8.848 9.928 11.901 14.802
baseline A–A −0.014 −0.009 −0.025 +0.002
this PR −0.355 (−4.0%) −0.225 (−2.3%) −0.072 (−0.6%) −0.306 (−2.1%)
  • Microbenchmark (CUDA-graph replay, 4–64 rows):
    • Fused quant saves 2.7–2.9 µs per call vs all-reduce + fp4_quantize.
    • Fused add saves 1.7–1.9 µs vs torch.add + all-reduce.
  • The savings are not additive. At C4 (16 rows) the main stream and the shared-expert side stream are nearly balanced. Each fold alone was slower there; all three together are not.

End-to-end six points (DSpark K3, synthetic acceptance length 2.772, 20 min per point, stream_interval=1, one run per point), against this PR's base. The baseline ran at ccc93f2, which has the same tree as 416b15a. Step time is 2.772 / (1/ITL).

Point 1/ITL p50 (tok/s/user) Step (ms) Total tok/s/GPU OK / errors (this PR)
TP4 c=1 289.9 → 294.3 (+1.5%) 9.56 → 9.42 +0.7% 146/1
TP4 c=4 271.4 → 283.1 (+4.3%) 10.21 → 9.79 +0.8% 246/0
TP4 c=8 216.5 → 218.2 (+0.8%) 12.81 → 12.70 +0.5% 459/0
TP8 c=1 312.9 → 322.4 (+3.0%) 8.86 → 8.60 +1.5% 146/1
TP8 c=4 297.0 → 305.6 (+2.9%) 9.33 → 9.07 +0.6% 246/0
TP8 c=8 237.9 → 246.3 (+3.5%) 11.65 → 11.26 +1.9% 476/0
  • Hardware differs. This PR ran on TS3 nodes and the baseline on TS4B nodes, and each point ran once. Differences of 1–2% may be rack offset or noise.
  • TP8 is the most consistent: active-decode p50 is also +3.4–4.1%.
  • TP4 c=1 and c=8 are within that noise.
  • TP4 c=4: 1/ITL rose, but active-decode p50 was flat.
  • Errors: the single c=1 error also occurs on the baseline.

Not measured

  • Runner routing above 32 rows (C8). Autotuner records show the runner's own routing about 6 µs slower than the pre-routed runner at 16 and 32 rows. That makes it the fold most likely to stop paying off at larger decode batches. Limiting it to ≤ 8 rows measured slower at C4/C8 than this ungated version.
  • A post-change nsys trace.

Related PRs

Test Coverage

  • tests/unittest/_torch/modeling_v2/models/mistral_large4/mistral_large_4_nvfp4__sm_107__tp4/comm/test_modeling_v2_moe_tail_allreduce.py (4 GPUs):
    • compares allreduce_add_rmsnorm_nvfp4 with the all-reduce entry + fp4_quantize, and allreduce_add2_rmsnorm with torch.add + the entry, bit for bit;
    • runs eager from 1 row to ONE_SHOT_ROWS, and in a replayed CUDA graph;
    • drives the quantization at global scales that put a block's scale on an e4m3 rounding midpoint, and a few fp32 ulp to either side. There the order of the fp32 scale arithmetic decides the rounding, which random scales almost never reach;
    • checks that both refuse a call above ONE_SHOT_ROWS.
  • .../mistral_large_4_nvfp4__sm_107__tp4/moe/test_modeling_v2_decode_runner_routing.py (1 GPU):
    • compares the runner's own routing with renorm_moe_routing_op + the pre-routed runner: the finalized rows and each slot's expert row;
    • covers the TP4 and TP8 expert widths, including near-ties and exact ties.
  • Both skip unless the devices are SM107. Like the other sm_107 tests, no pre-merge stage runs them.
  • GPU run on VR200 (vr-nvl72-ts3-l11-062-c05, E3 runtime image with this branch's source):
    • 101 passed (the 4-rank test and 100 routing cases).
    • The 4-rank job reports 0 mismatch(es) over 4 ranks.
  • Negative control: with the pre-PR scale order (global_scale * block_max * (1 / 6)), the same job fails on the midpoint cases. For example, 78–160 of 12288 scales differ, and codes differ too.
  • Out of scope: the NVFP4 branch of the two-shot epilogue (_twoshot_row_epilogue under EMIT_NVFP4) still uses the old order. The two-shot all-reduce does run above TWOSHOT_ABOVE rows, but with that branch compiled out (EMIT_NVFP4=False), and no entry builds it with NVFP4 output. This PR leaves it unchanged.

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.

🤖 Generated with Claude Code

…all-reduces and runner

In the SM107 TP4/TP8 decode target, each MoE layer's main stream ran the router
GEMM, renorm_moe_routing_op, fp4_quantize, the pre-routed routed-expert runner
and a torch.add of the routed and shared partials before the closing all-reduce.
Three of those ops now fold into the kernels around them:

- The post-attention all-reduce + RMSNorm also writes the routed experts' NVFP4
  codes and scales (the workspace's out-quant variant). Its scale arithmetic now
  follows fp4_quantize's, so the codes and scales are the same bits.
- The runner routes the fp32 router logits itself (Renormalize), replacing
  renorm_moe_routing_op ahead of the pre-routed runner. The router GEMM and its
  fp32 output are unchanged; the selected experts and their order are the same.
- The closing all-reduce adds the routed and shared partials: a second input
  of the split-row one-shot kernel, added in fp32 and rounded once to bf16,
  the same bits as torch.add.

The two folded collectives are one-shot only, so a step of more rows than
ONE_SHOT_ROWS keeps the separate fp4_quantize and torch.add; the dense layer
is unchanged.

Signed-off-by: yt0428 <[email protected]>
…ainst the separate ops

- comm/test_modeling_v2_moe_tail_allreduce.py: four ranks compare
  allreduce_add_rmsnorm_nvfp4 with the all-reduce entry plus fp4_quantize, and
  allreduce_add2_rmsnorm with torch.add plus the entry, bit for bit, eager from
  one row to ONE_SHOT_ROWS and in a replayed CUDA graph; both refuse a call
  above ONE_SHOT_ROWS. The quantization is also driven at global scales that
  put a block's scale on an e4m3 rounding midpoint and a few fp32 ulp to either
  side, where the order of the scale arithmetic decides the rounding.
- moe/test_modeling_v2_decode_runner_routing.py: on one GPU, the runner routing
  the fp32 logits itself selects the same experts in the same slot order, and
  finalizes the same rows, as renorm_moe_routing_op ahead of the pre-routed
  runner, at the tp4 and tp8 expert widths, including near-ties and exact ties.

Both need SM107 devices and are skipped elsewhere.

Signed-off-by: yt0428 <[email protected]>
@yt0428
yt0428 force-pushed the codex/ml4-vr200-pr-moe-tail branch from 20a9866 to b592ea2 Compare November 17, 2026 03:56
@yt0428
yt0428 marked this pull request as ready for review November 17, 2026 06:30
@yt0428

yt0428 commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

/bot skip --comment "Format check only"

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #85381 [ skip ] triggered by Bot. Commit: b592ea2 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #85381 [ skip ] completed with state SUCCESS. Commit: b592ea2
Skipping testing for commit b592ea2

Link to invocation

@yt0428 yt0428 closed this Nov 18, 2026
@yt0428
yt0428 deleted the codex/ml4-vr200-pr-moe-tail branch November 18, 2026 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants