Skip to content

Studio INT8 attention: compile the Triton kernel on NVIDIA Triton 3.3 (torch 2.7) - #19319

Merged
danielhanchen merged 3 commits into
mainfrom
studio-int8-attn-old-triton
Nov 24, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-int8-attn-old-triton

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Summary

Studio's Triton INT8 attention kernel did not compile on NVIDIA Triton 3.3 (torch 2.7.x). #17957 dealt with this by switching the Triton path off on that version. This PR makes the kernel compile there, so the Triton path can stand in on 3.3 like it does on every other version.

  • The failure: the Q / K rotation joins (a + b, a - b) with tl.join. On Triton 3.3, the TritonGPURemoveLayoutConversions pass assigns the join a result layout with the new axis as the last register bit. JoinOp's own type inference expects that axis as the first register bit, so verification fails with 'tt.join' op failed to infer returned types / PassManager::run failed. This breaks prep, kv and quant_rows on every NVIDIA arch: sm80, sm86, sm89, sm100 and sm120. attn compiles. Triton 3.2 and 3.4+ compile the join.
  • The fix: on NVIDIA Triton 3.3 only, the rotation builds the same interleave with tl.where on a size-2 axis instead of tl.join. Every other Triton, ROCm included, keeps the existing source, so its compiled kernels and output bits do not change.
  • Why two definitions instead of one with a constexpr switch: the two _rotate bodies are separate definitions, picked in Python at import. Triton 3.2 and 3.3 leave a constexpr global's value out of JITFunction.cache_key. I checked this directly: the same source with G = tl.constexpr(True) and with False gives identical keys on 3.2.0 and 3.3.1, and different keys on 3.7.1. With one body behind a constexpr if, both variants would share one compiled kernel.
  • Clearer fallback log: when the kernel falls back to native attention, the log now gives the reason (UNSLOTH_DIFFUSION_INT8_ATTENTION_IMPL=cuda, Triton not installed, or no Triton config for sm_XY head_dim=N). It used to say only "no kernel for this card". The install log line also includes the verdict's error.
  • Persisted probe key: the key now reads the Triton version from triton-windows, triton-rocm, triton-xpu, pytorch-triton-rocm, pytorch-triton and pytorch-triton-xpu as well as triton. Before, it was None on Windows and ROCm wheels, so verdicts from different Triton versions shared one key there. NVIDIA Linux keys do not change (the triton distribution comes first).
  • One re-race on Windows and ROCm: saved INT8 attention verdicts there are invalidated once after upgrading, because the key now carries the Triton distribution version (triton-windows, triton-rocm, pytorch-triton-rocm, and so on). The first load re-runs the check and race; later loads reuse the new verdict.

Who this reaches

  • Old torch is a real install path: install.sh installs torch 2.6.0 for drivers reporting CUDA 12.0 to 12.5 (cu124 index, newest is 2.6.0) and torch 2.7.1 for CUDA 11.x drivers (cu118 index, newest is 2.7.1). It also keeps an existing 2.4+ torch on update. install_python_stack.py sets up the diffusion stack (torchao, diffusers) for torch 2.6 to 2.9.
  • NVIDIA tries the CUDA kernel first: on NVIDIA, Studio runs the CUDA (NVRTC) INT8 kernel and uses Triton only when that kernel cannot be built or loaded, or when IMPL=triton is set. I compiled the CUDA kernel's attention and quantiser sources with NVRTC 11.8, 12.4 and 12.8 for every configured card and head dim. All of them compile, except sm120 on 11.8 and 12.4, which those wheels cannot run anyway.
  • So this PR covers the fallback: it restores the Triton stand-in on torch 2.7 for cards where the CUDA kernel fails at runtime, or where it is turned off. Studio still races the Triton path against native attention, so it only engages where it is faster.

Evidence

CPU compile: fresh TRITON_CACHE_DIR per run, all prep / kv / quant_rows / attn variants for head dims 64 and 128.

Triton origin/main this branch
3.2.0 (torch 2.6) 0 failures (sm80, sm89) 0 failures (sm80, sm86, sm89)
3.3.1 (torch 2.7) 120 failures of 200 (prep, kv, quant_rows on sm80 / 86 / 89 / 100 / 120) 0 failures (sm80 / 86 / 89 / 90 / 100 / 120)
3.7.1 0 0

Studio's own verdict() (untimed accuracy check): B200, torch 2.7.1+cu128, Triton 3.3.1, IMPL=triton, the sm89 Triton config.

arm triton_available implementation verdict
origin/main False None ok=False, "no kernel for this card"
this branch True triton ok=True, cos 0.999943, rel L1 0.00934
this branch, Triton 3.7.1 (control) True triton ok=True, cos 0.999943, rel L1 0.00934

Numerics on B200: Studio's Triton path vs fp32 attention and vs bf16 SDPA, at real attention shapes. bf16 inputs; keys with a per-channel offset; values with a spread of channel ranges.

shape (B, Sq, Sk, H, D) arm max abs vs fp32 max abs / max ref rel L1 vs fp32 min row cos rel L1 vs bf16 SDPA
Qwen-Image-2.1 1024 px (1, 4096, 4127, 32, 128) 3.3.1 branch 1.193e-2 1.644e-2 1.099e-2 0.999891 1.097e-2
3.7.1 current kernel 1.193e-2 1.644e-2 1.099e-2 0.999891 1.099e-2
Wan2.2 5B 480p (1, 12090, 12090, 24, 128) 3.3.1 branch 4.246e-3 1.344e-2 1.111e-2 0.999910 1.110e-2
3.7.1 current kernel 4.246e-3 1.344e-2 1.111e-2 0.999910 1.111e-2
Wan2.2 A14B 480p (1, 32760, 32760, 40, 128) 3.3.1 branch 2.323e-3 1.257e-2 1.109e-2 0.999924 1.109e-2
3.7.1 current kernel 2.323e-3 1.257e-2 1.109e-2 0.999924 1.110e-2
LTX-2.5 audio (1, 126, 126, 32, 64) 3.3.1 branch 3.633e-2 9.655e-3 9.977e-3 0.999923 9.935e-3
3.7.1 current kernel 3.633e-2 9.655e-3 9.977e-3 0.999923 9.928e-3

For scale, bf16 SDPA vs fp32 on the same inputs has a rel L1 of 2.1e-3 to 2.3e-3.

Bits:

  • The rotation alone (64x128, 64x64, 32x128, 128x64) is bit-identical to a torch reference on both Triton versions.
  • On 3.3.1 the join-free kernel's operands and outputs are bit-identical to the same join-free source on 3.7.1, on every shape.
  • Against the current join kernel on 3.7.1, K codes and both scale tensors are bit-identical. A few Q int8 codes differ by one at rounding ties: 21 of 16.8M for Qwen and 42 of 37.2M for Wan 5B. Against a float64 single-rounding reference, the join-free codes miss 40 and the join codes miss 59. The outputs differ by at most 9.8e-4, with the same error against fp32 to four digits.
  • Repeated calls are bit-identical on every arm.

Colab A100-40 (sm80) with the wheels install.sh picks for older drivers: torch 2.7.1+cu126 (Triton 3.3.1) and torch 2.6.0+cu124 (Triton 3.2.0). Both versions run the same inputs and the same branch.

shape Triton 3.3.1 (join-free) vs Triton 3.2.0 (join) max abs vs fp32 rel L1 vs fp32 min row cos rel L1 vs bf16 SDPA
Qwen-Image-2.1 1024 px outputs and int8 operands bit-identical 7.256e-3 1.109e-2 0.999923 1.107e-2
Wan2.2 5B 480p bit-identical 4.568e-3 1.114e-2 0.999918 1.113e-2
Wan2.2 A14B 480p bit-identical 3.655e-3 1.112e-2 0.999919 1.111e-2
LTX-2.5 audio (head dim 64) bit-identical 4.401e-2 1.002e-2 0.999926 9.987e-3
1039 tokens, head dim 64 bit-identical 1.848e-2 1.037e-2 0.999921 1.034e-2

On sm80, the join-free kernel on Triton 3.3.1 matches the current join kernel on Triton 3.2.0 bit for bit.

Studio verdict() on the same A100:

torch / Triton arm IMPL serves verdict
2.7.1 / 3.3.1 origin/main triton None ok=False, "no kernel for this card"
2.7.1 / 3.3.1 this branch triton triton ok=True, cos 0.999945, rel L1 0.00914
2.7.1 / 3.3.1 both auto cuda ok=True, cos 0.999925
2.6.0 / 3.2.0 both triton triton ok=True, cos 0.999945 (same as 3.3.1 branch)
2.6.0 / 3.2.0 both auto cuda ok=True, cos 0.999925

With the default IMPL=auto, the CUDA (NVRTC) kernel serves on both old-torch installs, so default users were already getting INT8 attention there. This PR fixes the Triton stand-in, which is used when that CUDA kernel cannot be used.

Tests

  • tests/test_diffusion_int8_attention_triton33.py, rewritten:
    • version gate (NVIDIA 3.3 only; not ROCm 3.3, 3.2, 3.4, 3.7, 3.10 or unparsable versions)
    • both _rotate bodies present, with tl.join only in the default one
    • the reloaded module picks the right body for its Triton
    • probe-key Triton version for each distribution name
    • keys differ across Triton versions
    • decline reasons and the no-kernel verdict message
  • Related CPU suites: 539 passed. 5 tests fail the same way on origin/main (diffusers / dynamo device import on a CPU host).

Not covered

  • No render timing. The Triton path's speed on Triton 3.3 against native attention is not measured here. The existing per-card race decides whether it engages, and a slower stand-in keeps native attention.
  • No sm89 (L4) or sm120 (G4) run on a real card; those archs only have the CPU compile. Windows triton-windows 3.3 and ROCm 3.3 were not run. ROCm keeps the unchanged join source.

….3 (torch 2.7)

Triton 3.3's TritonGPURemoveLayoutConversions pass hands the rotation's tl.join a
result layout that JoinOp's own type inference rejects ("'tt.join' op failed to
infer returned types"), so prep / kv / quant_rows failed to compile on every NVIDIA
arch and #17957 turned the Triton path off on 3.3. On NVIDIA Triton 3.3 the
rotation now interleaves (a + b, a - b) with tl.where on a size-2 axis instead;
every other Triton (and ROCm 3.3) keeps the tl.join source unchanged. The two
bodies are separate definitions picked in Python: Triton 3.2 / 3.3 leave a
constexpr global's value out of the JIT cache key, so one body behind a constexpr
if would share compiled kernels.

Also: a fallback to native attention now says why Triton could not stand in
(IMPL=cuda, no Triton, no config for the card), the install log line carries the
verdict's error, and the persisted probe key reads the Triton version from
triton-windows / pytorch-triton-rocm / pytorch-triton too (it was None there).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 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-24T19:06:47.739208Z c2cb3de 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 59546d9888

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

Comment on lines +95 to +99
monkeypatch.setattr(triton, "__version__", version)
name = "core.inference.diffusion_int8_attention_triton"
fresh = importlib.reload(sys.modules[name])
try:
assert ("tl.join" in inspect.getsource(fresh._rotate.fn)) is join_in_rotate

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Control the build type in the rotation reload test

On a ROCm PyTorch installation with Triton available, the 3.3.1 case fails even though the implementation behaves correctly: reloading the module calls join_free_rotation(), which detects the real ROCm build and deliberately selects the tl.join variant, while this test unconditionally expects the join-free variant. Mock the build detection dependency across the reload or parameterize the expected result by build type so the test also passes on the supported ROCm environment.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4d5e01cc73: the reload test now pins the build identity (torch.version.hip and torch.__version__) and runs four cases, 3.3.1 and 3.7.1 on NVIDIA and on ROCm, with both ROCm cases expecting the tl.join variant. Run as a ROCm build, the old version fails [3.3.1-False] and the new one passes all four.

…el; triton-rocm / triton-xpu keys

A split load raced and filtered only the primary card, then ran an untimed
verdict() on each secondary. With the Triton path now compiling on Triton 3.3, a
secondary whose CUDA module failed fell through to an unraced Triton check and
served Triton, even for a CUDA-only Auto family (FLUX.3). verdict() takes
impl_only: a secondary is checked only on the implementation the primary's floor
raced and accepted, anything else is declined unchecked, so that card keeps
native attention as before.

The probe-cache Triton version also reads the current triton-rocm and triton-xpu
distribution names (it read None there). The rotation reload test pins the torch
build identity and covers ROCm 3.3 keeping tl.join.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 24, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: d5270d5937

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

…imary's raced kernel

The generic Auto path (no family floor) still ran an untimed verdict() on each
split / CFG-replica secondary without impl_only, so a secondary whose CUDA module
failed could adopt the unraced Triton stand-in. Pass the primary's implementation
there too, and keep the family-race floor's untimed check on the CUDA kernel only.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 24, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: c2cb3def23

ℹ️ 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 added a commit that referenced this pull request Nov 24, 2026
…rsion

Same _TRITON_DISTS + _triton_version() helper as #19319 (triton, triton-windows, the ROCm and XPU
distributions), replacing the triton-windows-only fallback, so the fingerprint key changes once.

[skip ci]
@danielhanchen
danielhanchen merged commit 5ea3aec into main Nov 24, 2026
1 check passed
@danielhanchen
danielhanchen deleted the studio-int8-attn-old-triton branch November 24, 2026 21:32
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