Repository navigation
Description
Found on main @ c03416c, RTX 5090 (sm_120), torch 2.15 + CUDA 13.2, Python 3.14 (GIL build), transformers 5.22.
What is wrong
#5291 replaced the load RLock that every compiled call used to take with a reader/writer lock. Only the transformers construction calls that are explicitly wrapped in with model_global_state_lock(): now exclude compiled forwards. Before #5291, the whole from_pretrained/from_quantized body ran under _MODEL_LOAD_LOCK, and compiled calls took that same lock, so every construction site was covered implicitly.
One construction site was not wrapped: the quantize-load path with offload_to_disk=False. In gptqmodel/models/loader.py:2596, inside from_pretrained(..., quantize_config=...), the else branch "loading model directly to CPU" calls _hf_loader_from_pretrained_with_dynamic_module_retry(cls.loader, ...) with no writer. The neighbouring sites at 2410 (no quantize_config) and 2545 (meta-shell fallback) do take it.
transformers' from_pretrained runs construction inside local_torch_dtype(dtype), which sets the process-global default dtype. A compiled forward in another thread can now re-trace inside that window. Dynamo's preserve_global_state then restores the bf16 default after the load has already restored fp32. This is the #5127/#5209 failure mode, back on this path.
Failure scenario
A process serves an optimize()d model in one thread and starts a quantization job in another with GPTQModel.load(src, quantize_config=GPTQConfig(..., offload_to_disk=False), dtype=torch.bfloat16). The process default dtype silently becomes bf16 for everything built afterwards (new tensors, later model constructions, torch.tensor(...) scales). Sometimes the load itself fails with the assert_loaded_model_state guard.
Repro
Qwen2.5-0.5B-Instruct, GPTQ 4-bit g128 checkpoint (A), BACKEND.TORCH:
import threading, torch
from gptqmodel import GPTQModel, BACKEND
from gptqmodel.quantization import config as C
SRC = "Qwen/Qwen2.5-0.5B-Instruct"
m = GPTQModel.load(ckpt_A, backend=BACKEND.TORCH, dtype=torch.float16, device="cuda:0")
m.optimize()
with torch.inference_mode(): m.model(torch.randint(0, 1000, (1, 8), device="cuda"))
stop = threading.Event(); errs = []; dts = []
def loader():
for _ in range(6):
try:
q = GPTQModel.load(SRC, quantize_config=C.GPTQConfig(bits=4, group_size=128, offload_to_disk=False),
dtype=torch.bfloat16); del q
except Exception as e:
errs.append(repr(e)[:200])
dts.append(str(torch.get_default_dtype()))
def fwd():
n = 0
with torch.inference_mode():
while not stop.is_set():
m.model(torch.randint(0, 1000, (1, 8 + n % 5), device="cuda")); n += 1
b = threading.Thread(target=fwd); a = threading.Thread(target=loader); b.start(); a.start(); a.join(); stop.set(); b.join()
print(errs, dts, torch.get_default_dtype())Results (fresh process each run):
| variant | default dtype after each load | errors |
|---|---|---|
main, offload_to_disk=False (run 1) |
f32, bf16, bf16, bf16, bf16, bf16 | none |
main, offload_to_disk=False (run 2) |
f32, f32, bf16, bf16 | RuntimeError: Qwen2QModel.from_pretrained: model load left a corrupted state: torch.get_default_dtype() changed from torch.float32 to torch.bfloat16 |
main, offload_to_disk=True (writer-guarded build_shell_model) |
f32 x6 | none |
main, offload_to_disk=False, no forward thread |
f32 x3 | none |
c9bb73f55^ (before #5291), offload_to_disk=False |
f32 x6 | none |
Suggested fix
Wrap the loader.py:2596 _hf_loader_from_pretrained_with_dynamic_module_retry(...) call in with model_global_state_lock():, the same as the 2410 and 2545 sites. Better still, take the writer inside _hf_loader_from_pretrained_with_dynamic_module_retry itself, and add a check (or a test that walks every loader.from_pretrained/from_config call site) so a new construction site cannot be added unguarded. Before #5291 that coverage came for free from the load lock.
Test that should have caught it
tests/test_compile_lock_scope_5268.py and tests/test_compile_load_race_5209.py only overlap compiled calls with from_quantized (or no-quantize-config) loads. Add the quantize-path offload_to_disk=False load to the #5209 race test and assert torch.get_default_dtype() is unchanged after every load.
Severity
High. It silently corrupts process-wide state (the default dtype becomes bf16) or fails the load, whenever an optimized model serves while a non-offloaded quantization load runs. It is a regression of the #5209 fix introduced by #5291.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 19, 2026
Agent claim released:
agent-fb5145b84190finished this issue.Pull request: #5303
Take the model-global-state writer around the unguarded quantize-path construction site (or inside the loader helper so no site can miss it), plus the concurrent-load default-dtype regression.
fix(load): take the global-state writer inside the construction helper (
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…