Skip to content

fix(dispatch): keep a whole-model cpu> placement off the accelerator (#6592) - #6594

Merged
Qubitium merged 1 commit into
mainfrom
fix/cpu-offload-single-entry
Dec 1, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/cpu-offload-single-entry

Conversation

@Qubitium

@Qubitium Qubitium commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Fixes #6592

Problem

docs/weight-loader.md says "cpu>accel:N" keeps weights on CPU. On a quantized load, simple_dispatch_model drives each cpu> entry through CpuOffload. That hook moves its module to the accelerator on use and moves it back only when the next member of the chain runs. A whole-model placement="cpu>accel:0" is a chain of one, so after the first forward the whole model stays resident. On Llama-3.2-1B GPTQ, cpu>accel:0 holds 985.6 MiB after generate, the same as accel:0.

Change

  • _split_cpu_offload_entries (gptqmodel/utils/model.py): a cpu> entry that spans placement blocks is replaced by one entry per placement unit (decoder layers and top-level units). Each unit becomes its own CpuOffload chain member, so at most one unit is on the accelerator at a time, and the existing first-offloads-last wrap applies. An entry that owns tensors directly is left whole. More specific rules keep their own targets. Entries without blocks are unchanged, including per-layer cpu> rules.
  • The split entry module gets two hooks:
    • AlignDevicesHook(execution, io_same_device=True, place_submodules=False) keeps activations on the execution device between units and returns outputs to the caller's device, as the whole-module CpuOffload did.
    • New CpuOffloadChainTail (weight_loader/hooks.py) sends the chain's last unit back to CPU when the entry module's forward returns.
  • docs/weight-loader.md: one bullet.
flowchart TD
    A["placement = cpu>accel:0 (one entry: '')"] --> B{"entry spans blocks?"}
    B -. "before: no split" .-> X["CpuOffload(whole model)<br/>chain of one: never offloaded<br/>985.6 MiB resident after generate"]
    B -- "after: yes" --> C["one entry per placement unit<br/>embed, layers.0..N, norm, lm_head"]
    C --> D["CpuOffload chain: each unit offloads the previous one<br/>first offloads last"]
    D --> E["root: AlignDevicesHook(io_same_device) + CpuOffloadChainTail"]
    E --> F["forward returns: last unit back to CPU<br/>0 MiB resident"]
    B -- "no (e.g. per-layer rules)" --> G["unchanged"]
    style X fill:#f8d7da,stroke:#b02a37
Loading

Test plan

New tests/test_cpu_offload_whole_model_residency_6592.py:

  • test_whole_model_cpu_offload_is_not_resident_after_forward[tied|untied]: tiny Llama through simple_dispatch_model with {"": "cpu>cuda:0"}. It checks four things:
    • forward and generate run, and layers execute on the accelerator;
    • logits match a CPU FP32 reference;
    • allocated VRAM after the calls is under 1/8 of the weight bytes;
    • all parameters are back on CPU.
  • test_quantized_cpu_offload_releases_vram_after_generate: GPTQModel.load(ModelCloud/Llama-3.2-1B-gptqmodel-ci-4bit) with accel:0 and with cpu>accel:0, then 8-token greedy generate. Allocated VRAM after generate is compared with accel:0, and the tokens must match.
R9700 gfx1201 (ROCm 7.16) RTX 4090 (CUDA)
origin/main (red) 3 failed: tiny 6821376 < 6820352 // 8, 8918528 < 8917504 // 8; quantized (1033491968, 1033491968), i.e. cpu> resident == accel:0 resident 3 failed: same tiny numbers; quantized (1032443392, 1030011392)
this branch (green) 3 passed 3 passed

Measured directly on the R9700 (Llama-3.2-1B GPTQ 4-bit g128 quantized there, 8-token greedy generate):

placement allocated after load after generate peak tokens
accel:0 985.6 MiB 985.6 MiB 1066.8 MiB "... is Paris"
cpu>accel:0, main 0.0 MiB 985.6 MiB 1066.8 MiB same
cpu>accel:0, this branch 0.0 MiB 0.0 MiB 583.2 MiB (largest unit: tied embedding/lm_head) same

wikitext2 PPL over 8 x 2048 windows: cpu>accel:0 on this branch 13.6148, resident accel:0 13.6148.

Regression suites (R9700, HIP 2+3), run on this branch: test_weight_loader test_cpu_offload_device_map_tied_head_4866 test_cpu_offload_tied_both_copies_4943 test_hf_bridge_cpu_offload_5022 test_optimize_generate_cpu_offload_5099 test_marlin_cpu_offload_post_init test_offload_other_device_io_6558 test_offload_block_inputs test_layerwise_device_placement test_offload_autograd test_explicit_placement_tied_head_6513 test_loader test_rehome_hook_realign. Result: 242 passed, 6 skipped, 2 failed. The 2 failures are test_hf_bridge_cpu_offload_5022.py::test_native_load_uses_an_offload_safe_kernel[late-layer|embeddings], which fail identically on clean origin/main on ROCm ("native load picked TorchLinear"), so they are unrelated. After the rebase onto current main, the new tests, test_weight_loader, 4866 and 4943 give 93 passed, 6 skipped. test_docs_contract_refs -k weight-loader: 2 passed. uvx [email protected] check: clean.

Cost (not changed by this PR, noted for review)

Keeping weights on CPU means every token re-uploads every unit, so decode is slower than resident. On this heavily loaded host, 8 tokens took 27.5 s. A profile shows about 45% of that time in TorchLinear.clear_weight_cache -> _refresh_decode_dtypes -> scales_fit_dtype, which runs on every Module.to; that is a separate follow-up. Callers who want speed over VRAM can keep using accel:0, or per-layer cpu> rules for a partial offload.

🤖 Generated with Claude Code

simple_dispatch_model drove a cpu> entry through one CpuOffload, which
moves its module to the accelerator and back only when the next chain
member runs. A whole-model placement="cpu>accel:0" was a chain of one,
so after the first forward the quantized model stayed fully resident
(985.6 MiB on Llama-3.2-1B, same as accel:0). Split such an entry into
its placement units and offload the chain's last unit when the entry
module returns.

Fixes #6592

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

[BUG] Quantized load with placement="cpu>accel:0": whole model becomes resident on the accelerator after the first forward (no VRAM saving)

1 participant