Skip to content

Studio: keep single-file int8 denoiser weights as int8 on CPU instead of expanding them to float32 - #17178

Merged
danielhanchen merged 6 commits into
mainfrom
studio-cpu-int8-storage
Nov 9, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio-cpu-int8-storage

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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 (#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 #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 #17167, ROCm, XPU, MPS, a CPU target in bf16 / fp16, LoRA loads (storage stays off under a LoRA, as in #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.

… of expanding them to float32

A CPU target computes in float32, and no Studio int8 runtime runs there, so the
single-file quant loader dequantized every int8 layer at load time: 4 bytes a
weight held for the whole session. The int8 storage layer from #17079 already
runs these layers bit-identically by widening each weight per forward.

- int8_storage_host: a float32 CPU target stores checkpoint int8 codes, and on
  CPU (never offloaded) this is the default; UNSLOTH_DIFFUSION_INT8_STORAGE=0
  still expands them.
- NativeWeightOnlyLinear: a float32 CPU forward widens into a reused per-thread
  float32 buffer (copy_ then mul_, bitwise the existing widen). A fresh
  weight-sized tensor per call was the bulk of the cost (page faults), which
  made the stored layers about 25% slower per step; with the buffer the step
  time matches the expanded model within noise. Not used under autograd or
  inside torch.compile.

CUDA, ROCm, XPU, the Turing W8A8 path and MPS are unchanged.
@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-09T11:21:01.072112Z b4a7f64 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2670fb9b0b

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

Comment on lines +479 to +481
if buf is None or buf.numel() < numel:
_CPU_WIDEN.buf = None
buf = _CPU_WIDEN.buf = torch.empty(numel, dtype = torch.float32)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Allocate the reusable scratch outside inference mode

If a thread first runs a CPU int8 layer under torch.inference_mode(), this allocation creates an inference tensor and retains it in _CPU_WIDEN.buf. A subsequent forward on that thread outside inference mode (including under torch.no_grad()) reuses the buffer whenever it is large enough, but _forward_weight then raises RuntimeError: Inplace update to inference tensor outside InferenceMode is not allowed at out.copy_. Because the cache is module-global, this also affects subsequent, unrelated models on the same thread. Allocate the scratch under torch.inference_mode(False) so it remains writable across both execution modes.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in abcd419: the buffer is now allocated under torch.inference_mode(False), with a test that runs a layer under inference_mode first and then under no_grad and with grad on.

A buffer first made under torch.inference_mode() is an inference tensor, and the
next forward on that thread under no_grad (or with grad on) then failed at the
in-place widen. Allocate it as a normal tensor so it serves both.
@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: abcd419b25

ℹ️ 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 added a commit to Imagineer99/unsloth that referenced this pull request Nov 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

(Python 3.13, rest) fails on this PR with a test this PR does not edit. It passes on main and fails on this PR's head locally, so it comes from the change:

studio/backend/tests/test_video_backend.py:12379: in test_an_explicit_int8_under_offload_on_nvidia_runs_native_on_both_experts
    assert all(c["offload"] is True and c["act_int8"] is True for c in calls)
E   assert False

The test loads Wan2.2 A14B with transformer_quant = "int8" under model offload on a stubbed NVIDIA card. It expects both experts to take the native int8 path (offload=True, act_int8=True). With the change in diffusion_native_quant.py / diffusion_transformer_quant.py, at least one expert no longer gets act_int8=True there. If that path should stay native on NVIDIA, the CPU-only branch probably needs a device check. If the behaviour change is intended, the test needs updating with it.

test_an_explicit_int8_under_offload_on_nvidia_runs_native_on_both_experts
main 1 passed
this PR 1 failed

… path

native_int8_act returned False for any int8 storage host, and the CPU
fp32 storage branch made that cover targets reporting a native offload or
native quant host, turning off act_int8 for the explicit int8 video
offload path. Only short-circuit to weight-only storage when the host has
no native W8A8 path.

[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. Bravo.

Reviewed commit: b4a7f64261

ℹ️ 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 added a commit that referenced this pull request Nov 9, 2026
… a fixed sleep (#17234)

## Summary

`tests/studio/playwright_tool_activity.py` scene 3 clicks the controlled
and uncontrolled tool cards back to back, sleeps a fixed 600 ms, then
reads `aria-expanded`. On a loaded runner the second click is sometimes
lost: the first card's 200 ms Radix expansion is still moving the second
trigger, so the gate reports

```
PROBLEMS (chromium):
  - 3: uncontrolled did not open on click
```

while the controlled card opened fine. It has turned `Chat UI Tests
(extra)` red on unrelated PRs three times since yesterday:

- #17118 (staging run, Nov 8)
- #17213 (Nov 9)
- #17178 (staging run, Nov 9, a backend-only int8 change)

No frontend file changed on `main` between the last green run of that
job and those failures.

The fix opens each card in turn and waits, bounded at 5 s, until its
trigger reports `aria-expanded="true"` before clicking the next one. The
scene still reads `aria-expanded` afterwards and records the same
problem, so a card that really does not open on click still fails the
gate.

## Changes

- `tests/studio/playwright_tool_activity.py`: new `click_open(page,
name)` helper, used for the two scene 3 clicks. Every other scene is
unchanged.

## Testing

- Three local runs of the gate on Chromium: `problems == []` each time.
- Sabotage: with a capture-phase listener that swallows clicks on the
uncontrolled trigger, the gate still fails with `3: uncontrolled did not
open on click`.
- The 22 test files that read the Playwright drivers or
`_playwright_robust` pass (714 passed), and ruff and the exec-literal
lint are clean.
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 9, 2026
[skip ci]
@danielhanchen
danielhanchen merged commit 1489693 into main Nov 9, 2026
@danielhanchen
danielhanchen deleted the studio-cpu-int8-storage branch November 9, 2026 13:57
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