Skip to content

fix(offload): keep loader dispatch hooks across base-module restore (#6156) - #6242

Closed
Qubitium wants to merge 1 commit into
mainfrom
fix/6156-issue
Closed

Qubitium wants to merge 1 commit into
mainfrom
fix/6156-issue

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #6156.

requantize() on a checkpoint loaded across two GPUs completes, but the first in-session generate() fails with Expected all tensors to be on the same device because restore_offloaded_modules() -> undo_offload_to_disk() removes every accelerate hook on model.norm, model.rotary_emb, lm_head — including the GPTQModelAlignDevicesHook the multi-GPU loader installed. The weights return to their devices, but without the hook the norm no longer moves its input to its execution device.

What Changed

  • offload_to_disk() / attach_disk_offload_hook() snapshot resident (non-offload) accelerate hooks on the module subtree before disk_offload() wraps them.
  • undo_offload_to_disk() restores that snapshot after removing the temporary disk-offload hooks, so loader-installed dispatch hooks survive the round trip.
  • undo_offload_to_disk() is now a no-op for modules that were not actually offloaded (below the 4 KiB cutoff or tensor-less, e.g. model.norm / rotary_emb) instead of stripping their unrelated hooks.
  • Temporary quantization-time offload hooks are still removed, and the plain single-device restore path is unchanged.

Tests

Red at 2d3261d (fix removed from the working tree, regression test kept):

cd /root/openai-audit/issue-6156/tests && \
PYTHONPATH=/root/openai-audit/issue-6156:/root/openai-audit/issue-6156/tests \
/root/venv/bin/python -m pytest \
  test_offload_base_modules_restored.py::test_restore_offloaded_modules_preserves_loader_dispatch_hooks -q -rs

Result: 1 failed — AssertionError: assert None is AlignDevicesHook(...) for model.norm (the hook was stripped).

Green with the fix:

cd /root/openai-audit/issue-6156/tests && \
CUDA_VISIBLE_DEVICES="" \
PYTHONPATH=/root/openai-audit/issue-6156:/root/openai-audit/issue-6156/tests \
/root/venv/bin/python -m pytest test_offload_base_modules_restored.py -q -rs

Result: 5 passed.

Wider CPU-only offload family:

cd /root/openai-audit/issue-6156/tests && \
CUDA_VISIBLE_DEVICES="" \
PYTHONPATH=/root/openai-audit/issue-6156:/root/openai-audit/issue-6156/tests \
/root/venv/bin/python -m pytest \
  test_offload_autograd.py test_offload_files.py test_offload_reference_4395.py \
  test_offload_reference_rss_4463.py test_offload_quantized_modules_restored_4450.py \
  test_qzeros_disk_offload_4380.py test_offload_hooks_no_cache_flush.py -q -rs

Result: 121 passed, 7 skipped.

Also ran CPU-only: test_offload_disk_container_5145.py test_cancelled_quantize_default_offload_4860.py test_cancelled_quantize_disk_offload_4807.py test_run_exit_recovery_contract_5060.py test_post_loop_tail_failure_5023.py test_cpu_offload_device_map_tied_head_4866.py test_hf_bridge_cpu_offload_5022.py test_offload_run_cleanup_4368.py test_offload_run_namespace.py test_single_device_dispatch_4847.py test_rehome_hook_realign.py -q -rs -> 81 passed, 35 skipped.

Lint:

/root/venv/bin/ruff check gptqmodel/utils/offload.py tests/test_offload_base_modules_restored.py
# All checks passed!

CPU-only dispatch simulation

The regression test attaches the same GPTQModelAlignDevicesHook(execution_device=torch.device("cpu"), io_same_device=True, place_submodules=False) the loader attaches for a device map to model.norm, model.rotary_emb, and lm_head of a tiny untied Llama. It then runs capture_offloaded_base_modules() -> offload_to_disk() -> restore_offloaded_modules() and asserts each original hook object is still attached. lm_head is actually disk-offloaded; model.norm is below the 4 KiB cutoff and rotary_emb owns no tensors, so the restore visits them without offloading — exactly the path that stripped their hooks. Hook ownership/removal is device-agnostic; the two-GPU device mismatch itself was not reproduced (no GPU used).

Skips / unverified

  • 7 CPU-run skips are CUDA/MPS-only existing tests (test_offload_autograd.py:150, test_offload_files.py:979 and :2258, test_offload_quantized_modules_restored_4450.py:322, :380 x2).
  • Not verified on two real GPUs: the in-session generate() after requantize() was simulated at the hook level on CPU. The fix preserves the exact hook object, so the dispatch placement is retained.

Logic Flow Documentation

Not applicable: this is a hook-lifecycle fix inside the existing disk-offload/restore path; it does not change selection order or add a new decision mechanism.

@Qubitium

Copy link
Copy Markdown
Collaborator Author

Consolidated into #6271 (draft). This PR and #6225 both edit the same undo_offload_to_disk() region in gptqmodel/utils/offload.py, so #6271 carries both fixes and both test files with a single conflict-free resolution. Closing this PR in favour of #6271.

@Qubitium Qubitium closed this Nov 29, 2026
Qubitium added a commit that referenced this pull request Nov 29, 2026
…buffers (#6180, #6156) (#6271)

Consolidate #6225 and #6242, which both edited the disk-offload hook
lifecycle in gptqmodel/utils/offload.py.

#6180: move modules skipped by the 4 KiB cutoff to CPU, move
non-persistent buffers of offloaded modules to the requested restore
device after detach_hook, and drop the no-op io_same_device root hook
left by disk_offload().

#6156: snapshot resident (non-offload) accelerate hooks before
disk_offload() wraps them and reattach them after undo. A module that
was never offloaded is left untouched.

The two rules share one predicate in undo_offload_to_disk(): a tree was
offloaded iff it still has meta leaves or a disk-offload hook. A
never-offloaded tree restores its snapshot and returns; only an
offloaded tree computes and releases non-persistent buffers.
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