Skip to content

Studio diffusion: run the fused-dequant int8 GEMM on untuned sm80+ archs behind a probe and latency check - #15654

Merged
danielhanchen merged 6 commits into
mainfrom
int8gemm-untuned-arch-probe
Oct 28, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
int8gemm-untuned-arch-probe

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Studio diffusion: run the fused-dequant int8 GEMM on untuned sm80+ archs behind a probe and latency check

Follows #14988 (merged), which did the same for the ConvRot rotate + quant kernel. This PR adds no kernel.

Problem

An external benchmark measured Qwen-Image-2.1 int8 ConvRot (1024x1024, 25 steps, cfg 1) on an RTX 3090 (sm86) at 0.499 s/step. On an RTX 6000 Ada (sm89) the load log reads int8_gemm: 224 int8 Linear(s) run the fused-dequant GEMM (bf16 out) on sm_89, ConvRot rotation fused into the act quant (H16 x H16 kernel), .... The 3090 log has no int8_gemm line.

diffusion_int8_gemm._ARCH_CONFIG lists only sm80, sm89 and sm120, and arch_config() returns None for every other arch unless UNSLOTH_DIFFUSION_INT8_GEMM=1. So sm86 (RTX 30xx, A6000, A10), sm87, sm90 (H100) and sm121 run torchao's stock int8 mm: cuBLAS _int_mm writes an int32 accumulator and the next compiled kernel reads it back for the epilogue.

Fix

  • unlisted_config(index): on a CUDA sm80+ arch with no _ARCH_CONFIG tile, run _FALLBACK_CONFIG through the existing bit-exact probe (once per device), then a latency check (once per device and mode). The check times the fused-dequant GEMM against cuBLAS _int_mm on the Qwen-Image-2.1 shapes (attention projection, MLP in, MLP down at 2048 rows), each arm captured in a CUDA graph and replayed, best of 7 interleaved rounds, as in Studio diffusion: route untuned archs (sm86 / 90 / 100 / 121) onto the factored ConvRot kernel behind a probe and latency check #14988. Both arms end in one read of their output (int32 for stock, bf16 for the fused kernel), standing in for the consumer that the stock epilogue fuses into. The kernel engages only when it is no slower.
  • _FALLBACK_CONFIG (128x128x64, 4 stages) needs 64 KB of shared memory for its stages, so it fits a 99 KB part (sm86 / sm89 / sm120). sm100 / sm103 are skipped (Triton int8 is slower than cuBLAS there, as the module already documents) unless forced.
  • Composition with rotq_h16: on an unlisted card the swap takes the ConvRot Linears only when rotq_h16 is also armed there (its own Studio diffusion: route untuned archs (sm86 / 90 / 100 / 121) onto the factored ConvRot kernel behind a probe and latency check #14988 probe + latency check). The card then runs what sm89 runs: fused-dequant GEMM, rotation fused into the act quant, Q / K / V and the SwiGLU gate / value sharing one rotation + quant. Otherwise the rotated Linears stay on main's Qwen-Image-2.1 stock fusions and only plain int8 Linears take the fused GEMM. diffusion_qwenimage21_fused.arch_ok / _swap_covers follow this per-device decision (rotated_swapped) instead of the static arch table.
  • The load log names the decision: (latency check: 1597 vs 1635 us cuBLAS) on the int8_gemm line, fused-dequant GEMM kept off on cuda:0 (latency check: ...), or rotated Linears keep the stock path on cuda:0 (rotq_h16 off).
  • Switches: UNSLOTH_DIFFUSION_INT8_GEMM_UNLISTED=0 keeps main's path (read on every load), =1 skips the latency check (the probe still runs). UNSLOTH_DIFFUSION_INT8_GEMM=0 turns it off and =1 forces it as before. The new switch joins the compile cache's graph switches.
  • sm80 / sm89 / sm120 are untouched.

Results

Colab A100-SXM4-40GB, torch 2.11.0+cu130, Triton 3.6.0, torchao 0.17.0. Qwen-Image-2.1 int8 ConvRot (Studio's default load), 1024x1024, cfg 1. s/step = (25-step render - 5-step render, same prompt and seed) / 20, over 3 prompts, after a warm-up render. Each arm is a fresh process with a fresh Studio home and inductor cache. No sm86 card was available, so every arm drops sm80 from the four tile tables in-process and the A100 takes the route an unlisted card takes, as #14988 did. Base is the merge base.

Resident (40 GB)

arm s/step (3 prompts) median fused GEMM / rotq_h16 calls (timed renders)
base 0.2418 / 0.2420 / 0.2425 0.2420 0 / 11520
head 0.2327 / 0.2332 / 0.2334 0.2332 20160 / 20160
base 0.2421 / 0.2421 / 0.2423 0.2421 0 / 11520
head 0.2329 / 0.2332 / 0.2333 0.2332 20160 / 20160
base 0.2420 / 0.2423 / 0.2425 0.2423 0 / 11520
head 0.2332 / 0.2334 / 0.2335 0.2334 20160 / 20160
  • Speed: head is 3.6% faster per step. The spread between base repeats is 0.12%.
  • Latency check: 1590 to 1602 us fused against 1628 to 1643 us cuBLAS over all 10 head loads in these runs; the verdict never flipped.
  • Compile and CUDA graphs: 3 compiled graphs before and after the timed renders on every arm, 0 graph breaks.
  • Images: each arm reproduces itself pixel for pixel. Head is bit-identical to main's own A100 path with sm80 kept (tuned tile, PSNR inf on all 3 prompts). Base vs head is 43.7 / 36.9 / 34.8 dB, the same numbers as main's tuned A100 path vs main's unlisted A100 path, so the difference is main's own path-to-path difference, not new error.

24 GB (the 3090's placement)

The card made to look like a 24 GB part (driver-side ballast, total capped at 24576 MiB) loads with offload=group, the text encoders streamed, as on the tester's 3090.

arm median s/step
base 0.2553 / 0.2557 / 0.2559 (3 arms)
head 0.2327 / 0.2325 / 0.2323 (3 arms)

Head is 9.0% faster per step; base A/A spread 0.2%. Head's images are bit-identical to the resident head arms.

When the check says no

Same VM, same prompts and seeds, against base (md5 per image):

arm median s/step images vs base
base 0.2413, 0.2416 -
GEMM latency check forced to "slower" 0.2414 identical (3/3)
UNSLOTH_DIFFUSION_INT8_GEMM_UNLISTED=0 0.2416 identical (3/3)
rotq_h16 in IEEE mode (the sm86 / sm89 mode), which loses its own check on the A100 (156 vs 148 us) 0.2478 vs main 0.2479 in the same mode identical to main in that mode (3/3)

In each case the fused GEMM swaps 0 Linears and the card runs main's path.

Tests

  • New tests/test_diffusion_int8_gemm_unlisted.py (40 tests, CPU, capability / probe / timer faked): the arch table (exactly one route per sm80+ arch), the fallback tile fitting 99 KB, both kill switches, the latency check (faster, tie, slower, raising), force skipping the check but not the probe, a failed probe never timing, the per-device cache and the switch acting between loads, listed / skipped / pre-sm80 archs never timing, a failed launch sticking, the rotated-Linear swap needing both kernels (and the Qwen-Image-2.1 gates following it), the swap census for each case, the deferred install, the compile cache key, and the gate never taking diffusion_int8_fused's lock.
  • That last test covers a hang found in the cloud run: diffusion_int8_fused._finalize asks rotated_ff while holding its lock, and an earlier version of the gate armed rotq_h16 through arm_rotated, which takes the same lock. Every load whose fused GEMM stayed off hung. The test fails on that version and passes here.
  • On the merge base the new file fails at import (INT8_GEMM_UNLISTED_ENV).
  • Ran it plus test_diffusion_int8_gemm.py, test_diffusion_convrot_unlisted.py, test_diffusion_qwenimage21_fused.py, test_diffusion_int8_fused.py, test_diffusion_stream_int8_gemm.py, test_diffusion_compile_cache_variant.py, test_diffusion_convrot.py, test_diffusion_zimage_fused.py and test_diffusion_int8_runtime_convrot.py on CPU. Base: 275 passed, 1 failed (plus the new file erroring at import). Head: 317 passed, 1 failed. The one failure is the same test on both, test_diffusion_zimage_fused.py::test_model_unload_restores_the_process_global_patches, not touched here.

Not covered

  • sm86 was not measured on this branch: no sm86 card was available. On the tester's current-main 3090 logs, rotq_h16 already arms (rotation fused into the act quant (rotq_h16: cuda:0 151 vs 293 us stock), and 138 vs 273 us on a second load), so rotated Linears there meet the condition for this PR's fused GEMM; only the GEMM itself was missing (no int8_gemm line). The per-step gain on a real 3090 is unmeasured.
  • H100 (sm90), sm87 and sm121 were not measured: Colab had no H100 for this account during this work. They get the same probe + latency check.
  • The latency margin on the A100 stand-in is about 2.5%. A card where the two arms sit closer could pick either path between processes; both are correct, only the speed differs.
  • Linux / NVIDIA only. ROCm, CPU and Mac never reach this path.

@danielhanchen danielhanchen self-assigned this Oct 28, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-28T04:49:39.657974Z 60ed3a3 PR opened
ℹ️ 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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T04:50:45.534569Z 60ed3a3 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 60ed3a39f6

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

# Conflicts:
#	studio/backend/core/inference/diffusion_int8_gemm.py
@danielhanchen
danielhanchen merged commit 36bd1af into main Oct 28, 2026
2 of 35 checks passed
@danielhanchen
danielhanchen deleted the int8gemm-untuned-arch-probe branch October 28, 2026 11:11
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