Skip to content

[PERF] Regression from #4832 (fix of #4814): ParoLinear runs every CPU GEMM inside the process-wide _cpu_transform_lock, serializing all concurrent Paro inference (4-thread decode throughput 2.0x lower with GIL, 2.9x with GIL=0) #4885

Closed

Description

@Qubitium

Found on main @ 5cded90, x86-64 AVX512-BF16 host (CPU only, OMP_NUM_THREADS=8), CPython 3.14 (GIL) and 3.14t (PYTHON_GIL=0).

What is wrong

#4832 (fix of #4814) moved Paro's CPU int4 operands into the module's own buffers. To keep state_dict() consistent, both CPU forwards now run the whole GEMM, not only the cold layout check, inside _cpu_transform_lock:

  • paro.py::_forward_cpu_avx512, around line 727: with self._cpu_transform_lock: wraps _ensure_cpu_layout("avx512"), the per-call FP32 widening of scales/bias and gemm(...).
  • paro.py::_forward_cpu_aten, around line 779: the same lock wraps _ensure_cpu_layout("aten", ...) and _cpu_int4pack_mm(...).

_cpu_transform_lock is a single process-wide threading.RLock (BaseQuantLinear._cpu_transform_lock, qlinear/__init__.py:347), shared by every module of every model in the process. So every Paro CPU matmul in the process is now serialized: concurrent requests, a thread pool, or two models served side by side. It also blocks against the cold transform and state_dict() of every other CPU int4 kernel. The GPTQ/AWQ siblings (TorchAtenLinear.forward, torch_aten_kernel.py:465) hold the lock only for the cold check-and-transform, and run the matmul outside it.

Measured: Qwen2.5-0.5B-Instruct Paro 4-bit g128, GPTQModel.load(..., dtype=torch.float16, device="cpu") (AUTO = ParoLinear, ATen int4pack path)

4 threads each run 20 single-token decode forwards (torch.set_num_threads(2)), compared with the same 80 forwards run serially:

                                   4 threads concurrent   serial   speedup
5b5830789 (before #4832), GIL        16.11 s               33.40 s   2.07x
5cded90c9 (after),         GIL        32.53 s               32.82 s   1.01x
5b5830789, PYTHON_GIL=0               8.65 s               31.77 s   3.67x
5cded90c9, PYTHON_GIL=0              24.72 s               32.45 s   1.31x

Concurrent throughput is 2.0x (GIL) and 2.9x (free-threaded) lower. The bf16 AVX512 path has the same lock around gemm. At decode it showed no measurable difference here (4.52 s vs 4.83 s, GIL=0), because the M=1 GEMM is small next to the rotation work. The lock is still held for the whole GEMM at prefill.

Minimal repro

import threading, time, torch
from gptqmodel import GPTQModel
m = GPTQModel.load("<Paro 4-bit checkpoint>", dtype=torch.float16, device="cpu")   # AUTO -> ParoLinear
dec = torch.randint(100, 1000, (1, 1))
with torch.inference_mode():
    m.model(dec)                                   # cold transform
torch.set_num_threads(2)
def work():
    with torch.inference_mode():
        for _ in range(20): m.model(dec)
th = [threading.Thread(target=work) for _ in range(4)]
t = time.perf_counter(); [x.start() for x in th]; [x.join() for x in th]; print("concurrent", time.perf_counter() - t)
t = time.perf_counter(); [work() for _ in range(4)]; print("serial", time.perf_counter() - t)
# before #4832: concurrent 16.1 s, serial 33.4 s;  after: 32.5 s / 32.8 s

Suggested fix

Do what TorchAtenLinear.forward does: take the lock only for _ensure_cpu_layout(...), read qweight/qzeros/scales_and_zeros (and the widened scales/bias) into locals while holding it, then release it and run the GEMM on those locals. A concurrent state_dict()/load_state_dict() can then rebind the module attributes, but the in-flight GEMM keeps the consistent operands it captured. Re-take the lock only for the _restore_cpu_checkpoint_layout() fallback on error.

Test that should have caught it

tests/kernels/test_paro.py covers residency and numerics only. A concurrency test, as in test_cpu_lazy_transform_concurrency_4565.py, could assert that two threads' warm forwards overlap: for example, a forward on thread B completes while thread A holds _cpu_transform_lock.

Severity

Medium. Multi-threaded CPU serving of Paro models gets no parallelism, which costs 2-2.9x throughput. Because the lock is global, a warm Paro forward also stalls any other model's cold CPU transform and state_dict() in the same process.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-root-4885 finished this issue. Fix merged in #4922 (squash commit b98f4028a).

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