Skip to content

Studio: hosted ConvRot INT8 file for FLUX.1-schnell (inert until the file is published) - #18639

Merged
danielhanchen merged 2 commits into
mainfrom
studio-diffusion-flux1-convrot-int8
Nov 20, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-diffusion-flux1-convrot-int8

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

FLUX.1-schnell is the only one of Studio's main INT8 image families still loading an unrotated hosted INT8 file. Z-Image-Turbo, Qwen-Image-2.1 and FLUX.2-klein-4B already load a hosted ConvRot INT8 file (group-256 block Hadamard on the activations, rotation baked into the weights). This PR adds the same mapping for schnell, and it does nothing until the file is uploaded.

Change

  • diffusion_families.py: flux.1 gets int8_convrot = ConvRotSpec(group = 0, filename = "FLUX.1-schnell-INT8-ConvRot.safetensors", repo = "unsloth/FLUX.1-schnell-FP8", default_on = True). This is the same group 0 pattern LTX-2.5 uses. The rotation lives only in the file's metadata (every quantized Linear, group 256), so:
    • schnell INT8 asks for the rotated file first. The plain hosted chain (FLUX.1-schnell-INT8.safetensors, .pt, transformer_int8.pt) stays behind it, so until the file exists on the repo the plain file loads exactly as it does today;
    • quantizing at load (offline, offload routes, no hosted file) stays plain INT8, because convrot_spec_for_scheme is still (0, ());
    • FLUX.1-dev, Krea-dev and Kontext never ask for the schnell file (they resolve their own variant repos), and fp8 / nvfp4 / mxfp8 are unchanged;
    • UNSLOTH_DIFFUSION_INT8_CONVROT=0 resolves exactly what main resolves.
  • scripts/build_prequant_checkpoint.py: upload_destination refused a group 256 build for a family whose spec is group 0 ("reserved for the ConvRot group 0 build"). A group 0 spec has no runtime path to match, so the check now only applies when the spec has a group.

File to publish first

unsloth/FLUX.1-schnell-FP8 / FLUX.1-schnell-INT8-ConvRot.safetensors, built with this branch:

python scripts/build_prequant_checkpoint.py --base black-forest-labs/FLUX.1-schnell --family flux.1 \
  --base-model-id black-forest-labs/FLUX.1-schnell --scheme int8 --convrot-groupsize 256 \
  --out FLUX.1-schnell-INT8-ConvRot.safetensors

(torch 2.14.1, torchao 0.18.0, diffusers 0.41.0: 15,173,636,512 bytes, 419 rotated Linears, 137 s on an RTX PRO 6000).

Results

FLUX.1-schnell at 1024x1024, 4 steps, through DiffusionBackend.begin_load(..., transformer_quant="int8", speed_mode="default") (compiled + CUDA graphs). The rotated file was served through UNSLOTH_DIFFUSION_PREQUANT_MIRROR, so it went through this mapping. Every cell ran in a fresh process; arms were interleaved over 3 repeats. LPIPS (alex) is against Studio's own bf16 render over 8 prompts.

RTX PRO 6000 Blackwell (sm120):

arm LPIPS vs bf16 mean / max steady s/image (3 reps) s/step peak alloc GiB
bf16 - 0.864 0.199 32.42
INT8, main (plain hosted file) 0.1235 / 0.250 0.641 / 0.643 / 0.639 0.1430 24.44
INT8 ConvRot, this PR 0.0907 / 0.206 0.677 / 0.675 / 0.672 0.1505 24.45
FP8 0.1364 / 0.270 0.600 / 0.599 / 0.599 0.1322 21.44
  • ConvRot cuts LPIPS by 27% (better on 8 of 8 prompts) and costs 5.2% steady time. The repeat spread is under 0.6%, and repeated processes produced bit-identical images (LPIPS 0) in every arm.
  • Engagement: 419 ConvRotLinear/Int8Tensor in the ConvRot arm (419 plain Int8Tensor on main), int8_gemm in speed_optims, 0 graph breaks and 0 recompiles after the first render.
  • Inert check: this branch with no rotated file anywhere loaded the plain hosted file, and its 8 renders were bit-identical to main's.

L4 (sm89, 24 GB). Studio picks group offload for every arm here, the same policy on main and on this branch:

arm LPIPS vs bf16 mean / max steady s/image (3 reps) s/step peak alloc GiB
bf16 - 6.93 1.659 11.53
INT8, main 0.1444 / 0.291 3.72 / 3.55 / 3.57 0.82-0.86 15.8
INT8 ConvRot, this PR 0.0793 / 0.175 4.06 / 3.94 / 3.91 0.91-0.94 15.8
FP8 0.1910 / 0.306 4.24 / 3.93 / 3.89 0.90-0.99 15.8
  • ConvRot cuts LPIPS by 45% (better on 8 of 8 prompts) and costs about 10% steady time (+9.2% in rep 0, +10.2% over reps 1-2). It is as fast as FP8 here and 2.4x closer to bf16.
  • A/A: the ConvRot and FP8 repeats were bit-identical. One main INT8 repeat differed from its first process (LPIPS 0.12 over 2 prompts). That is the known per-process Inductor config spread on this model, and it was on main's arm, not this one.
  • The L4 file was built on CPU (277 s). Its bytes differ from the GPU build (sha256 2110ce61... vs 271f0d12...), and both are the same size. Each one was measured on its own card. Publish one build and recheck it on both cards.

Tests

tests/test_diffusion_flux1_convrot_hosted.py covers the schnell chain (rotated first, plain chain intact), the kill switch, dev / Krea-dev, the other schemes, plain at-load quantization, mirror lookup and the build script's upload name. Two assertions in test_diffusion_int8_runtime_convrot.py that listed flux.1 as opt-in are updated. test_differential.py against the merge base: DISCRIMINATING, 7 fixed, 0 head failures. test_build_prequant_checkpoint.py::test_the_recorded_base_must_be_the_canonical_id_not_just_the_same_tail fails on both main and this branch (it reads a local ./temp/qwen_image_21 path), so it is not caused by this change.

Not proven here

  • The real Hub download of the rotated file, because it is not uploaded yet. The mirror path goes through the same name chain.
  • FLUX.1-dev / Krea-dev ConvRot files: not built. Their mapping is unchanged.
  • The early int8 probe prewarm now also warms the rotation kernels for flux.1, even while the plain file is the one that loads. It is a warm-up only, and render output is unaffected (bit-identical above).

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 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-20T14:17:48.108310Z d4a263c 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. 👍

Reviewed commit: d4a263cbdb

ℹ️ 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 c71e231 into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the studio-diffusion-flux1-convrot-int8 branch November 20, 2026 15:59
danielhanchen added a commit that referenced this pull request Nov 20, 2026
…nell ConvRot mapping (#18652)

## Summary

`Backend CI / (Python 3.13, diffusion)` is red on `main` (seen on
#18650, reproduced on current `main`):

```
FAILED tests/test_diffusion_family_data_freeze.py::test_family_data_resolves_identically
E   AssertionError: per_family
E     {'flux.1': {... 'convrot_default_on': True, ...}} != {'flux.1': {... 'convrot_default_on': False, ...}}
```

## Cause

#18639 deliberately gave `flux.1` a hosted ConvRot INT8 spec:
`FLUX.1-schnell-INT8-ConvRot.safetensors` in
`unsloth/FLUX.1-schnell-FP8`, `default_on = True`. The freeze test
exists to make every such change explicit. Its docstring says a
deliberate change regenerates the fixture with
`UNSLOTH_FREEZE_UPDATE=1`, and the fixture diff is the review artifact.
#18639 did not regenerate it, so the code change is intended and the
frozen snapshot is stale.

## Change

`studio/backend/tests/fixtures/diffusion_family_data_freeze.json`,
regenerated with `UNSLOTH_FREEZE_UPDATE=1` and
`UNSLOTH_DIFFUSION_INT8_CONVROT` unset. The diff (6 lines added, 3
removed) contains only #18639's change for `flux.1`:

- `convrot_default_on`: `false` to `true`;
- `convrot_filename` and `convrot_repo`: `null` to the schnell ConvRot
file and repo;
- `flux.1` added to the family tables: the default-on set, filename map
and repo map.

No exclude-token, NVFP4 or other family entry moves. Those are the sets
baked into published prequant artifacts, so hosted checkpoints are
unaffected.

## Testing

- `tests/test_diffusion_family_data_freeze.py`: fails on `main` with the
diff above, passes with this fixture.
- `tests/test_diffusion_flux1_convrot_hosted.py` (#18639's own tests)
still passes: 14 passed together.
danielhanchen added a commit that referenced this pull request Nov 22, 2026
…tput into the rotation, and shares one Q/K/V quant (#18766)

FLUX.1-schnell on the hosted ConvRot INT8 file (#18639) was about 5%
slower per image than plain INT8 on an RTX PRO 6000. Two things caused
it. The FLUX.2 / FLUX.3 producer fusion (`diffusion_flux_convrot_fused`)
did not cover `FluxTransformer2DModel`, so every rotated Linear rotated
and quantized its own input: Q, K and V each rotated the same
activation, in both the double and the single blocks. Also, the
GELU(tanh) MLP output (and, in single blocks, the concat with the
attention output) was written to HBM in bf16, then read back by
`rotq_h16`.

## Change

- `diffusion_flux_convrot_fused`: FLUX.1 patches, scoped to the modules
`install` marks on a FLUX.1 denoiser with ConvRot Linears.
- `transformer_flux._get_projections` shares one rotation + act quant
for Q / K / V and for the added context Q / K / V. This is the same
wrapper FLUX.2 uses, and the diffusers body is the same.
- `FeedForward.forward` (GELU tanh, Dropout, rotated down projection)
and `FluxSingleTransformerBlock.forward` (diffusers' body, or the arch
patch's addcmul body when that one is installed) give `proj_out` /
`net[2]` the projection output, plus the attention output in its own
layout. Neither the GELU product nor the concat is written.
- Every gate is checked per call. Anything else (no mark, hooks, LoRA,
offload, other dtypes) runs the stock forward.
- `diffusion_convrot_quant`:
- `rotq_h16x` gets `ACT = 2`: GELU(tanh) of the gate only, so the value
is never loaded.
- New op `unsloth_studio::convrot_gelu_act_quant_int8(prefix?, gate,
group, v2)`, with a fake impl.
- The GELU helper follows ATen / Inductor op for op (one FMA for `x + k
x^3`, pinned `*_rn`, plain multiplies at the end so subnormal inputs
keep `0.5 * x`).
- The probe first checks every finite bf16 input bit for bit against
`F.gelu(approximate="tanh")`, then runs `codes_close` on FLUX.1's
shapes, including the [attention | GELU] row and GELU's non-monotonic
tail.
- Armed on sm89 and sm120 (measured). Each card keeps the stock path
where the fused kernel measured slower: below 1024 rows on L4; on RTX
PRO 6000 below 2048 rows with the attention prefix and always without it
(the FF measured 0.94x there). The 256-row text-stream MLP is excluded
everywhere.
- The probe verdict is persisted with the other ConvRot verdicts (card
fingerprint + source hash), and its kernels join the probe prewarm pool,
so only a card's first load compiles them (on a thread pool). Later
loads take the same time as main's.
- ROCm gets a portable helper built on `triton.language.extra.libdevice`
(no `*_rn` there).
- `diffusion_int8_gemm.gelu_linear`: the producer-fed GEMM, a copy of
`producer_linear` without the value.
- Kill switches:
- `UNSLOTH_DIFFUSION_FLUX_CONVROT_FUSED=0` restores main exactly: no
FLUX.1 patch is installed and the stock functions come back.
- `UNSLOTH_DIFFUSION_FLUX1_GELU_FUSED=0` or
`UNSLOTH_DIFFUSION_INT8_ROTQUANT_PRODUCER=0` turn off only the GELU
producer. The shared Q / K / V quant stays; it is the same op on the
same tensor, so images are bit-identical to main either way.
- Scope: the FLUX.1 patches sit on module-wide diffusers code that other
families reuse. Chroma calls `transformer_flux._get_projections`, and
every model has `FeedForward`. So they act only on the attentions,
FeedForwards and single blocks that `install` marked on a FLUX.1
denoiser. A ConvRot Chroma loaded after a ConvRot FLUX.1 in the same
process keeps its stock projections. The mark is read from the instance
`__dict__` first, so an unmarked module pays one dict lookup per call.
- A failed GELU probe is logged at info level with the reason; FLUX.1
then keeps the compiled GELU + concat path.

## Results

RTX PRO 6000 Blackwell (sm120), Colab. FLUX.1-schnell, 1024 px, 4 steps,
8 prompts, guidance 0, Studio `begin_load` int8 with the ConvRot file
built on the VM. Compiled + CUDA graphs, resident. Each cell is a fresh
process, and the A/A repeats were interleaved.

| arm | steady s/image (3 reps) | s/step | peak_alloc GiB | images vs
main |
|---|---|---|---|---|
| main | 0.6713-0.6737 | 0.1507-0.1510 | 24.42 | - |
| shared Q / K / V only (`UNSLOTH_DIFFUSION_FLUX1_GELU_FUSED=0`) |
0.6475 | 0.1443-0.1445 | 24.42 | 8/8 bit-identical |
| this PR | 0.6243-0.6277 | 0.1389-0.1391 | 24.42 | 8/8 bit-identical |

- s/step is 7.9% lower than main and s/image 7.1% lower. The A/A spread
is under 0.4%. These numbers are from the final head of this branch. An
earlier session gave the same result: 0.1390 vs 0.1512.
- Load time in a new process: 8.2 s for both main and this PR once the
card's verdict is stored. The first load on a card takes 14.4 s, because
it prewarms the probe kernels on 8 threads and stores the verdict.
- LPIPS is 0.0 against main on every prompt. Repeats are bit-identical.
- 0 graph breaks, 0 recompiles, and no CUDA graph fallbacks in any cell.
- Rotation launches per render: 1676 on main, 1068 with shared Q / K / V
only, 916 here. GELU producer calls: 152. The FF producer stays off on
sm120 (0.94x alone), so those calls are the single-block proj_out.

Kernel alone (quant stage, `do_bench_cudagraph`, codes and scales
`torch.equal` to the compiled GELU + concat + `rotq_h16` on every shape,
v1 and v2):

| shape | L4 main / fused ms | RTX PRO 6000 main / fused ms |
|---|---|---|
| single-block proj_out, 4352 x (3072 + 12288) | 2.30 / 1.54 (1.50x) |
0.361 / 0.283 (1.28x) |
| FF, 4096 x 12288 | 1.46 / 1.22 (1.19x) | 0.240 / 0.255 (0.94x, kept
stock) |
| single block at 512 px, 1280 rows | 0.575 / 0.472 (1.22x) | 0.068 /
0.090 (0.76x, kept stock) |
| text FF, 256 rows | 0.045 / 0.077 (0.58x, kept stock) | 0.013 / 0.021
(0.63x, kept stock) |

GELU helper over all 65280 finite bf16 inputs: 0 bit differences against
ATen eager and against Inductor (with and without
`emulate_precision_casts`), on L4 and RTX PRO 6000.

## Tests

`tests/test_diffusion_flux1_convrot_gelu.py` (CPU):
- A tiny FLUX.1 transformer with the patches forced on (int8 pieces
replaced by the plain modules) matches stock bit for bit, eager and
dynamo fullgraph, in fp32 and bf16, with the expected call counts.
- The arch-patched addcmul single block is kept.
- Unmarked FeedForwards and unrotated Linears keep the stock path.
- Install scoping and the kill switch.
- The patch spec covers FLUX.1.
- Reference, fake shapes, the CPU op body equal to the stock reference,
the min-rows gate, and the kernel source contract.

Also covered:
- A tiny Chroma run after a FLUX.1 install takes the stock projections.
This test fails if the mark check is removed.
- An unmarked FeedForward is rejected without an `nn.Module.__getattr__`
miss.
- What each kill switch restores.
- A Dropout in the FF with a forward hook, a pre-hook or an instance
`forward` keeps the stock FeedForward, so the callback still runs. This
test fails without the check.
- The info log on a failed probe.
- `tests/test_diffusion_flux1_convrot_gelu.py` also covers the persisted
verdict (a later load adopts it without probing) and the prewarm
plumbing for FLUX.1 loads.

`test_differential.py` against 8b7cde7 (6 files): DISCRIMINATING,
FIXED 4, HEAD_FAIL 0, 107 unchanged. The broader backend suite (315
files, about 17300 tests; the 4 `test_mcp_tools_*` files fail to collect
on both sides and were left out) has 61 failures, the same test IDs on
main (8b7cde7) and here. They come from compat_preflight,
compile_cache, layer split lifecycle, memory, the QwenImage 2.1 eager
and strided attention tests, the media model index, SDXL whole-pipeline
GGUF, hosted video int8 offload, the prequant base id and a FLUX.2
mirror shape guard.

## Not proven here

- L4 end to end: Studio group-offloads FLUX.1 on L4, so `_swapped_rec`
is None and none of this engages there. The L4 numbers above are the
kernel alone.
- A100 (sm80) is not armed (not measured). B200 and T4 were not run.
- AMD: the ROCm helper is written but not run (probe ready). The FLUX.1
shared Q / K / V route also covers the RDNA native W8A8 Linears through
`_rdna_shared`. That is bit-identical by construction but was not run on
gfx1151.
- The RTX PRO 6000 end-to-end run had the prefix-free FF still fused; it
is gated off there now (about 1 ms per image). Not re-measured after
that change.
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