Skip to content

fix: cache and release LazyTurtle .bin conversions (#4205) - #4285

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

Qubitium merged 1 commit into
mainfrom
pr/4205

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

.bin/.pt checkpoints are converted to safetensors with a full model-sized copy. Before this change every LazyTurtle made a new copy in the system TMPDIR and only removed it at interpreter exit, so repeated load() calls leaked one model copy each.

This change makes the conversion a refcounted, per-source cache:

  • Two live LazyTurtles for the same source + conversion-relevant config share one converted directory; only one conversion runs.
  • The directory is released when the last turtle is garbage-collected (via weakref.finalize), or explicitly through the new LazyTurtle.close() / context-manager API.
  • An in-use directory is never deleted: leases are refcounted under per-path locks, and directory names include the process id so another process cannot delete this process's copy.
  • The conversion is published atomically (staging dir + completion marker + os.replace), so a partial/crashed conversion is never reused.
  • The conversion root is the configured offload root when one is available (offload_path= argument, offload_to_disk_path/offload_folder in model-init kwargs, or target_model.quantize_config.offload_to_disk_path); otherwise it falls back to the system temp dir. atexit cleanup remains as a last-resort safety net.

What Changed

  • gptqmodel/utils/structure.py
    • Added the conversion cache (_CONVERSION_CACHE, per-path RLocks, refcounts), atomic staging/publish, and weakref.finalize lease release.
    • LazyTurtle.__init__/maybe_create accept offload_path; _resolve_checkpoint_source reuses cached conversions.
    • Added LazyTurtle.close(), __enter__/__exit__, and lease registration.
    • Split the transformers export body into _export_checkpoint_to_safetensors() so it writes into the staging dir.
  • tests/test_lazy_turtle_conversion_cache.py (new)
    • Same source loaded twice reuses one dir and one conversion.
    • Dropping the last turtle (weakref finalizer) removes the dir.
    • close() releases it while the turtle is alive (and is idempotent).
    • Explicit offload path is honoured.
    • Concurrent loads convert exactly once.

Tests

export CUDA_VISIBLE_DEVICES=
export GPTQ_BYPASS_SAFETY_CHECKS=1
export PYTHONPATH=$PWD

python3 -m pytest tests/test_lazy_turtle_conversion_cache.py -q
# 5 passed

python3 -m pytest \
  tests/test_lazy_turtle_conversion_mapping.py \
  tests/test_lazy_turtle_shard_layout_warning.py \
  tests/test_lazy_turtle_materialize_map.py \
  tests/test_checkpoint_path_security.py \
  tests/test_lazy_turtle_conversion_cache.py -q
# 93 passed

python3 -m pytest \
  tests/test_lazy_sync_packed_modules.py \
  tests/test_local_model_paths.py \
  tests/test_fp8_save_offload.py -q
# 33 passed

python3 -m ruff check gptqmodel/utils/structure.py tests/test_lazy_turtle_conversion_cache.py
# All checks passed!

CPU-only (no GPU); the CUDA extensions are not required for these tests.

Model Support / Module Plan

N/A — no new model types or registry entries.

Notes

LazyTurtle.maybe_create(..., offload_path=...) is the wiring point for quantize_config.offload_to_disk_path. The models/loader.py call site is owned by #4204, so it is not touched in this PR; forwarding quantize_config.offload_to_disk_path there (one kwarg) makes the default quantization path place the conversion on the configured offload volume as well. The leak/reuse fixes and the explicit offload-path support are already active without that forwarding.

Fixes #4205

Comment thread gptqmodel/utils/structure.py Dismissed
@Qubitium
Qubitium merged commit 2c625bb into main Nov 16, 2026
6 checks passed
@Qubitium
Qubitium deleted the pr/4205 branch November 16, 2026 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] LazyTurtle converts .bin/.pt checkpoints into a full temp safetensors copy on every load() and keeps it until interpreter exit

1 participant