Repository navigation
Studio: run the bit-identical int8 GEMM routes on uncompiled int8 denoisers - #17617
Conversation
…oisers torchao's eager int8 Linear is cuBLAS _int_mm plus four float32 passes over the [rows, N] output. On sm_120 cuBLASLt picks a forward-compat sm_80 WMMA int8 kernel, so an eager int8 ConvRot Linear costs about 3x the bf16 one (MiniMax-H3 at 640x384x124: 3.3 vs 1.56 s/step). Compiled loads already swap in Studio's fused-dequant GEMM; speed off / eager loads never did. install_eager swaps the same GEMM onto an uncompiled resident denoiser, limited to the routes that are bit-identical to torchao's eager Linear: the fused-dequant GEMM and the dense rotq_i8 rotation + act quant (never rotq_h16), no SwiGLU fusion. apply_speed_optims runs it on speed off and whenever the denoiser stays uncompiled. UNSLOTH_DIFFUSION_INT8_GEMM_EAGER=0 keeps torchao.
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. |
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. 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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ 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". |
|
Codex Review: Didn't find any major issues. 👍 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". |
…rs (#17658) Stacked on #17617 (base branch `h3-int8-sm120`). Retarget to `main` once that merges. ## Summary #17617 moved uncompiled, resident int8 denoisers onto Studio's bit-identical int8 GEMM routes. A denoiser that streams block by block still ran torchao's eager int8 Linear. That covers MiniMax-H3 INT8 at speed `off` on L4, A100-40 and any card where the other components need the room. This PR gives those streamed denoisers the same routes in call-time mode. MiniMax-H3 INT8-ConvRot, speed `off`, streamed, 640x384, 124 frames, 20 steps: - G4: 2.06 -> 1.14 s/step, the same speed as the resident tier (1.13). - L4: 12.26 -> 9.16 s/step (-25%). - Frames are byte-identical to the base on both cards (sha256). ## Root cause `apply_speed_optims` runs `_install_eager_int8_gemm` (#17617) before placement, and that function skips any denoiser that will move (`moved = offload_active if denoiser_offloaded is None else denoiser_offloaded`). The compiled streamed install (`video.py`, `if denoiser_streamed and "compiled" in speed_optims`) covers only the compiled tier. So a streamed H3 denoiser on speed `off` / `eager`, or after a declined compile, kept torchao's Linear for all 300 int8 calls per step: a float32 epilogue, an eager rotation and an eager act quant. The generic video placement (`apply_memory_plan`, with the event-fenced prefetch from `diffusion_memory`) has the same gap for any other family that streams int8 Linears. ## Change - `diffusion_int8_gemm.install_streamed_eager(transformer, device)` is `install(..., device=device, exact=True)`. These are `install_eager`'s routes: the fused-dequant GEMM, the dense `rotq_i8` rotation + act quant (never `rotq_h16`), and no SwiGLU fusion. They run in the existing call-time mode: each call checks that the Linear's own weight is still the installed Parameter and sits on the activation's card. Otherwise the call takes torchao's path. - `diffusion_speed.install_streamed_eager_int8_gemm(pipe, device)` runs after placement. It engages only on a denoiser that: - is driven by the event-fenced prefetcher (`module_prefetcher`). That prefetcher copies into the same Parameters (`swap_tensors` / inner-tensor re-pointing), so the identity check sees every onload; - is not compiled (`compiled_denoiser`), not already swapped, and has int8 candidates. - `video.py` calls it in two places: - in the MiniMax-H3 branch, for a streamed denoiser the compile did not take; - in the generic video placement, right after `compile_pipe_below_offload_hooks`, for every denoiser view. Status reports `int8_gemm_streamed`, and `int8_gemm_live` drops it when nothing swapped. - `shared_rotq_linears` (shared Q/K/V act quant) now passes `exact` when every Linear in the group is on the eager routes. The shared quant is then the same dense kernel each Linear runs on its own. Compiled installs never set the mark, so they are unchanged. Switches: - `UNSLOTH_DIFFUSION_INT8_GEMM_STREAMED_EAGER=0` keeps torchao's path on streamed denoisers. - `UNSLOTH_DIFFUSION_INT8_GEMM_EAGER=0`, `UNSLOTH_DIFFUSION_INT8_GEMM_STREAMED=0` and `UNSLOTH_DIFFUSION_INT8_GEMM=0` also turn it off. ## Results MiniMax-H3 fl2va, hosted INT8-ConvRot (text encoder int8), 640x384, 124 frames, 20 steps, guidance 1.0, torch 2.14.1+cu130, torchao 0.18.0, Triton 3.8.0. Setup: - Base is this PR's parent with #17618 and #17613 merged in, and it is repeated (A/A). The base already includes #17613's TN-layout `_int_mm`, so it is faster than the 3.31 s/step #17617 started from. - One Colab session per card. Each arm runs in a fresh process. - s/step is the median over steady denoiser forwards. A `cuda.synchronize` at each forward boundary applies to every arm alike. - s/clip is the `generate` wall time, which includes the encode and the decode. G4 (RTX PRO 6000 Blackwell), streaming forced with the VRAM budget, 2 clips: | placement | arm | s/step | s/clip (clip 1, clip 2) | peak VRAM alloc GiB | frames vs base | |---|---|---:|---|---:|---| | fully streamed (budget 30%, `UNSLOTH_H3_DIT_RESIDENT=0`) | base | 2.056 | 45.9, 43.9 | 6.37 | - | | | base repeat | 2.057 | 45.7, 43.9 | 6.37 | identical | | | this PR | **1.140** | 28.6, 25.6 | 6.38 | identical | | | this PR, switch off | 2.057 | 45.2, 43.9 | 6.37 | identical | | part resident (budget 20%, 27 of 50 blocks resident) | base | 2.049 | 45.7, 43.7 | 16.95 | - | | | base repeat | 2.050 | 45.7, 43.7 | 16.95 | identical | | | this PR | **1.137** | 26.7, 25.5 | 16.95 | identical | | budget 30% (per-request refit holds all 50 blocks) | base | 2.037 | 45.9, 43.5 | 26.60 | - | | | base repeat | 2.039 | 45.2, 43.5 | 26.60 | identical | | | this PR | **1.126** | 27.3, 25.3 | 26.60 | identical | | | this PR, switch off | 2.039 | 45.2, 43.5 | 26.60 | identical | | resident (no budget, #17617's eager install) | this PR | 1.128 | 25.4, 25.3 | 25.32 | identical | | speed default (compiled), budget 30% | base / this PR | 0.891 / 0.891 | 28.9, 19.8 / 22.9, 19.8 | 25.13 / 25.13 | identical | | speed default (compiled), fully streamed | this PR | 0.895 | 28.6, 20.0 | 4.91 | - | L4, 1 clip (it streams on its own: 9 blocks resident at load, 37 of 50 after the per-request refit): | arm | s/step | s/clip | peak VRAM alloc GiB | frames vs base | |---|---:|---:|---:|---| | base, speed off | 12.261 | 298.7 | 20.31 | - | | base repeat | 12.264 | 288.0 | 20.31 | identical | | this PR, speed off | **9.156** | 225.7 | 20.31 | identical | | base, speed default (compiled) | 6.345 | 189.2 | 19.30 | - | | this PR, speed default (compiled) | 6.359 | 176.3 | 19.30 | identical | Host RSS peaks were 96-105 GiB on G4 and 46.4-46.7 GiB on L4. The spread was the same between the base and its repeat, with no shift this PR adds. What runs per denoiser forward (torch.profiler, two steady forwards, per forward): | card, placement | arm | int8 Linear calls | route | int8 GEMM kernel | H2D copy (overlapped) | |---|---|---:|---|---|---| | G4, fully streamed | base | 300 | torchao: eager rotation + quant, cuBLAS `i16832gemm_s8 tn`, float32 epilogue | 451 ms | 344 ms (330 ms) | | G4, fully streamed | this PR | 300, 0 fallbacks | `rotq_i8` + `i8mm_dq_tma` | 431 ms | 344 ms (333 ms) | | L4, streamed | base | 300 | torchao | 2555 ms | 414 ms (394 ms) | | L4, streamed | this PR | 300, 0 fallbacks | `rotq_i8` + cuBLAS TN + torchao's epilogue op for op (the Triton GEMM stays off on sm_89) | 2699 ms | 412 ms (392 ms) | The prefetcher already hides the copies (14 ms exposed per forward on G4, 20 ms on L4), so the streamed tier was compute-bound on torchao's eager Linear. On G4 the base's three largest elementwise kernels (the float32 / bf16 epilogue passes and the bf16 copy, about 490 ms per forward) are gone, and the eager rotation + quant becomes one `rotq_i8` launch per Linear. With them gone, the streamed tier runs at the resident tier's speed. ## Tests - New CPU tests: - both kill switches, and `install_streamed_eager` threading `device` + `exact` through `install`; the streamed and global switches also stop it; - `install_streamed_eager_int8_gemm` engages behind the prefetcher and skips: - a denoiser with no prefetcher; - a compiled denoiser; - a dense denoiser; - an already swapped denoiser; - a call with no device; - it swallows an install failure; - `int8_gemm_live` drops the new key; - the shared Q/K/V quant is exact only when every Linear is; - an AST check that `video.py` calls it in the H3 streamed-uncompiled branch and after the generic offload hooks. - New GPU test: a ConvRot int8 Linear (torchao v1 and v2 configs) is installed while its weight is on the host. After a `swap_tensors` onload (what the prefetcher does) it takes the eager routes, bit-identical to torchao's Linear, with no `rotq_h16`. - `test_diffusion_int8_gemm.py` + `test_diffusion_stream_int8_gemm.py` with CUDA tests on (`-o addopts=`): - G4: 188 passed (base 181). - L4: 187 passed, 1 failed (base 180 passed, 1 failed). The L4 failure, `test_no_compiled_variant_spills_to_local_memory`, fails the same way on the base (Triton 3.8 spills on sm_89). - The commit applies cleanly on #17617 alone, and `test_diffusion_int8_gemm.py` + `test_diffusion_speed.py` pass there (308 passed). ## Not covered - Other video families that stream int8 Linears through the generic placement (Wan, LTX, HunyuanVideo-1.5) are covered by the CPU wiring tests only. No GPU render was run for them. - A100-40 was not run. On sm_80 the route is the same as on L4: the dense rotation kernel plus cuBLAS TN, because the Triton GEMM stays off on sm_8x with Triton 3.8. - Streamers that swap in new Parameters per call (H3-Image's pageable `ModuleListStreamer`) are excluded by the prefetcher gate. Diffusers' own onload without the prefetcher is excluded too. - The shared Q/K/V quant change affects the resident `eager` speed tier with strided attention, which now takes the dense kernel instead of `rotq_h16`. That tier was not timed here. - ROCm, MPS and Windows were not run. The install refuses ROCm and non-CUDA devices.
…ded cuDNN admits only the probed dtype, fused int8 takes plain SYMMETRIC only, `_int_mm` fallback stays on the operand's card (unslothai#18140) Fixes from a re-check of recent Studio INT8 kernel PRs. 1. **CFG replicas lose the INT8 sink mode (unslothai#18004, route from unslothai#17758).** `Int8Route.__reduce__` (`studio/backend/core/inference/diffusion_int8_attention_dispatch.py`) rebuilt the route from its constructor arguments only, so `copy.deepcopy` (Studio's CFG-parallel replica builder), `copy.copy` and pickle reset the sink kernel mode chosen at install to 0. With CFG split across two cards, the replica ran the plain INT8 path while the primary ran the sink path, so the guided output depended on the card count. The reduce tuple now carries the mode as state. 2. **Qwen-Image-2.1 strided cuDNN (unslothai#18083).** The `StridedCudnn` marker kept only the probed card ids, so a bf16 probe also sent fp16 calls (an autocast, or a later cast of the pipe) down the strided path. The marker now carries the probed dtype, `plain_qkv` declines other dtypes, and a re-install at a new dtype probes that dtype. The INT8 dispatch race named the strided incumbent `dispatch:_native_cudnn`, the same key as the contiguous cuDNN call, so cached verdicts crossed over; it now races as `dispatch:_native_cudnn:strided`. The GPU no-copy check asserted `flatten(2, 3).is_contiguous()`, which also holds when flatten copies; it now checks the flatten shares storage. 3. **Fused act quant and exact ConvRot take plain SYMMETRIC weights only (unslothai#17114, unslothai#17617).** `_plain_int8_weight` accepted any mapping containing SYMMETRIC, so a torchao weight with `SYMMETRIC_NO_CLIPPING_ERR` activation quant reached kernels that always divide amax by 127.5. Studio's own loaders build plain SYMMETRIC only, so this narrows a path no Studio load takes. 4. **`_int_mm` off-grid fallback returns on the operand's card (unslothai#17613, inherited from torchao).** For K or N not a multiple of 8 the safe `_int_mm` wrapper (Studio `diffusion_torchao_patches.py` and the copy in `unsloth/import_fixes.py`) ran the matmul on the CPU and moved the result with `.to(input.device.type)`, which is the current card. With the operands on cuda:1 and cuda:0 current, the int32 result landed on cuda:0. It now moves back to `input.device`. 5. **FLUX.3 strided attention: a test for the no-copy claim (unslothai#17180).** No test checked it: restoring diffusers' `.contiguous()` copies passed every test. The new test spies on `scaled_dot_product_attention` and asserts Q / K / V arrive as the permuted views. Test only. ### GPU results, base (main 62d66cb) vs this branch CFG sink mode, Kaggle 2x T4 (sm75), torch 2.11.0+cu130, diffusers 0.42.1. Tiny FLUX.1 transformer with random weights at the real head dim (128), fp16, one attention-sink key; Studio's `CfgParallelRouter` with the replica built by `build_replica` on cuda:1; INT8 attention installed with the sink kernels (mode 2, NVRTC CUDA kernel) on both cards; guided 3-step render, cond on cuda:0 and uncond on the replica. | | base | this branch | |---|---|---| | sink mode, primary (cuda:0) | 2, 2 | 2, 2 | | sink mode, replica (cuda:1) | **0, 0** | **2, 2** | | kernel path on cuda:1 | plain x4 | sink x4 | | final latent, CFG split vs single card | rel L2 4.82e-4, max abs 3.9e-3 | bitwise equal | | uncond step 2, CFG split vs native attention | rel L2 6.43e-4 (single card 6.11e-4) | 6.11e-4, same as single card | `_int_mm` fallback, same Kaggle 2x T4: int8 operands on cuda:1, cuda:0 current, both copies of `_make_safe_int_mm` with the real `out_dtype`. | case (Studio and core copies alike) | base | this branch | |---|---|---| | K=65 (off grid) | result on cuda:0 (values exact); the next op with a cuda:1 tensor raises "Expected all tensors to be on the same device" | result on cuda:1, exact | | N=36 (off grid) | result on cuda:0, same error | result on cuda:1, exact | | K=64, N=32 (cuBLAS, control) | cuda:1 | cuda:1 | L4 (sm89), Colab, torch 2.12.1+cu130, cuDNN 9.20.0, torchao 0.17.0: | fix | check | base | this branch | |---|---|---|---| | strided cuDNN | tiny QwenImage21, bf16 probe at install, then the pipe cast to fp16 | fp16 call takes the strided path (marker has no dtype) | fp16 call goes to diffusers; marker records bf16 | | strided cuDNN | fp16 output vs diffusers cuDNN | equal (on L4 the fp16 strided call happens to be correct; the defect is the admission) | equal | | plain SYMMETRIC | Int8Tensor FeedForward 256 to 1024, `SYMMETRIC_NO_CLIPPING_ERR` weights | fused, identical to the SYMMETRIC result, differs from torchao in 106941 of 153600 elements (max abs 0.086) | stays on torchao, bitwise equal | | plain SYMMETRIC | plain SYMMETRIC weights | fused, bitwise equal to torchao | same, same hash as base | | no-copy test | real cuDNN, bf16, head dim 128 | SDPA gets the views | SDPA gets the views; with the copies restored it gets contiguous tensors and the new test fails | ### Tests - `studio/backend/tests/test_diffusion_int8_attention_dispatch.py::test_route_copies_keep_the_sink_mode[1|2]`: fails on main. - On the T4 pair: `test_diffusion_int8_attention_dispatch.py` base 2 failed (`test_route_copies_keep_the_sink_mode[1|2]`) / 46 passed, this branch 48 passed with the two-card tests included. - `studio/backend/tests/test_torchao_int_mm_transposed.py::test_off_grid_fallback_returns_on_the_operand_card`: fails on main; on the T4 pair base 1 failed / 9 passed, this branch 10 passed (including the CUDA transposed `_int_mm` exactness cases). - `studio/backend/tests/test_diffusion_qwenimage21_strided_attn.py`: `test_marker_admits_only_its_probed_dtype`, `test_reinstall_at_a_new_dtype_probes_it`, `test_strided_incumbent_races_under_its_own_name` fail on main; on the L4 base 3 failed / 14 passed, this branch 17 passed, and the GPU bitwise test ran in both arms. - `studio/backend/tests/test_diffusion_int8_fused.py::test_only_plain_symmetric_activation_quant_is_eligible[SYMMETRIC_NO_CLIPPING_ERR-False]`: fails on main; on the L4 this branch 101 passed / 1 skipped (a torchao >= 0.18 case). - `studio/backend/tests/test_diffusion_flux3_strided_attn.py::test_strided_processor_hands_sdpa_the_projection_views`: passes on main, fails with the copies restored. - CPU (torch 2.14.1) on this branch: the five files pass (100 passed, 78 skipped, running the Qwen-Image-2.1 file in its own process). Running the Qwen-Image-2.1 file in one pytest process with the others, `test_real_processor_decode_call_takes_the_strided_path` hits a RecursionError on main and on this branch alike (test-order interaction, not touched here). ### Not covered - No real checkpoint or decoded image: the CFG check compares DiT latents of a tiny random model, and the auto (raced) route was not exercised (the dropped field is the same for any non-zero mode). - sm80 and sm120 cards; the strided cuDNN and SYMMETRIC fixes ran on L4 only, the CFG and `_int_mm` checks on T4 only. - torchao 0.18 for the SYMMETRIC narrowing. - The GPU runs used main 62d66cb; the branch was then rebased onto main 142c398 and only the CPU tests were rerun there.
Summary
MiniMax-H3 video with the hosted INT8 ConvRot denoiser ran at 3.31 s/step on an RTX PRO 6000 Blackwell (sm_120) with
speed_modeoff, against 1.55 s/step for bf16. The int8 path should be faster than bf16, and it is once it runs on the right GEMM. With this change the same load runs at 1.13 s/step and produces bit-identical frames.Root cause
Compiled loads swap every torchao int8 Linear onto Studio's fused-dequant int8 GEMM (
diffusion_int8_gemm.install). Uncompiled loads never did, and H3 stays eager on speedoffandeager. Those loads used torchao's own eager int8 Linear, which has two problems:Int8Tensor's linear passesqdata.contiguous().t()tosafe_int_mm, andsafe_int_mmmakes that view contiguous again on every call. The result is a full transposed copy of the int8 weight, then annnlayout int8 GEMM. cuBLASLt only serves that layout with slow kernels: on sm_120 a forward-compat sm_80 WMMA kernel (cutlass_80_wmma_tensorop_i161616gemm_s8_forwardCompat_..._nn_align4), on sm_80 / sm_89 the legacyIgemmTraitskernel.[rows, N]output.On H3's shapes at 640x384x124 (about 30k rows), one int8 Linear costs about 3x a bf16 Linear on G4. On A100 and L4 it costs about 5x.
Kernel time per denoiser forward on G4, from torch.profiler over 2 steady forwards:
i8mm_dq_tma)rotq_i8)"Other elementwise / copies" covers the whole forward. The base arm's extra ~780 ms over bf16 is the per-call weight transpose copy and the float32 epilogue.
Change
diffusion_int8_gemm.install_eagerruns the same swap on a denoiser that stays uncompiled. It is limited to the routes that are bit-identical to torchao's eager Linear:rotq_i8rotation + act quant kernel, also probed bit-exact. It never takesrotq_h16, which is within tolerance but not exact;apply_speed_optimscalls it on speedoffand whenever the denoiser ends up uncompiled (eager tier, or a declined compile). A moved (offloaded / streamed) denoiser keeps torchao's path, the same rule the compiled install follows. A dense denoiser never imports the kernels. Status reports the lever asint8_gemm_eager, and only when it engages, so loads without int8 Linears report exactly what they did before.Switches:
UNSLOTH_DIFFUSION_INT8_GEMM_EAGER=0keeps torchao's eager path.UNSLOTH_DIFFUSION_INT8_GEMM=0still turns every int8 GEMM swap off.Compiled loads are untouched: the compiled install and its forward take the same branches as before.
Results
MiniMax-H3 fl2va, hosted INT8 ConvRot, 640x384, 124 frames, 20 steps, 2 clips, Colab G4 (RTX PRO 6000 Blackwell), torch 2.14.1+cu130, torchao 0.18. All arms ran in one session. The base arm is this PR's merge base and was repeated (A/A).
No regression elsewhere. G4, Studio defaults, base vs this PR vs a base repeat, 1024x1024, 2 prompts:
Layer level, H3 shapes, eager. Here "stock" is torchao's Linear and "eager swap" is this PR. All outputs are bit-identical to stock:
On sm_8x with Triton 3.8 the Triton GEMM stays off as before. There the eager swap runs the fused rotation + act quant and cuBLAS in the TN layout with torchao's epilogue, op for op.
Tests
install_eagerkill switch, andexactthreaded throughinstall;rotq_h16and falls back on a failed launch;apply_speed_optimsengages on speed off and on a declined compile, and skips a moved denoiser, a dense denoiser and a compiled one;int8_gemm_livedrops the key when nothing swapped.install_eageron a ConvRot torchao Linear (v1 and v2) is bit-identical, withrotq_h16armed on the card.test_diffusion_int8_gemm.py+test_diffusion_speed.pywith CUDA tests enabled (-o addopts=): G4 406 passed; L4 405 passed, 1 failed. The L4 failure,test_no_compiled_variant_spills_to_local_memory(Triton 3.8 spills on sm_89 with the forced L4 tile), fails the same way on main.test_diffusion_int8_gemm.py,test_diffusion_speed.py, the convrot, int8 runtime, autoopt core / parity and stream int8 GEMM suites pass.Not covered