Skip to content

[BUG] Regression from #4856 (fix of #4805): concurrent cold QQQ CPU forwards race the checkpoint drop: a second thread reads s_channel=None (AttributeError, 4/400 on 3.14t) #4942

Closed

Description

@Qubitium

Found on main @ 3867c82, x86-64 AVX512-BF16 host (CPU only), CPython 3.14t free-threaded (PYTHON_GIL=0), torch 2.15.

What is wrong

#4856 (fix of #4805) makes the first CPU forward of QQQTorchLinear drop the checkpoint B/s_group/s_channel buffers once the packed cache is built (_maybe_drop_checkpoint_tensors, gptqmodel/nn_modules/qlinear/qqq.py:943-976, sets self._buffers[name] = None at :975).

The cold path of _packed_weight_for_cpu (qqq.py:978-1020) still reads the checkpoint attributes directly and without the lock:

  • key = self._torch_weight_state_key() (:994) reads self.B/self.s_channel/self.s_group
  • self._unpermute_scales(self.s_channel, ...) (:1002), self.s_group (:1012), self.B.device (:1008)

The early return at :990 only covers a thread that arrives after the first thread published both the cache and the drop. A second thread that enters the cold path while the first one is still building passes that check. If the first thread then drops the buffers, the second thread reads self.s_channel is None:

AttributeError: 'NoneType' object has no attribute 'device'
  qqq.py:1040 _packed_cpu_matmul -> packed, s_group, s_channel = self._packed_weight_for_cpu()
  qqq.py:1002 _packed_weight_for_cpu -> self._unpermute_scales(self.s_channel, self._scale_perm_single_i)
  qqq.py:763  _unpermute_scales -> inverse_perm.to(device=scales.device)

Before #4856 the checkpoint buffers were never removed, so concurrent cold forwards were safe. Other CPU int4 kernels guard this case: Paro/AWQ/GPTQ _ensure_cpu_layout runs under _cpu_transform_lock, and test_cpu_lazy_transform_concurrency_4565.py covers them. QQQ's cold build is not under the lock.

Repro (real qqq_int4 AVX512 kernel, no stand-in)

import os, threading, time, random, torch          # CUDA_VISIBLE_DEVICES="", run from the repo root
from tests.test_qqq_kernel_memory_4387 import _build_torch_module
x = torch.randn(4, 2048, dtype=torch.float16)
errs = []
for trial in range(400):
    m = _build_torch_module(in_features=2048, out_features=2048, group_size=128, dtype=torch.float16)
    bar = threading.Barrier(4)
    def run(i):
        bar.wait(); time.sleep(random.random() * 0.015 * i)   # stagger the cold forwards
        try:
            with torch.inference_mode(): m(x)
        except Exception as e: errs.append(repr(e))
    th = [threading.Thread(target=run, args=(i,)) for i in range(4)]
    [t.start() for t in th]; [t.join() for t in th]
print(len(errs), set(errs))
build 3.14t PYTHON_GIL=0 3.14 GIL
07f8b8aa1 (parent of #4856) 0 / 400 trials 0 / 400
main @ 3867c82 4 / 400 trials raise AttributeError (2 / 200 in an earlier run) 0 / 400 (the window is still there, but the GIL makes it rare)

Successful outputs were bit-identical to a single-threaded reference in every run, so the failure is a crash, not a silent mismatch.

Suggested fix

Run the cold build under _cpu_transform_lock, as the other CPU int4 kernels do: re-check self._torch_packed_cache/_torch_dropped_checkpoint after taking the lock, and keep the warm path (cached is not None and dropped) lock-free. Or read the checkpoint tensors through _checkpoint_B()/_checkpoint_scale() and compute the key from them, so a concurrent drop cannot remove them halfway through the build.

Test that should have caught it

tests/test_qqq_cpu_residency_4805.py runs only single-threaded, with a stand-in kernel. A concurrent cold-forward case like test_cpu_lazy_transform_concurrency_4565.py (N threads, one fresh module, staggered first forward) would have caught it. This host has AVX512-BF16, so it can use the real qqq_int4 kernel.

Severity: medium. Multi-threaded CPU serving of a QQQ model can crash on its first request per layer, mostly on free-threaded Python.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4951

    One workload with #4940, #4942: Both are regressions from #4856 (QQQTorchLinear runtime state): the class-swap path reads attributes that only init sets, and the cold packed-cache build reads the checkpoint buffers without the transform lock while another thread may drop them.

    Add class-level runtime-state defaults + a class-swap-safe clear_weight_cache, and serialize the QQQ CPU cold build under the transform lock; regression tests for both.

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 is no longer working on this issue.

    The issue is free to claim.

    fixed upstream by #4951 (a66721a): _packed_weight_for_cpu now runs under _cpu_transform_lock; tests/test_qqq_cpu_cold_build_concurrency_4942.py is 2 passed on HEAD and 2 failed at a66721a^

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Fixed upstream by #4951 (a66721ae, merged 2026-11-17T19:42Z); that PR just did not auto-close this issue.

    Verified on the current base: gptqmodel/nn_modules/qlinear/qqq.py runs the whole cold build of _packed_weight_for_cpu under _cpu_transform_lock with a double-check inside the lock, and tests/test_qqq_cpu_cold_build_concurrency_4942.py (4 threads, barrier, staggered cold forwards, bit-identical outputs) covers it.

    Red/green evidence on this tree:

    • HEAD: 2 passed
    • a66721ae^ (last commit before the fix): 2 failed, exactly AttributeError: 'NoneType' object has no attribute 'device'

    No further change needed.

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