Skip to content

fix(offload): leave a normal in-session model after disk-offloaded quantize (#4450) - #4498

Merged
Qubitium merged 1 commit into
mainfrom
fix/in-session-after-quantize-4450-4451
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/in-session-after-quantize-4450-4451

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

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:

  • Every forward re-read the bundles from disk.
  • model.to("cuda") failed with "Cannot copy out of meta tensor".
  • After the run-dir finalizer ran (del wrapper + gc), forward raised FileNotFoundError.
  • AWQ/QQQ rehome saw only meta qweights and kept CUDA-only kernels on CPU.
  • FP8/GGUF/RTN left the layer norms on cuda:0 next to a CPU model.

Change

At loop exit (ModuleLooper.loop, WeightOnlyLooper.loop):

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;
Loading

Measured (Qwen2.5-0.5B-Instruct, RTX 4090, default offload)

Method Before In-session CPU NLL Reload (Torch, CPU) NLL / max logit diff In-session PPL (cuda) Reload PPL (cuda, AUTO)
GPTQ v1 168 hooks / 912 meta, .to() fails 3.01487 3.01487 / 0.0 20.0945 20.1028
GPTQ v2 same 3.01487 3.01487 / 0.0 20.0945 20.1028
AWQ CUDA op on CPU 3.15779 3.15779 / 0.0 23.5629 23.5649
FP8 norms on cuda:0 2.89791 2.89791 / 0.0 18.7225 18.7299
GGUF norms on cuda:0 2.99465 2.99465 / 0.0 20.7839 20.7917
RTN norms on cuda:0 3.27720 3.27720 / 0.0 24.8144 24.7993
QQQ (bf16) .to() fails 3.09397 (= offload off) — 21.1721 (= offload off) 21.1749

For every method, the forward is bitwise unchanged after the run-dir finalizer or del wrapper + gc, and model.to("cuda:0") + generate work.

Tests

  • tests/test_offload_quantized_modules_restored_4450.py:
    • stub-looper restore for both loopers;
    • wrapper teardown;
    • strict failure;
    • forward dispatch after the class switch;
    • movable weight-only base modules;
    • a CUDA end-to-end tiny RTN/GPTQ model (in-session == reloaded bitwise, plain CPU model, survives teardown).
  • tests/test_rehome_cpu_fallback_pairs.py: every CPU-fallback pair from the kernel registry keeps complete target state and a bitwise-equal forward after rehome_quantized_kernel.
  • After rebasing on d4015a0, RTX 4090: 161 passed across the offload, rehome, weight-only, qzeros, checkpoint-dtype and validation suites. The 2 test_weight_only_looper failures also fail on main (one assumes offload is off; the other needs several GPUs). ruff is clean.

Docs: docs/in-session-model-after-offload.md (new); docs/offload-run-directories.md updated.

🤖 Generated with Claude Code

…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant