You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] #4140/#4102 (fixes of #4129/#4094) incomplete: TorchLinear's lazy first compile is outside _MODEL_LOAD_LOCK; dynamo's default-dtype save/restore races a concurrent load (PYTHON_GIL=0: load fails 'corrupted state', process default dtype left bf16) #5127
Found on main @ 7b8f127, CPU, free-threaded Python 3.14t with PYTHON_GIL=0 (the configuration GPTQModel recommends at import), torch 2.15.
What is wrong
#4140 (fix of #4129) serializes model construction with _MODEL_LOAD_LOCK, because transformers' from_pretrained saves, sets and restores the process-global torch.set_default_dtype. The lazy first-use torch.compile of TorchLinear.dequantize_weight (nn_modules/qlinear/torch.py:465, entered through utils/torch.py:433call) does not take that lock. Dynamo's preserve_global_state (torch/_dynamo/convert_frame.py:356/384) does the same save/restore of the default dtype around every compile, so the two interleave:
Compile starts while another thread's load has the default dtype set to bf16, and finishes after that load has restored fp32. The compile then "restores" bf16, and the process default dtype stays bf16 for the rest of the process. Every later torch.ones(...), torch.tensor([1.0]) or nn.Linear(...) in user code is silently bf16.
Compile restores while a load is inside the lock. assert_loaded_model_state then rejects that load with model load left a corrupted state: torch.get_default_dtype() changed from torch.float32 to torch.bfloat16. This is a correct load failed by another thread's forward.
#4102 (fix of #4094 item 3) handled only the forward side of this race (it falls back to eager on dynamo's guard AssertionError). With PYTHON_GIL=0 the concurrent compile also raised a dynamo-internal KeyError from torch/_dynamo/utils.py:1119 (get_guard_fail_reason) out of a plain forward. _is_compiler_failure (utils/torch.py:338-388) does not classify that KeyError, so it propagated to the caller.
Repro
Two threads, each repeatedly loads a quantized checkpoint (Qwen2.5-0.5B-Instruct GPTQ 4-bit and RTN 4-bit, backend=BACKEND.GPTQ_TORCH, bf16, CPU) and runs one forward. This is the documented "loads serialize, everything else may overlap" usage from docs/concurrency.md.
importthreading, torchfromgptqmodelimportGPTQModel, BACKENDdefjob(path):
m=GPTQModel.load(path, device="cpu", dtype=torch.bfloat16, backend=BACKEND.GPTQ_TORCH)
ids=m.tokenizer("The capital of France is Paris. The capital of Germany is", return_tensors="pt").input_idswithtorch.inference_mode(): returnm.model(ids).logits.float()
defworker(path):
for_inrange(5): job(path) # count successes / exceptionsts= [threading.Thread(target=worker, args=(p,)) forpin (gptq_ckpt, rtn_ckpt)]
[t.start() fortints]; [t.join() fortints]
print(torch.get_default_dtype(), torch.ones(1).dtype)
Output with PYTHON_GIL=0:
default dtype before threads: torch.float32
1 x gptq RuntimeError: Qwen2QModel.from_quantized: model load left a corrupted state: torch.get_default_dtype() changed from torch.float32 to torch.bfloat16
4 x gptq ok
5 x rtn ok
default dtype at end: torch.bfloat16 torch.ones(1).dtype: torch.bfloat16
In a four-model variant (GPTQ/AWQ/GPTQ desc_act/RTN, each loaded and run once per thread), a forward also raised KeyError: 5190054990720 from torch/_dynamo/guards.py get_guard_fail_reason → torch/_dynamo/utils.py:1119, reached through qlinear/torch.py:571 _forward_eager → utils/torch.py:437 call. The same 2-thread script with PYTHON_GIL=1 was clean in 5 iterations, so the window is much narrower there.
Suggested fix
Run the lazy first compile (_install_callable_compile_fallback.call while uncompiled, and optimize()) under the same _MODEL_LOAD_LOCK / global-state lock as model construction. This is the "compile under a lock" alternative #4094 already proposed. It keeps dynamo's save/restore from overlapping a load. Also treat exceptions whose innermost frame is in torch/_dynamo/ (such as the KeyError) as compiler failures for the eager fallback.
Test that should have caught it
A thread test that overlaps GPTQModel.load() with a first forward of another loaded TorchLinear model, run under PYTHON_GIL=0, and asserts torch.get_default_dtype() is unchanged afterwards.
Severity
Medium. With the recommended free-threaded runtime, a serving process that loads a model while another one warms up can end up with a permanently wrong global default dtype, which silently affects all later tensor creation, or with a spuriously failed load.
Fixed: the lazy first TorchLinear compile and optimize() now take the model-load global-state lock, and dynamo-internal exceptions are classified as compiler failures.
Activity
Qubitium commentedon Nov 18, 2026
Agent claim released:
agent-4e3a0639ecaffinished this issue.Pull request: #5159
Fixed: the lazy first TorchLinear compile and optimize() now take the model-load global-state lock, and dynamo-internal exceptions are classified as compiler failures.
fix(torch): serialize TorchLinear's first compile with model loads (#…
fix(compile): serialize every compiled call with model loads (#5209) (#…
fix(load): hold the global-state writer only for the default-dtype wi…
fix(load): hold the global-state writer only for the default-dtype wi…