Repository navigation
perf(paro): run the CPU int4 GEMM outside the process-wide transform lock (#4885) - #4922
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, inBaseQuantLinear._cpu_transform_lock— one process-widethreading.RLockshared 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.forwardalready 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 captureqweight,qzeros, the per-call FP32-widenedscales/biasinto locals; release the lock; rungemm(...)on those locals.gptqmodel/nn_modules/qlinear/paro.py::_forward_cpu_aten: under the lock, run_ensure_cpu_layout("aten", dtype)and captureqweight,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.state_dict()still emits the checkpoint tensors, and outputs are bitwise identical to the pre-fix reference._forward_dense's restore and_apply()/train()still restore without the lock, unchanged from before.Tests
New regression test:
tests/kernels/test_paro_cpu_lock_concurrency_4885.py. It builds a realParoLinear(packed AWQ buffers + rotation), warms it, then gates the first GEMM and asserts:_cpu_transform_lockis free while that GEMM runs (fails onmain: the lock is held across the GEMM);main: it blocks on the lock);state_dict()hammer racing warm forwards never raises and always returns the checkpoint tensors;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).Throughput (concurrent vs serial)
Two independent
ParoLinearmodules, FP16 ATen int4pack path, 4096x4096,torch.set_num_threads(1), 20 warm decode-shaped (M=1) forwards per module, 5 repetitions. Speedup isserial / concurrentfor the best (min) timing of each run. Host: 256-core EPYC, shared with other CI agents, CPython 3.14 free-threading build (2 runs withPYTHON_GIL=0, 1 run withPYTHON_GIL=1):436053174(parent)Raw timings for the
PYTHON_GIL=0runs (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
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:
Kernel Accuracy Requirements
tests/kernels/test_paro.pystill passes its independent FP32 oracle nodes.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 rebindingqweightbetween the reads ofqweight,scales_and_zeros, andbias. Holding it across the GEMM is what this PR removes.Closes #4885.