Skip to content

Studio: quantise the hosted int8 leftovers on the GPU on offloaded loads - #17002

Merged
danielhanchen merged 5 commits into
mainfrom
studio-coldload-fastpath
Nov 7, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
studio-coldload-fastpath

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Summary

The hosted Qwen-Image-2512 int8 file stores 360 Linears as int8. It leaves the text-stream projections (add_q/k/v_proj, to_add_out, txt_mlp) and the 120 img_mod / txt_mod modulations dense, and the runtime quantises them after the load. On an offloaded load, where the transformer stays on the host, the two runtime passes ran like this:

  • the text-stream leftovers hopped to the GPU one module at a time, through pageable host memory;
  • the modulations were split (outlier columns) and quantised on the CPU.

On a fresh 8 GiB load on a Colab A100 that came to about 10 s plus 41-44 s of the load.

This PR sends both passes through one helper. Each host-placed Linear is uploaded to the CUDA device through page-locked copies, split and quantised there, and moved back. The next module's upload overlaps the current quantise on a helper thread. The codes, the scales and the outlier columns are the same as the CPU path: the helper only moves tensors, and torchao's per-row int8 math gives the same bits on both devices (checked below). Without a CUDA device, or for a tensor that cannot be staged exactly, the old in-place / module.to path runs.

The integrity fingerprint is untouched: it still hashes every quantised weight.

Measurements

Colab A100-40 GB, Qwen/Qwen-Image-2512 with the default int8 build (hosted unsloth/Qwen-Image-2512-FP8 int8 + int8 ConvRot encoder), 1024x1024, 20 steps, CFG 4. Each arm runs in a fresh process with a fresh Studio home, and base and head are interleaved. Base is main 3d41bf4af3, and head is this branch.

8 GiB VRAM budget (streamed placement), final head:

fresh home base head
load (s) 110.1, 86.0, 85.5 (median 86.0) 55.2, 48.7, 48.2 (median 48.7)
first image (s) 57.5, 48.7, 49.2 48.2, 49.1, 48.7
load + first image (s) 167.7, 134.7, 134.7 (median 134.7) 103.4, 97.8, 96.9 (median 97.8)
steady image (s) 31.95, 31.93, 31.88 31.94, 31.89, 31.93
peak VRAM own / nvidia-smi (GiB) 6.58 / 38.31 6.58 / 38.31
process tree peak RSS (GiB) 60.1, 60.4, 60.3 58.5, 60.6, 60.6
text-stream leftovers quantise (log) 9-12 s 9 s
modulation quantise (log) 41-44 s 9-10 s

The first base arm of each run is slower, because it reads the checkpoint cold. An earlier run of the first commit alone (no page-locked staging) gave a median load of 88.6 s for base and 56.7 s for head.

Resident (no budget): load 24.2 s vs 23.9 s, first image 26.2 s vs 26.1 s, steady 11.26 s on both. The resident path never quantises on the host, so this is the no-change control.

Outputs: every image is bit-identical between base and head (same file md5 for all 3 prompts in every pair, at 8 GiB and resident).

Quantise parity on the A100 (torch 2.13.0+cu130, torchao 0.18.0): the CPU path and the CUDA hop were run on the Qwen-Image Linear shapes (3072x18432, 3072x3072, 3072x12288, 12288x3072). This covered int8 weight-only with and without 4 outlier columns, and W8A8. All of them give identical canonical weight digests and identical dense tensors (PARITY_OK).

Tests

  • tests/test_diffusion_quant_hop.py (new) covers four cases:
    • the work device only resolves for CUDA;
    • modules quantise on the work device and go home;
    • a module already off the host is never moved;
    • a raise leaves every module on its home device, and the staged upload refuses anything it cannot copy exactly.
  • tests/test_diffusion_qwenimage_mod_int8.py, tests/test_diffusion_qwenimage_text_int8.py and tests/test_diffusion_prequant.py pass unchanged.

…e uploads

The hosted Qwen-Image int8 file leaves the text-stream projections and the img_mod/txt_mod
modulations dense. On an offloaded load both were quantised on the host: the leftovers hopped
to the GPU one module at a time, the modulations were split and quantised on the CPU. Both now
go through one helper that quantises each host-placed Linear on the CUDA device and moves it
back, with the next upload overlapping the current quantise. Same codes, scales and outlier
columns as the CPU path.
A pageable host-to-device copy of the dense leftovers ran at about 1-2 GB/s on the Colab A100
host (11 s for the 360 text-stream Linears, 13 s for the 120 modulations). Each host tensor now
goes through a pinned copy (the caching host allocator, emptied after the loop) and a
non-blocking upload; anything that cannot be staged exactly falls back to module.to.
@danielhanchen danielhanchen self-assigned this Nov 7, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 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-07T09:50:32.689748Z b22df9e 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: b8ed270884

ℹ️ 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 +2169 to +2172
work = torch.device(device)
except Exception: # noqa: BLE001 - an unparsable device quantises in place
return None
return work if work.type == "cuda" else None

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 Resolve the CUDA ordinal before starting the upload thread

When device is the unindexed "cuda" and the loading thread has selected a nonzero GPU, this leaves work.index unset. _hop_loop now executes uploads on a fresh executor thread, which does not inherit the caller's thread-local CUDA device, so .to(work) resolves to GPU 0 instead of the selected GPU. This is reachable through the cached-prequant loader in diffusion.py, which passes target.device (intentionally unindexed in DiffusionDeviceTarget). Consequently, offloaded loads can allocate large temporary weights on another user's GPU or fail quantization because GPU 0 lacks memory, whereas the previous synchronous loop used the caller's selected device. Resolve an omitted index using torch.cuda.current_device() before handing the device to the worker, or explicitly propagate the caller's CUDA device into that thread.

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 b22df9e: _hop_device resolves an unindexed cuda to torch.cuda.current_device() in the calling thread before the upload thread starts. Added a test for the resolution.

The hop now uploads on a helper thread, which does not inherit the loading thread's current
CUDA device, so an unindexed "cuda" (the cached prequant loader passes one) would land the
uploads on cuda:0. _hop_device resolves it to the caller's current card first.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: b22df9e2c6

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant