Repository navigation
Description
Found on main @ 1405751, RTX 4090 (CUDA 13.2, torch 2.15, transformers 5.23.0), GIL build.
What is wrong
_serialized_model_load (models/loader.py:1595) serializes GPT-QModel loads against each other. #5304/#5329 then made the global-state writer cover only transformers' local_torch_dtype window. Neither protects a GPTQModel.load from a transformers construction running in another thread that does not go through _MODEL_LOAD_LOCK. That includes a user's AutoModelForCausalLM.from_pretrained and GPT-QModel's own norm-tuning teacher load in requantize(norm=NormConfig(teacher=<path>)) (models/base.py:3253).
Two process-global pieces leak across the threads:
no_tie_weightsand the other init contexts. transformers'get_init_contextopens[local_torch_dtype(...), init.no_tie_weights(), apply_patches()](transformers/modeling_utils.py:3807).no_tie_weightsmonkeypatchesPreTrainedModel.tie_weightsclass-wide. Onlylocal_torch_dtypeis wrapped byinstall_model_global_state_dtype_guard(). While the other thread is inside that context, the GPT-QModel shell'spost_inittie is a no-op.lm_headthen stays untied withtorch.emptystorage, and the load fails withLoader: ... does not provide tensors for 1 non-quantized parameter(s): lm_head.weightfor tied-embedding models (Qwen2.5, Llama-3.2-1B, gemma, ...).default_dtype_beforeis read atfrom_quantizedentry (loader.py:2732) and atfrom_pretrainedentry (loader.py:2245) without the writer, then compared inassert_loaded_model_state. If the other thread's dtype window is open at either moment, a correct load is rejected:model load left a corrupted state: torch.get_default_dtype() changed from torch.bfloat16 to torch.float32.
Repro
gptq4 is a GPTQ 4-bit checkpoint of Qwen2.5-0.5B-Instruct truncated to 4 layers. q05_4l is its fp16 base.
def hf(): # thread 2
while not stop.is_set():
m = transformers.AutoModelForCausalLM.from_pretrained("q05_4l", dtype=torch.bfloat16, device_map="cpu"); del m
threading.Thread(target=hf).start()
for i in range(20): # thread 1
try: q = GPTQModel.load("gptq4", backend=BACKEND.TORCH, dtype=torch.float16); del q
except Exception as e: fails[...] += 1main: q_fail: 'does not provide tensors for 1 non-quantized parameter(s): lm_head.weight': 7,
'load left a corrupted state: get_default_dtype() changed from float32 to bfloat16': 2,
'... changed from bfloat16 to float32': 1 -> 10/20 loads fail
main, HF call under model_global_state_lock(): lm_head 5, dtype 7 -> 12/20 fail
67de3469f^ (before #5329), unlocked HF thread: dtype 4 -> 4/20 fail
67de3469f^, HF call under model_global_state_lock(): lm_head 8, dtype 2 -> 10/20 fail
This is not a regression of #5329: the parent fails as well. It is a hole in the #4129/#4140 serialization that the window-level writer does not close.
The GPT-QModel-internal version: thread 1 loops RQ.requantize(CalibrationConfig(dataset=calib2, batch_size=1), norm=NormConfig(teacher="q05_4l", steps=1)), and its teacher load is a plain AutoModelForCausalLM.from_pretrained. Thread 2 loops GPTQModel.load("gptq4"). Result: RESULT N 30 ok 29 load_fail {'... model load left a corrupted state: torch.get_default_dtype() changed from torch.float16 to t...': 1}. The teacher window is short relative to requantize, which is why the rate is lower.
Suggested fix
- Have the import-time guard hold the writer for the whole transformers init-context group, not just
local_torch_dtype. WrapPreTrainedModel.get_init_context(orinit.no_tie_weightsandapply_patchesas well), so a foreign construction and a GPT-QModel construction exclude each other for every class-wide patch. - Take
model_load_lock()around the norm-teacherfrom_pretrainedinrequantize(), because it is a GPT-QModel-owned load. - Read
default_dtype_beforeunder the writer, or compare againsttorch.float32/the value captured inside the window, so another thread's open window cannot make a correct load fail the invariant check.
Test that should have caught it
tests/test_model_load_lock_4204.py / test_compile_load_scope_5304.py only race GPT-QModel loads against each other. Add a test that races a tied-embedding GPTQModel.load against a looping plain from_pretrained, and one against requantize(norm=...), and asserts that every load succeeds and lm_head is tied.
Severity
Medium. Loads of tied-embedding models fail loudly (no silent corruption was observed: assert_loaded_model_state and the missing-tensor check catch it) whenever any other transformers construction runs concurrently, including GPT-QModel's own requantize teacher.
🤖 Generated with Claude Code
Activity
ZX-ModelCloud commentedon Nov 19, 2026
Agent claim released:
agent-5b0ff02ce61ffinished this issue.Pull request: #5560
Reproducing concurrent transformers-construction vs GPTQModel.load global-state race; then fix and validate
fix: regressions from #4620 (CPTC HF refusal, LFM3/DeepSeek-V2 ports,…