Repository navigation
Studio diffusion: route untuned archs (sm86 / 90 / 100 / 121) onto the factored ConvRot kernel behind a probe and latency check - #14988
Conversation
On archs where diffusion_int8_gemm swaps the rotated Linears (sm80 / sm89 / sm120), the ConvRot path cost Qwen-Image-2.1 about 8% per step over plain int8: - Each rotated Linear ran the fused rotation + act quant on its own input, 7 kernels per block for 4 distinct inputs. Inductor does not merge identical opaque custom ops in an inference graph. shared_rotq_linears quantizes a shared input once (Q / K / V through the fingerprinted QKV helper, gate / value through a SwiGLU MLP forward). Same codes, outputs bit-identical to the separate calls. - The SwiGLU product silu(gate) * value went to HBM as bf16 and was read back by the down projection's rotation kernel; with ConvRot off Inductor fuses it into the quant. convrot_swiglu_act_quant computes the product inside the rotation kernel. - rotq_h16 rotates each 256-group as H16 . X . H16 (one bf16 tensor-core dot, one fp32 dot: FMAs, or tf32x3 on the datacenter parts) instead of a dense 256 x 256 GEMM. Probe-gated per device against the stock path (codes_close); closer to the exact rotation than the dense GEMM. UNSLOTH_DIFFUSION_INT8_ROTQUANT_H16=0 keeps the dense, bit-identical kernel; UNSLOTH_DIFFUSION_INT8_ROTQUANT_SWIGLU=0 the stock product.
…rch without a dense rotq tile Arches outside _ROTQ_CONFIG (sm86 RTX 30xx / A6000 / A10, sm87, sm90, sm100, sm103, sm121) ran the ConvRot rotation as a dense 256x256 bf16 GEMM plus a separate act quant. rotq_h16 needs no tile: it is probed there directly (codes_close) and kept only when, timed from CUDA-graph replays on the card, it is not slower than the rotation GEMM + act quant it replaces. The Qwen-Image-2.1 / Z-Image stock fusions in diffusion_int8_fused then rotate and quantize in one kernel. Kill switch UNSLOTH_DIFFUSION_INT8_ROTQUANT_UNLISTED=0.
|
@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: Didn't find any major issues. Hooray! 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_convrot_quant.py # studio/backend/core/inference/diffusion_int8_gemm.py # studio/backend/core/inference/diffusion_qwenimage21_fused.py # studio/backend/tests/test_diffusion_int8_gemm.py # studio/backend/tests/test_diffusion_qwenimage21_fused.py [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c5fc8d0b73
ℹ️ 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".
| if index in _UNLISTED_DEVICE: | ||
| return _UNLISTED_DEVICE[index] |
There was a problem hiding this comment.
Invalidate the cached verdict when the unlisted mode changes
When this switch is changed between model loads, the per-device cache bypasses the new mode: a device rejected in auto remains rejected after UNSLOTH_DIFFUSION_INT8_ROTQUANT_UNLISTED=1, so the documented force setting cannot enable it without restarting; conversely, a device first accepted under force remains enabled after returning to auto without ever running the latency check. Key the cached verdict by mode or clear/recompute it when the mode changes so the per-load switch semantics are honored.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, fixed in 72f8647. The unlisted verdict is now cached per (device, mode) instead of per device, so flipping UNSLOTH_DIFFUSION_INT8_ROTQUANT_UNLISTED between loads is honored without a restart:
- a card rejected under auto is enabled on the next load once the switch is set to 1
- a card first enabled under force goes through the latency check on the next auto load, and its
_H16_DEVICEentry is dropped if it loses
The _h16_probe result is cached once per device and the auto verdict (which holds the timing) once per (device, auto), so repeat loads in either mode do not re-probe or re-time. Listed archs never reach this path, so _ROTQ_DEVICE and the shared-QKV _swap_covers check are unchanged. Two new tests in test_diffusion_convrot_unlisted.py cover both switch directions and assert one probe and one timing across the loads; both fail on the previous head.
Switching UNSLOTH_DIFFUSION_INT8_ROTQUANT_UNLISTED between loads now takes effect: the verdict is cached per (device, mode), the probe once per device, and a card a forced load enabled is re-timed under auto. [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 72f8647b2a
ℹ️ 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".
| if _has_rotated(transformer): | ||
| for index in indices: | ||
| arm_rotated(index) |
There was a problem hiding this comment.
Preserve the unlisted verdict through the subsequent GEMM install
On the first resident Z-Image ConvRot load on an unlisted GPU that passes the latency check, this arming is undone before compilation. In diffusion_speed.py, install_int8_fused runs before install_int8_gemm; the latter's _swap_on calls rotquant_device_config, which finds no dense tile and unconditionally assigns _H16_DEVICE[index] = False. _RQ_DEVICES remains armed, but _rotq_run consequently falls back to the stock rotation and quantization instead of running rotq_h16. Unlike Qwen-Image-2.1, Z-Image has no subsequent arm_rotated call to restore the verdict. Preserve the independently probed unlisted verdict when initializing the dense route, or arm this path after the GEMM installation, so the optimization actually serves the first Z-Image load.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, confirmed. In diffusion_speed.py the fused MLP install runs arm_rotated, then install_int8_gemm calls _swap_on -> rotquant_device_config, which on an unlisted arch finds no dense tile and wrote _H16_DEVICE[index] = False. Qwen-Image-2.1 got the verdict back from its later arm_rotated, but Z-Image has no such call, so its first resident load ran the stock rotation.
Fixed in b8989da: when there is no dense tile, rotquant_device_config now keeps the _UNLISTED_PROBE verdict if unlisted_device_ok accepted the card for the current mode. Listed archs (dense tile found) are unchanged, and a card the latency check rejected still ends up False.
Added test_dense_route_init_keeps_the_unlisted_verdict, which replays the Z-Image order (arm_rotated, then rotquant_device_config) on a fake sm86 card, for both an accepted and a rejected latency result. It fails on 72f8647 and passes now. With diffusers 0.41 (which has the Qwen-Image-2.1 transformer, so those tests run instead of skipping), test_diffusion_qwenimage21_fused.py, test_diffusion_convrot_unlisted.py and test_diffusion_int8_fused.py pass together on a B200: 153 passed. That run also showed the unlisted tests' fixture kept a real-card h16 verdict from the earlier file, so it now starts each test with an empty table (6fa3c24).
rotquant_device_config finds no dense tile on an unlisted arch and reset _H16_DEVICE to False, undoing the verdict install_int8_fused armed one step earlier. Z-Image has no later arm_rotated call, so its first resident ConvRot load fell back to the stock rotation. Keep the probed verdict when the device passed unlisted_device_ok for the current mode. [skip ci]
|
@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". |
…chs behind a probe and latency check (unslothai#15654) Studio diffusion: run the fused-dequant int8 GEMM on untuned sm80+ archs behind a probe and latency check Follows unslothai#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 unslothai#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 unslothai#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 unslothai#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. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Builds on #14982 (merged), which added the
rotq_h16factored rotation kernel. This PR adds no kernel; it only routes the archs that kernel does not reach yet onto it, and composes with it unchanged.Problem
A user benchmark against ComfyUI measured Qwen-Image-2.1 (int8 ConvRot, 1024x1024, 25 steps) on an RTX 3090 at 0.570 s/step in Studio and 0.447 s/step in ComfyUI, 22% slower. With
UNSLOTH_DIFFUSION_INT8_CONVROT=0Studio ran 0.470 s/step, level with ComfyUI, so the cost is the ConvRot rotation. ComfyUI runs int8 ConvRot weights too, and its rotation costs nothing measurable.The fused rotation + act quant is only reached through
rotquant_device_config, which needs a_ROTQ_CONFIGtile: sm80, sm89 and sm120. Every other arch (sm86 RTX 30xx / A6000 / A10, sm87, sm90 H100, sm100 / sm103 B200 / B300, sm121) runs the Qwen-Image-2.1 / Z-Image stock fusions: the rotation as a dense 256x256 bf16 GEMM through cuBLAS, the rotated activation written to HBM, then a second act quant kernel. On GA102 (RTX 3090) bf16 with fp32 accumulate runs at 1/8 of the int8 rate, so that GEMM costs a large share of every int8 Linear.rotq_h16(the parent PR) needs no tile table, but it is still only probed behind a dense tile.Fix
diffusion_convrot_quant.unlisted_device_ok: on a CUDA sm80+ arch with no_ROTQ_CONFIGentry, proberotq_h16directly (_h16_probe,codes_close, the parent PR's tolerance), then run a latency check on the card.rotq_h16must not be slower than the two kernels it replaces (rotation GEMM, then Studio's probed act quant kernel). Both arms are timed from CUDA-graph replays on a 4096-wide and a 12288-wide activation, so host launch cost does not count. On success the card's second-stage mode goes into_H16_DEVICEand the existingconvrot_act_quant_int8op serves it. Otherwise the card keeps main's path.diffusion_int8_fused._rotated_act_quant: the rotated inputs of the Qwen-Image-2.1 stock fusions (shared QKV input,to_out, SwiGLU in and down) and the ConvRot SwiGLU call that op on an armed card, else the rotation GEMM + act quant as before. The int8 GEMMs stay cuBLAS_int_mmwith torchao's epilogue, as on main.arm_rotated: int8 fused finalize, layer-split relocation, Qwen-Image-2.1 install). Theq21_convrot_fusedload log says what was decided, with the latency numbers, for examplerotation fused into the act quant (rotq_h16: cuda:0 151 vs 417 us stock)orfused rotation kept off (...).UNSLOTH_DIFFUSION_INT8_ROTQUANT_UNLISTED=0keeps main's path (read on every load), and=1skips the latency check (the probe still runs).UNSLOTH_DIFFUSION_INT8_ROTQUANT_H16=0,UNSLOTH_DIFFUSION_INT8_ROTQUANT=0andUNSLOTH_DIFFUSION_INT8_GEMM=0turn it off too. The new switch joins the compile cache's graph switches.Results
End to end, Colab A100 standing in for an unlisted Ampere card
Colab A100-SXM4-40GB, torch 2.12.1+cu130, torchao 0.18.0. Qwen-Image-2.1 int8 ConvRot (Studio's default load), 1024x1024, cfg 1. s/step = (25-step render - 5-step render of the same prompt and seed) / 20, over 3 new prompts. Each arm is a fresh process with a fresh Studio home and inductor cache, run in the order base, head, head, base on one VM.
No unlisted card was available for an end-to-end run. Both arms therefore drop sm80 from the four tile tables in-process, so the A100 takes the route an RTX 3090 takes. Base is the parent PR (
studio-convrot-fused-cost), which on such a card runs the rotation GEMM + act quant. Head is this PR.rotq_h16callsrotation fused into the act quant (rotq_h16: cuda:0 130 vs 152 us stock).compiledandcuda_graphare on in every arm.B200 (local, real unlisted arch)
The latency check kept main's path on 4 of 4 runs:
rotq_h16(tf32x3) at 68 to 70 us against 35 to 36 us for stock, both from CUDA-graph replays. B200's cuBLAS runs the dense rotation close to bandwidth, so B200 renders run exactly what main runs. This was a shared card, so the numbers are ratios only.Tests
tests/test_diffusion_convrot_unlisted.py.arm_rotatedfollowing the gate._rotated_act_quantrunningrotq_h16once armed (4 shapes,codes_closeagainst the stock rotation + torchao quant), main's path on an unarmed card, the latency check repeating within 25% (graph replay, not host time), and afullgraph=True, dynamic=Truecompile with no graph break and one graph across three token counts, plus CUDA graph capture and replay equal to eager.diffusion_convrot_quanthas noINT8_ROTQUANT_UNLISTED_ENV.test_diffusion_int8_fused.pyandtest_diffusion_qwenimage21_fused.pyassert bit equality with the stock rotation, so their autouse fixtures pinUNSLOTH_DIFFUSION_INT8_ROTQUANT_UNLISTED=0.test_diffusion_int8_gemm.py,test_diffusion_convrot.py,test_diffusion_int8_fused.py,test_diffusion_int8_runtime_convrot.py,test_diffusion_qwenimage21_fused.py,test_diffusion_zimage_fused.py,test_diffusion_stream_int8_gemm.py,test_diffusion_te_int8_convrot.py,test_diffusion_compile_cache_variant.pyandtest_diffusion_layer_split_speed.pyon B200 (torch 2.14.1, Triton 3.8, torchao 0.18), the parent branch and this branch under the same GPU lease.test_diffusion_int8_gemm.py::test_no_compiled_variant_spills_to_local_memory. The forced fused GEMM spills on sm100 with Triton 3.8. It is not touched here.Not covered
rotq_h16is not faster than the rotation GEMM + act quant, it stays off and the card runs exactly what main runs.rotq_h16's first stage is a bf16 tensor-core dot. Studio's T4 diffusion route runs float32 activations anyway.diffusion_int8_gemm's own swap on an unlisted arch (MiniMax-H3) keep main's path, because that swap stays off on those archs. The parent PR's SwiGLU prologue and shared Q/K/V rotate + quant run on the swap path, so they do not reach unlisted archs either.