Skip to content

fix(offload): keep loader dispatch hooks and release skipped modules/buffers (#6180, #6156) - #6271

Merged
Qubitium merged 1 commit into
mainfrom
fix/offload-hooks-consolidated
Nov 29, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/offload-hooks-consolidated

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #6180
Fixes #6156

Summary

Consolidates #6225 and #6242 into one change. Both PRs fixed different bugs in the same attach_disk_offload_hook() / undo_offload_to_disk() region of gptqmodel/utils/offload.py, so they conflicted. This PR carries every behavioural change and every test from both.

#6180 — release skipped modules and non-persistent buffers

  • _offload_disk_locked() moves a module skipped by the 4 KiB cutoff to CPU instead of leaving it on the quantization device.
  • undo_offload_to_disk() moves non-persistent buffers of offloaded modules (inv_freq, original_inv_freq, SigLIP position_ids) to the requested restore device after detach_hook, since they are absent from the offload index and are not meta leaves.
  • restore_disk_offloaded_modules() removes the no-op root io_same_device hook that disk_offload() leaves behind once the subtree is fully materialized.

#6156 — keep loader dispatch hooks across base-module restore

  • 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() reattaches that snapshot after removing the temporary disk-offload hooks.
  • undo_offload_to_disk() is a no-op for modules that were never actually offloaded (below the 4 KiB cutoff or tensor-less, e.g. model.norm / rotary_emb) instead of stripping their unrelated hooks.

Conflict resolution

undo_offload_to_disk() now computes one predicate — a tree was offloaded iff it still has meta leaves or a disk-offload hook — and both rules hang off it:

There is one early return, not two competing ones. The skipped-module release in _offload_disk_locked() is what makes the #6156 no-op path safe for #6180: the skipped module has already been moved to CPU by the time the restore visits it.

Files

  • gptqmodel/utils/offload.py
  • tests/test_offload_base_modules_restored.py
  • tests/test_offload_quantized_modules_restored_4450.py

Tests

CPU-only (as required; the #6180 node is CUDA-marked and skips):

cd /root/openai-audit/issue-offload/tests && \
PYTHONPATH=/root/openai-audit/issue-offload:/root/openai-audit/issue-offload/tests \
CUDA_VISIBLE_DEVICES="" /root/venv/bin/python -m pytest \
  test_offload_quantized_modules_restored_4450.py \
  test_offload_base_modules_restored.py \
  test_offload_*.py test_rehome_*.py test_hf_bridge_cpu_offload_5022.py \
  -q -rs -p no:cacheprovider
# 181 passed, 20 skipped

Both new nodes on one CUDA device (CUDA_VISIBLE_DEVICES=1):

cd /root/openai-audit/issue-offload/tests && \
CUDA_VISIBLE_DEVICES=1 \
PYTHONPATH=/root/openai-audit/issue-offload:/root/openai-audit/issue-offload/tests \
/root/venv/bin/python -m pytest \
  test_offload_quantized_modules_restored_4450.py \
  test_offload_base_modules_restored.py -q -rs -p no:cacheprovider
# 21 passed

Lint:

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

Red evidence on the combined branch

Both test files kept, combined source reverted (git checkout origin/main -- gptqmodel/utils/offload.py).

#6156 node

cd /root/openai-audit/issue-offload/tests && \
CUDA_VISIBLE_DEVICES="" \
PYTHONPATH=/root/openai-audit/... /root/venv/bin/python -m pytest \
  test_offload_base_modules_restored.py::test_restore_offloaded_modules_preserves_loader_dispatch_hooks \
  -q -rs -p no:cacheprovider
# 1 failed
E       AssertionError: assert None is AlignDevicesHook(execution_device=cpu, offload=False, io_same_device=True, ...)
E        +  where None = <built-in method get of dict object at ...>('_hf_hook')
test_offload_base_modules_restored.py:163: AssertionError

#6180 node

cd /root/openai-audit/.../tests && \
CUDA_VISIBLE_DEVICES=1 PYTHONPATH=/root/openai-audit/... \
/root/venv/bin/python -m pytest \
  test_offload_quantized_modules_restored_4450.py::test_llava_offload_hook_round_trip_leaves_no_accelerator_tensors \
  -q -rs -p no:cacheprovider
# 1 failed
E       AssertionError: assert {'model.langu...eq': 'cuda:0'} == {}
E         Left contains 4 more items:
E         {'model.language_model.norm.weight': 'cuda:0',
E          'model.language_model.rotary_emb.inv_freq': 'cuda:0',
E          'model.language_model.rotary_emb.original_inv_freq': 'cuda:0',
E          'model.vision_tower.embeddings.position_ids': 'cuda:0'}
test_offload_quantized_modules_restored_4450.py:662: AssertionError

Green evidence

With the combined source restored, the CPU-only family is 181 passed, 20 skipped, the two test files on CUDA are 21 passed, and both new nodes pass individually (test_restore_offloaded_modules_preserves_loader_dispatch_hooks and test_llava_offload_hook_round_trip_leaves_no_accelerator_tensors).

Skips / unverified

Supersedes

Closes #6225 and #6242, which edited the same undo_offload_to_disk() region.

…buffers (#6180, #6156)

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.
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 08:20
@Qubitium
Qubitium merged commit cb1530d into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/offload-hooks-consolidated branch November 29, 2026 08:20
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