Skip to content

Studio: int8 ConvRot by default for FLUX.2 klein and FLUX.3 klein (LPIPS 0.10 to 0.04) - #17209

Merged
danielhanchen merged 2 commits into
mainfrom
studio-int8-convrot-flux-default
Nov 9, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-int8-convrot-flux-default

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Summary

Studio's INT8 quantizes every Linear's activation per row. For FLUX.2 klein and FLUX.3 klein, the at-load int8 build on a resident Ampere or newer card and the hosted int8 files were the only int8 paths that skipped the group-256 block-Hadamard rotation (ConvRot). Z-Image, Qwen-Image-2.1 and MOSS-Image already run it by default. Nothing gated it: the fused rotation + activation-quant kernel already covers sm80 / sm89 / sm120 and is probe-gated elsewhere. The families simply never had a table entry. The torchao-free W8A8 path (fp16 cards, ROCm, offloaded NVIDIA loads) already rotates every int8 Linear for every family, so until now klein's int8 quality depended on whether the load offloaded.

This PR makes ConvRot the int8 default for flux.2-klein, flux.3-klein and flux.3-klein-base:

  • ConvRotSpec(group = 256, default_on = True). On klein it covers all 101 int8 Linears (15 suffixes). On FLUX.3 it covers all 228 int8 Linears (6 block suffixes); the FLUX.3 keep-list leaves the rest dense.
  • Hosted files follow Qwen-Image-2.1's layout: FLUX.2-klein-4B-INT8-ConvRot.safetensors in unsloth/FLUX.2-klein-4B-FP8 and flux-3-image-klein-INT8-ConvRot.safetensors in unsloth/flux-3-image-klein-FP8, both now published. A new ConvRotSpec.hosted_plain marks the families whose older hosted int8 files are unrotated: while ConvRot is on, only the rotated name is requested, and if it were absent the load would rotate and quantize at load. The plain file is never read with ConvRot on.
  • flux.3-klein-base keeps reading its hosted int8 file, which is already rotated. fp8 is unchanged.
  • Kill switch: UNSLOTH_DIFFUSION_INT8_CONVROT=0 restores the previous behaviour exactly: plain hosted file, plain at-load build.

FLUX.2-dev and FLUX.1 are left alone (unmeasured).

Accuracy

Setup: the default resident int8 pick, 1024x1024, 8 prompts x 2 seeds = 16 paired renders, LPIPS (alex) against the dense bf16 render, 10k-resample paired bootstrap. "main" = kill switch (plain hosted file); "head" = this PR (rotated hosted file through the real resolver). Every head process was checked to have all Linears rotated with the fused rotation kernel engaged (101 / 228).

model, GPU main LPIPS mean (max) head LPIPS mean (max) head - main [95% CI] better / worse
FLUX.2-klein-4B, A100 40GB 0.092 (0.197) 0.038 (0.094) -0.054 [-0.069, -0.040] 16 / 0
FLUX.2-klein-4B, L4 0.102 (0.420) 0.044 (0.123) -0.058 [-0.099, -0.029] 16 / 0
FLUX.3 klein, A100 40GB 0.056 (0.334) 0.0094 (0.028) -0.046 [-0.088, -0.017] 16 / 0
FLUX.3 klein, L4 0.045 (0.346) 0.019 (0.121) -0.026 [-0.055, -0.008] 13 / 3

On a T4 (fp16 card, the torchao-free path, which already rotates today), klein-4B scores 0.103 plain vs 0.037 rotated: -0.066 [-0.086, -0.049], 16 / 0.

Speed

s/step of the steady renders, 3 fresh processes per arm in alternating order. An explicit int8 pick runs Studio's default speed path, so this is the compiled path; speed_mode off gave the same times. Every arm had 0 graph breaks and 0 recompiles.

model, GPU bf16 dense main int8 head int8 head vs main (A/A spread)
FLUX.2-klein-4B, A100 40GB 0.178 0.1236 / 0.1236 / 0.1238 0.1280 / 0.1281 / 0.1285 +3.6% (0.2%)
FLUX.2-klein-4B, L4 0.751 0.431 / 0.435 / 0.436 0.463 / 0.464 / 0.475 +6.6% (1.2%)
FLUX.3 klein, A100 40GB 0.234 0.1549 / 0.1552 / 0.1559 0.1653 / 0.1661 / 0.1662 +7.0% (0.5%)
FLUX.3 klein, L4 0.914 0.543 / 0.549 / 0.550 0.594 / 0.597 / 0.597 +8.7% (1.2%)

Head int8 stays 28-40% faster than bf16. Where the cost comes from (L4 klein, kernel profile of one render):

  • With ConvRot off, the int8 activation quant rides inside the producing Inductor kernel (layer norm + modulate, the single-block concat, the SwiGLU product): about 134 ms per render.
  • With it on, those kernels write bf16 (about 144 ms), and rotq_h16 reads the activation back, rotates and quantizes it (112 ms, 404 calls). The int8 GEMMs are unchanged (964 vs 975 ms).
  • The single blocks' 12288-wide attn.to_out inputs carry most of that traffic (an estimated 57% of the rotated elements).
  • rotq_h16 already runs at about 70% of L4's bandwidth, so tiling or launch settings do not buy the gap back. Sharing one rotation across Q/K/V (as Qwen-Image-2.1 does) would save about 0.4% of a step.
  • The real fix is to rotate inside the producing kernel, which needs the block forward patched (the item left open in Studio diffusion: cut the int8 ConvRot overhead (one rotation per shared input, SwiGLU fused into the down quant, factored rotation kernel) #14982). That is not in this PR.

Load

klein-4B on A100 40GB. Cold = empty HF cache; warm = files cached but a fresh Studio home, Triton cache and Inductor cache; repeat = second load on the same machine (same Studio home and caches).

main (plain hosted file) head (rotated hosted file) at-load rotation (rotated file missing)
denoiser bytes read 4.05 GB 4.05 GB 7.75 GB (bf16)
cold load / first image 67-69 s / 88 s 97-100 s / 117-120 s 82 s / 139 s
warm load / first image 20.4 s / 40.4 s 55.0 s / 74.8 s 29-50 s / 88-109 s
repeat load / first image 12.8-13.3 s / 18.5-21.1 s 14.3-14.4 s / 18.7-19.2 s
  • The extra ~35 s of a head load on a fresh machine is the fused rotation kernels' device probes compiling their Triton kernels (_rotq_probe 12.2 s, _h16_probe 11.8 s, _swiglu_probe 9.0 s, from a stack sample of the load), plus about 1 s for the hosted file's fingerprint check.
  • Both are cached per machine, so a repeat load is within about 1 s of main's.
  • L4 shows the same pattern: warm 22 s main vs 62 s head; cold 78-79 s vs 109-110 s.
  • FLUX.3: A100 warm 20.5 s vs 56 s, cold 65-68 s vs 97-99 s; L4 warm 20 s vs 60 s.

Tests

studio/backend/tests/test_diffusion_int8_runtime_convrot.py checks:

  • the rotated set equals the int8-quantized set on the real klein and FLUX.3 layouts;
  • the default-on behaviour and the kill switch;
  • resolution only ever names the rotated hosted file while ConvRot is on, and the plain file with the switch off;
  • a missing rotated file falls through to the at-load build without fetching the plain file;
  • FLUX.3 base still reads its hosted file;
  • the runtime quantize rotates all 101 klein Linears.

The new tests fail on the merge base. The family data freeze fixture was regenerated, and only these three families changed. On the related CPU subset (5,800+ tests), head and merge base fail the same tests by ID.

The published rotated files are bit-identical to this PR's at-load rotated build of the same weights on the same torch / torchao (270 / 587 tensors compared, 0 mismatches, same rotation lists).

Not covered here

  • FLUX.3 klein base was not run on a GPU (its hosted file was already rotated before this PR).
  • On T4, FLUX.3 int8 with the torchao-free path's default rotation hits a device mismatch in that path's rotation on the merge base too. This PR does not touch that path; it is tracked separately.

@danielhanchen danielhanchen self-assigned this Nov 9, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T05:22:14.396558Z 4867599 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

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 4867599428

ℹ️ 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 4a45dbd into main Nov 9, 2026
@danielhanchen
danielhanchen deleted the studio-int8-convrot-flux-default branch November 9, 2026 09:08
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Nov 9, 2026
… FLUX.3 (ConvRot cost +8.7% to +0.5%) (unslothai#17230)

## Summary

Follow-up to unslothai#17209 (int8 ConvRot by default for FLUX.2 klein and FLUX.3
klein), and the open item from unslothai#14982.

On the int8 GEMM path every rotated Linear quantized its own input with
`rotq_h16`: the producer kernel wrote the bf16 activation, and
`rotq_h16` read it back, rotated it (group-256 Hadamard) and quantized
it. This PR removes that second pass where it costs the most, without
changing a single code or scale:

- **Shared rotation + act quant.** Linears reading the same activation
quantize it once: Q / K / V in the FLUX.2 double blocks (image and text
streams), and Q / K / V plus the MLP input in every FLUX.3 block (they
all read the same modulated input). Same op on the same tensor, so this
is exact.
- **`rotq_h16x`: the rotation runs in front of the producer's own
loads.** It is `rotq_h16` (same 16 x 16 two-stage rotation, amax, bf16
rounding of scale and product) with the loads of the tensors the
producer would have combined:
- the MLP down projection reads the two SwiGLU halves straight from the
`linear_in` output (strided, no copy), so the SiLU product is never
written;
- the FLUX.2 single-block output projection (12288 wide, about 57% of
the rotation bytes) reads the attention output in the layout the
attention backend returned (any strides) plus the SwiGLU halves, so
neither the product nor the concat is written.
- **The shared int8 paths now engage inside `torch.compile`.**
`_swapped_rec` checked `forward.__func__ is _linear_forward`, which
Dynamo resolves to False, so every shared path fell back to stock in the
compiled forward (also the existing Qwen-Image-2.1 shared Q / K / V and
SwiGLU paths and MiniMax-H3's shared Q / K / V). The swap now stores the
bound forward it installs and the traced check compares by identity.
- **Probe verdicts persist per card and toolchain.** The rotation probes
cost about 50 s per fresh process on an L4 even with a warm Triton cache
(the new producer probe another 14 s). A passing device's verdicts are
stored next to `diffusion_probe_cache`'s file, keyed by its fingerprint
(torch, CUDA, driver, Triton, torchao, card UUID, the
`UNSLOTH_DIFFUSION_*` switches) plus the kernel sources; any mismatch
probes again.

Folding the rotation into the previous Linear's weights (QuaRot style)
is not exact here: every rotated input comes from LayerNorm +
modulation, SwiGLU or attention, none of which commute with a block
Hadamard, and the producing Linears are themselves int8 (rotating their
output channels changes their per-channel scales). So the fusion is in
the kernels instead.

Each patch checks its Linears per call (swapped rotated int8, no hook,
stock diffusers body by fingerprint) and otherwise runs the stock code;
any device whose probe fails keeps the current path. Kill switches:
`UNSLOTH_DIFFUSION_FLUX_CONVROT_FUSED=0` (all of it),
`UNSLOTH_DIFFUSION_INT8_ROTQUANT_PRODUCER=0` (keeps only the shared
quants), `UNSLOTH_DIFFUSION_PROBE_CACHE=0` (probe every load).

## Parity

`rotq_h16x` against the current compiled path (Inductor writes the bf16
product / concat with Studio's `emulate_precision_casts`, then
`rotq_h16`), FLUX.2 / FLUX.3 klein shapes at 1024x1024, v1 and v2 quant,
A100 and L4 (torch 2.14.1, Triton 3.8):

| case | M x K | codes equal | scales equal |
|---|---|---|---|
| FLUX.2 single-block output projection (attention + SwiGLU) | 4608 x
12288 | 100% | 100% |
| same, batch 2, head-major attention output | 2200 x 12288 | 100% |
100% |
| MLP down projection, image / text stream | 4096 / 512 x 9216 | 100% |
100% |
| FLUX.3 MLP down projection | 4608 x 9216 | 100% | 100% |

On real activations (every producer-fused call outside a CUDA graph
capture recomputed the current way, in the render itself): klein 90 of
90 calls and FLUX.3 79 of 79 calls with identical codes and scales, on
L4 and A100.

Whole renders: FLUX.2 klein and Qwen-Image-2.1-Turbo are bit-identical
to the merge base (16 of 16 images, both cards for klein). FLUX.3 klein
renders are not bit-reproducible across processes even on the merge
base: unlike flux.2-klein, the FLUX.3 families do not pin Inductor's
reduction configs, so each process picks its own. A/A on A100 (8 renders
per arm, fresh process each):

| pair | LPIPS mean / max | identical |
|---|---|---|
| merge base vs merge base (second process) | 3e-5 / 4e-5 | 0 / 8 |
| this PR vs this PR (second process) | 3e-5 / 5e-5 | 0 / 8 |
| this PR vs merge base | 3e-5 / 5e-5 | 0 / 8 |
| this PR (second process) vs merge base (second process) | 0 / 0 | 8 /
8 |
| kill switch vs merge base | 3e-5 / 4e-5 | 0 / 8 |

So the difference is the merge base's own process-to-process spread;
when the two processes picked the same configs the renders were
identical. On some L4 VMs that spread is far larger: on one, two
merge-base processes differed by LPIPS 0.0131 mean (0.039 max) and this
PR vs the merge base by 0.0115 (0.036), with every producer-fused call
still identical to the current path (79 / 79).

## Accuracy

16 renders (8 prompts x 2 seeds, 1024x1024), LPIPS (alex) against the
bf16 render. `plain` is the ConvRot kill switch (plain int8), `unslothai#17209`
the merge base, `this PR` the head; every int8 arm quantizes at load.

| model | card | plain int8 | unslothai#17209 | this PR | this PR vs unslothai#17209 |
|---|---|---|---|---|---|
| FLUX.2 klein 4B | L4 | 0.0977 | 0.0421 | 0.0421 | identical (16 / 16)
|
| FLUX.2 klein 4B | A100 | 0.0923 | 0.0410 | 0.0410 | identical (16 /
16) |
| FLUX.3 klein | L4 | 0.0538 | 0.0182 | 0.0182 | LPIPS 3e-5 mean, 9e-5
max |
| FLUX.3 klein | A100 | 0.0465 | 0.0161 | 0.0161 | LPIPS 4e-5 mean, 1e-4
max (kill switch arm: the same) |

## Speed

Colab, compiled default speed mode with CUDA graphs, resident, torch
2.10. Three fresh processes per arm, alternating, median per-step time;
A/A spread is max - min over the three.

| model | card | plain int8 | unslothai#17209 | this PR | ConvRot cost before /
after | A/A spread |
|---|---|---|---|---|---|---|
| FLUX.2 klein 4B (4 steps) | L4 | 0.4122 s | 0.4403 s | 0.4204 s |
+6.8% / +2.0% | <= 0.9% |
| FLUX.2 klein 4B | A100-40 | 0.1238 s | 0.1280 s | 0.1256 s | +3.4% /
+1.5% | <= 0.5% |
| FLUX.3 klein (16 steps) | L4 | 0.5361 s | 0.5827 s | 0.5387 s | +8.7%
/ +0.5% | <= 0.7% |
| FLUX.3 klein | A100-40 | 0.1556 s | 0.1661 s | 0.1569 s | +6.8% /
+0.9% | <= 0.6% |
| Qwen-Image-2.1-Turbo (8 steps, hosted ConvRot file) | A100-40 | |
0.2343 s | 0.2254 s | -3.8% vs unslothai#17209 | <= 0.4% |

Per image: FLUX.3 klein on L4 is 8.88 s plain int8, 9.64 s with unslothai#17209
and 8.93 s with this PR; on A100 2.60 / 2.77 / 2.62 s.

Per-kernel GPU time for one render (torch profiler, ms; `inductor` is
every Inductor-generated kernel):

| model / card | arm | rotq_h16 | rotq_h16x | inductor | all kernels |
|---|---|---|---|---|---|
| klein / L4 | plain | | | 127.6 | 1929.7 |
| | unslothai#17209 | 108.8 (404 calls) | | 270.0 | 2024.9 |
| | this PR | 26.0 (204) | 123.3 (120) | 151.6 | 1952.3 |
| klein / A100 | plain | | | 66.7 | 548.3 |
| | unslothai#17209 | 44.2 (404) | | 73.5 | 566.3 |
| | this PR | 12.1 (204) | 47.2 (120) | 51.7 | 558.4 |
| FLUX.3 / L4 | plain | | | 536.3 | 8676.7 |
| | unslothai#17209 | 795.3 (3168) | | 1262.4 | 9474.3 |
| | this PR | 173.2 (1056) | 478.4 (528) | 669.2 | 8743.0 |
| FLUX.3 / A100 | plain | | | 167.7 | 2506.0 |
| | unslothai#17209 | 310.2 (3168) | | 323.2 | 2676.4 |
| | this PR | 78.3 (1056) | 172.5 (528) | 227.2 | 2526.4 |

What is left on A100 is the rotation's own arithmetic (the 16 x 16
two-stage rotation is compute bound there; on L4 the fused kernel is
bandwidth bound and beats the plain int8 producer kernel at the same
shape). The remaining rotated inputs are the LayerNorm + modulation
outputs (one shared quant per block); fusing the rotation into that
kernel too is the next step.

All arms: 0 graph breaks, 0 recompiles, the same number of compiled
graphs. Each process ran the fused kernels (klein: 120 `rotq_h16x` calls
and 204 instead of 404 `rotq_h16` calls per render; FLUX.3: 528 and 1056
instead of 3168), and with `UNSLOTH_DIFFUSION_FLUX_CONVROT_FUSED=0`
none.

## Load

With the persisted verdicts, the second start on an L4 reaches its first
klein image in 31 s instead of 73 s (first image, 1024x1024, cold
Inductor cache in both); the merge base takes 61 s every start. The
first start pays the probes once (about 12 s more than the merge base
for the producer probe).

## Tests

`studio/backend/tests/test_diffusion_flux_convrot_fused.py`:
- each patched FLUX.2 / FLUX.3 block equals stock bit for bit with the
int8 pieces replaced by the plain modules, eager and under
`torch.compile(fullgraph = True)`, and counts one shared quant / one
producer call where expected;
- unswapped Linears never reach the int8 paths; a drifted diffusers body
keeps stock; uninstall restores every patched attribute; the kill
switch;
- `_swapped_rec` sees a swapped Linear inside a compiled block and not a
forward replaced after the swap (fails on the merge base);
- the probe verdict cache round-trips and misses on a changed
fingerprint;
- GPU: `rotq_h16x` equals the current path bit for bit (v1 / v2,
one-pass, two-pass, padded, concat, batch 2), declines what it cannot
read, and its switch.

On an A100 (torch 2.10) the ConvRot / int8 GEMM / autoopt test files
fail the same 3 tests by ID on the merge base and on this branch
(pre-existing); the new file passes. The related CPU subset passes.

## Not covered here

- Folding the rotation into the LayerNorm + modulation kernel (the
inputs of Q / K / V and the MLP); measured cost left: +2.0% klein L4,
+1.5% klein A100.
- MiniMax-H3: its shared Q / K / V path now also engages under compile
(the `_swapped_rec` fix) but was not run here. Qwen-Image-2.1 was run
only as Turbo on A100 (bit-identical, -3.8% per step).
- Cards other than L4 and A100-40 (no T4 path changes: fp16 cards do not
use this kernel), FLUX.3 klein base, offloaded loads (the shared paths
decline hooked modules by design), LoRA (declines, stock path).
- The kernel's FMA flip check did not flip: with `enable_fp_fusion` on,
the outputs were still bit-identical on these shapes, so that guard is
unproven here.
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