Skip to content

Kernels: keep sequence length out of Triton specialization in the RoPE kernels - #16690

Merged
danielhanchen merged 5 commits into
mainfrom
dh/constexpr-audit-decode
Nov 4, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
dh/constexpr-audit-decode

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Triton specializes plain int kernel arguments on == 1 and % 16 == 0, so a size that is 1 at decode and something else in prefill compiles a second variant even though it is not a tl.constexpr. The fused RoPE and the fused QK norm + RoPE kernels did this with seqlen. This marks it do_not_specialize, keeps the specializations that are worth their variant, and adds the missing why-static comment to every tl.constexpr in the NF4, NF4 GEMV, FP8 LoRA and multi-LoRA kernels.

Every tl.constexpr in the NF4 dequant, NF4 GEMV, fast LoRA (and what it launches), RoPE, QK norm + RoPE, RMS / layer norm, fused add + RMS norm and SwiGLU / GeGLU kernels was audited: none carries a per-call value (head dims, weight-derived tiles, dtype and feature flags, eps, block sizes from static dims).

Change per kernel

kernel change
_rope_embedding_QK (unsloth/kernels/rope_embedding.py:96, jit at :228) seqlen is do_not_specialize: it only feeds the row to (batch, position) split
_qk_norm_rope_forward (unsloth/kernels/qk_norm_rope.py:90) same for seqlen
_qk_norm_rope_backward (unsloth/kernels/qk_norm_rope.py:200) same for seqlen
_gemv_nf4_kernel, _gemv_nf4_words_kernel (unsloth/kernels/nf4_gemv.py:34, :103) M stays specialized on purpose, with a comment: batch 1 (fast_gemv) and 2 to 4 rows (gemv_nf4_rows, capped per GPU) are separate call sites and no cap reaches 16, so it adds no variant per call site, and M == 1 folds the row split (unspecialized measured 0.5 to 0.9% slower at batch 1)
nf4.py, nf4_gemv.py, fp8_lora.py, multi_lora.py why-static comment on every tl.constexpr

Sizes that mask or offset a vector load keep their % 16 hint (n_elements in SwiGLU / GeGLU and the NF4 dequant, numel in the FP8 tensorwise cast, K / OUT_STRIDE in the GEMV, the RoPE batch and head strides).

Testing

Failing first: tests/test_triton_runtime_size_args.py (AST, CPU) extended with the RoPE / QK norm + RoPE runtime sizes, the keep-specialized sizes (including the GEMV M) and the constexpr comment check. The three existing test IDs are unchanged.

tree result
base 6 failed, 8 passed
head 14 passed

Compiled variants per kernel over one inference run (tiny Llama / Qwen3, hidden 256, head dim 64; QLoRA 4bit + LoRA with one train step per shape, and NF4 without LoRA; prefill + 6 decode tokens at prompt lengths 7, 33, 130, 513, batch 1 and 4; Tesla T4, fp16, torch 2.11.0, Triton 3.6.0; base, head and a repeated base in separate processes on one card):

kernel mode base base repeat head
_rope_embedding_QK Llama NF4, Llama QLoRA 2 2 1
_qk_norm_rope_forward Qwen3 QLoRA 2 2 1
_gemv_nf4_words_kernel all 2 2 2 (BLOCK_K 256 vs 512: two weight shapes)
_add_rms_layernorm_inference all 2 2 2 (residual on / off: two call sites)
_rms_layernorm_forward Qwen3 NF4 2 2 2 (hidden norm vs head dim norm)
NF4 dequant, SwiGLU, RMS backward, multi-row RMS all 1 1 1

Kernel time (one process, one T4: the base kernel is built twice from the same function with default specialization for the A/A spread, the head kernel once; each captured in a CUDA graph of 200 launches, replayed in rotating order for 15 rounds; median us per launch; outputs bit-equal across arms):

case base A base B head A/A % head %
RoPE batch 1, seqlen 1 1.831 1.833 1.824 +0.09 -0.44
RoPE batch 4, seqlen 1 1.804 1.849 1.806 +2.51 -1.09
RoPE batch 1, seqlen 512 46.369 46.428 46.422 +0.13 +0.05
RoPE batch 4, seqlen 512 176.833 176.789 176.846 -0.02 +0.02
QK norm + RoPE batch 1, seqlen 1 1.854 1.899 1.895 +2.42 +0.95
QK norm + RoPE batch 4, seqlen 1 1.942 1.900 1.935 -2.16 +0.73
QK norm + RoPE batch 1, seqlen 512 46.122 46.090 46.122 -0.07 +0.03
QK norm + RoPE batch 4, seqlen 512 175.421 175.306 175.121 -0.07 -0.14
NF4 GEMV words N 4096 K 4096 M 1, M unspecialized 78.002 78.152 78.766 +0.19 +0.88
NF4 GEMV words N 14336 K 4096 M 1, M unspecialized 262.437 263.526 264.230 +0.41 +0.47

The last two rows are why M stays specialized.

GPU suites on the head tree (T4): test_triton_runtime_size_args.py, test_rope_constexpr_recompiles.py, test_rope_kernel_layouts.py, test_decode_fused_rope.py, test_qk_norm_rope.py, test_qwen3_decode_offset_rope.py, test_nf4_triton_gemv.py, test_fast_gemv_dispatch.py, test_nf4_triton_dequant.py: 825 passed, 62 skipped.

Not covered

  • T4 / fp16 only: no Ampere, Hopper or Blackwell timing, no bf16 path.
  • The byte GEMV kernel is the default only on Triton 3.7 and newer and was not timed; its code is unchanged from main.
  • The GPU suites, variant counts and timings above ran on the branch before its latest merge of main; only the CPU test and the pre-commit checks were rerun after that merge.

…ation

seqlen in the RoPE and fused QK-norm + RoPE kernels and M in the NF4 decode
GEMV only feed index math, but Triton specialized them (== 1, % 16), so
decode (seqlen 1, batch 1) and prefill / batched decode compiled separate
variants. Mark them do_not_specialize; sizes that mask or offset a vector
load keep their hint. Add the missing why-static comments to every
tl.constexpr in nf4.py, nf4_gemv.py, fp8_lora.py and multi_lora.py, with an
AST test for both.
Interleaved T4 timing showed the unspecialized M 0.5-0.9% slower at batch 1
(the row split by M no longer folds), and it bought no variant: batch 1
(fast_gemv) and 2..4 rows (gemv_nf4_rows, capped per GPU) are separate call
sites and no cap reaches 16. Keep Triton's specialization and say why.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 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-11-04T18:15:28.778386Z b1c5154 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: b1c51541aa

ℹ️ 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 2a256a1 into main Nov 4, 2026
@danielhanchen
danielhanchen deleted the dh/constexpr-audit-decode branch November 4, 2026 21:12
danielhanchen added a commit that referenced this pull request Nov 12, 2026
… now-unused jit_any_alignment; comment main's new constexprs; GPU binary-count guard

[skip ci]
danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 12, 2026
…ery tl.constexpr (unslothai#16575)

## Summary

Guards against Triton kernels building new binaries in the middle of a
generate, and documents every `tl.constexpr` parameter in Unsloth's own
kernels.

This PR started as a specialization change for the RoPE, QK norm + RoPE
and NF4 GEMV kernels. unslothai#16690 has since landed on main with the same
goal: `seqlen` is `do_not_specialize` in the RoPE kernels, and GEMV `M`
stays specialized on purpose because the rows path is capped at 4 by
default. After merging main, this PR takes main's kernels as they are.
An L4 A/B of the earlier `M` change measured it 2 to 3% slower at decode
sizes (N 1024, M 1 to 3, above an A/A spread of about 2%), which agrees
with main's choice. The `jit_any_alignment` helper and the `unaligned`
launch key had no callers left, so they are gone.

What remains:

* `tests/test_triton_kernel_binaries.py` (GPU): counts real compiled
binaries in each kernel's Triton cache. After a warm-up of one decode
shape and one prefill shape, more batch sizes (GEMV rows 3 and 4) and
prompt lengths (16, 32, 48, 300) must build nothing new, and outputs
must still match a reference. Covers the NF4 GEMV rows kernel, RoPE and
the fused QK norm + RoPE kernels, forward and backward.
* `tests/test_triton_constexpr_comments.py` (CPU): fails when an owned
kernel takes a per-call size (rows, tokens, sequence length, batch) as a
`tl.constexpr`, or adds a `tl.constexpr` without a comment saying what
it is.
* A short comment on every `tl.constexpr` that lacked one, including the
kernels main added since (`bf16_adamw.py`, the RoPE launch kernel).
Every changed product file is AST-identical to main: comments only.

## Testing

* GPU guard, base vs head on a Tesla T4 (Kaggle, torch 2.14.1, Triton
3.8.0): on the old merge base (before unslothai#16690, `seqlen` specialized)
`test_rope_no_new_binary_per_seqlen` and
`test_qk_norm_rope_no_new_binary_per_seqlen` fail (6 binaries after
warm-up vs 4: prompt lengths 16 / 32 / 48 built new ones); on this head
the guard and the kernel suites pass (857 passed, 0 failed: NF4 GEMV,
fast launch, RoPE, QK norm + RoPE, compiled RoPE single-graph, decode
fused RoPE).
* AMD gfx1151 (ROCm, AMD CI), gpu-marked tests included: 411 passed, 0
failed, no regression against main; the three binary guards pass there
too.
* Regression switchboard (base vs head): `regression_smoke` SAME (L4),
`jobs/sft.py` and `jobs/inference_smoke.py` NO_REGRESSION (T4), Studio
journeys unchanged.
* CPU lint test on Linux, macOS 15 and Windows: pass.
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