Skip to content

RMSNorm kernels: do not specialize on the row count and row stride - #14786

Merged
danielhanchen merged 2 commits into
mainfrom
ta-u-dnspec
Oct 22, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
ta-u-dnspec

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

What was wrong

The RMSNorm Triton kernels take the row count as a plain runtime integer. Triton specializes integer arguments on == 1 and on divisibility by 16, so every new token count class can compile another copy of the same kernel. In a training run with variable-length batches and then decode (one row), the backward kernel ends up compiled several times for identical code. The affected arguments are row counts or derived counts that only drive loops and masks:

  • _rms_layernorm_backward: n_rows, rows_per_program
  • _rms_layernorm_forward_rows: n_rows
  • _rms_layernorm_backward_rows: n_rows, blocks_per_program
  • _rms_sum_dw_partials: n_programs

Change

Mark those arguments do_not_specialize (names checked against the kernel signatures). Strides and n_cols are left alone, so the inner loads keep their vectorization hints.

Validation

B200, torch 2.14 + Triton 3.8, shared GPU. Compiled variants counted from JITFunction.device_caches, bf16, Gemma and non-Gemma, 31 row counts (1 to 19, 31, 32, 33, 100, 511, 512, 1000, 2048, 3001, 4096, 8192):

kernel width base this PR
_rms_layernorm_backward 2048 10 2 (Gemma / non-Gemma)
_rms_layernorm_forward_rows 128 (multi-row path) 6 2
_rms_layernorm_backward_rows 128 10 4
_rms_sum_dw_partials 128 3 1
_rms_layernorm_forward, _gemma_rms_layernorm_forward 2048 1, 1 1, 1

Outputs are bitwise identical to base for Y, dX and dW in 36 cases: bf16 and fp16, Gemma and non-Gemma, widths 128 / 1024 / 4096, rows 1 / 17 / 4096.

New tests/test_triton_variant_budget.py fails on base and passes here.

Tests (-o addopts= -m "not server and not native_mxc"): the new test plus the RMSNorm / norm test files (multirow, multirow settings, fast launch, gradient layout, save output, weight grad, grad alias, test_rms_rope_compile.py, fused add inference): 1052 passed, 16 skipped on torch 2.10 + Triton 3.6 and on torch 2.14 + Triton 3.8.

Not covered

  • No exclusive-GPU timing. do_not_specialize drops the == 1 / % 16 hints for these arguments only; they feed loop bounds and row masks, not the per-element address math, and the bitwise checks above show no numeric change.
  • ROCm and Triton below 3.6 (multi-row path is off there) were not run.

…orm, RoPE and q/k norm RoPE kernels

Triton specializes integer arguments on == 1 and divisibility by 16, so every
token count compiled another copy of the same kernel: RMSNorm backward 4,
RoPE 2x per direction, q/k norm RoPE forward 3 across a few sequence lengths.
These arguments only bound loops and masks or feed a division, so the
specialization buys nothing. One variant per configuration now.
…ths are owned elsewhere)

The do_not_specialize change stays on the RMSNorm kernels only; the RoPE and
q/k norm RoPE seqlen specialization is left for the owners of those paths.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T19:31:13.881496Z 2be420e Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 2be420e690

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 64c7a18 into main Oct 22, 2026
3 of 30 checks passed
@danielhanchen
danielhanchen deleted the ta-u-dnspec branch October 22, 2026 20:06
danielhanchen added a commit that referenced this pull request Oct 22, 2026
pre-commit.ci fails `ruff-format-with-kwargs` on every open PR (#14806,
#14805, #14803, #14790, #14785, #14781, #14780, #14777, #14776, #14775,
#14774, #14773, #14772, #14770, #14769, #14768, and others): "files were
modified by this hook". pre-commit.ci runs the hooks on all files, and
20 files on main (26 by the time this lands) are not in the formatter's
output, so every PR is reformatted outside its own diff. Where
pre-commit.ci can push, its autofix commit drags those unrelated files
into the PR (for example #14790's touches `tests/test_block_compile.py`,
`tests/test_bnb_integration_compile.py` and `unsloth/models/_utils.py`).

The drift came in with PRs merged today, among them #14702, #14751,
#14759, #14784, #14786, #14787, #14788, #14791 and the FP8 block decode,
GRPO routing replay, 4-bit decode, compiled blocks and MoE LoRA target
changes.

### Change

`python scripts/run_ruff_format.py` (the hook's own entry point,
`ruff==0.6.9` as pinned in `.pre-commit-config.yaml`) applied to those
20 files, plus 6 more from #14739, #14779 and #14806, which merged while
this was open. Formatting only.

### Verification

- Every changed file parses to an identical AST before and after.
- A second run of the hook over the same files changes nothing (fixed
point), and over all tracked Python files on this branch it changes
nothing.
- `ruff check` passes on the changed files;
`tests/test_formatter_fixed_point.py` passes (16).

Open PRs pick this up on their next pre-commit.ci run after merging
main.
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.

1 participant