Repository navigation
Studio diffusion: run the fused-dequant int8 GEMM on untuned sm80+ archs behind a probe and latency check - #15654
Conversation
…chs behind a probe and latency check
…fused lock (hung loads where the GEMM stayed off)
|
@codex review |
Codex Review SummaryThis 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. |
Codex Review SummaryThis 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. |
|
Codex Review: Didn't find any major issues. Bravo. 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". |
# Conflicts: # studio/backend/core/inference/diffusion_int8_gemm.py
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 noint8_gemmline.diffusion_int8_gemm._ARCH_CONFIGlists only sm80, sm89 and sm120, andarch_config()returns None for every other arch unlessUNSLOTH_DIFFUSION_INT8_GEMM=1. So sm86 (RTX 30xx, A6000, A10), sm87, sm90 (H100) and sm121 run torchao's stock int8 mm: cuBLAS_int_mmwrites 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_CONFIGtile, run_FALLBACK_CONFIGthrough 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_mmon 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.rotq_h16: on an unlisted card the swap takes the ConvRot Linears only whenrotq_h16is 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_coversfollow this per-device decision (rotated_swapped) instead of the static arch table.(latency check: 1597 vs 1635 us cuBLAS)on the int8_gemm line,fused-dequant GEMM kept off on cuda:0 (latency check: ...), orrotated Linears keep the stock path on cuda:0 (rotq_h16 off).UNSLOTH_DIFFUSION_INT8_GEMM_UNLISTED=0keeps main's path (read on every load),=1skips the latency check (the probe still runs).UNSLOTH_DIFFUSION_INT8_GEMM=0turns it off and=1forces it as before. The new switch joins the compile cache's graph switches.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)
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.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):
UNSLOTH_DIFFUSION_INT8_GEMM_UNLISTED=0rotq_h16in IEEE mode (the sm86 / sm89 mode), which loses its own check on the A100 (156 vs 148 us)In each case the fused GEMM swaps 0 Linears and the card runs main's path.
Tests
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 takingdiffusion_int8_fused's lock.diffusion_int8_fused._finalizeasksrotated_ffwhile holding its lock, and an earlier version of the gate armedrotq_h16througharm_rotated, which takes the same lock. Every load whose fused GEMM stayed off hung. The test fails on that version and passes here.INT8_GEMM_UNLISTED_ENV).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.pyandtest_diffusion_int8_runtime_convrot.pyon 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
rotq_h16already 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 (noint8_gemmline). The per-step gain on a real 3090 is unmeasured.