Skip to content

Studio INT8 attention (Triton): direct launches after the first call, sizes out of the attention kernel's compile key - #17855

Merged
danielhanchen merged 17 commits into
mainfrom
studio-int8-attention-triton-speed-2
Nov 15, 2026
Merged

danielhanchen merged 17 commits into
mainfrom
studio-int8-attention-triton-speed-2

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Follow-up to #17789 for Studio's Triton INT8 attention kernel, the one that serves gfx1151 (Strix Halo) and NVIDIA cards whose CUDA kernel cannot load. Two changes, both in diffusion_int8_attention_triton.py. The output does not move a bit at any of the 17 protocol shapes (126 to 75600 tokens) on any card we ran: RTX PRO 6000 Blackwell, L4 and gfx1151.

  • Direct launches after the first call per card. The first call of a launch plan on a card goes through kernel[grid](...) as before. Later calls hand the same argument list straight to the compiled kernel's launcher, which is the call JITFunction.run makes after its cache lookup. So a call no longer pays Triton's per-argument binding and specialisation on the host. It is taken only while every tensor is 16-byte aligned (as at the first call, since alignment is part of what was compiled), only on the card the launcher was cached for and with the operands on that card, and never under the Triton interpreter. The cached template holds no tensors. Any launcher error switches the process back to regular launches. UNSLOTH_DIFFUSION_INT8_TRITON_FAST_LAUNCH=0 turns it off.
  • Sequence lengths and head count out of the attention kernel's compile key. Studio INT8 attention (Triton): two quantiser launches and a launch plan per call, same output bits #17789 did this for the two quantiser kernels. The attention kernel now gets do_not_specialize on sq, sk and n_heads as well, so a new resolution does not compile another variant. ROCm builds keep sq / sk in the key: on gfx1151 the unspecialised kernel ran Wan 2.2 A14B 720p (75k tokens) 3% slower in two separate runs, while a render keeps one resolution, so the extra variants cost nothing per step there.

Results

Main = 3dd22b6. Three rounds of main / head / head with the direct launch off / head with the direct launch off / head / main, one fresh process each, so 6 processes per arm. A/A = the spread of main's own processes, or the spread within one process if larger. Eager = one call between CUDA events with L2 flushed. Loop = back-to-back calls, which is where host time shows. Host = wall time of one synchronised call.

card output device time (eager, CUDA graph) back-to-back, 126 tokens host per call peak memory
RTX PRO 6000 (sm120), Triton 3.8 same bits, 34/34 TIE at all 17 shapes 0.0651 -> 0.0461 ms (0.71); 1k head dim 64: 0.72 68-90 -> 50-70 us equal at all shapes
L4 (sm89), Triton 3.8 same bits, 34/34 TIE at 15 shapes; see the note on two 1k shapes 0.2414 -> 0.1589 ms (0.66); 1k head dim 64: 0.68 251-573 -> 173-475 us equal at all shapes
gfx1151 Linux (ROCm 7.13, Triton 3.6) same bits, 14/14 TIE at all 7 shapes (wan22_a14b 1.002) 0.1393 -> 0.0903 ms (0.65) 120-363 -> 66-186 us equal at all shapes

Compiled attention kernel variants over the 17 shapes go from 8 to 4 on the RTX PRO 6000 and from 9 to 4 on L4 (one EVEN_K pair per head dim). The first call at every shape on an empty Triton cache took 5.7 -> 3.6 s and 19.2 -> 10.7 s (one run each). gfx1151 keeps the same variants as main (6 over its 7 shapes), as intended.

L4 note: across processes, two synthetic 1k-token shapes read slower in eager right after the back-to-back loop (1k head dim 64: 1.11, 1k x 24 heads: 1.04), and only with the direct launch on. This comes from the card's clock, not the kernels. With the direct launch on the GPU does about 50% more work per second in the loop, so the L4 drops its SM clock further (1620 vs 1950 MHz after 400 calls at 1k head dim 64). The same calls on a rested card, toggled on and off in one process, take the same time: 0.1864 vs 0.1864 ms and 0.2724 vs 0.2734 ms. No other card shows it.

Engagement, 10 steady calls per shape: head 510 direct launches and 0 regular ones on NVIDIA (210 / 0 on gfx1151). Main and the kill switch make 510 regular launches. Host syncs: 0 in every arm.

Triton versions

On L4, main and head ran side by side in one process per version at 5 shapes:

  • Triton 3.4, 3.5 and 3.6: direct launches engage, and the output is the same bits as main.
  • Triton 3.2 (torch 2.6): the first direct attempt raises, so the process keeps the regular launch, with the same bits.
  • Triton 3.3.1 (torch 2.7.1): the attention kernel does not compile on main either ('tt.join' op failed to infer returned types), so the op takes the native fallback on both sides. This predates this change and is not fixed here.

Tests

  • CPU, all test_diffusion_int8_attention*.py files: 240 passed, 88 skipped, 0 failed.
  • New CPU tests:
    • The argument template follows the kernel's parameter order and holds no tensors.
    • A cached launcher never serves another card.
    • The attention kernel is now in the compile-key test, which uses Triton's own specialiser: one key over 15 lengths x 5 head counts on NVIDIA, more than one on ROCm builds. With the attention kernel's lengths put back in the key, the test fails.
  • RTX PRO 6000 and L4 on-card pytest: head 310 passed / 20 skipped / 0 failed; main 307 / 20 / 0.
  • gfx1151 (AMD CI): head 146 passed / 1 failed / 84 skipped; main 143 / 1 / 84. The one failure is test_the_attention_kernel_compiles_through_the_8bit_prefetch_fix on both sides, an NVIDIA prefetch test on ROCm. All 10 ROCm card tests pass. The AMD CI verdict is NO_REGRESSION.

Not covered

  • Two real Triton-capable cards: cross-card behaviour is covered by the wiring test only.
  • A100 / sm86 and Windows (gfx1151 or triton-windows) did not run at this head.
  • On Triton 3.4, direct launches are inferred from zero regular launches and correct output, because that version has no hook chain to count them.
  • Timings are per call on synthetic DiT-shaped inputs. There were no end-to-end renders.

…lan per call signature

prep_kernel runs everything that reads only the inputs (K mean, V range, Q tiles, the channel-major V copy) in one
launch, kv_kernel what needs those statistics (K tiles, INT8 V tiles). Each block runs the code its standalone
kernel ran, so the operands and the output are bit for bit the same. Padded lengths, grids and every scalar argument
are computed once per call signature. Calls past the grid limit keep one launch per quantiser.
…launch plans

Under the Triton interpreter the two quantiser launches and a cached plan give the output of one launch per
quantiser bit for bit; a plan is made once per call signature, takes the long config from long_keys keys on and
falls back to one launch per quantiser past the grid limit.
…d kernels' compile key

prep_kernel and kv_kernel took the token counts (sq, sk) and block counts (n_qblk, n_kblk) as plain ints, so Triton
specialised them on % 16 and == 1 and compiled up to 4 variants each across real sequence lengths. Mark them
do_not_specialize so one compile serves every length; the padded sizes and strides stay specialised (always
multiples of 16). Adds a CPU test that computes the compile key with Triton's own specialiser over 15 token counts.
The kernels split the (batch, head) program index and multiplied it by the i32 strides in i32 before widening, so
b * x_stride_b wrapped once the product passed 2**31 (a batch stride of 2**30 at batch 2 gave -2**31). Widen b, h and
bh to int64 before any stride or channel multiply in every kernel: the per-quantiser kernels, the fused prep / kv
kernels and the attention kernel. Adds a CPU test that lowers each kernel to TTIR and rejects any i32 multiply taking
a stride or the (batch, head) index.
Triton launches on the current device and stream, so quantise, attend, int8_attention and half_attention launched
on the wrong card when called directly with tensors on another one. Studio's dispatcher already runs the Triton path
under torch.cuda.device(q.device); it now calls the unchecked _int8_attention, so a render pays nothing, and the
module's public entry points switch to the operands' card themselves, only when it differs from the current one.
Adds a CPU test with fake CUDA tensors on a second card and recording launchers.

[skip ci]
Sequence lengths, head counts and block counts are do_not_specialize on the three kernels, so a new shape does not
compile another variant (% 16 / == 1 specialisation). Over the 17 protocol shapes: 8 / 7 / 8 compiled variants of the
prep / kv / attention kernels become 2 / 2 / 4 on NVIDIA (the EVEN_K pair per head dim), with the same output bits.
…per device

The first call of a launch plan per device goes through kernel[grid](...); later calls hand the same arguments
straight to the compiled kernel's launcher, as JITFunction.run does after its cache lookup, so a short call does not
pay Triton's per-argument specialisation again (0.08 to 0.1 ms of host time per call on an A100). Taken only while
every tensor is 16-byte aligned as at the first call; the cached template keeps no tensors; any launcher error falls
back to the regular launch for good; UNSLOTH_DIFFUSION_INT8_TRITON_FAST_LAUNCH=0 turns it off. The interpreter keeps
the regular launch.
…e attention kernel's compile key

On gfx1151 the attention kernel without the % 16 facts on sq / sk ran Wan 2.2 A14B 720p (75k tokens) 3% slower, in
two separate ABBA runs; with them it matches main again (1.002). NVIDIA times the same either way, so only ROCm
builds keep them; prep and kv stay unspecialised everywhere (the same speed on gfx1151 with or without).
…llow-up

Conflicts: the prep / kv do_not_specialize lists (both sides added one); kept the union (sq, sk, n_heads, n_qblk /
sk, n_heads, n_kblk). The direct launch now also requires the operands to sit on the card it runs on, next to
#17789's per-call device guard, so a cached launcher never serves another card.
Main's head-count fix for the fused quantisers (9289f21), the one-GPU ROCm device-test stub (c091e9448b) and the
comment pass are kept; round 2 keeps only what main lacks: sizes out of the attention kernel's compile key (sq / sk
kept on ROCm), and the direct launches after the first call per device.
…compile-key test

Main's compile-key test computes the key with Triton's own specialiser; the attention kernel now goes through it too
(one key over every protocol length and head count on NVIDIA, more than one on ROCm builds, which keep sq / sk). The
round-2 attribute check it replaces repeated main's prep / kv coverage.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 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-15T17:21:24.372941Z 607611b 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

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. Already looking forward to the next diff.

Reviewed commit: 607611b232

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

[skip ci]
@danielhanchen
danielhanchen merged commit fc39fc1 into main Nov 15, 2026
@danielhanchen
danielhanchen deleted the studio-int8-attention-triton-speed-2 branch November 15, 2026 18:35
danielhanchen added a commit that referenced this pull request Nov 16, 2026
… red on main) (#17889)

## Summary

`(Python 3.13, diffusion)` has failed on `main` since #17855 (direct
INT8 attention launches after the first call). 13 cases of
`studio/backend/tests/test_diffusion_int8_attention_triton_device.py::test_int8_attention_launches_on_the_operands_card`
fail:

```
core/inference/diffusion_int8_attention_triton.py:1153: in _launch
    if _fast_launch_ok() and isinstance(kernel, triton.runtime.jit.JITFunction):
E   AttributeError: 'NoneType' object has no attribute 'runtime'
```

The module keeps `triton = None` when Triton cannot be imported, and
that CI leg has no Triton. The device test drives the launcher with
stand-in kernels, so it reaches `_launch` without Triton. #17855's gate
then dereferences `triton.runtime`. The fast path itself is right; it
just needs to be off when there is no Triton to launch through.

## Change

- `_fast_launch_ok()` returns `False` when `triton is None`, so every
launch takes the regular `kernel[grid](...)` path. With Triton installed
nothing changes, and the `UNSLOTH_*` kill switch keeps working.
- New `test_fast_launch_is_off_without_triton` pins it: `T.triton =
None` with the switch forced on still answers `False`.

## Verification

The two INT8 Triton device and compile-key test files, run with Triton
blocked (`sys.modules["triton"] = None`, as on the diffusion leg) and
with Triton installed:

| | no Triton | Triton installed |
|---|---|---|
| main | 13 failed | passes |
| this branch | 15 passed, 2 skipped | 15 passed, 2 skipped |
| new test, source change reverted | fails | |
Penguinjanator pushed a commit to Penguinjanator/unsloth that referenced this pull request Nov 16, 2026
…on without Triton, kernel and test gates (unslothai#17882)

Backend CI on main has been red since today's merges. Every failure
traces to one commit; this fixes the unsloth side (the gloo-cp shard
also needs unslothai/unsloth-zoo#2542, since CI installs zoo from main).

| CI shard / test | Cause | Fix |
|---|---|---|
| gloo: `test_qwen4exp_parallel_matches_one_process[pp*]`,
`test_pp_splits_streams_and_runs_the_mixer_last`, all of
`test_frozen_table_pp_gloo.py` | unslothai#17785: transformers 5.17 (the CI pin)
names the Qwen4Exp layer type `qwen_sparse_attention`, 5.22 renamed it
`indexed_attention`; pipeline parallel only accepted the new name, so
every rank raised "hybrid layer stacks" | accept both names in
`_PP_HYBRID_FAMILIES` |
| diffusion (Python 3.13): 13 tests in
`test_diffusion_int8_attention_triton_device.py` | unslothai#17855: `_launch`
read `triton.runtime` while `triton` is `None` on that shard | check
`triton is not None` first |
| rest: `test_every_constexpr_line_has_a_comment` | unslothai#17860: 23
uncommented `tl.constexpr` parameters in `large_head_attention.py` |
comments added; `_tma_tile`'s signature split one parameter per line |
| rest: `test_every_launch_requests_fp_fusion` | unslothai#17860: the `_launch`
helper forwards `enable_fp_fusion` from every caller, but the gate did
not know it is a helper | registered like `layernorm.py`'s |
| rest: `test_no_file_is_dropped_or_run_twice` | unslothai#17868:
`tests/test_ddp_push_rank0_gloo.py` matched both the gloo shard and
rest-2's `test_[dimsu]*.py` | renamed
`tests/test_hub_push_rank0_ddp_gloo.py` |
| rest-2: `test_checked_in_file_reads_name_an_encoding` | unslothai#17867:
`read_text()` without an encoding in
`tests/test_bind_local_cuda_device.py` | `encoding = "utf-8"` |
| rest-2:
`test_sentence_transformer_trainer_checks_the_placement_before_the_deferred_compile[*]`
| unslothai#17859: `patch_train_dataloader` assumed every trainer class has
`get_train_dataloader` | skip the patch when the method is missing |
| rest-2:
`test_router_aux_loss_gradient_survives_unsloth_checkpointing[*]` |
unslothai#17873: the gpu-marked test still runs under CI's `-m 'not server and
not e2e'` | `has_real_accelerator()` skip next to the gpu mark |
| studio: `test_no_skip_guard_reads_a_spoofable_accelerator_probe` |
unslothai#17850: `test_block_cudagraphs_side_grad.py` skipped on
`torch.cuda.is_available()`, which the CUDA spoof fakes | skip on
`has_real_cuda()` |

## Tests

In a venv matching CI (Python 3.12, torch 2.10.0+cpu, transformers
5.17.0, TRL 1.15, PEFT 0.21.3, `UNSLOTH_COMPILE_DISABLE=1`, `-m 'not
server and not e2e'`):

- gloo: `test_qwen4exp_parallel_gloo.py` failed on main, 9 / 9 pass
here; `test_frozen_table_pp_gloo.py` 13 / 13.
- rest / rest-2 / studio: the nine affected files had exactly CI's 9
failures on main; 153 passed / 6 skipped here.
- diffusion: with Triton blocked (as on the Python 3.13 shard), the 13
failures pass here; with Triton present everything passes.

The only non-test code changes are the PP layer-type name, the Triton
`None` check and the Sentence Transformers method check;
`large_head_attention.py` changes are comments only.
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