Repository navigation
[WS1][CUDA][Qwen3-Next] MoE route/combine contract with a batch-invariant grouped expert GEMM (RFC #428 stack 5/11) - #503
Conversation
…rouped expert GEMM RFC RL-Align#428 moe_route_combine_contract (CUDA). - Routing: FP32 router through the pinned vLLM batch-invariant GEMM, a fixed-order softmax, stable descending argsort (ties go to the lower expert id) and FP32 renormalisation of the ten selected weights. - Experts: per-route gate_up and down projections through vLLM 0.30.0's fused_moe Triton kernel with the batch-invariant tile passed explicitly (BLOCK 64/64/32, SPLIT_K=1); no routed-weight multiply and no sum inside the kernel, so every (token, route) row has one writer. Bitwise equal to the previous per-expert GEMM loop at TP4 and TP1 widths; fails closed if VLLM_TRITON_USE_TD is enabled. - Combine: ten ordered FP32 multiply-adds and one BF16 cast; no atomics. - Backward: per-expert pinned GEMMs into one dense gradient buffer; dx is summed in ascending expert order. - TP4 block: HF load/export for all 512 experts, replicated router and shared-expert gate, Megatron-style copy/reduce autograd boundaries moved into qwen3_next_tp.py. - Prior-art runner and plot comparing HF transformers, vLLM fused_moe (BI=0/1) and FlashInfer cutlass_fused_moe; TP4 real-weight gate script. Signed-off-by: Wenbo Ji <[email protected]>
…nce (B200, 2e950f3) Signed-off-by: Wenbo Ji <[email protected]>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
…r-art runner - Megatron-core 0.16 MoELayer with Transformer Engine grouped GEMM and fused permute, configured as VIME runs Qwen3-Next (FP32 softmax router, top-10, all-to-all dispatcher), including its backward. - SGLang 0.5.21 Triton fused_moe in default and deterministic-inference mode. sgl_kernel cannot load next to torch 2.13, so its two kernels on this path are replaced by SGLang's own Triton moe_sum_reduce and JIT moe_align_block_size; any other sgl_kernel call fails loudly. - --only and --merge combine reports from different Python environments. Signed-off-by: Wenbo Ji <[email protected]>
…ured (B200, c58816b) Every library in the reuse table is now measured: Megatron-core 0.16 + TE 2.16 as VIME configures Qwen3-Next, and SGLang 0.5.21 in default and deterministic-inference mode. The TP4 gate evidence is unchanged (2e950f3, identical MoE code). Signed-off-by: Wenbo Ji <[email protected]>
Signed-off-by: Wenbo Ji <[email protected]>
…p as E402-exempt The gate scripts insert the repo root into sys.path before importing rl_engine and tools, which flake8 reports as E402. Mark those imports with noqa, as the other tools/ scripts do. Signed-off-by: Wenbo Ji <[email protected]>
Annotate the latency samples and accept the default-argument lambdas used as timed calls. No behaviour change. Signed-off-by: Wenbo Ji <[email protected]>
The tree is unchanged: RL-Align#469's refactor merge carries nothing this branch lacks; this only makes the current RL-Align#469 tip an ancestor. Signed-off-by: Wenbo Ji <[email protected]>
Per the layout review on RL-Align#549: - the TP4 MoE gate moves to tools/validation/models/qwen3_next/; - the prior-art runner and its plot move to the Qwen3-Next benchmark PR (benchmarks/models/qwen3_next/); - the committed evidence directory becomes a ZIP release attachment with a short summary in reports/experiments/qwen3_next/ (download link, SHA-256, tested commits, commands); the bytes are unchanged. The GPU workflow now also triggers on files under the qwen3_next/ validation directories. Signed-off-by: Wenbo Ji <[email protected]>
The raw results (JSON, figures, audit files) now live on the fork's pr_assets branch; the documents link to them at a fixed commit. Signed-off-by: Wenbo Ji <[email protected]>
f9caee2 to
8943c14
Compare
d5253a5 Signed-off-by: Wenbo Ji <[email protected]>
…note The empty-token branch of _SharedLinear.backward returned a zero bias gradient whenever a bias was present; gate it on needs_input_grad[2] like the non-empty branch. The shared_moe docstring said the forward has no host synchronisation, but stable_top10_routes checks the router logits for finite values on the host; say so. Signed-off-by: Wenbo Ji <[email protected]>
Not changed. This workflow file is shared by the whole Qwen3-Next stack, and the next PR, #507, adds the files this entry covers: |
Not changed. For an empty input the backward returns |
Not changed. |
Not changed. This assertion checks the gradient contract, not compute sparsity: an expert that no token selects must get an exactly zero |
Not changed. The check compares vector-Jacobian products, so both sides must use the same upstream vector. |
There was a problem hiding this comment.
Overall looks good, just some questions before approve:
- Is TP invariance supported? Is Tp4 == Tp1? If this RFC does not require TP invariance, that's ok ignore my questions.
- Non-blocking: to make best performance, consider using MegaMoE to overlap communication and computation? And I believe MegaMoE can also reach BI by some configuration. (Maybe it is not this PR's scope, you can ignore me.)
- This query on RFC (Non-blocking): Is Qwen3-Next really needs TP4? Normally MoE only use EP, TP4 would cause more overhead. I just want to check if TP4 is really worth to do.
…, EP4 router/combine gate) into RL-Align#551 Signed-off-by: Wenbo Ji <[email protected]>
Fixed in 8e33c45: the |
Fixed in 8e33c45: the empty-input branch now returns the bias gradient only when |
Not changed. Two empty tensors of the same shape and dtype are identical, and the shape check already fails when a broken path drops tokens while the reference does not (for example |
Not changed in this PR. |
|
@KJLdefeated, replying to your review. Thanks for the review. I measured both points before answering. Everything below is on the official checkpoint's layer-0 MoE weights, 4 × B200, torch 2.13 + vLLM 0.30.0, 1024 tokens unless stated. 1. TP invariance (TP4 == TP1). Not bitwise as the PR stands, and the PR does not claim it.
Forward at 8 / 256 / 1024 tokens, forward + backward at 256 / 1024. So TP1 == TP4 is reachable by construction, because the collective was built for it: its fixed tree gives the same result at TP 1, 2, 4 and 8 as long as each rank's input is the matching subtree of one canonical reduction. The price in this direct form is 3.2× the forward and 3.8× the forward + backward of plain TP1 (6.5 vs 2.0 ms and 656 vs 171 ms; TP4 is 1.9 / 170 ms per rank), since the four shards run one after another. What the PR does hold at TP4: identical routes on every rank, full == chunked == reordered batches at 8 to 1024 tokens, training forward == no-grad forward, identical replicated router and shared-gate gradients (the TP4 gate, 12 / 12), and forward and backward batch invariance. As I read RFC #428, the parity target is the same topology on both sides, vLLM rollout and VIME replay at TP4 (the 2. DeepGEMM MegaMoE. Thanks for the pointer; it was not in the survey, so I measured it. vLLM 0.30.0 vendors it (
So MegaMoE's forward is deterministic, batch-invariant on this grid, EP-invariant, and about 2× faster than RL-Kernel's at EP4. It does not give the same bits as RL-Kernel's forward (max difference 3.1e-5; 3. Is TP4 worth it for Qwen3-Next? Agreed that EP is the natural layout for the routed experts, and the same run shows it: RL-Kernel's MoE forward + backward is 170 ms per rank at TP4 × EP1 (this PR) and 53 ms (2.25 + 51) at EP4 (#557), and EP4 equals EP1 bit for bit, forward and backward. TP4 × EP1 came first because it is what the pinned vLLM 0.30.0 rollout runs by default: with |
…e33c45 and the run order The five Qwen3-Next stack heads were pushed on 2026-11-17 and are the PR heads. Optional step 4 reruns RL-Align#503's suites at 8e33c45; the EP4 gate does not start vLLM and can run before step 0 is solved. Signed-off-by: Wenbo Ji <[email protected]>
…in the provider job, RL-Align#503 bias grad gate) into RL-Align#551 Signed-off-by: Wenbo Ji <[email protected]>
Signed-off-by: Wenbo Ji <[email protected]>
Signed-off-by: Wenbo Ji <[email protected]>
Signed-off-by: Wenbo Ji <[email protected]>
…rovider job, #503 bias grad gate) into #544 Signed-off-by: Wenbo Ji <[email protected]>
Signed-off-by: Wenbo Ji <[email protected]>
…rovider job, #503 bias grad gate) into #549 Signed-off-by: Wenbo Ji <[email protected]>
Signed-off-by: Wenbo Ji <[email protected]>
…rovider job, #503 bias grad gate) into #557 check_qwen3_next_vllm_model_contract.py stays only in this PR's four-GPU step, which already runs it. Signed-off-by: Wenbo Ji <[email protected]>
…th the combine_routes patch Signed-off-by: Wenbo Ji <[email protected]>
… vs public head d5253a5 Signed-off-by: Wenbo Ji <[email protected]>
d5253a5 Signed-off-by: Wenbo Ji <[email protected]>
What
The routed-MoE contract for Qwen3-Next-80B-A3B (512 experts, top-10), per stage (full table in
docs/operators/qwen3-next-moe-route-combine.md):x,Wupcast; one pinned vLLM batch-invariant GEMM, IEEE FP32argsort(logits, descending, stable)[:, :10]: score descending, ties to the lower expert idgate_upGEMM → SwiGLU in FP32 → BF16 →downGEMM; no routed weight in the kernel, no sum over routese0·w0, then+ e_k·w_kfor k = 1..9 in route order, FP32; one BF16 castfixed_order_row_sum(custom autograd function incombine_routes)rl_engine/models/qwen3_next/qwen3_next_forward.py:shared_linear,shared_router,stable_top10_routes,combine_routes,grouped_route_linear,shared_moe. Fails closed on CPU or ROCm tensors, on any vLLM other than 0.30.0, onVLLM_TRITON_USE_TDand on TF32 Triton dots.rl_engine/models/qwen3_next/qwen3_next_tp_blocks.py:TP4MoEwith the HF load/export of all 512 experts, a replicated router and shared-expert gate, and MCore TP attributes.rl_engine/models/qwen3_next/qwen3_next_tp.py: Megatron-style copy/reduce autograd boundaries through RL-Kernel's fixed-order collective.rl_engine/validation/common/tensor_identity.py: raw-bit identity (signed zero distinct, NaN/Inf rejected, dtype must match).tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py: the TP4 gate on real layer-0 weights (torchrun, 4 GPUs).combine_routeshas a custom autograd function: the stock autograd backward summedgrad * expert_outputover H withtorch.sum, whose reduction order depends on the batch shape, so the route-weight gradient (anddxthrough the router) stopped being batch-invariant from 16 rows up on H100 and B200. The function sums H withfixed_order_row_sum. New test: first 8 rows alone vs the same rows inside larger batches.reports/experiments/qwen3_next/.Prior art & reuse decision
Decision: reuse vLLM's batch-invariant GEMM (router, backward) and its
fused_moeTriton kernel (expert GEMMs); implement the FP32 routing, tie rule, combine, backward and TP4 boundaries. No surveyed implementation is batch-invariant and has a batch-invariant backward. The batch-invariant ones (vLLM BI=1, SGLang deterministic) are inference-only and route in BF16, which picks a different expert set than FP64 routing for 11 of 256 tokens; Megatron-core + TE, VIME's training path, routes in FP32 but changes routing, output, dx and dW with batch size.cutlass_fused_moe(grouped GEMM)fused_moe.cutlass_fused_moefused_topk+fused_experts,VLLM_BATCH_INVARIANT=0VLLM_BATCH_INVARIANT=1fused_moe, default / deterministic inferenceradixark/miles@23d3d38)miles/kernels/moe/fused_experts.py(SGLang forward) +fused_experts_bwd.py, imported unmodifieddxof probe rows 0 / 10 batch cellsdx,dWgate/up anddWdown equal the first in 1 / 20 (BF16tl.atomic_add); route-weight gradient 20 / 20volcengine/verl@e5a627c)verl/); trains through Megatron-core, HF/FSDP or VeOmni, rolls out through vLLM/SGLangMoELayer(no MoE kernel of its own)MoELayer, TE grouped GEMM, FP32 router, all-to-all (VIME's Qwen3-Next config)Qwen3NextExpertseager (verl FSDP path)shared_moe/TP4MoEBatch invariance: 8 probe tokens alone vs first and last in 16-1024-token batches, forward (routes, output) and backward (dx, dW), bitwise. Miles was measured by importing its own kernels at the pinned commit (20 identical launches on the real layer-0 weights, bit for bit against the first; its forward uses SGLang's kernel with SGLang's deterministic tile).
Results (B200, H = 2048, 512 experts, top-10, width 512, BF16)
Summary, tested commits and commands:
reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md; raw reports ZIP (SHA-2569fcc7042…58cb0). Prior art measured atc58816b, TP4 gate at2e950f3(identical MoE code).Status
8e33c453, on top of [WS1][CUDA][Qwen3-Next] C6: GDN decode-step goldens and recurrent/conv state contract (RFC #428 stack 4/4) #469 ande514d42f(the route-weight gradient fix above). The last commit answers OpenCodeReview: the empty-input bias gradient is gated onneeds_input_grad, and theshared_moedocstring now states the host synchronisation of the router-logit finiteness check; no result changes. CI is green on this head: lint, unit-tests, docs, DCO and the bot review pass;providerandws1-gtestskip because no self-hosted runner is attached.90c09164, 4 × B200): the standalone TP4 MoE gate passed 12/12 on all four ranks, and thelayersstages of the EP1 and EP4 chains passed with it. (before the route-weight fix; B200 rerun one514d42fbelow, see History)History
d53ac95etriggers the provider job onrl_engine/validation/common/**. The fixes of the PRs below it are merged in; new head9800e1a4. Kernel arithmetic is unchanged, so the measured results stand.test-qwennextafter [WS1][CUDA][Qwen3-Next] C1: zero-centred RMSNorm references and CUDA kernel (RFC #428 stack 2/4) #467 landed (c382dd4b), through [WS1][CUDA][Qwen3-Next] C6: GDN decode-step goldens and recurrent/conv state contract (RFC #428 stack 4/4) #469's new head27212b65, in68240ec3. One conflict, in the provider workflow's pytest step: [WS1][CUDA][Qwen3-Next] C6: GDN decode-step goldens and recurrent/conv state contract (RFC #428 stack 4/4) #469's fail-closed skip check now also runs this PR's check modules.combine_routesbackward gave a route-weight gradient that was not batch-invariant at 16, 32, 64, 128, 256 and 1024 rows (equal at 8 and 9).e514d42ffixes it with a fixed-order row sum. H100 A/B on 4 × H100: previous headd5253a5c322 passed, probe unequal at T ≥ 16; treece3f499d(=e514d42f) 323 passed (the extra one is the new regression test), probe equal at every T, TP4 MoE gate 4/4 ranks on both. Output and expert gradients were equal on both. Same on B200 (4 × B200, same A/B and probe):d5253a5c322 passed, probe unequal at T ≥ 16;e514d42f323 passed, new test passed, probe equal at every T; TP4 MoE gate 12/12 cases on 4/4 ranks on both. So the break is not Hopper-specific, and the fix holds on both architectures. Raw data:hopper-qwen3-next/pr503/b200-e514d42f(TP4 gate on the index plus the single layer-0 shard of revision9c7f2fbe, which is all the gate reads).e514d42f: seven findings, each answered in its own comment. Two fixed in8e33c453(empty-input bias gradient nowNonewhen the bias does not require grad; docstring states the finiteness-check sync), five answered without a change. On 4 × B200 at8e33c453: the qwen3_next suites 323 passed, 0 failed, 0 skipped, and an empty input leavesbias.gradasNonefor a bias withoutrequires_grad. Raw data:hopper-qwen3-next/pr503/b200-8e33c453.Tests
mkdocs build --strictcheck_qwen3_next_forward.py(GEMM batch/chunk/reorder, route ties, combine order, MoE VJP vs FP64, route and output BI, grouped == per-expert bitwise)check_qwen3_next_forward.py -k combine_gradients(new: route-weight gradient, 8 rows alone vs inside larger batches), H100 and B200tests/models/qwen3_next/test_*.py+check_*.py, 4 × H100, treece3f499d(=e514d42f)tests/models/qwen3_next/test_*.py+check_*.py, 4 × B200,e514d42fe514d42ftests/models/qwen3_next/test_*.py+check_*.py, 4 × B200,8e33c453shared_linear, bias with and withoutrequires_grad, B200,8e33c453bias.grad/bias.grad is NoneCI: the CPU tests are in
ci.yml;check_qwen3_next_forward.pyimports vLLM and runs in the self-hostedQwen3-Next-provider-GPUworkflow withTRITON_F32_DEFAULT=ieee, which now also triggers onrl_engine/validation/models/qwen3_next/**andtools/validation/models/qwen3_next/**.Scope
CUDA, TP4 × EP1 (all 512 experts on every rank). EP4 is in #557; ROCm and other TP sizes are separate claims. Model-level parity uses this block but belongs to
full_model_chain(#543).Results (H100 / Hopper, H = 2048, 512 experts, top-10, width 512, BF16)
Run at tree
ce3f499d(= heade514d42f). 4 × NVIDIA H100, driver 580.173.02, CUDA 13.0, torch 2.13.0+cu130, triton 3.7.1, vLLM 0.30.0, transformers 5.17.0,VLLM_BATCH_INVARIANT=0. Fresh worktrees, fresh builds, clean tree before and after. Baseline run at the previous headd5253a5c. Raw data and report:hopper-qwen3-next/pr503/b960f8ce.d5253a5c(before)e514d42ftreece3f499dtests/models/qwen3_next/test_*.py+check_*.pydWbatch invariantdxbatch invariantThe prior-art figure and table above were measured before
e514d42f(the RL-Kernel row recordsf9caee24) and were not rerun ate514d42f. FlashInfer sm90a JIT neededlibnvrtc.soonLD_LIBRARY_PATH(symlink tolibnvrtc.so.13). Non-RL-Kernel rows use the #551 runner at77d59a59. Diagnostics for the originaltorch.sumbehaviour:hopper-qwen3-next/pr503/diagnostics.