Skip to content

perf(paro): run the CPU int4 GEMM outside the process-wide transform lock (#4885) - #4922

Merged
Qubitium merged 2 commits into
mainfrom
root/paro-lock-4885
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
root/paro-lock-4885

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

#4832 (the fix of #4814) moved Paro's packed CPU int4 operands into the module's own buffers so state_dict() keeps emitting the checkpoint layout. To make that safe it wrapped the whole GEMM, not just the cold layout transform, in BaseQuantLinear._cpu_transform_lock — one process-wide threading.RLock shared by every module of every model. Every warm Paro CPU matmul in the process was therefore serialized, which cost 2-2.9x multi-threaded decode throughput (#4885).

This runs the product on captured operands outside the lock, as TorchAtenLinear.forward already does for the GPTQ/AWQ CPU kernels.

What Changed

  • gptqmodel/nn_modules/qlinear/paro.py::_forward_cpu_avx512: under _cpu_transform_lock, run _ensure_cpu_layout("avx512") and capture qweight, qzeros, the per-call FP32-widened scales/bias into locals; release the lock; run gemm(...) on those locals.
  • gptqmodel/nn_modules/qlinear/paro.py::_forward_cpu_aten: under the lock, run _ensure_cpu_layout("aten", dtype) and capture qweight, scales_and_zeros, bias; release the lock; run _cpu_int4pack_mm(...) on those locals. The failure fallback re-takes the lock only to run _restore_cpu_checkpoint_layout(), which rebinds registered buffers.
  • No API or behavior change: state_dict() still emits the checkpoint tensors, and outputs are bitwise identical to the pre-fix reference.
  • Out of scope: _forward_dense's restore and _apply()/train() still restore without the lock, unchanged from before.

Tests

  • Unit tests have been executed and passed.
  • I added a new simple/fast unit test for this change.

New regression test: tests/kernels/test_paro_cpu_lock_concurrency_4885.py. It builds a real ParoLinear (packed AWQ buffers + rotation), warms it, then gates the first GEMM and asserts:

  1. the process-wide _cpu_transform_lock is free while that GEMM runs (fails on main: the lock is held across the GEMM);
  2. a second warm forward completes before the first GEMM is released (fails on main: it blocks on the lock);
  3. a state_dict() hammer racing warm forwards never raises and always returns the checkpoint tensors;
  4. all concurrent and post-concurrent outputs are bitwise equal to the single-threaded reference.

It covers the ATen path natively and the AVX512 path with the pure-torch stand-ins already used by tests/kernels/test_paro.py (this EPYC has no AVX512-BF16).

# red, parent 436053174 (unmodified main)
pytest tests/kernels/test_paro_cpu_lock_concurrency_4885.py -q
# 2 failed, 2 passed  (both overlap nodes fail; state_dict nodes pass)

# green, this branch
pytest tests/kernels/test_paro_cpu_lock_concurrency_4885.py -q
# 4 passed

pytest tests/kernels/test_paro.py \
       tests/test_cpu_lazy_transform_concurrency_4565.py \
       tests/test_cpu_int4pack_dispatch_contracts.py \
       tests/kernels/test_paro_cpu_lock_concurrency_4885.py -q
# 70 passed, 18 skipped (skips are CUDA/AVX512-BF16-only nodes)

ruff check gptqmodel/nn_modules/qlinear/paro.py \
           tests/kernels/test_paro_cpu_lock_concurrency_4885.py
# All checks passed!

Throughput (concurrent vs serial)

Two independent ParoLinear modules, FP16 ATen int4pack path, 4096x4096, torch.set_num_threads(1), 20 warm decode-shaped (M=1) forwards per module, 5 repetitions. Speedup is serial / concurrent for the best (min) timing of each run. Host: 256-core EPYC, shared with other CI agents, CPython 3.14 free-threading build (2 runs with PYTHON_GIL=0, 1 run with PYTHON_GIL=1):

GIL 436053174 (parent) this branch
off 0.99x, 1.00x 1.87x, 1.96x
on 1.01x 2.13x

Raw timings for the PYTHON_GIL=0 runs (serial / concurrent): parent 1.561 s / 1.570 s and 1.558 s / 1.620 s; this branch 1.507 s / 0.808 s and 1.580 s / 0.807 s.

Model Support / Module Plan

Not applicable: no MODEL_MAP, definition, registry, or fusion changes.

AWQ onboarding safety

Not applicable: no model onboarding or AWQ grouping/search/replay/fold changes. The AWQ-packed layout is untouched; the change only moves the GEMM outside the transform lock.

Logic Flow Documentation

  • This change does not alter a major logic flow.
  • New-flow Mermaid diagram(s) ...
  • The removed/replaced mechanism ...
  • Failure and ambiguity paths ...
  • A reference page under docs/ ...

Diagram summary: not applicable. The synchronization contract is unchanged — state_dict()/load_state_dict() and the cold layout transform still share _cpu_transform_lock; only the in-flight GEMM no longer holds it. The decision mechanism, resolution order, and tensor lifecycle are untouched.

Review Requirements

I or an AI agent (such as Codex or Claude) have:

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • Accuracy is unchanged: the new test requires bitwise equality with the single-threaded pre-fix reference for every concurrent forward, and tests/kernels/test_paro.py still passes its independent FP32 oracle nodes.
  • Fusion support changes ... — not applicable.
  • Complete kernel inventory/device report ... — not applicable; no new kernel or device path.

Notes

The lock still covers the operand capture, so two warm forwards briefly serialize on it (microseconds) before their GEMMs overlap. That is what keeps a concurrent load_state_dict() from rebinding qweight between the reads of qweight, scales_and_zeros, and bias. Holding it across the GEMM is what this PR removes.

Closes #4885.

…lock (#4885)

#4832 (the fix of #4814) keeps Paro's packed CPU operands on the module so
`state_dict()` still emits the checkpoint layout. To make that safe it wrapped
the whole GEMM, not only the cold layout transform, in
`BaseQuantLinear._cpu_transform_lock` - one process-wide RLock shared by every
module of every model in the process. Warm Paro CPU matmuls therefore
serialized: two modules on two threads scale 0.99-1.00x before this change and
1.87-1.96x after it.

Both CPU forwards now capture `qweight`/`qzeros`/`scales_and_zeros` (and the
widened FP32 scales/bias) into locals under the lock, release it, and run the
product on those locals. The ATen failure path re-takes the lock only to
restore the checkpoint layout. A concurrent `state_dict()`/`load_state_dict()`
still sees a consistent attribute set, while an in-flight GEMM keeps the
operands it captured.

Adds `tests/kernels/test_paro_cpu_lock_concurrency_4885.py`. It gates the
first GEMM and asserts the process-wide lock is free while that GEMM runs and
that a second warm forward completes before the gate is released. It covers
the ATen path natively and the AVX512 path with pure-torch stand-ins, and it
hammers `state_dict()` against concurrent warm forwards, requiring bitwise
identical outputs and checkpoint tensors.

Tests (CPU-only, CUDA_VISIBLE_DEVICES=""):
  tests/kernels/test_paro_cpu_lock_concurrency_4885.py
    base 2 failed, 2 passed -> head 4 passed
  tests/kernels/test_paro.py
  tests/test_cpu_lazy_transform_concurrency_4565.py
  tests/test_cpu_int4pack_dispatch_contracts.py
  70 passed, 18 skipped (the skips are CUDA/AVX512-BF16-only nodes)
CodeQL code quality flags a direct BaseException handler. The workers only
need to surface normal runtime failures; KeyboardInterrupt/SystemExit should
propagate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant