Repository navigation
Studio diffusion: run an eager native int8 W8A8 linear as one compiled function (bit-identical, 1.33x streamed) - #15183
Conversation
… on CUDA A streamed or offloaded DiT on the torchao-free W8A8 path stays eager when speed is off, and each linear then runs about ten eager kernels (rotation, row absmax, scale, quantise, torch._int_mm, rescale) with fp32 temporaries. On CUDA the eager forward now calls one torch.compile'd function of plain tensors (dynamic shapes, two graphs: with and without bias), so the offload hooks moving the buffers never invalidate it and new prompt lengths do not recompile. The scale's reciprocal is taken in fp64 so the int8 codes, and the images, are bit-identical to the eager kernels. A compiled block still traces the eager body; ROCm, no-Triton hosts and CUDA graph capture keep the eager kernels; a failed compile falls back once. UNSLOTH_NATIVE_INT8_FUSED=0 turns it 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. |
|
The |
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! 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". |
…eager peak, honor force-eager (#15456) Two regressions from #15183 in `studio/backend/core/inference/diffusion_native_quant.py`. **Peak memory above eager.** The compiled W8A8 linear used a chunk budget of `1 << 27` elements, 4x the eager `1 << 25`, on the reasoning that it only holds the int32 accumulator. It also holds the output chunk at the same time, and the chunk outputs were then joined with `torch.cat`, so a large linear peaked above the eager path. For an 8192-row bf16 linear 3072 -> 14336 (Wan 2.2 5B sized) with rotation 256: 558 MiB before #15183, 672 MiB on main, so it runs out of memory with 650 MiB of headroom where the eager path fits. Now the fused budget equals the eager one and the chunks are written into one preallocated output with `copy_` (no list of parts plus a `torch.cat` copy). **Force-eager bypass.** `_forward_fused` checked Dynamo tracing and CUDA graph capture but not the shared `eager_forced()` flag from `diffusion_bg_compile`, so a cold native int8 linear inside the Detailer's `auxiliary_pass()` compiled a graph despite the #15063 contract. It now returns to the eager kernels while the flag is set. The same check also applies while a background compile is running, so those generations run the int8 linears eagerly for that window. Per-linear peak (MiB) and time (ms), shared card, 20-iteration means, so timings are indicative only: | shape (rows, in -> out), rotation | before #15183 | main | this PR | before ms | main ms | this PR ms | |---|---:|---:|---:|---:|---:|---:| | 8192, 3072 -> 14336, 256 | 558.06 | 672.02 | 392.00 | 1.58 | 0.58 | 0.74 | | 8192, 3072 -> 14336, 0 | 510.06 | 672.02 | 392.00 | 1.57 | 0.58 | 0.73 | | 8192, 14336 -> 3072, 256 | 400.01 | 336.02 | 132.00 | 1.80 | 0.55 | 0.67 | | 20000, 3072 -> 14336, 256, no bias | 1210.94 | 1093.75 | 729.23 | 3.76 | 1.56 | 1.70 | | 4096, 3072 -> 3072, 256 | 180.03 | 72.01 | 72.01 | 0.26 | 0.15 | 0.15 | Outputs are bit-identical across all arms on every shape. Inside `auxiliary_pass()`: main makes 1 fused call and 1 compiled graph, this PR makes 0 and 0, and the layer goes back to the fused path after the context exits. Tests in `test_diffusion_native_quant_fused.py`: chunks fill one output in row order, the fused peak never exceeds eager across widths, no fused call under `auxiliary_pass()` / `force_eager()`, and a CUDA test for peak at or below eager plus bit-equality. The CUDA skip guards use `has_real_cuda()`. Against main's module the new CPU tests give 5 failed, with this PR 21 passed, 4 skipped on CPU and all 25 passed on a GPU. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
…en-Image-2.1 int8 21% faster per step on gfx1151, now ahead of bf16) (unslothai#16825) ## What Studio's native int8 W8A8 linear (`transformer_quant=int8` with int8 activations, the path AMD GPUs use since torchao's dense schemes are NVIDIA-only) now runs as one compiled function on ROCm RDNA3 / RDNA3.5 / RDNA4 (gfx11xx / gfx12xx), as it already does on CUDA. Other ROCm archs keep the eager kernels. ## Why unslothai#15183 added the compiled `_w8a8_rows` and kept it off on ROCm as a precaution ("stays off on ROCm and on hosts without Triton"); no ROCm failure was recorded. On AMD the eager path is slower than bf16: at 4096 rows, 3072 -> 3072 on a Strix Halo (Radeon 8060S, gfx1151) it takes 4.97 ms against 2.85 ms for bf16 with bias, split into per-token quantisation 1.82 ms, `_int_mm` 2.19 ms, scale / bias epilogue 1.37 ms and the ConvRot rotation 0.42 ms. The int8 GEMM itself is fine (hipBLASLt int8 is about 1.27x bf16); the separate quantise and epilogue passes eat the gain. Measured on the gfx1151 runner with torch 2.11.0+rocm7.13.0 (what Studio installs there) and torch 2.14.1+rocm7.2: - `torch.compile(_w8a8_rows, dynamic=True, fullgraph=True)` compiles in 0.1-2 s and its output is bit-identical to the eager kernels, with and without ConvRot256. - Against bf16 + bias per linear: 1.05x at 4096 x 3072 -> 3072, 1.23-1.25x at 3072 -> 12288, 1.31-1.34x at 12288 -> 3072 (1.16x at 8192 rows for 3072 -> 12288). With the rotation, 3072 -> 3072 is level with bf16. - The old inductor `_alloc_from_pool` failure on gfx1151 did not reproduce on either torch. Runs: https://github.com/oobabooga/unsloth/actions/runs/41989449486 and https://github.com/oobabooga/unsloth/actions/runs/42001287645. End-to-end A/B on the same gfx1151 runner, base = main `f7e499a349`, head = this branch, same seeds, `transformer_quant=int8` (+ `UNSLOTH_NATIVE_INT8_ACT=1` for W8A8). Run: https://github.com/oobabooga/unsloth/actions/runs/42051745713 (job "Native int8 W8A8 on ROCm"). | model | arm | base s/step | head s/step | change | steady s/image base -> head | LPIPS vs bf16 | |---|---|---|---|---|---|---| | Qwen-Image-2.1 (25 steps) | int8 W8A8 | 4.533 | 3.596 | -20.7% | 115.3 -> 92.0 | 0.005 | | Qwen-Image-2.1 | bf16 (reference) | 4.482 | 4.254 | noise | 110.7 -> 106.0 | - | | Qwen-Image-2.1 | int8 weight-only | 4.590 | 4.598 | 0% | 117.0 -> 117.5 | 0.021 | | FLUX.2-klein-4B | int8 W8A8 | 2.202 | 1.742 | -20.9% | 9.96 -> 8.06 | 0.054 | | Z-Image-Turbo | int8 W8A8 | 3.757 | 2.918 | -22.3% | 31.2 -> 24.5 | 0.073 | On Qwen-Image-2.1, int8 W8A8 on this branch is now faster than bf16 (3.60 vs 4.25 s/step, 92 vs 106 s per 1024x1024 image), where on main it was slower. Head logs `diffusion.native_quant: int8 W8A8 linear runs as one compiled function` in every W8A8 cell; weight-only int8 and bf16 cells are unchanged (pixel-identical renders base vs head). The smaller FLUX.2-klein and Z-Image DiTs gain the same ~21% on their int8 W8A8 path but stay behind their compiled bf16 runs. ## How - `_fused_w8a8()`: the ROCm refusal now applies only when the device arch is not gfx11xx / gfx12xx (read with the existing `rocm_bf16._device_gfx_arch`). The reason is recorded as before (`ROCm gfx942`, `ROCm unknown arch`), the `UNSLOTH_NATIVE_INT8_FUSED=0` kill switch still wins, and a compile or runtime failure still falls back to eager once and stays there. - One info line says which way it went: `int8 W8A8 linear runs as one compiled function` or `int8 W8A8 linear stays eager (<reason>)`. ## Tests `studio/backend/tests/test_diffusion_native_quant_fused.py`: - gfx1151, gfx1100, gfx1103 and gfx1201 get the compiled function and its in-place chunk variant - gfx942, gfx90a, gfx1030, gfx906 and an unreadable arch stay eager with the reason recorded - the kill switch wins on RDNA - the existing ROCm test now asserts the unknown-arch refusal (this host has no ROCm device) `test_diffusion_native_quant_fused.py` + `test_diffusion_native_quant.py`: 185 passed, 9 skipped (CUDA-only cases) on CPU. ## Risk - CDNA and RDNA2 behave as before. - Only callers that already chose int8 activations are affected; bf16 and weight-only int8 loads do not touch this path.
Summary
A streamed or offloaded denoiser on the torchao-free native int8 W8A8 path runs eager. Examples:
speed_mode=off.Each linear then runs the ConvRot rotation, the activation quantise,
torch._int_mmand the rescale as separate kernels, with Python between them. The render thread spends almost all its time there: py-spy puts it in_int_mm_rows/_rotate. Under host CPU load, GPU utilisation drops from 99% to 77%.Why it was eager:
Compiling the whole block (the existing regional compile) is fast, but it is not bit-identical to eager. Triton's approximate fp32 reciprocal moves the per-row scale by one ulp and flips about 1 in 10^4 int8 codes. LPIPS against eager is 0.0175, against a bf16 eager-vs-compiled floor of 0.0097.
Changes
torch.compile'd function of plain tensors (_w8a8_rows, dynamic shapes): rotation, scale, quantise,torch._int_mmand rescale together.UNSLOTH_NATIVE_INT8_FUSED=0.Results
Exclusive B200, Studio pinned to 8 cores, base and head interleaved, 7 processes per arm. FLUX.2-klein-4B int8 streamed, 1024x1024, 8 steps. The host's load average stayed at 160-385 on 192 cores, so the quiet figure is the best phase per process.
Wan2.2-TI2V-5B,
low_vramint8,speed_mode=off(one process per arm, no A/A): 9.71 / 8.77 -> 6.72 / 6.81 s per clip (about 1.35x); first render 55.6 -> 50.9 s.Tests
tests/test_diffusion_native_quant_fused.py, CPU parts:Limits