Repository navigation
Studio diffusion: race the unlisted-arch int8 GEMM tiles held under load (RTX 3090 class: Qwen-Image-2.1 -10%, MiniMax-H3 -9%) - #17026
Merged
Conversation
…chs and keep the fastest
…oad, and keep the winner per card The short best-of-7 race ranks tiles by their boost-clock speed. On a power-capped part the tile that wins a burst draws more power and clocks the whole render down (emulated sm86 on an L4: 1005 vs 1170 MHz, 10% slower end to end). Each tile is now held under load before it is timed, at a 1024^2 image's rows, and the winner is stored per card and toolchain so later processes do not race again. UNSLOTH_DIFFUSION_INT8_GEMM_RACE=0 keeps the fallback tile.
…nner, so a faster tile can beat cuBLAS where the fallback could not
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 |
|
Codex Review: Didn't find any major issues. You're on a roll. 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". |
This was referenced Nov 7, 2026
Studio int8 GEMM: keep sm8x GPUs on cuBLAS under Triton 3.8+, where every shipped tile spills
#17036
Merged
danielhanchen
added a commit
to danielhanchen/unsloth-staging-2
that referenced
this pull request
Nov 7, 2026
…very shipped tile spills (unslothai#17036) ## Problem On Triton 3.8 (torch 2.14), Studio's fused-dequant int8 GEMM runs 4-8x slower on A100 (sm80) and RTX 40 / L4 (sm89) than on Triton 3.7.1, and the tile unlisted sm8x cards (RTX 30 / A10 / A6000, sm86; Orin, sm87) would use spills the same way. The output is still bit-exact; only speed changed. Studio's cu130 route allows torch up to `<2.15` (`_CU130_TORCH_CEILING` in `install.sh`), so these users can be on Triton 3.8 today. On the same card, the kernel then loses to cuBLAS `_int_mm` by a wide margin, so int8 image generation gets much slower than the kill switch (`UNSLOTH_DIFFUSION_INT8_GEMM=0`). Cause: since 3.8.0, Triton's sm8x prefetch pass keeps the next tile's 8-bit operands live across the loop one byte per register (triton-lang/triton#12486, still open, and Triton 3.9.0 behaves the same). Any int8 tile holding 128 int32 accumulators per thread (`BLOCK_M * BLOCK_N / (32 * num_warps)`) then hits the 255-register cap and spills. Offline compile for sm_89 / sm_80, ptxas `-v`: | tile (BM, BN, BK, GROUP_M, warps, stages) | acc / thread | Triton 3.7.1 regs / spill bytes | Triton 3.8.0 | Triton 3.9.0 | |---|---|---|---|---| | (256, 128, 128, 8, 8, 3) shipped sm89 | 128 | 252 / 0 | 255 / 700 | 255 / 816 | | (128, 128, 128, 8, 4, 3) shipped sm80 | 128 | 253 / 0 | 255 / 764 | 255 / 764 | | (128, 128, 64, 8, 4, 4) | 128 | 219 / 0 | 255 / 644 | 255 / 644 | | (256, 64, 128, 8, 8, 3) | 64 | 170 / 0 | 254 / 0 | 254 / 0 | The 3.8 PTX spills under both the 12.8 and 12.9 ptxas, so it is Triton's codegen, not ptxas. A plain int8 `tl.dot` loop with no Studio epilogue spills the same way. The upstream report pins it to the sm8x prefetch pass, and the other archs Studio runs this kernel on do not spill. `_FALLBACK_CONFIG` (128, 128, 64, 8, 4, 4), offline compile, ptxas `-v`: | arch | Triton 3.7.1 regs / spill bytes | Triton 3.8.0 | |---|---|---| | sm86 / sm87 (unlisted) | 219 / 0 | 255 / 644 | | sm90 (unlisted) | 203 / 0 | 193 / 0 | | sm120 (also its default and TMA tile) | 220 / 0 | 217 / 0 | | sm121 (unlisted) | 220 / 0 | 217 / 0 | sm120's deep-K tile (256, 128, 128, 8, 8, 3) is spill-free on both versions too (224 registers). ## Change On Triton 3.8 or newer, every sm8x card stays on the stock torchao + cuBLAS path in auto mode: `arch_config` returns None for sm80 and sm89, and `unlisted_wanted` returns False for the unlisted sm86 / sm87, so they skip the probe and the load-time latency check (which would have sent them to cuBLAS anyway, after timing a spilling kernel). Nothing else moves: - Triton 3.7.1 and older: same tiles as before on every arch. - sm120 (RTX PRO 6000 / RTX 50): unchanged, including its TMA tile and deep-K shape rule. Those tiles hold 128 accumulators per thread but compile spill-free on Triton 3.8 (table above), and unslothai#13772 measured RTX PRO 6000 on Triton 3.8 with no slowdown. I did not re-time sm120 for this PR. - Unlisted sm90 / sm121: unchanged (no spill), still behind their probe + latency check. - `UNSLOTH_DIFFUSION_INT8_GEMM=1` or `UNSLOTH_DIFFUSION_INT8_GEMM_UNLISTED=1` (force) still runs the kernel, as before. - The listed archs' shared-memory fallback to `_FALLBACK_CONFIG` cannot reach a spilling tile any more: on Triton 3.8 sm80 / sm89 never probe, and sm120 does not spill. I first tried picking spill-free 64-accumulator tiles for Triton 3.8. The best one recovers most of the loss, but it still trails cuBLAS `_int_mm` on both archs (table below), and end to end it is slower than the stock path. So stock is the faster and simpler choice there. The version check is host-side, cached once (`_triton_at_least`), and runs before any trace: no new kernel specialisation, no recompiles, no graph breaks. ## Results Colab, one VM per GPU, two venvs on the same VM: torch 2.13.0+cu130 / Triton 3.7.1 and torch 2.14.1+cu130 / Triton 3.8.0 (Triton 3.9.0 swapped into the 2.14 venv for the sweep only). Real DiT shapes (FLUX.2-klein, Z-Image, Wan 2.1 linears) at M = 4096 / 8192 / 16384, 24 shapes in total, three interleaved rounds. Geometric-mean TOPS, int8 in, bf16 out with the full epilogue for the Triton arms. `_int_mm` is cuBLAS alone (no epilogue, so a lower bound on the stock path's GEMM time). The A/A spread over rounds was 0.1-0.8% (A100) and 1.7-5.4% (L4) median per arm. | GPU | Triton | shipped tile | best spill-free tile | cuBLAS `_int_mm` | |---|---|---|---|---| | A100-SXM4-40GB | 3.7.1 | 436.3 | 391.9 | 395.9 | | A100-SXM4-40GB | 3.8.0 | 56.4 | 284.0 (64, 128, 128, 8, 4, 4) | 402.3 | | A100-SXM4-40GB | 3.9.0 | 56.3 | 283.5 (64, 128, 128, 8, 4, 4) | 402.1 | | L4 | 3.7.1 | 116.1 | 107.9 | 110.0 | | L4 | 3.8.0 | 29.6 | 88.4 (256, 64, 128, 8, 8, 3) | 109.8 | | L4 | 3.9.0 | 28.1 | 89.0 (256, 64, 128, 8, 8, 3) | 109.0 | Every one of the 13 tiles swept is bit-identical to the torchao eager epilogue on all 24 shapes and passes Studio's own probe (bias, fp32 scales, ragged M / N / K), on every Triton version, except one tile that needs 120 KB of shared memory and does not launch on L4. ### Whole image Z-Image-Turbo, 1024x1024, 8 steps, torchao int8 dynamic quant with Studio's Linear filter (239 Linears), Studio's fused GEMM installed, repeated blocks compiled. Median seconds per step over 3 rounds; each row is one process, arms inside a process interleaved. | GPU | Triton | main | this PR | kill switch | best spill-free tile | this PR / main | |---|---|---|---|---|---|---| | A100-SXM4-40GB | 3.7.1 | 0.1998 | 0.1992 | 0.2143 | 0.2201 | 0.997 | | A100-SXM4-40GB | 3.8.0 | 0.9595 | 0.2147 | 0.2153 | 0.2515 | 0.224 | | L4 | 3.7.1 | 0.7351 | 0.7408 | 0.8010 | 0.8106 | 1.008 | | L4 | 3.8.0 | 1.9574 | 0.7989 | 0.8196 | 0.8762 | 0.408 | Round-to-round spread per arm was at most 0.5% on A100 and 3.2% on L4 (L4 is power capped). On Triton 3.8 this PR is 4.5x faster per step than main on A100 and 2.4x faster on L4, level with the kill switch. On Triton 3.7.1 main and this PR run the same tile; the gap is inside the noise. - **main** is the current table, **this PR** is this branch's own selection (on Triton 3.8 it installs nothing on these archs, so it runs the stock path), **kill switch** is `UNSLOTH_DIFFUSION_INT8_GEMM=0`, **best spill-free tile** is the fastest tile from the sweep above forced onto the same model. - Inside one process the shipped tile and the best spill-free tile give pixel-identical images, as the kernel-level check predicts. ## Overlap - unslothai#13772 attacks the same regression with a per-shape latency-gated dispatcher and a timing cache. This PR is the minimal static version of its outcome on sm80 / sm89 (cuBLAS on Triton 3.8, the shipped tiles on 3.7.1) and can land first; unslothai#13772 can build on it or replace it. - unslothai#17026 (merged) races `_FALLBACK_CONFIG` against (128, 128, 128, 8, 4, 3) and (256, 128, 128, 8, 8, 3) on unlisted archs, then keeps the existing latency check against cuBLAS. All three tiles hold 128 accumulators per thread and spill on Triton 3.8 for sm8x, and neither a 64-accumulator tile nor `_int_mm` is a race candidate, so on Triton 3.8 this PR keeps sm86 / sm87 out of that path (`unlisted_wanted`) instead of timing spilling kernels at load. `_unlisted_tile` and the race are untouched, and sm90 / sm121 still race as before. This branch already merges current main, including unslothai#17026. ## Not covered - sm120 on Triton 3.8: not re-timed here (offline compile and unslothai#13772's measurement only); its tiles are unchanged. - sm86 / sm87 on a real card: not measured (no such Colab tier). The decision rests on the identical spill (644 bytes, the same as sm80 / sm89) and on A100 / L4, where no tile beat cuBLAS on Triton 3.8. ## Tests - `test_triton38_keeps_every_sm8x_stock`: on Triton 3.8+ sm80 / sm89 return None in auto mode and keep their tile under force; unlisted sm86 / sm87 are not wanted in auto and still are under force; sm90 / sm121 / sm120 unchanged; the pre-3.8 table unchanged. - `test_triton38_runtime_turns_sm80_sm89_stock`: the same through a Triton 3.8 version string, without the new helper (fails on main). - `test_triton_at_least_parses_the_installed_version`: 3.7.1, 3.8.0, 3.9.0+git, 4.0. - `test_arch_gate_auto` and the unlisted-gate tests now pin the pre-3.8 behaviour, so they no longer depend on the Triton the test host has installed. - Every test file that imports the int8 GEMM (15 files): 1178 passed with Triton 3.7.1 and with Triton 3.8.0 importable (CPU host; the GPU kernel checks ran as the sweep above).
danielhanchen
added a commit
to danielhanchen/unsloth-staging-2
that referenced
this pull request
Nov 7, 2026
…hai#17044) ## Summary unslothai#17026 added a per-card cache for the unlisted-arch int8 GEMM tile race (`diffusion_int8_tile_race.json`). Its three `read_text()` / `write_text()` calls do not name an encoding, so `tests/test_runtime_text_encoding.py::test_shipping_code_names_an_encoding` fails on main: ``` 3 text read/write call sites in shipping code let the operator's locale decide the encoding ... studio/backend/core/inference/diffusion_int8_gemm.py:807: write_text() studio/backend/core/inference/diffusion_int8_gemm.py:799: read_text() studio/backend/core/inference/diffusion_int8_gemm.py:783: read_text() ``` This passes `encoding = "utf-8"` on all three. The file is plain ASCII JSON, so existing cache files read back unchanged and behaviour is identical on Linux and macOS; on Windows the read and write no longer depend on the locale code page. ## Testing - `tests/test_runtime_text_encoding.py`: 9 passed (was 1 failed on main). - `studio/backend/tests/test_diffusion_int8_gemm.py` and `test_diffusion_int8_gemm_unlisted.py`: 109 passed, 109 skipped (CPU host).
Chroma01
pushed a commit
to Chroma01/unsloth
that referenced
this pull request
Nov 9, 2026
…nslothai#17084) ## Problem unslothai#17026 races a few Triton tiles for the int8 GEMM on cards with no measured tile table (unlisted sm80+ cards), and keeps the winner for the process. `UNSLOTH_DIFFUSION_INT8_GEMM_RACE=0` is meant to keep the fallback tile. `device_config` re-reads the unlisted path per call "so its switch acts between loads", but the race switch was only read before a winner was kept: - first load with the race on: the winner, e.g. `(256, 128, 128, 8, 8, 3)`; - set `UNSLOTH_DIFFUSION_INT8_GEMM_RACE=0`, next load: still the raced winner instead of the fallback `(128, 128, 64, 8, 4, 4)`. The fused-vs-stock latency verdict was also cached per `(card, mode)` without the tile, so a verdict measured for one tile was reused for another. A process started with the switch already off behaved correctly. Only an in-process change (a rollback between loads, an A/B in one server) was ignored. ## Change - `_unlisted_tile` reads the switch on every load, ahead of the kept winner: off returns the fallback tile, on returns the kept winner (no second race). - The latency verdict is keyed by `(card, mode, tile)`. Nothing changes for a process that never flips the switch: the same race, the same persisted winner, the same verdict. ## Tests `test_race_switch_acts_between_loads_after_a_winner_is_kept` (fake sm86 probe / race / latency harness): winner, then the fallback with the switch off (with its own latency check), then the kept winner again with one race in total. | arm | `test_diffusion_int8_gemm_unlisted.py` + `test_diffusion_int8_gemm.py` | |---|---| | main (`a174823`) | 116 passed, 1 failed (the new test: the raced winner with the switch off) | | this PR | 117 passed |
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
On sm80+ cards with no tuned int8 GEMM tile (RTX 3090 / A6000 / A10 class sm86, sm87, sm90, sm121), Studio runs the fused-dequant int8 GEMM on a fallback tile chosen by a short best-of-7 timing. On a power-capped card that timing picks the wrong tile: the fallback wins a short burst, but it draws more power, so the whole render runs at a lower clock.
This PR:
diffusion_int8_tile_race.jsonunder the cache root), so later processes skip the race.UNSLOTH_DIFFUSION_INT8_GEMM_RACE=0keeps the fallback tile (today's behaviour).Listed archs (sm80, sm89, sm120) and
forcemode take the same path as before. The int8 accumulation and the epilogue are unchanged, so outputs are bit-identical; only the tile shape changes.Why
Profile of Qwen-Image-2.1 1024^2 on an L4 with the sm80 / sm89 rows removed, so it takes the path a 3090 takes, on main:
Kernel names and counts are identical. Even the cuDNN attention kernel, which this code does not touch, is 16% slower: the difference is the clock.
Tile timing on the same card, Studio's torch 2.13.0 / Triton 3.7.1, three GEMMs at 4096 rows (us per pass):
On an A100 (400 W cap) the same harness ranks the A100 tile first under both methods (best of 7: 2125 vs 2325 us for the fallback; held: 2230 vs 2425 us).
Results
Same Colab L4 VM, a fresh process per arm, 3 arms each, interleaved E H C H E C E H C M. Base is merge base 91935a1. Every Studio arm except M carries the sm86 emulation (the sm80 / sm89 rows removed). 25 steps, CFG 1, 3 new-prompt renders per arm.
The H arm logs
latency check: 3238 vs 3690 us cuBLAS, tile (256, 128, 128, 8, 8, 3) of 128x128x64 10038 us / 128x128x128 9290 us / 256x128x128 8667 us held. Renders are pixel-identical between E and H (3 of 3 images, max abs diff 0).MiniMax-H3 960x544x124, 20 steps (int8 default path), same L4 at full card with the same emulation, E H H E E H, a fresh process per arm:
H3 H arm logs
latency check: 3278 vs 3784 us cuBLAS, tile (256, 128, 128, 8, 8, 3) of 128x128x64 9852 us / 128x128x128 9282 us / 256x128x128 8596 us held. Frames are bit-identical between E and H (32 of 32 sampled frames over 2 prompts and 2 pairs, max abs diff 0).No-regression cells:
rotq_h16stays off, so both trees run the stock rotated path and the tile choice cannot act.Tests
tests/test_diffusion_int8_gemm_unlisted.pycovers:Test counts:
Not covered