Skip to content

fix: serialize save-side shell build, dequant and TT from_config under the model load lock (#4204) - #4278

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

Qubitium merged 1 commit into
mainfrom
pr/4204

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4204. _MODEL_LOAD_LOCK only wrapped from_pretrained / from_quantized, so three other model-construction sites could still interleave transformers' process-global torch.set_default_dtype window (local_torch_dtype) with a concurrent load:

  • BaseQModel.get_model_with_quantize — the save-side shell rebuild for a loaded quantized model (models/writer.py)
  • _discover_compressed_tensors_module_schemes — the compressed-tensors dequant path (utils/model_dequant.py)
  • build_blockfloat_model — the TT block-float _from_config path (utils/tt_blockfloat_quant.py)

What Changed

  • Added model_load_lock() in models/loader.py: a single accessor for the existing threading.RLock. It is looked up at call time (not imported as a lock object) so every site shares one lock and tests can spy on it.
  • _serialized_model_load now enters the lock through that accessor.
  • Save-side shell build now runs from_config under model_load_lock() together with suspend_hf_weight_init().
  • Compressed-tensors dequant from_config and TT block-float _from_config take the same lock.
  • Re-entrancy is preserved: the lock remains an RLock, so the TT path called from from_quantized (already locked) does not deadlock.
  • New test file tests/test_model_load_lock_4204.py:
    • spies each of the three construction sites and asserts it enters _MODEL_LOAD_LOCK while from_config runs;
    • holds the save-side shell build in its bf16 default-dtype window and asserts a concurrent locked load blocks and observes the original dtype. The three spy tests and the concurrency test fail on the base commit and pass with the fix.

Tests

export GPTQ_BYPASS_SAFETY_CHECKS=1 PYTHONPATH=$PWD CUDA_VISIBLE_DEVICES=
python3 -m pytest tests/test_model_load_lock_4204.py tests/test_trust_remote_code_isolation.py -q
# 9 passed

python3 -m ruff check gptqmodel/models/loader.py gptqmodel/models/writer.py \
  gptqmodel/utils/model_dequant.py gptqmodel/utils/tt_blockfloat_quant.py \
  tests/test_model_load_lock_4204.py
# All checks passed!

Pre-existing on the base commit and unrelated: tests/test_tt_blockfloat_sequential.py has 2 failures (test_sequential_replay_uses_each_hybrid_layer_mask, test_sequential_replay_full_attention_context_unchanged) both before and after this change.

Model Support / Module Plan

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

Fixes #4204

Comment thread tests/test_model_load_lock_4204.py Dismissed
@Qubitium
Qubitium merged commit 6d961d8 into main Nov 16, 2026
8 of 9 checks passed
@Qubitium
Qubitium deleted the pr/4204 branch November 16, 2026 07:10
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] Re-saving a loaded quantized model builds the shell with from_config outside _MODEL_LOAD_LOCK, reopening the #4129 default-dtype race

1 participant