Repository navigation
fix(offload): finish the in-session materialisation contract (#4572, #4562, #4597) - #4611
Merged
Merged
Conversation
This was referenced Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4572
Fixes #4562
Fixes #4597
TL;DR
Three gaps in the #4498 in-session contract are closed in the same loop-exit path: resumed packed modules now carry the offload hook the restore needs (#4562), tied
lm_heads are re-tied instead of read from a checkpoint that never stores them (#4597), and parameters left without a version counter byparam.data =are rebound (#4572). A model that still has meta tensors at loop exit now raises instead of only logging. Net: +166/−12 inutils/offload.py, +18 across the two loopers and the checkpoint adapter, 4 unit tests, 1 end-to-end resume test, 2 docs pages updated.1. Root causes
#4572 —
pre_quantizematerializes unquantized layers (MoE routers, dynamic-:exclusions, norms) undertorch.inference_mode();post_quantizethen moves them to CPU withparam.data = cpu_copy. The result reportsis_inference() == Falsebut has no version counter, soF.linearraisesInference tensors do not track version counter.replace_inference_parameterskeyed onis_inference(), so it never repaired them.#4562 —
gptq_checkpoint.pyrebuilds completed layers withrestore_packed_module(...)onmetaandset_submodules them with no accelerate offload hook.restore_disk_offloaded_modulesonly targets hooked modules, andmaterialize_remaining_meta_modulesskipsQUANT_TYPEmodules, so every resumed packed layer stayed meta with no diagnostic.#4597 — a tied checkpoint stores only the input embedding.
restore_offloaded_modulesties right after restoring base bundles, but the weight-only looper materializes the meta base modules afterwards, and nothing ties again beforematerialize_remaining_meta_modulesgives up onlm_head. The log-only path then led into a partial.to()and a CUDA device-side assert.replace_inference_parameterstested onlyis_inference()_tracks_version+ensure_version_countersrebind parameters whose._versionraises, at loop exitattach_disk_offload_hookinstalls the same hook a fresh offload installs, over the per-attemptindex.jsonAlignDevicesHook.original_devicesallmeta(so detach restored nothing)lm_headattempted from the checkpoint_retie_shared_embeddingsre-ties after materialization and before the remaining-meta verdictlog.errorRuntimeErrortelling the caller to reload the saved checkpoint2. New loop-exit flow
flowchart TD L["looper _loop_impl() returns"] --> B["_restore_offloaded_base_modules(strict=True)"] B --> Q["_restore_disk_offloaded_modules(strict=True)"] Q --> Q1["undo_offload_to_disk for every hooked module<br/>(fresh offloads and resumed ones)"] Q1 --> W{"WeightOnlyLooper?"} W -- yes --> M["_materialize_meta_base_modules(strict=True)"] W -- no --> R M --> R["_materialize_remaining_meta_modules()"] R --> T["_retie_shared_embeddings(model)<br/>tied lm_head aliases embed_tokens"] T --> V{"any non-quantized module still meta?"} V -- yes --> X["RuntimeError: reload the saved checkpoint"] V -- no --> C["ensure_version_counters(model)"] C --> K["rehome_quantized_kernel"] classDef failure fill:#ffe8e8,stroke:#b30000,color:#111; class X failure;3. Per-issue detail
flowchart TD A["quantize(checkpoint=...) resumes"] --> B["restore_packed_module -> meta"] B --> C["per-attempt index.json"] C --> D["attach_disk_offload_hook"] D --> E["loop-exit restore materializes CPU tensors"] E -. "bundle or index unreadable" .-> X["RuntimeError"] classDef failure fill:#ffe8e8,stroke:#b30000,color:#111; class X failure;flowchart TD P["pre_quantize: inference_mode materialization"] --> Q["post_quantize: param.data = cpu_copy"] Q --> R{"is_inference or _version usable"} R -- no --> S["ensure_version_counters: one fresh Parameter per aliased old object"] R -- yes --> U["untouched"] classDef bad fill:#fee2e2,stroke:#b91c1c,color:#7f1d1d; class S bad;4. Failure and ambiguity contract
RuntimeError(reload the checkpoint)lm_headafter base materialization.to()raised halfway, nextgenerate()hit a device-side assertembed_tokens; no metalm_headthat cannot be materializedRuntimeError(reload the checkpoint)param.data =F.linearraisedInference tensors do not track version counter.to()workQUANT_TYPE)5. Change inventory
gptqmodel/utils/offload.py_tracks_version, extendedreplace_inference_parameters,ensure_version_counters,attach_disk_offload_hook,_retie_shared_embeddings, loud remaining-meta failuregptqmodel/looper/module_looper.pyensure_version_countersafter restore/materializegptqmodel/looper/weight_only_looper.pygptqmodel/looper/gptq_checkpoint.pytests/test_offload_quantized_modules_restored_4450.pytests/test_checkpoint_quantization.py-:exclusiondocs/in-session-model-after-offload.md,docs/looper-checkpoint-redesign.md6. Tests / guarantees
test_unquantized_layers_keep_a_version_counter_after_the_cpu_moveparam.data =state rejects a forward;ensure_version_countersandreplace_inference_parametersrepair ittest_resumed_packed_module_hook_is_materialized_at_loop_exitattach_disk_offload_hook+ the strict restore, and its forward is bit-identicaltest_tied_vl_lm_head_is_retied_after_base_materializationlm_headends with zero meta tensors andlm_head.weight is embed_tokens.weighttest_untied_meta_output_embedding_fails_loudlylm_headthat cannot be loaded raisesRuntimeErrortest_resumed_quantize_leaves_a_usable_in_session_model-:^model\.layers\.1\.: zero meta tensors, every parameter has a usable_version, in-session forward works7. Validation
All runs with
OMP_NUM_THREADS=4 MKL_NUM_THREADS=4 OPENBLAS_NUM_THREADS=4 NUMEXPR_NUM_THREADS=4.Hardware: CPU-only tests; CUDA venv pinned to RTX 4090
GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae, ROCm venv to R9700HIP_VISIBLE_DEVICES=0. No accelerator kernel executed.8. Reviewer notes
materialize_remaining_meta_modulesnow raisesRuntimeErrorwhen a non-quantized module still owns meta tensors, instead of logging. The issue asked for this because the log-only path led into a partial.to()and a CUDA device-side assert. The message names the modules and says to reload the saved checkpoint.quantize()instead of producing a saveable-but-unusable in-session model.save()correctness is unchanged for runs that reach it.Qwen2VLForConditionalGenerationmodule tree but simulates the fix(offload): leave a normal in-session model after disk-offloaded quantize (#4450) #4498 end state (materialized embedding, broken tie, meta head) rather than running a full VL quantize; the end-to-end resume test covers the loop wiring with a text model.tests/test_offload_base_modules_restored.py::test_loop_restores_base_modules_when_quantize_failsfails identically on unmodifiedorigin/main(AttributeError: 'ModuleLooper' object has no attribute '_inflight_layer') — pre-existing, unrelated.