Repository navigation
[Triton/Gluon] fix: remove exact-M RMSNorm forward specialization - #7311
Conversation
The RMSNorm forward kernels took the program count min(M, 4 * CU) as the constexpr NUM_PRGMS, so every new token count M triggered a ~170 ms Triton recompile. The kernels now read it at runtime via tl.num_programs(0); the grid and math are unchanged. In 30-step Kimi-K3 DSpark MXFP4 training, RMSNorm compile variants drop from 644 to 4 and median step time from 4.01 s to 3.35 s (-16%). Co-authored-by: Cursor <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
There was a problem hiding this comment.
🔵 Needs a closer look
The updated nested RMSNorm call in the fused Iris multi-GPU pipeline lacks runtime GPU validation, and the reported tests predate the call-site change.
No open issues 🎉
What changed in this PR
This PR reduces recompilation for dynamic token counts in Triton RMSNorm forward paths. It replaces the compile-time program count with tl.num_programs(0) while preserving launch grids, per-row math, and public APIs.
Changes:
- Remove
NUM_PRGMSfrom four forward kernels and their kernel-name keys. - Update six forward wrappers and the nested fused-communication call to omit the removed argument.
- Leave backward kernels unchanged.
| File | Description |
|---|---|
aiter/ops/triton/normalization/rmsnorm.py |
Update forward launches while retaining the existing grids. |
aiter/ops/triton/comms/fused/reduce_scatter_rmsnorm_quant_all_gather.py |
Remove the obsolete argument from the nested RMSNorm call. |
aiter/ops/triton/_triton_kernels/normalization/rmsnorm.py |
Use the runtime program count as the row-loop stride in four forward kernels. |
Review effort: Balanced
The RMSNorm forward kernels took the program count min(M, 4 * CU) as the constexpr NUM_PRGMS, so every new token count M triggered a ~170 ms Triton recompile. The kernels now read it at runtime via tl.num_programs(0); the grid and math are unchanged. In 30-step Kimi-K3 DSpark MXFP4 training, RMSNorm compile variants drop from 644 to 4 and median step time from 4.01 s to 3.35 s (-16%).
Summary
The four Triton RMSNorm forward kernels (
_rms_norm_kernel,_fused_add_rmsnorm_kernel,_quant_rms_norm_kernel,_quant_fused_add_rmsnorm_kernel) no longer take the launched program count as theNUM_PRGMSconstexpr. They read it at runtime withtl.num_programs(0)and use it as the persistent row-loop stride. The launch grid and the per-row math are unchanged.Motivation
The forward wrappers launch
num_programs_fwd(x)programs, which ismin(M, 4 * CU)on gfx950 andmin(M, CU)on other architectures, and they also passed that value to the kernel asNUM_PRGMS: tl.constexpr. Whenever M is below the cap, the program count equals the token count, so every previously unseen M becomes a new Triton specialization and pays a full compile (about 170 ms on MI355X).Workloads with dynamic token counts, such as variable-length or packed training and speculative-decoding draft training, hit this constantly. In a 30-step Kimi-K3 DSpark draft training run, the draft rank compiled 644
_rms_norm_kernelvariants, and the compile stalls made up a large share of the step time.Changes
aiter/ops/triton/_triton_kernels/normalization/rmsnorm.py: removeNUM_PRGMSfrom the four forward kernels and from theirmake_kernel_reprkey lists. The persistent row loops now stride bytl.num_programs(0).aiter/ops/triton/normalization/rmsnorm.py: the forward wrappers (_rmsnorm_forward,_rmsnorm_forward_with_add,rmsnorm2d_fwd_with_smoothquant,rmsnorm2d_fwd_with_dynamicquant,rmsnorm2d_fwd_with_add_smoothquant,rmsnorm2d_fwd_with_add_dynamicquant) keep the same grid but no longer pass the program count to the kernel.aiter/ops/triton/comms/fused/reduce_scatter_rmsnorm_quant_all_gather.py: the nested_rms_norm_kernelcall insidefused_pipeline_kernelno longer passesNUM_PRGMS=NUM_WORKERS. That kernel is launched withgrid = (NUM_SMS,)andNUM_WORKERS=NUM_SMS, sotl.num_programs(0)inside the nested call equals the value previously passed.NUM_PRGMS.Performance (if applicable)
All on MI355X (gfx950, 256 CUs), PyTorch 2.13.0 + ROCm 7.14.
rms_norm, BF16, N=7168, coldTRITON_CACHE_DIR, warmed at M=400, then first call at 7 unseen M (401, 403, 407, 511, 777, 1001, 1215)rms_norm, BF16, N=7168, median of 10 trials x 200 iterations, M = 403 / 777 / 1215 / 2048_rms_norm_kernelcompile variants on the draft rankThe remaining compile at M=401 is Triton's normal divisibility-by-16 specialization of
n_rows(M=400 is divisible by 16, M=401 is not). After this change, that integer-argument specialization is the only M-dependent part of the compile key. The end-to-end "Before" numbers come from earlier baseline runs of the same training setup; they were not interleaved with the "After" runs in one allocation.Testing
Tested on MI355X (gfx950) only; see "Validation and unverified scope".
Documentation
Only a private Triton kernel meta-parameter changes. The public Python APIs, their arguments, and their outputs are unchanged.
Dependencies
Breaking Changes
None for public APIs. The private
@triton.jitforward kernels listed above no longer acceptNUM_PRGMS. Out-of-tree code that launches them directly must drop that argument, and its launch grid becomes the row-loop stride. All in-tree callers are updated.PR Self-Review
Reviewed revision
a4819123a5a68433d44cbaf6b96e80292b255be7Behavior and intent
Only the compile key changes. The RMSNorm forward kernels no longer specialize on the M-dependent program count, so a new token count reuses the existing binary instead of triggering a roughly 170 ms recompile on MI355X. Grid size, row-to-program assignment, row iteration order, and per-row arithmetic are identical, so outputs are expected to match the old kernels exactly. The existing behavior is insufficient for dynamic-shape workloads: a 30-step draft training run compiled 644 forward variants, and removing them cut median step time by 16-20%.
Validation and unverified scope
Run on MI355X (gfx950):
op_tests/triton_tests/normalization/test_rmsnorm.py: 452 passed.aiter.ops.triton.normalization.rmsnorm.rms_normwith a coldTRITON_CACHE_DIR. It checks every output against a PyTorch reference withtorch.testing.assert_close(atol=1e-2, rtol=1e-2); the max abs error is 0.03125 both before and after. Timings are in the table above.git grep -n 'NUM_PRGMS\s*='only finds the quant kernels inaiter/ops/triton/quant/and the RMSNorm backward path, which have their ownNUM_PRGMSparameter. Nothing passes it to the four forward kernels.Unverified:
min(M, CU)instead ofmin(M, 4 * CU), and the same reasoning applies.fused_pipeline_kernel, requires iris and multiple GPUs) was not run. The one-line change there is verified only by reading the launch code, andop_tests/multigpu_tests/triton_test/test_fused_rs_rmsnorm_quant_ag.pywas not run.PR-specific question
The most important failure mode is a kernel launch whose grid differs from the row-loop stride the caller expects. The persistent row loop now strides by
tl.num_programs(0), so rows are covered exactly once only if every launched program enters the loop and the stride is the launched program count. For the six host wrappers, the grid is(num_programs_fwd(x),), exactly the value previously passed asNUM_PRGMS, so each program processes the same rows in the same order. The nested call infused_pipeline_kernelinherits the outer launch:grid = (NUM_SMS,)withNUM_WORKERS=NUM_SMS, sotl.num_programs(0) == NUM_WORKERSand its behavior is unchanged.A second failure mode is a caller that still passes
NUM_PRGMS, which would fail when Triton compiles it. Thegit grepabove shows no such caller for these kernels. Evidence for the wrapper path is the 452 passing tests, the unchanged reference error, and the two training runs. The nested fused-comm path is covered only by the launch-code reasoning above and was not run.Changed op tests
None.