Skip to content

Triton kernels: GPU guard against mid-generate recompiles, comment every tl.constexpr - #16575

Merged
danielhanchen merged 15 commits into
mainfrom
triton-specialization-audit
Nov 12, 2026
Merged

danielhanchen merged 15 commits into
mainfrom
triton-specialization-audit

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

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. #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 Kernels: keep sequence length out of Triton specialization in the RoPE kernels #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.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T15:13:27.311159Z 247e7b1 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 danielhanchen self-assigned this Nov 3, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: c696afb031

ℹ️ 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".

…ioural launch-key test; document launch() int contract

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: fbbe50294d

ℹ️ 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".

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

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!

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: b24b7ec245

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: c8ab6e2bea

ℹ️ 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".

… unused unspecialized; GPU binary-count tests

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 49d1656445

ℹ️ 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".

… now-unused jit_any_alignment; comment main's new constexprs; GPU binary-count guard

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 247e7b1ecf

ℹ️ 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 changed the title Triton kernels: no new binaries mid-generate, comment every tl.constexpr Triton kernels: GPU guard against mid-generate recompiles, comment every tl.constexpr Nov 12, 2026
@danielhanchen
danielhanchen merged commit 2c94592 into main Nov 12, 2026
@danielhanchen
danielhanchen deleted the triton-specialization-audit branch November 12, 2026 15:41
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.

2 participants