Skip to content

Studio diffusion: keep single-file int8 weights as int8 on T4 / pre-Ampere NVIDIA and Intel XPU under offload - #17079

Merged
danielhanchen merged 4 commits into
mainfrom
studio-int8-storage-fp16-xpu
Nov 8, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-int8-storage-fp16-xpu

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Problem

An int8 single-file checkpoint (int8_tensorwise, with or without ConvRot) keeps its int8 codes only where one of Studio's int8 runtimes runs:

  • torchao on a resident bf16 NVIDIA plan
  • the torchao-free native layer on ROCm, MPS, or an offloaded bf16 NVIDIA plan

Pre-Ampere NVIDIA GPUs (T4, the Kaggle / free Colab tier, and other sm75 / sm70 cards) compute in fp16, and Intel XPU has no torchao path. On both, the single-file quant helper returns None, and the single-file quant helper expands every int8 Linear to 16-bit at load. The DiT then needs 2x the int8 bytes in host RAM and in VRAM, and an offloaded plan uploads 2x the bytes on every step. A Qwen-Image-2.1 int8 DiT (about 20 GB) widens to about 40 GB, which is more than a Kaggle / Colab T4 VM has.

Fix

On those hosts the codes now stay int8 on the existing NativeWeightOnlyLinear as weight-only storage:

  • int8_storage_host(target): NVIDIA CUDA with an fp16 target (not ROCm, not the torchao stub), or XPU in bf16 / fp16.
  • int8_storage_scheme(): engages for an offloaded plan, or everywhere with UNSLOTH_DIFFUSION_INT8_STORAGE=1. It respects the family deny table, like the native route.
  • the single-file quant helper returns "native" there, after the existing native and torchao checks, so every host that has a runtime today keeps it.
  • native_int8_act() is False on these hosts: no int8 GEMM, no activation quant. Each forward widens the codes with the same formula as the load-time expansion (codes.float() * scale, cast to the compute dtype).

For an offloaded single-file load, the planner already re-prices the DiT with the offloaded backends (the single-file quant helper), so the plan and the load see the same int8 size.

Two cases keep the expansion:

  • Resident plans, because the expanded DiT is faster when it fits (the same rule fp8 storage follows).
  • Image loads with an active LoRA, because adapters need dense Linears (the fp8 storage rule again). The planner prices them the same way.

UNSLOTH_DIFFUSION_INT8_STORAGE=0 restores the expansion everywhere.

Outputs:

  • Plain int8 layers give bitwise equal outputs: the widened weight is the same tensor the load built before.
  • ConvRot layers rotate the activations, as the native route already does on ROCm, MPS and offloaded Ampere+.

Results

These numbers come from Kaggle T4 x2 (Tesla T4, torch 2.14.1+cu130), one variant per GPU, each lane pinned to its own CPU half.

  • The real loader read a synthetic int8 single-file checkpoint through the single-file quant helper: 4 blocks of 3072 / 12288 Linears, 453M int8 weights, fp16 target.
  • Studio's own _apply_group_offload handled offload.
  • Arms are main 0148e62926 against this branch, run ABBA in fresh processes, 3 per arm; medians, with the [min .. max] range for times.
plain int8 file, offloaded plan main this PR
backend / layers kept int8 None / 0 native / 24, act_int8 0
pinned host copy of the DiT 864.2 MiB 432.6 MiB
load time (host expansion on the Kaggle CPU) 22.4 s [22.3 .. 22.5] 1.4 s [1.4 .. 1.5]
process max RSS 3663 MiB 3584 MiB
offloaded forward, 1024 tokens, eager 87.0 ms [81.7 .. 89.8] 83.2 ms [82.3 .. 83.9]
peak allocated while offloaded 740.3 MiB 752.6 MiB
output (sha256 of the fp16 output) 6eeb4a28 6eeb4a28, bitwise equal

The offloaded forward times overlap: this 4-block model is compute-bound here. The upload halves with the bytes, so the saving grows with the DiT. Widening adds 12 MiB of peak (one layer's temporaries).

ConvRot int8 file, offloaded plan main this PR
pinned host copy 864.2 MiB 435.6 MiB
load time 26.9 s [26.7 .. 27.2] 1.4 s [1.3 .. 1.6]
offloaded forward 88.1 ms [82.8 .. 90.2] 88.8 ms [88.0 .. 89.7]
peak allocated 740.3 MiB 778.8 MiB
output vs main (first 64 tokens) max abs 1.95e-3 (one fp16 ulp at that magnitude), rel L2 3.7e-4, cos 0.999994

ConvRot rotates the activations instead of unrotating the weights at load, as the native route already does on ROCm, MPS and offloaded Ampere+.

Resident plans are unchanged: the DiT expands as before, with 864.2 MiB on the card and the same output. Forcing storage on a resident plan (UNSLOTH_DIFFUSION_INT8_STORAGE=1) keeps 433.5 MiB on the card with bitwise equal output. Its eager forward is slower, 74.2 ms [73.8 .. 75.3] against 47.5 ms, so storage stays off for resident plans by default.

Input space

Every input to the single-file quant helper / native_int8_act / native_quant_scheme / native_offload_host / native_quant_host was enumerated against main: device (cuda / xpu / mps / cpu / none) x dtype (fp16 / bf16 / fp32) x ROCm x torchao stub x offload x UNSLOTH_DIFFUSION_INT8_STORAGE (unset / 0 / 1 / auto / garbage) x the single-file int8 switch x UNSLOTH_NATIVE_INT8_ACT x family deny.

That is 7,200 cells: 6,579 are identical and 621 moved. Every moved cell is an NVIDIA fp16 or XPU bf16 / fp16 target that had no runtime on main (backend None), and none has the kill switch set. native_quant_scheme, native_offload_host and native_quant_host did not move in any cell. Ampere+, ROCm, MPS and CPU are byte-identical.

Tests

the single-file quant tests:

  • the backend choice table, including the kill switch, ROCm, the torchao stub and the family deny
  • no int8 GEMM on storage hosts
  • a plain int8 file loaded as storage holds 8 * (512 * 512 * (itemsize - 1) - 4 * 512) fewer bytes and matches the expanded DiT bitwise (fp16 NVIDIA and bf16 XPU targets)
  • a ConvRot file rotates activations, with cosine above 0.9999
  • storage stays off under a LoRA

The new tests that cover a moved cell fail on main and pass here. The unchanged cells pass on both.

The wider diffusion quant suites give the same results on main and on this branch, except for the new tests:

  • the single-file quant tests and test_diffusion_transformer_quant
  • test_diffusion_native_quant*, test_video_native_*, test_diffusion_backend, test_diffusion_layer_split_order, test_diffusion_native_stream_build, test_diffusion_image_native_prequant_cache

Over 1,503 tests, the only failure on both sides is test_unpinned_torchao_host_copy_is_a_distinct_wrapper_over_the_same_data, which fails the same way on main.

Not covered

  • No Intel XPU hardware was available. XPU is covered by the mocked target and the CPU loader tests only.
  • On a first load, an int8-stored DiT counts as packed, so like GGUF and fp8 storage it has no static activation seed: the auto partial residency starts from the second load, once a learned peak exists. The full-size DiT streamed before this change got a static-seeded resident prefix.
  • No end-to-end render of a real int8 single-file checkpoint was run on a T4. The numbers above use the real loader on a synthetic file.

…IDIA and XPU

An int8 ComfyUI single file on a GPU none of Studio's int8 runtimes serve (sm75 /
sm70 computing in fp16, Intel XPU) expanded the whole DiT to 16-bit at load. Under an
offloaded plan the codes now stay int8 on the existing torchao-free native layer as
weight-only storage (no int8 GEMM, act_int8 off) and widen per layer with the same
formula as the load-time expansion, so plain int8 layers give bitwise equal outputs
at half the weight bytes and half the per-step upload. Resident plans keep the
faster expansion. UNSLOTH_DIFFUSION_INT8_STORAGE=1 also stores resident loads, =0
restores the expansion.
A LoRA needs the dense Linears (fp8 storage follows the same rule), so an image load
with an active LoRA keeps today's expansion; the planner prices it the same way.
@danielhanchen danielhanchen self-assigned this Nov 8, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 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-08T03:37:18.748474Z 98af783 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.

[skip ci]
@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: 98af78300a

ℹ️ 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 changed the title Studio diffusion: keep ComfyUI int8 weights as int8 on T4 / pre-Ampere NVIDIA and Intel XPU under offload Studio diffusion: keep single-file int8 weights as int8 on T4 / pre-Ampere NVIDIA and Intel XPU under offload Nov 8, 2026
@danielhanchen
danielhanchen merged commit 3967e81 into main Nov 8, 2026
@danielhanchen
danielhanchen deleted the studio-int8-storage-fp16-xpu branch November 8, 2026 20:26
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Nov 9, 2026
… of expanding them to float32 (unslothai#17178)

## Summary

On a CPU target the single-file quant loader expands every int8 layer of
an int8 denoiser to float32 at load time and keeps that copy for the
whole session: 4 bytes a weight instead of 1. CPU computes in float32
and has no Studio int8 runtime, and `int8_storage_host` (unslothai#17079)
excluded CPU even with `UNSLOTH_DIFFUSION_INT8_STORAGE=1`, so the
loader's int8 backend choice came back empty and the loader dequantized.

The int8 storage layer unslothai#17079 added for pre-Ampere CUDA and XPU already
runs these layers on CPU with outputs identical to the expanded model.
This PR turns it on for CPU:

- `int8_storage_host`: a float32 CPU target keeps checkpoint int8 codes
as weight-only storage. A CPU target is never offloaded, so on CPU this
is the default rather than an offload-only choice.
`UNSLOTH_DIFFUSION_INT8_STORAGE=0` still expands them (the existing kill
switch).
- `NativeWeightOnlyLinear`: a float32 CPU forward widens into a reused
per-thread float32 buffer (`copy_` from the codes, then `mul_` by the
row scale, bit-identical to the existing widen). Allocating a fresh
weight-sized tensor every call was most of the cost (page faults: 23 ms
vs 5 ms to widen a 12288x3072 layer on 8 threads) and made the stored
model about 25% slower per step. With the buffer the step time matches
the expanded model within noise. The buffer is not used under autograd
(a graph would keep the buffer the next layer overwrites) or while
compiling, and `weight` never returns it.

Saving: about 2.79 GiB per billion int8 parameters the loader keeps, on
CPU-only Linux, Windows and macOS.

Unchanged: CUDA (Ampere+ W8A8 and torchao routes, pre-Ampere fp16
storage), the Turing W8A8 path from unslothai#17167, ROCm, XPU, MPS, a CPU target
in bf16 / fp16, LoRA loads (storage stays off under a LoRA, as in
unslothai#17079) and ConvRot files on every non-CPU target.

## Results

Single-file `int8_tensorwise` Qwen-Image checkpoint built from the real
`Qwen/Qwen-Image` transformer config (full 3072 width, 4 of the 60
blocks, random weights, 1.40B parameters, 1.44 GB file). Each arm is a
fresh process on the Studio CPU target, with Studio's own int8 backend
choice and the single-file quant loader, then 5 transformer steps at
1024 image tokens plus 64 text tokens. 8 threads, 3 interleaved runs per
arm, step time is the median excluding the first step.

| Arm | int8 backend | Layers kept int8 | Weights held | Peak RSS |
s/step (per run) | s/step (all) |
|---|---|---:|---:|---:|---|---:|
| main | none (expanded to float32) | 0 | 5,599,394,048 B | 7,890 MiB |
1.016 / 0.936 / 0.970 | 0.974 |
| this PR | native storage | 24 | 4,240,881,920 B | 6,648 MiB | 0.997 /
0.982 / 0.988 | 0.987 |
| this PR without the reused buffer | native storage | 24 |
4,240,881,920 B | 6,610 MiB | 1.157 / 1.196 / 1.204 | 1.192 |

- The transformer output is bit-identical between main and this PR
(`torch.equal` on the final step output).
- The +1.3% median difference is inside main's own run-to-run spread
(0.936 to 1.016 s).
- 24 of the block Linears are kept int8 here; the rest are layers the
Qwen-Image int8 filter leaves dense, so the saving on this file is 1.27
GiB. A full 60-block file scales the same way.
- A 2-block tiny copy (20M parameters) is 0.030 vs 0.029 s/step, weights
80.6 MB vs 61.8 MB, output bit-identical.

Per-layer timing showed why the buffer is needed.
`torch._weight_int8pack_mm` on CPU was 15x to 60x slower than float32
GEMM at 256 to 4096 rows, and a single mixed int8 x float32 `torch.mul`
was slower than the existing chunked widen. Neither is used.

## Tests

New in the single-file quant loader tests (`studio/backend/tests/`), run
on CPU:

- backend choice table: CPU float32 is stored by default and with `=1`,
expanded with `=0`; CPU bf16, MPS fp16 / fp32 and every existing CUDA /
XPU row stay as before.
-
`test_cpu_keeps_int8_codes_resident_and_matches_the_expanded_dit_bitwise`:
held bytes drop by exactly 3 bytes a weight minus the row scales, every
layer is weight-only int8 (no W8A8, no rotation), and outputs equal the
expanded load bitwise at 1, 3 and 17 rows. The kill switch load has no
int8 layer.
- `test_cpu_forward_widens_into_a_reused_scratch_bitwise`: bitwise
against `_stored_weight`, the buffer grows once to the largest layer and
is reused, and `weight` is not the buffer.
- `test_cpu_forward_under_autograd_keeps_its_own_weight` and
`test_cpu_native_linear_compiles_without_a_graph_break` (Dynamo
`fullgraph=True`, dynamic shapes).
- CPU never takes the int8 GEMM (`native_int8_act` is False).

5 of these fail on main and all pass here. The single-file quant loader
tests, `test_diffusion_fp16_card_int8.py`,
`test_diffusion_native_quant.py` and
`test_diffusion_native_quant_mps.py` pass, apart from one torchao
host-copy test that fails on main too. A wider sweep of 78 diffusion
quant, memory, device and video quant test files gives the same 11
failures on main and on this branch (environment related: rope
fingerprint, vision attention, segment and one MOSS pipeline test), with
no new ones.

## Not covered

- MPS: an MPS target with bf16 already keeps int8 codes through the
native route, so there is no float32 expansion there. Only a macOS 13
MPS target (no bf16, float32) still expands, and that is left unchanged
because storage on MPS float32 has not been run on Apple hardware.
- The timing above is one x86 Linux host. Windows and macOS CPU take the
same code path but were not timed.
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