Skip to content

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
danielhanchen merged 5 commits into
mainfrom
studio-int8-gemm-sustained-tile-race
Nov 7, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
studio-int8-gemm-sustained-tile-race

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

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:

  1. Races the listed tiles (the A100 and L4 tiles) against the fallback on unlisted cards. Each tile must pass the same bit-exact probe, then is held under load (0.3 s to settle, 0.3 s timed) at a 1024^2 image's 4096 rows.
  2. Runs the existing latency check against cuBLAS on the race winner, not on the fallback.
  3. Stores the winner per card, toolchain and source digest (diffusion_int8_tile_race.json under the cache root), so later processes skip the race.
  4. Adds a kill switch: UNSLOTH_DIFFUSION_INT8_GEMM_RACE=0 keeps the fallback tile (today's behaviour).

Listed archs (sm80, sm89, sm120) and force mode 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:

tuned sm89 rows unlisted path (main)
s / image (profiled) 20.92 22.87
kernel time 20.70 s 22.62 s
median SM clock while busy 1155-1185 MHz 990-1005 MHz
throttle reason SW power cap SW power cap

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):

tile best of 7 (today's method) held 0.3 s + 0.3 s held 3 s + 2 s SM clock held
128x128x64 (fallback) 7748 9251 10487 870-990 MHz
128x128x128 (A100 tile) 8931 8715 9437 1080-1170 MHz
256x128x128 (L4 tile) 8654 7850 8325 1170-1245 MHz

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.

arm s / image (3 arms) median vs base
E: base, unlisted path 22.20 / 22.13 / 21.99 22.13 -
H: this PR, unlisted path 19.91 / 19.94 / 19.91 19.91 -10.0%
M: base, tuned sm89 rows (reference) 19.94 19.94 -9.9%

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:

arm s / clip (3 arms) median s / step
E: base, unlisted path 375.2 / 374.5 / 376.5 375.2 18.05
H: this PR, unlisted path 342.0 / 343.2 / 342.3 342.3 (-8.8%) 16.37 (-9.3%)

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:

  • A100 with the sm80 rows removed, Qwen-Image-2.1: E 6.53 / 6.55 / 6.56 s, H 6.55 / 6.55 / 6.55 s. On that card rotq_h16 stays off, so both trees run the stock rotated path and the tile choice cannot act.
  • MiniMax-H3 960x544x124 on the same card held to 24 GiB: E 112.6 / 115.3 / 108.5 s, H 109.3 / 113.2 / 109.3 s. The spread is the same, and the per-step time is 5.15 s in both.
  • Listed archs: unit tests pin that they never race.

Tests

tests/test_diffusion_int8_gemm_unlisted.py covers:

  • the race keeps the fastest probed tile, and a tie keeps the fallback;
  • the latency check times the race winner, and a faster tile can pass where the fallback would not;
  • the winner is reused by the next process, re-raced for another card or toolchain, and dropped when it no longer passes its probe;
  • corrupt or foreign cache entries are a miss;
  • the kill switch neither races nor reads the file;
  • the cache file stays bounded;
  • the held timer skips the settle window.

Test counts:

  • the int8 GEMM, ConvRot and compile-cache test files: 454 passed on the branch, 429 on base, 132 skipped on both;
  • the new tests fail to import on base.

Not covered

  • No real sm86 card was available, so the 3090 number itself is not measured. The race picks per card, so a 3090 keeps whichever tile it actually sustains fastest.
  • The first load on an unlisted card pays for the race once (two extra tile compiles plus about 2 s of timing). Later loads read the stored winner.

…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
@danielhanchen danielhanchen self-assigned this Nov 7, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 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-07T13:40:26.582778Z 7160fb5 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 7160fb5cb7

ℹ️ 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 Studio diffusion: race the unlisted-arch int8 GEMM tiles held under load (RTX 3090 class 10% faster on Qwen-Image-2.1) Studio diffusion: race the unlisted-arch int8 GEMM tiles held under load (RTX 3090 class: Qwen-Image-2.1 -10%, MiniMax-H3 -9%) Nov 7, 2026
@danielhanchen
danielhanchen merged commit 682e73a into main Nov 7, 2026
@danielhanchen
danielhanchen deleted the studio-int8-gemm-sustained-tile-race branch November 7, 2026 16:37
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 |
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