Repository navigation
Kernels: keep sequence length out of Triton specialization in the RoPE kernels - #16690
Merged
Merged
Conversation
…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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Triton specializes plain int kernel arguments on
== 1and% 16 == 0, so a size that is 1 at decode and something else in prefill compiles a second variant even though it is not atl.constexpr. The fused RoPE and the fused QK norm + RoPE kernels did this withseqlen. This marks itdo_not_specialize, keeps the specializations that are worth their variant, and adds the missing why-static comment to everytl.constexprin the NF4, NF4 GEMV, FP8 LoRA and multi-LoRA kernels.Every
tl.constexprin 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
_rope_embedding_QK(unsloth/kernels/rope_embedding.py:96, jit at :228)seqlenisdo_not_specialize: it only feeds the row to (batch, position) split_qk_norm_rope_forward(unsloth/kernels/qk_norm_rope.py:90)seqlen_qk_norm_rope_backward(unsloth/kernels/qk_norm_rope.py:200)seqlen_gemv_nf4_kernel,_gemv_nf4_words_kernel(unsloth/kernels/nf4_gemv.py:34, :103)Mstays 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, andM == 1folds the row split (unspecialized measured 0.5 to 0.9% slower at batch 1)nf4.py,nf4_gemv.py,fp8_lora.py,multi_lora.pytl.constexprSizes that mask or offset a vector load keep their
% 16hint (n_elementsin SwiGLU / GeGLU and the NF4 dequant,numelin the FP8 tensorwise cast,K/OUT_STRIDEin 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 GEMVM) and the constexpr comment check. The three existing test IDs are unchanged.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):
_rope_embedding_QK_qk_norm_rope_forward_gemv_nf4_words_kernel_add_rms_layernorm_inference_rms_layernorm_forwardKernel 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):
MunspecializedMunspecializedThe last two rows are why
Mstays 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