Repository navigation
fix(offload): leave a normal in-session model after disk-offloaded quantize (#4450) - #4498
Merged
Merged
Conversation
…antize (#4450) With the default offload_to_disk=True the loop restored only the base modules. Packed modules stayed meta behind accelerate offload hooks, so the in-session model re-read its weights from disk on every forward, could not be moved with .to(), broke once the run-directory finalizer ran, and AWQ/QQQ rehome saw only meta qweights and kept CUDA-only kernels on CPU. At loop exit (ModuleLooper and WeightOnlyLooper): - restore_disk_offloaded_modules materializes every hooked module on CPU (live GPTQ qzero layout via the existing weights map) and removes hooks; - undo_offload_to_disk drops the instance-bound forward accelerate leaves behind, so a later __class__ kernel switch dispatches to the new class; - materialize_remaining_meta_modules loads non-quantized modules the lazy shell left on meta (untied lm_head); - replace_inference_parameters rebinds lazily materialized base-module parameters so model.to() works. The weight-only looper always runs post_quantize, so layer norms no longer stay on the accelerator. Peak memory during the loop is unchanged. Also add a registry-driven guard over every CPU-fallback pair that rehome_quantized_kernel can reach (#4451 regression class). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 17, 2026
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 #4450
Refs #4451 (fixed for QQQ by #4490; this adds a registry-wide guard for every CPU-fallback kernel switch)
The problem
With the default
offload_to_disk=True,quantize()returned a model whose packed layers were still meta tensors behind accelerate disk-offload hooks:model.to("cuda")failed with "Cannot copy out of meta tensor".delwrapper + gc), forward raisedFileNotFoundError.qweights and kept CUDA-only kernels on CPU.cuda:0next to a CPU model.Change
At loop exit (
ModuleLooper.loop,WeightOnlyLooper.loop):restore_disk_offloaded_modulesloads every hooked module back into CPU memory and removes the hooks. GPTQ gets its live v2 qzeros through the [BUG] In-session use afterquantize()with the defaultoffload_to_disk=Truegives garbage GPTQ output: offloaded layers are reloaded with v1 qzeros but decoded as v2 (incomplete fix of #4203) #4380 weights map.undo_offload_to_diskdrops the instance-boundforwardthat accelerate leaves behind, so the later AWQ/QQQ/GGUF__class__switch dispatches to the portable kernel.materialize_remaining_meta_modulesloads non-quantized modules that the lazy shell left on meta, such as an untiedlm_head.replace_inference_parametersmakes lazily materialized parameters movable with.to().post_quantize(it is meta-safe), so norms follow the layer to CPU.Peak memory during the loop is unchanged; only the end state changes, to match
offload_to_disk=False. Run directories are still removed at teardown (#4368), and the live model no longer reads from them.flowchart TD L["_loop_impl() returns"] --> B["_restore_offloaded_base_modules(strict=True)"] B --> Q["restore_disk_offloaded_modules(model, CPU, strict=True)<br/>undo_offload_to_disk per hooked module + _drop_restored_class_forward"] Q --> W{"WeightOnlyLooper?"} W -- yes --> M["_materialize_meta_base_modules + replace_inference_parameters"] W -- no --> R M --> R["materialize_remaining_meta_modules (untied lm_head)"] R --> K["rehome_quantized_kernel: CPU kernel for AWQ/QQQ/GGUF"] Q -. "module still meta/hooked" .-> F["RuntimeError: reload the saved checkpoint"] classDef failure fill:#ffe8e8,stroke:#b30000,color:#111; class F failure;Measured (Qwen2.5-0.5B-Instruct, RTX 4090, default offload)
.to()fails.to()failsFor every method, the forward is bitwise unchanged after the run-dir finalizer or
delwrapper + gc, andmodel.to("cuda:0")+generatework.Tests
tests/test_offload_quantized_modules_restored_4450.py:tests/test_rehome_cpu_fallback_pairs.py: every CPU-fallback pair from the kernel registry keeps complete target state and a bitwise-equal forward afterrehome_quantized_kernel.test_weight_only_looperfailures also fail on main (one assumes offload is off; the other needs several GPUs).ruffis clean.Docs:
docs/in-session-model-after-offload.md(new);docs/offload-run-directories.mdupdated.🤖 Generated with Claude Code