Skip to content

fix: restore offloaded base modules after quantization (#4203) - #4295

Merged
Qubitium merged 1 commit into
mainfrom
pr/4203
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
pr/4203

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

With the default offload_to_disk=True, quantization offloads the base modules
(embed_tokens/lm_head) before the layer loop and never restores them.
LazyTurtle.materialize_submodule then calls tie_weights() after every layer,
re-pointing lm_head.weight at the meta embedding parameter, which has no
offload hook. After quantize() the model reports device == meta, CPU
input_ids silently generate garbage, and string prompts / CUDA inputs crash.

This restores the offloaded base modules when the quantization loop exits so the
in-session model is usable again. If restoration cannot materialize a module, the
run raises a clear "reload the saved checkpoint" error instead of returning a
broken model.

What Changed

  • gptqmodel/utils/offload.py
    • Add capture_offloaded_base_modules() to snapshot base modules (and their
      device) before offload, skipping modules that are already on meta.
    • Add restore_offloaded_modules() to materialize them again, re-tie shared
      weights, and raise the reload error in strict mode.
    • Guard undo_offload_to_disk()'s tie_weights() call with callable():
      submodules such as LlamaRotaryEmbedding expose config but have no
      tie_weights(), which previously made a restore attempt raise.
  • gptqmodel/looper/module_looper.py: record the offloaded base modules in
    _loop_impl() and restore them in loop() (strict on success, best-effort on
    failure so the original exception is preserved).
  • gptqmodel/looper/weight_only_looper.py: same capture/restore for the
    weight-only lifecycle.

Tests

$ python3 -m pytest tests/test_offload_base_modules_restored.py -q
4 passed

$ python3 -m pytest tests/test_offload_base_modules_restored.py \
    tests/test_module_looper_disk_probe.py \
    tests/test_quantize_config_offload_tempdir.py \
    tests/test_checkpoint_processor_adapter.py \
    tests/test_offload_files.py -q
102 passed, 7 skipped

$ ruff check gptqmodel/utils/offload.py gptqmodel/looper/module_looper.py \
    gptqmodel/looper/weight_only_looper.py tests/test_offload_base_modules_restored.py
All checks passed!

End-to-end repro on a tiny tied Llama (CPU, 2 layers, offload_to_disk=True):
before the fix model.device=meta, embed=meta, lm_head=meta and logits were
~1e28; after the fix all three report cpu and the forward returns finite
logits.

Fixes #4203

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