Repository navigation
Conversation
bind_private_offload_root() created run-<uuid16> under the configured offload_to_disk_path for every quantization run and nothing removed it. An explicit path therefore grew by one full model per run, including EoRA generation's extra run. The directory now belongs to the model that created it. It is removed when the model is collected (weakref.finalize), when the quantization loop fails, when the same model binds another run, and after the EoRA adapter is written. The module-level atexit hook removes anything left at interpreter exit, covering models that cannot be weakly referenced. The bundles are not deleted at the end of save(): the quantized modules stay meta-backed with accelerate offload hooks, so an in-session forward after save() still reads them (test_planar_model_e2e.py). They are released at teardown, when the model can no longer use them. Fixes #4368
Collaborator
Author
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.
Summary
Every quantization run created a private
<offload_to_disk_path>/run-<uuid16>directory for bundle isolation (#4202) and nothing ever removed it. With an
explicit path, four runs of a 3-layer model left 1.3 GB of
run-*trees; theleak also grew across processes. EoRA generation allocated one more run
directory on top.
The run directory is now owned by the model that created it. It is removed when
that model is collected, when the quantization loop fails, before the same model
binds another run, after the EoRA adapter is written, and at interpreter exit
for anything still tracked. Per-run isolation is unchanged.
Fixes #4368What Changed
gptqmodel/utils/offload.pybind_private_offload_root()records the run root and aweakref.finalizecallback on the model, plus the caller's base root so a second run on the
same model stays a sibling instead of nesting inside the old run directory.
release_private_offload_root()detaches the finalizer, clears theownership attributes, and removes the directory. Removal is
shutil.rmtree(..., ignore_errors=True)so it is safe from a finalizerthread and at shutdown.
atexithook removes any run root this process still owns.This covers lightweight model doubles that cannot be weakly referenced and
normal interpreter exit. The auto
gptqmodel_*temp root keeps its existingexit cleanup.
gptqmodel/looper/module_looper.py,gptqmodel/looper/weight_only_looper.py:the existing failure branch releases the run directory next to
_restore_offloaded_base_modules(strict=False).gptqmodel/models/base.py:_eora_generate()releases its run directoryafter
eora_save()completes.tests/test_offload_run_cleanup_4368.py: new CPU-only regression coverage.docs/offload-run-directories.md: reference page for the lifecycle, linkedfrom
README.mdanddocs/README.md.API/behavior changes:
run-*directory under an explicitoffload_to_disk_pathno longer outlives its model.save()deliberately keeps the directory: the quantized modules remainmeta-backed with accelerate offload hooks, so an in-session forward after
save()still reads those bundles (tests/test_planar_model_e2e.pydoesexactly this). The bundles are released at teardown, when the model can no
longer use them.
Out of scope: a
SIGKILLed process runs no Python cleanup hook, so its rundirectory remains. Removing another process's directory at bind time would
break concurrent runs that share an explicit path.
Tests
New regression test (
tests/test_offload_run_cleanup_4368.py, 6 tests):test_release_private_offload_root_removes_bundlestest_run_directory_survives_save_and_is_removed_at_teardownsave()resolves bundles from the run directory, the in-session module stays meta-backed, and teardown removes the directorytest_sequential_runs_share_path_without_accumulatingrun-*accumulatestest_run_directory_is_removed_when_weight_only_run_failsWeightOnlyLooperfailure removes the bound run directorytest_run_directory_is_removed_when_module_looper_failsModuleLooper.loop()removes the run directory when_loop_implraisestest_rebinding_same_model_releases_previous_runExact commands and results (CPU only:
CUDA_VISIBLE_DEVICES="",GPTQ_BYPASS_SAFETY_CHECKS=1,PYTHONPATH=/root/worktrees/issue-4368,/root/gptqmodel-test-venv/bin/python):The two
test_weight_only_looper.pyfailures were reproduced on a clean0b7582feaworktree with the same environment; they are not caused by thischange.
End-to-end check of the reported repro (two sequential runs, explicit path,
del model; gc.collect()):Model Support / Module Plan
Not applicable: no model type, alias, definition, or module-plan change.
AWQ onboarding safety
Not applicable: no model/alias or AWQ grouping/search/replay/fold change.
Logic Flow Documentation
The change alters the offload scratch-directory lifecycle and adds a failure
path, so the diagrams are included here and committed in
docs/offload-run-directories.md, linkedfrom
README.md.New flow (shipped code):
flowchart TD A["bind_private_offload_root(model)"] --> B{"model already owns a run?"} B -- Yes --> C["release_private_offload_root(model)"] B -- No --> D["base = model._offload_run_base or configured root"] C --> D D --> E["makedirs base/run-uuid; add to _ACTIVE_RUN_ROOTS"] E --> F["weakref.finalize(model, _remove_offload_run_root, run_root)"] F --> G["private config copy points at run-uuid"] G --> H{"run outcome"} H -- "loop raises" --> I["looper except: release_private_offload_root"] H -- "eora_save completes" --> J["_eora_generate: release_private_offload_root"] H -- "save() completes" --> K["bundles retained: model still materializes from them"] H -- "model collected" --> L["finalizer: _remove_offload_run_root"] H -- "interpreter exit" --> M["atexit: _cleanup_active_offload_run_roots"] classDef failure fill:#ffe8e8,stroke:#b30000,color:#111; class I failure;Before/after, with the old mechanism:
0b7582fea)bind_private_offload_root()createdrun-<uuid16>and stored the private config (gptqmodel/utils/offload.py:79-110)weakref.finalize(gptqmodel/utils/offload.py:133)gptqmodel/looper/module_looper.py:1889,gptqmodel/looper/weight_only_looper.py:904eora_save()(gptqmodel/models/base.py:2898)atexitremoves any tracked run dirFailure and ambiguity contract:
save()raisessave()succeedsatexitremoves it at exitSIGKILLKernel Accuracy Requirements
Not applicable: no kernel or numerical change.
Notes
removes it at teardown instead, because
save()does not materialize thequantized modules: they stay on
metawith accelerate hooks pointing intothe run directory, and
tests/test_planar_model_e2e.pyforwards from thesame model after
save(). Deleting at the end ofsave()would raiseFileNotFoundErrorthere. The regression test pins the retained-until-teardown behavior.
_RUN_BASE_ATTRis retained after a release so a retry/requantize on thesame model creates a sibling of the original root rather than nesting inside
the deleted path.