Skip to content

[BUG][GIL=0] ExllamaV3Linear: concurrent first forward races in unlocked _ensure_inner(): another thread's _drop_inner() nulls the handle -> AttributeError 'NoneType' object has no attribute 'forward' (111/4480 cold calls) #4793

Closed

Description

@Qubitium

Found on main @ dba142d, RTX 4090 (sm_89), torch 2.16 / CPython 3.14t with PYTHON_GIL=0.

What is wrong

ExllamaV3Linear.forward (gptqmodel/nn_modules/exllamav3.py:316) gets its native handle from _ensure_inner() (:227-267). That function takes no lock and does the following:

  1. Checks the signature (:239-240).
  2. Calls _drop_inner(), which sets self._inner = None (:242, :179).
  3. Builds a new LinearEXL3 and stores it in self._inner.
  4. Returns self._inner (:267), re-reading the attribute instead of returning the object it just built.

Every loaded model starts with _inner is None. post_init builds the handle, but the model-wide _apply (.to() / dtype cast) after it drops the handle again (:305-306). Verified right after GPTQModel.load(...): inner is None: True, and the signature check fails.

So when two threads hit the first forward of a module at the same time:

  • Thread B's _drop_inner() can null self._inner between thread A's assignment and A's return self._inner. A's forward then raises AttributeError: 'NoneType' object has no attribute 'forward'.
  • Each thread also calls _normalize_inference_metadata() / _bias_version(), which rebind buffers inside the forward. The signature therefore keeps changing while the other thread checks it.

The same happens after any clear_weight_cache().

GPT-QModel enables multi-threaded paths when it detects PYTHON_GIL=0, and EXL3 is the AUTO kernel for EXL3 checkpoints on CUDA. With the GIL enabled the window did not trigger (0/4480).

Repro (Qwen2.5-0.5B-Instruct truncated to 4 layers, EXL3Config(bits=4), loaded fp16 with BACKEND.EXL3_EXLLAMA_V3)

import threading, collections, torch
from gptqmodel import GPTQModel, BACKEND
q = GPTQModel.load(CKPT, backend=BACKEND.EXL3_EXLLAMA_V3, dtype=torch.float16, device="cuda:0")
mods = [m for m in q.model.modules() if type(m).__name__ == "ExllamaV3Linear"]
errs = collections.Counter(); T = 8
for _ in range(20):
    for m in mods:
        m.clear_weight_cache()                       # same state as right after load (_inner is None)
        x = torch.randn(4, m.in_features, device="cuda", dtype=torch.float16)
        b = threading.Barrier(T)
        def w():
            s = torch.cuda.Stream(); b.wait()
            try:
                with torch.inference_mode(), torch.cuda.stream(s): m(x)
                s.synchronize()
            except Exception as e: errs[f"{type(e).__name__}: {e}"] += 1
        th = [threading.Thread(target=w) for _ in range(T)]; [t.start() for t in th]; [t.join() for t in th]
print(dict(errs))

Output:

PYTHON_GIL=0: RESULT calls 4480 errors {"AttributeError: 'NoneType' object has no attribute 'forward'": 111} wrong_outputs 0
GIL enabled : RESULT calls 4480 errors {} wrong_outputs 0

It also showed up in a generic cold-start stress run (8 threads, one stream each, first forward of the loaded model under PYTHON_GIL=0): COLD_ERRORS 1 ["AttributeError: 'NoneType' object has no attribute 'forward'"].

Suggested fix

  • Guard _ensure_inner (and _ensure_torch_inner / _drop_inner) with a per-module lock.
  • Build into a local variable and return that local, never re-reading self._inner.
  • Publish the handle and its signature together.
  • Do the _normalize_inference_metadata rebinding once under the same lock, not on every forward.

Test that should have caught it

tests/test_exl3_dispatch_concurrency.py covers the native dispatch caches (#3951) but not the Python handle. Add a PYTHON_GIL=0 test that clears the handle and runs one module's first forward from 8 threads behind a barrier.

Severity

Medium. Under free-threading, concurrent first requests to a freshly loaded EXL3 model, or the first requests after clear_weight_cache(), crash intermittently (about 2.5% of cold calls at T=8). Outputs that do return are correct.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Claiming. Will guard _ensure_inner/_ensure_torch_inner/_drop_inner with a per-module lock, return the locally built handle instead of re-reading self._inner, and add a cold-start concurrency regression test that runs with the native builder stubbed.

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

Metadata

Metadata

Assignees

No one assigned

    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