Skip to content

[BUG] GPTQModel.load fails when another thread runs a transformers construction (user from_pretrained or requantize(norm) teacher): class-wide no_tie_weights untie lm_head, unguarded default_dtype_before -> false 'corrupted state' #5421

Closed

Description

@Qubitium

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:

  1. no_tie_weights and the other init contexts. transformers' get_init_context opens [local_torch_dtype(...), init.no_tie_weights(), apply_patches()] (transformers/modeling_utils.py:3807). no_tie_weights monkeypatches PreTrainedModel.tie_weights class-wide. Only local_torch_dtype is wrapped by install_model_global_state_dtype_guard(). While the other thread is inside that context, the GPT-QModel shell's post_init tie is a no-op. lm_head then stays untied with torch.empty storage, and the load fails with Loader: ... does not provide tensors for 1 non-quantized parameter(s): lm_head.weight for tied-embedding models (Qwen2.5, Llama-3.2-1B, gemma, ...).
  2. default_dtype_before is read at from_quantized entry (loader.py:2732) and at from_pretrained entry (loader.py:2245) without the writer, then compared in assert_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[...] += 1
main:                    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. Wrap PreTrainedModel.get_init_context (or init.no_tie_weights and apply_patches as 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-teacher from_pretrained in requantize(), because it is a GPT-QModel-owned load.
  • Read default_dtype_before under the writer, or compare against torch.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

  1. ZX-ModelCloud commented on Nov 19, 2026

    @ZX-ModelCloud
    Collaborator

    Agent claim released: agent-5b0ff02ce61f finished this issue.

    Pull request: #5560

    Reproducing concurrent transformers-construction vs GPTQModel.load global-state race; then fix and validate

  2. added a commit that references this issue on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions