Repository navigation
Triton kernels: GPU guard against mid-generate recompiles, comment every tl.constexpr - #16575
Conversation
Every tl.constexpr parameter line of a kernel under unsloth/ must carry a comment, and no kernel may take a per-call size (rows, tokens, sequence length, batch) as a constexpr, since each distinct value compiles a new variant. The comments land in the next commit.
Each tl.constexpr parameter line now says what the value is and, for block sizes picked from a table, what the table is bucketed on. One-line kernel signatures whose comments no longer fit are written one parameter per line. Comment and layout only: every file parses to the same AST as before. The audit found no constexpr fed by a per-call size: row, token, sequence and batch counts are runtime arguments, and the M-dependent block sizes (fp8 BLOCK_SIZE_M, nvfp4 tiles, int4 SPLIT_K) come from tables with at most four buckets.
Triton specializes int arguments on == 1 and on divisibility by 16. seqlen is 1 in decode and the prompt length in prefill, so the RoPE and fused QK norm + RoPE kernels built a third binary the first time a prompt length was a multiple of 16, mid-session. jit_any_alignment (triton_launch.py) keeps the == 1 specialization and drops the alignment one via do_not_specialize_on_alignment, falling back to plain triton.jit on a Triton without it. The constexpr comment test also recognises the wrapper.
Triton specialized M (rows) of the NF4 GEMV kernels on == 1, so a generate at batch 3 after one at batch 1 compiled a second binary mid-session (w5_recompile G1 on Kaggle T4, base and head). M is now do_not_specialize in both kernels and launch() takes the positions of such integers (unspecialized) and keys them by width only, so one cache entry serves every row count. The == 1 fold of pid % M is gone at batch 1; scripts/decgen/w5_gemv_spec.py times that.
Fully unspecializing M cost batch-1 decode 1-5 % on T4 (w5_gemv_spec: Llama-1B kv 11.93 -> 12.47 us, 8B q/o 58.36 -> 60.06 us), since M == 1 folds pid % M. M now goes through jit_any_alignment: == 1 stays specialized and the divisibility class does not, so every M > 1 shares one binary. launch() gains an unaligned mode that keys such integers by == 1 and width only; nf4_gemv uses it only when Triton really dropped the % 16 specialization, so an older Triton never reuses a binary compiled for a divisible M.
Codex Review Summary: ✅ CompletedThis 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. |
|
Codex Review: Didn't find any major issues. Chef's kiss. 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". |
…ioural launch-key test; document launch() int contract [skip ci]
|
Codex Review: Didn't find any major issues. Keep it up! 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". |
|
I cancelled the CI runs on this PR for now, since Codex hasn't converged at the current head yet: the latest push hasn't been reviewed by Codex yet. CI here runs on the team's shared runner pool, and it's backed up right now (380+ runs queued), so runs on code that's still changing push everyone else's back. Once Codex says "Didn't find any major issues" on the head commit, one push or a re-run brings CI back. A small ask going forward: hold the push until Codex is clean, and batch follow-up fixes into a single push. Thanks! |
… main added or collapsed [skip ci]
|
Codex Review: Didn't find any major issues. 👍 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". |
[skip ci]
|
Codex Review: Didn't find any major issues. Hooray! 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". |
… unused unspecialized; GPU binary-count tests [skip ci]
|
Codex Review: Didn't find any major issues. Hooray! 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". |
… now-unused jit_any_alignment; comment main's new constexprs; GPU binary-count guard [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 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". |
Summary
Guards against Triton kernels building new binaries in the middle of a generate, and documents every
tl.constexprparameter in Unsloth's own kernels.This PR started as a specialization change for the RoPE, QK norm + RoPE and NF4 GEMV kernels. #16690 has since landed on main with the same goal:
seqlenisdo_not_specializein the RoPE kernels, and GEMVMstays 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 earlierMchange 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. Thejit_any_alignmenthelper and theunalignedlaunch 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 atl.constexpr, or adds atl.constexprwithout a comment saying what it is.tl.constexprthat 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
seqlenspecialized)test_rope_no_new_binary_per_seqlenandtest_qk_norm_rope_no_new_binary_per_seqlenfail (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).regression_smokeSAME (L4),jobs/sft.pyandjobs/inference_smoke.pyNO_REGRESSION (T4), Studio journeys unchanged.