Skip to content

fix(offload): release skipped modules and non-persistent buffers (#6180) - #6225

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

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

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #6180

Summary

The LLaVA and Gemma 3 VL pre-quantize hooks move embed_tokens, norm, rotary_emb, vision_tower and multi_modal_projector onto the quantization device, then release them through offload_to_disk() for the layer loop. Two generic gaps left tensors behind on the accelerator:

  • Modules whose state_dict() is at or below the 4 KiB floor were skipped without being moved, so a small norm (and a rotary module whose only state is non-persistent buffers) stayed on CUDA.
  • Accelerate's hook leaves non-persistent buffers on its execution device. undo_offload_to_disk() only re-materialized meta leaves, so position_ids and inv_freq stayed on CUDA after the rest of the module was restored to CPU.

This PR:

  • moves a skipped small module to CPU in offload_to_disk();
  • moves non-persistent buffers of offloaded modules to the requested restore device after detach_hook has run;
  • removes the no-op io_same_device root hook that disk_offload() leaves on a module after its hooked children are restored one by one.

Disk offload stays enabled for the layer loop; the memory benefit is unchanged.

Red evidence

Base b95b4c6 with only the new test (source fix reverted):

cd /root/openai-audit/issue-6180/tests
CUDA_VISIBLE_DEVICES=4 PYTHONPATH=/root/openai-audit/issue-6180:/root/openai-audit/issue-6180/tests \
  /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
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'}

The same state also reproduced the reported end-to-end failure: a tiny local LLaVA fixture quantized with the default offload_to_disk=True on cuda:0, then forwarded with an image:

RuntimeError: Expected all tensors to be on the same device, but got index is on cuda:0,
different from other tensors on cpu (when checking argument in method wrapper_CUDA__index_select)

Green evidence

cd /root/openai-audit/issue-6180/tests
CUDA_VISIBLE_DEVICES=4 PYTHONPATH=/root/openai-audit/issue-6180:/root/openai-audit/issue-6180/tests \
  /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
# 1 passed

CUDA_VISIBLE_DEVICES=4 PYTHONPATH=/root/openai-audit/... /root/venv/bin/python -m pytest \
  test_offload_quantized_modules_restored_4450.py -q -rs
# 16 passed

PYTHONPATH=/root/openai-audit/... /root/venv/bin/python -m pytest \
  test_offload_files.py test_offload_hooks_no_cache_flush.py \
  test_offload_base_modules_restored.py test_offload_run_namespace.py \
  test_offload_disk_container_5145.py test_offload_reference_4395.py \
  test_ernie4_5_vl_moe_offload_hook.py -q -rs
# 135 passed

End-to-end tiny local LLaVA after the fix:

noncpu: {}
generate ok torch.Size([1, 8])

Lint:

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

Test

tests/test_offload_quantized_modules_restored_4450.py::test_llava_offload_hook_round_trip_leaves_no_accelerator_tensors

It drives the real LlavaQModel.pre_quantize_generate_hook_start/end pair on a tiny native LLaVA model, restores the disk-offloaded modules, asserts every parameter/buffer is on CPU and no offload hooks remain, then runs an image forward.

Skips / unverified

  • The new node is CUDA-marked and skips on CPU-only hosts; the split-device state cannot exist without an accelerator.
  • Gemma 3 VL was not run end-to-end (no tiny Gemma 3 fixture in this change). Its embed_scale and rotary inv_freq tensors go through the same generic offload_to_disk/undo_offload_to_disk paths covered here.
  • tests/test_llava_qwen2_local_port.py was not run: this environment lacks timm (ImportError from ports/llava_qwen2/fastvit.py), unrelated to this change.

LLaVA and Gemma 3 VL pre-quantize hooks move the vision tower and small language-model modules onto the quantization device, then offload them to disk for the layer loop. offload_to_disk() skipped sub-4KB modules without moving them, and accelerate left non-persistent buffers (position_ids, rotary inv_freq) on the execution device. Restoring the model left those tensors on the accelerator while the rest was on CPU, so the first in-session image forward failed in position_embedding.

Move skipped small modules to CPU, move non-persistent buffers of offloaded modules to the requested restore device after hook detach, and drop the no-op disk-offload root hooks left behind by child-by-child restore.

Test: tests/test_offload_quantized_modules_restored_4450.py::test_llava_offload_hook_round_trip_leaves_no_accelerator_tensors
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Consolidated into #6271 (draft). This PR and #6242 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