Repository navigation
Studio INT8 attention (Triton): direct launches after the first call, sizes out of the attention kernel's compile key - #17855
Merged
Conversation
…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.
…ngle-GPU hosts too
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.
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. |
Member
Author
|
@codex review |
1 similar comment
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". |
[skip ci]
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.
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
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.kernel[grid](...)as before. Later calls hand the same argument list straight to the compiled kernel's launcher, which is the callJITFunction.runmakes 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=0turns it off.do_not_specializeonsq,skandn_headsas well, so a new resolution does not compile another variant. ROCm builds keepsq/skin 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.
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:
'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
test_diffusion_int8_attention*.pyfiles: 240 passed, 88 skipped, 0 failed.test_the_attention_kernel_compiles_through_the_8bit_prefetch_fixon both sides, an NVIDIA prefetch test on ROCm. All 10 ROCm card tests pass. The AMD CI verdict is NO_REGRESSION.Not covered