Repository navigation
Studio: keep single-file int8 denoiser weights as int8 on CPU instead of expanding them to float32 - #17178
Conversation
… 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.
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. |
|
@codex review |
There was a problem hiding this comment.
💡 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".
| if buf is None or buf.numel() < numel: | ||
| _CPU_WIDEN.buf = None | ||
| buf = _CPU_WIDEN.buf = torch.empty(numel, dtype = torch.float32) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
|
The test loads Wan2.2 A14B with
|
… 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]
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. 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". |
… 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.
[skip ci]
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 withUNSLOTH_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=0still expands them (the existing kill switch).NativeWeightOnlyLinear: a float32 CPU forward widens into a reused per-thread float32 buffer (copy_from the codes, thenmul_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, andweightnever 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_tensorwiseQwen-Image checkpoint built from the realQwen/Qwen-Imagetransformer 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.torch.equalon the final step output).Per-layer timing showed why the buffer is needed.
torch._weight_int8pack_mmon CPU was 15x to 60x slower than float32 GEMM at 256 to 4096 rows, and a single mixed int8 x float32torch.mulwas slower than the existing chunked widen. Neither is used.Tests
New in the single-file quant loader tests (
studio/backend/tests/), run on CPU:=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, andweightis not the buffer.test_cpu_forward_under_autograd_keeps_its_own_weightandtest_cpu_native_linear_compiles_without_a_graph_break(Dynamofullgraph=True, dynamic shapes).native_int8_actis 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.pyandtest_diffusion_native_quant_mps.pypass, 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