← Back to Pull requests

perf(moe): use compiled router-weight cores below the chunk threshold #4529

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 27, 2026 09:51 UTC
Author

What does this PR do ?

GLM-5.3-Flash applies the router weight after the down projection, as an fp32 multiply in _apply_router_weight_fp32. compile_router_weight compiles that multiply, but only dispatches with more than 12288 rows reach the compiled _RouterWeightMulFunction; smaller ones still run the eager cast / multiply / cast. The GLM-5.3-Flash MedPix recipe averages 8192 routed rows per rank (2048-token packs over CP2, top-8), so with balanced routing the switch has no effect there; only ranks that receive more than 1.5x their share cross the threshold. This PR lets dispatches of at least 8M elements (rows x hidden) use the compiled cores below that threshold, and turns the switch on in that recipe. Smaller dispatches keep the eager multiply, which has less launch overhead.

Changelog

  • _apply_router_weight_fp32: once compile_router_weight has compiled the cores, a [rows, hidden] x [rows, 1] dispatch uses _RouterWeightMulFunction when it has more than 12288 rows (as before) or at least _RW_COMPILED_MIN_NUMEL = 2^23 elements. With the switch off every size takes the same path as before.

  • examples/vlm_finetune/glm5_next/glm5_3_flash_medpix_packed2k_ep72_cp2_100steps.yaml: compile_router_weight: true.

  • BackendConfig.compile_router_weight docstring: which dispatch sizes the switch covers.

  • New test test_compiled_cores_take_inputs_from_min_numel: an input at the floor takes the compiled cores (fails on main), one element below it stays eager.
  • Code provenance

    Not applicable.

    Before your PR is "Ready for review"

    Pre checks:

  • Make sure you read and followed Contributor guidelines

  • Did you write any new necessary tests?

  • Did you add or update any necessary documentation? (docstring only)
  • Additional Information

    Router-weight multiply and torch_mm expert compute, fwd + bwd, 1x H100 80GB HBM3, nvcr.io/nvidia/nemo-automodel:26.08.00 + main's Python deps, main at f284ecaea. GLM-5.3-Flash expert shapes (hidden 4096, moe inter 2048, swiglu limit 10, after-down), 4 local experts (EP72), identity dispatch. The three modes run in alternating processes (the switch is process-wide), two processes each, each 5 rounds x 20 iterations after warm-up, median. "main + switch" is main's size gate with the compiled cores. The bound is the minimum HBM traffic (forward reads x and writes the output, backward reads the gradient and x and writes grad x, 335.5 MB) over 3.35 TB/s.

    | 8192 rows per rank (MedPix recipe, balanced) | eager | main + switch | this PR + switch | bound |
    |---|---:|---:|---:|---:|
    | router-weight multiply | 0.838 ms | 0.838 ms | 0.283 ms | 0.100 ms |
    | % of bound | 12% | 12% | 35% | |
    | torch_mm expert compute | 2.88 ms | 2.88 ms | 2.37 ms (-18%) | |
    | multiply transient memory | 576 MiB | 576 MiB | 128 MiB | |

    At 16384 and 32768 rows (2048 / 4096 tokens per rank) this PR and main + switch give the same times, 0.55 and 1.08 ms. Output and grad x are bitwise equal to the fp32 reference; the router-weight gradient differs by 1.1e-7 relative (fp32 sum order), same as the large-dispatch compiled path.

    Where the 8M-element floor comes from, 1x H100, warmed fwd + bwd of the helper alone, eager vs the compiled Function in the same process, 20 warm-up calls then 5 x 100 back-to-back calls. At small sizes these calls are launch bound: about 190 us each on the compiled path against 80-110 us eager, so the compiled path loses until the eager kernels take longer than that. On GPU time alone (a long sleep kernel queued first) it wins at every size, 0.40-0.68x.

    | width, output dtype | compiled / eager time at rows x hidden elements |
    |---|---|
    | 2048, bf16 | 1.60 at 4.2M, 1.15 at 6.3M, 0.83 at 8.4M |
    | 4096, bf16 | 1.50 at 4.2M, 1.06 at 6.3M, 0.81 at 8.4M |
    | 4096, fp32 | 1.82 at 4.2M, 1.32 at 6.3M, 1.02 at 8.4M, 0.70 at 12.6M |
    | 6144, bf16 | 1.81 at 3.1M, 1.04 at 6.3M, 0.70 at 9.4M |
    | 7168, bf16 | 1.55 at 3.7M, 0.87 at 7.3M, 0.62 at 11.0M |

    The reversed arm order gives ratios within 0.04 of these. At width 256 every dispatch under the chunk threshold is below 3.2M elements and stays eager.

    Native callers with the gate switched in one process (main's rule, every-size rule, this PR's floor), gates interleaved per round, both orders, 5 x 100 calls:

    | warmed expert fwd + bwd, 1x H100 | main + switch | baaea9a9f + switch (every size) | this PR + switch |
    |---|---:|---:|---:|
    | TE fixture from test_te_routing_gradients (112 x 256, BF16, trainable probs) | 1.83 / 1.73 ms | 1.98 / 1.87 ms | 1.82 / 1.70 ms |
    | torch_mm, GLM shapes, 512 x 4096 | 0.657 / 0.667 ms | 0.725 / 0.717 ms | 0.658 / 0.667 ms |
    | torch_mm, 2048 x 4096 | 1.136 / 1.137 ms | 0.974 / 0.973 ms | 0.973 / 0.973 ms |
    | torch_mm, 8192 x 4096 | 2.94 / 2.90 ms | 2.50 / 2.50 ms | 2.50 / 2.49 ms |

    At 1024 x 4096 the torch_mm caller runs about 9% faster with the compiled cores, but the helper alone is 1.5x slower there, so that size stays eager.

    I don't have a clean end-to-end number. In 2x H100 few-layer proxies (HybridEP EP=2, GLM-5.3-Flash KDA + MoE layers, 16 experts, 1024 tokens per rank, activation checkpointing, random init) the difference stays inside run-to-run noise (6 layers: 343.9 vs 344.3 ms/step averaged over 3 runs each, single runs spread up to 5%). Loss and peak memory are the same in both arms. With balanced routing the table works out to about 23 ms of fwd + bwd per micro-batch per rank over the 42 MoE layers, plus the multiply's share of the recompute forward.

    Related to #2952 (keep performance overrides close to the canonical model recipe).

    Tests on 25182f8f6: tests/unit_tests/moe/test_router_weight_mul.py (24 passed on 1x H100, including the compiled-execution test), tests/functional_tests/moe/test_te_routing_gradients.py (17 passed) and tests/unit_tests/moe/test_experts.py -k "router_weight or after_down or te" (201 passed) on 1x H100; tests/unit_tests/moe/test_backend_config.py (89 passed) and tests/unit_tests/_peft -k expert (79 passed, 54 skipped) on CPU. ruff and tools/lint_no_globals.py clean.

    Not tested: the recipe itself (9 nodes, real weights, CP2), the cuDNN / FlashMLA sparse attention path, Blackwell.

    Implementation and validation were AI-assisted.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: ischencheng Review duration: 30576m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #4529"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%