Repository navigation
fix(dispatch): keep a whole-model cpu> placement off the accelerator (#6592) - #6594
Merged
Merged
Conversation
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]>
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.
Fixes #6592
Problem
docs/weight-loader.mdsays"cpu>accel:N"keeps weights on CPU. On a quantized load,simple_dispatch_modeldrives eachcpu>entry throughCpuOffload. 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-modelplacement="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:0holds 985.6 MiB aftergenerate, the same asaccel:0.Change
_split_cpu_offload_entries(gptqmodel/utils/model.py): acpu>entry that spans placement blocks is replaced by one entry per placement unit (decoder layers and top-level units). Each unit becomes its ownCpuOffloadchain 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-layercpu>rules.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-moduleCpuOffloaddid.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:#b02a37Test 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 throughsimple_dispatch_modelwith{"": "cpu>cuda:0"}. It checks four things:generaterun, and layers execute on the accelerator;test_quantized_cpu_offload_releases_vram_after_generate:GPTQModel.load(ModelCloud/Llama-3.2-1B-gptqmodel-ci-4bit)withaccel:0and withcpu>accel:0, then 8-token greedygenerate. Allocated VRAM aftergenerateis compared withaccel:0, and the tokens must match.origin/main(red)6821376 < 6820352 // 8,8918528 < 8917504 // 8; quantized(1033491968, 1033491968), i.e. cpu> resident == accel:0 resident(1032443392, 1030011392)Measured directly on the R9700 (Llama-3.2-1B GPTQ 4-bit g128 quantized there, 8-token greedy
generate):accel:0cpu>accel:0, maincpu>accel:0, this branchwikitext2 PPL over 8 x 2048 windows:
cpu>accel:0on this branch 13.6148, residentaccel:013.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 aretest_hf_bridge_cpu_offload_5022.py::test_native_load_uses_an_offload_safe_kernel[late-layer|embeddings], which fail identically on cleanorigin/mainon ROCm ("native load pickedTorchLinear"), 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 everyModule.to; that is a separate follow-up. Callers who want speed over VRAM can keep usingaccel:0, or per-layercpu>rules for a partial offload.🤖 Generated with Claude Code