Skip to content

[Triton/Gluon] fix: remove exact-M RMSNorm forward specialization - #7311

Merged
azaidy merged 1 commit into
ROCm:mainfrom
ZhuoBohang-AMD:k3-dspark-from-scratch-mxfp4
Nov 3, 2026
Merged

azaidy merged 1 commit into
ROCm:mainfrom
ZhuoBohang-AMD:k3-dspark-from-scratch-mxfp4

Conversation

@ZhuoBohang-AMD

Copy link
Copy Markdown
Contributor

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 the NUM_PRGMS constexpr. They read it at runtime with tl.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 is min(M, 4 * CU) on gfx950 and min(M, CU) on other architectures, and they also passed that value to the kernel as NUM_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_kernel variants, and the compile stalls made up a large share of the step time.

Changes

  • aiter/ops/triton/_triton_kernels/normalization/rmsnorm.py: remove NUM_PRGMS from the four forward kernels and from their make_kernel_repr key lists. The persistent row loops now stride by tl.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_kernel call inside fused_pipeline_kernel no longer passes NUM_PRGMS=NUM_WORKERS. That kernel is launched with grid = (NUM_SMS,) and NUM_WORKERS=NUM_SMS, so tl.num_programs(0) inside the nested call equals the value previously passed.
  • The RMSNorm backward kernels are not changed; they still take their own NUM_PRGMS.
  • No public API change.

Performance (if applicable)

All on MI355X (gfx950, 256 CUs), PyTorch 2.13.0 + ROCm 7.14.

Hardware Workload/configuration Before After Change
1x MI355X rms_norm, BF16, N=7168, cold TRITON_CACHE_DIR, warmed at M=400, then first call at 7 unseen M (401, 403, 407, 511, 777, 1001, 1215) 166-170 ms per new M, 1179 ms total 182 ms for M=401, 0.07-0.14 ms for the other six, 183 ms total -84%
1x MI355X Same run, Triton cache files written 73 17 -77%
1x MI355X Steady-state rms_norm, BF16, N=7168, median of 10 trials x 200 iterations, M = 403 / 777 / 1215 / 2048 20.31 / 20.43 / 20.45 / 20.32 us 20.37 / 20.43 / 20.36 / 20.19 us +0.29% / -0.03% / -0.43% / -0.59% (noise)
3 nodes x 8 MI355X Kimi-K3 DSpark draft training, BF16 draft, median step time over steps 11-30 of a 30-step run 4.199 s 3.337 s -20.5%
3 nodes x 8 MI355X Same, MXFP4 draft 4.012 s 3.354 s -16.4%
3 nodes x 8 MI355X Same runs, _rms_norm_kernel compile variants on the draft rank 644 4 -99%

The 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

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

Tested on MI355X (gfx950) only; see "Validation and unverified scope".

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

Only a private Triton kernel meta-parameter changes. The public Python APIs, their arguments, and their outputs are unchanged.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None for public APIs. The private @triton.jit forward kernels listed above no longer accept NUM_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

a4819123a5a68433d44cbaf6b96e80292b255be7

Behavior 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):

  • RMSNorm forward, fused-add, smooth-quant and dynamic-quant cases in op_tests/triton_tests/normalization/test_rmsnorm.py: 452 passed.
  • A standalone microbenchmark (not part of this PR) calls aiter.ops.triton.normalization.rmsnorm.rms_norm with a cold TRITON_CACHE_DIR. It checks every output against a PyTorch reference with torch.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.
  • Two 30-step Kimi-K3 DSpark draft training runs (BF16 and MXFP4 draft, 3 x 8 MI355X) both reached step 30 with no NaN, OOM or GPU fault. Forward compile variants on the draft rank dropped from 644 to 4.
  • Static check: after this change, git grep -n 'NUM_PRGMS\s*=' only finds the quant kernels in aiter/ops/triton/quant/ and the RMSNorm backward path, which have their own NUM_PRGMS parameter. Nothing passes it to the four forward kernels.

Unverified:

  • MI300X and MI250X were not tested. On those architectures the grid is min(M, CU) instead of min(M, 4 * CU), and the same reasoning applies.
  • The fused reduce-scatter + RMSNorm + quant + all-gather path (fused_pipeline_kernel, requires iris and multiple GPUs) was not run. The one-line change there is verified only by reading the launch code, and op_tests/multigpu_tests/triton_test/test_fused_rs_rmsnorm_quant_ag.py was not run.
  • The tests and measurements above were run before that one-line fused-comm change was added. That change is not on any of the measured paths.
  • Outputs were not compared bitwise against the old kernels; only the reference-tolerance checks above were run.
  • The RMSNorm backward kernels still specialize on exact M; that is out of scope for this PR.

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 as NUM_PRGMS, so each program processes the same rows in the same order. The nested call in fused_pipeline_kernel inherits the outer launch: grid = (NUM_SMS,) with NUM_WORKERS=NUM_SMS, so tl.num_programs(0) == NUM_WORKERS and its behavior is unchanged.

A second failure mode is a caller that still passes NUM_PRGMS, which would fail when Triton compiles it. The git grep above 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.

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]>
@ZhuoBohang-AMD
ZhuoBohang-AMD requested a review from a team November 2, 2026 06:35
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the gfx1201 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 7311 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@aiter-bot

aiter-bot commented Nov 2, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision a4819123a5a6 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior 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):

- RMSNorm forward, fused-add, smooth-quant and dynamic-quant cases in `op_tests/triton_tests/normalization/test_rmsnorm.py`: 452 passed.
- A standalone microbenchmark (not part of this PR) calls `aiter.ops.triton.normalization.rmsnorm.rms_norm` with a cold `TRITON_CACHE_DIR`. It checks every output against a PyTorch reference with `torch.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.
- Two 30-step Kimi-K3 DSpark draft training runs (BF16 and MXFP4 draft, 3 x 8 MI355X) both reached step 30 with no NaN, OOM or GPU fault. Forward compile variants on the draft rank dropped from 644 to 4.
- Static check: after this change, `git grep -n 'NUM_PRGMS\s*='` only finds the quant kernels in `aiter/ops/triton/quant/` and the RMSNorm backward path, which have their own `NUM_PRGMS` parameter. Nothing passes it to the four forward kernels.

Unverified:

- MI300X and MI250X were not tested. On those architectures the grid is `min(M, CU)` instead of `min(M, 4 * CU)`, and the same reasoning applies.
- The fused reduce-scatter + RMSNorm + quant + all-gather path (`fused_pipeline_kernel`, requires iris and multiple GPUs) was not run. The one-line change there is verified only by reading the launch code, and `op_tests/multigpu_tests/triton_test/test_fused_rs_rmsnorm_quant_ag.py` was not run.
- The tests and measurements above were run before that one-line fused-comm change was added. That change is not on any of the measured paths.
- Outputs were not compared bitwise against the old kernels; only the reference-tolerance checks above were run.
- The RMSNorm backward kernels still specialize on exact M; that is out of scope for this PR.

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 as `NUM_PRGMS`, so each program processes the same rows in the same order. The nested call in `fused_pipeline_kernel` inherits the outer launch: `grid = (NUM_SMS,)` with `NUM_WORKERS=NUM_SMS`, so `tl.num_programs(0) == NUM_WORKERS` and its behavior is unchanged.

A second failure mode is a caller that still passes `NUM_PRGMS`, which would fail when Triton compiles it. The `git grep` above 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.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 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_PRGMS from 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

@azaidy azaidy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@azaidy
azaidy merged commit 56d3d28 into ROCm:main Nov 3, 2026
shantipriya-amd pushed a commit to shantipriya-amd/aiter that referenced this pull request Nov 18, 2026
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%).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants