Skip to content

Studio diffusion: run an eager native int8 W8A8 linear as one compiled function (bit-identical, 1.33x streamed) - #15183

Merged
danielhanchen merged 4 commits into
mainfrom
studio-native-int8-compile
Oct 25, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-native-int8-compile

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

A streamed or offloaded denoiser on the torchao-free native int8 W8A8 path runs eager. Examples:

  • FLUX.2-klein-4B int8 under a small budget;
  • Wan2.2 offloaded with speed_mode=off.

Each linear then runs the ConvRot rotation, the activation quantise, torch._int_mm and 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

  • Each eager native int8 linear on CUDA runs as one torch.compile'd function of plain tensors (_w8a8_rows, dynamic shapes): rotation, scale, quantise, torch._int_mm and rescale together.
    • The function takes tensors, not the module, so the offload hooks moving weights never invalidate the graph.
  • Bit-identical to eager: the reciprocal is taken in fp64, which gives exactly the eager CUDA value, so the int8 codes match bit for bit.
  • No guard churn: inputs are detached, so guards on view bases and Parameter shapes do not pile up. That gives 2 graphs (with and without bias), and 8 input shapes x 4 layers still give 2.
  • Regional compile unchanged: compiled blocks still trace the eager code.
  • When it stays off:
    • on ROCm and on hosts without Triton;
    • during CUDA graph capture;
    • for too few rows.
  • Fallback: a failed compile falls back to eager once and stays there.
  • Kill switch: 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.

base head
ms / step (median of per-process best) 103.0 77.6 (1.33x)
A/A spread, calm processes 102.8-103.1 (5 of 7) 74.9-77.6 (5 of 7)
wall per image, calm processes 0.93-0.94 s 0.71-0.76 s
first step, cold compile cache 0.55-0.73 s 2.4-2.8 s (about +2 s compile)
first step, warm compile cache - 1.86 s

Wan2.2-TI2V-5B, low_vram int8, 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.

  • Images: bit-identical to eager, 12 of 12 (2 prompts x 6 seeds) for klein and 3 of 3 clips for Wan 5B.
  • Compile behaviour: 0 graph breaks, 2 graphs (one recompile at the first no-bias layer), and 0 recompiles on follow-up seeds and prompts.
  • Under CPU contention: 251 ms (range 170-315) on base against 229 ms (137-349) on head. The host load makes this inconclusive, so no claim is made for it.

Tests

  • New tests/test_diffusion_native_quant_fused.py, CPU parts:
    • the kill switch, and staying eager without an inductor runtime, on ROCm, for CPU input and for too few rows;
    • fallback after a failed compile, while an out-of-memory error propagates;
    • detached, non-Parameter inputs to the compiled function;
    • chunking on the accumulator;
    • the fused maths tracking the eager kernels.
  • The CUDA test (bit-identical to eager, with and without rotation) was run on a GPU.
  • Native quant, speed, cold start and video suites: 3089 passed, 115 skipped.

Limits

  • No contended speed claim: the shared host was too loaded.
  • Not run: Wan2.2-A14B, other GPU generations, and the torch 2.6 floor (only torch 2.14.1 was tested).
  • The path only runs on bf16 CUDA targets, so a T4 (fp16) never takes it.
  • On main, the regional int8 compile drifts from eager for the same reciprocal reason. That is not changed here.

… 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.
@danielhanchen danielhanchen self-assigned this Oct 25, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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-25T02:56:40.586420Z 45e31f4 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

The (Python 3.13, a-f) red is from this PR: test_no_diffusion_or_video_gate_reads_version_hip_directly flags a raw torch.version.hip read at studio/backend/core/inference/diffusion_native_quant.py:313. AMD SDK / Radeon wheels leave torch.version.hip unset, so that gate would treat them as non-ROCm. Use core.inference.rocm_bf16.is_rocm_torch(torch) there instead, the way the other diffusion gates do. Main does not read version.hip in that file, so the rest of the run is clean.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 45e31f4e08

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

@danielhanchen
danielhanchen merged commit 34304b5 into main Oct 25, 2026
@danielhanchen
danielhanchen deleted the studio-native-int8-compile branch October 25, 2026 03:40
danielhanchen added a commit that referenced this pull request Oct 27, 2026
…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>
STUNSLAP pushed a commit to STUNSLAP/unsloth that referenced this pull request Nov 5, 2026
…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.
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