Repository navigation
Studio diffusion: keep single-file int8 weights as int8 on T4 / pre-Ampere NVIDIA and Intel XPU under offload - #17079
Merged
Merged
Conversation
…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.
Codex Review Summary: ✅ CompletedThis 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. |
[skip ci]
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 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". |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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
NativeWeightOnlyLinearas 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 withUNSLOTH_DIFFUSION_INT8_STORAGE=1. It respects the family deny table, like the native route."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:
UNSLOTH_DIFFUSION_INT8_STORAGE=0restores the expansion everywhere.Outputs:
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.
_apply_group_offloadhandled offload.0148e62926against this branch, run ABBA in fresh processes, 3 per arm; medians, with the [min .. max] range for times.act_int806eeb4a286eeb4a28, bitwise equalThe 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 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_hostwas enumerated against main: device (cuda / xpu / mps / cpu / none) x dtype (fp16 / bf16 / fp32) x ROCm x torchao stub x offload xUNSLOTH_DIFFUSION_INT8_STORAGE(unset / 0 / 1 / auto / garbage) x the single-file int8 switch xUNSLOTH_NATIVE_INT8_ACTx 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_hostandnative_quant_hostdid not move in any cell. Ampere+, ROCm, MPS and CPU are byte-identical.Tests
the single-file quant tests:
8 * (512 * 512 * (itemsize - 1) - 4 * 512)fewer bytes and matches the expanded DiT bitwise (fp16 NVIDIA and bf16 XPU targets)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:
test_diffusion_transformer_quanttest_diffusion_native_quant*,test_video_native_*,test_diffusion_backend,test_diffusion_layer_split_order,test_diffusion_native_stream_build,test_diffusion_image_native_prequant_cacheOver 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