Skip to content

fix(paro): keep one packed CPU layout per layer (#4814) - #4832

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4814
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4814

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4814.

ParoLinear (AUTO for Paro on CPU) kept a second kernel-layout copy of every
packed weight next to the checkpoint buffers: the bf16 AVX512 path stored
_cpu_packed_* attributes, and the fp16 ATen path built a hidden
TorchAtenAwqLinear child that cloned every packed buffer. Measured 2.02x
(bf16) / 2.05x (fp16) on the quantized linears, ~+3.5 GiB for an 8B model.

Change

Reuses the existing _fused_train_state machinery (#4532/#4788) instead of a
new mechanism: ParoLinear rebinds qweight/qzeros to the packed operands of
the single layout it is running and rebuilds checkpoint words for
state_dict()/save from a verified inverse (dropped clone only when byte-exact;
fail-closed otherwise). AVX512 and ATen each own exactly one layout; layout
switch, .to(), train(), dense fallback, checkpoint load and cache clear all
restore the checkpoint layout first. Scales/bias keep the checkpoint dtype and
are widened per call.

Measured residency (512->256, g128, krot=2, after first forward)

path parent fixed
ATen fp16 2.070x 1.169x (1.052x quantized)
ATen bf16 2.099x 1.197x
AVX512 — 1.015x quantized

Residual ATen overhead is the FP32 scales_and_zeros operand the op requires.

Tests

CUDA_HOME=... pytest tests/kernels/test_paro.py -> 47 passed, 6 skipped
pytest tests/test_paro.py -> 219 passed, 23 failed

All failures are CUDA fused-rotation JIT builds (CUDA_HOME unset in that run);
identical on the parent commit. New residency/round-trip tests: state_dict()
bit-exact for all four tensors on both paths; .to(), train(),
load_state_dict, dense fallback all restore int32.

Red on parent: 3 failed; fixed 7 passed, 3 skipped.

ruff: clean.

The CPU inference path built a hidden `TorchAtenAwqLinear` child that
cloned every packed buffer, then left the checkpoint `qweight`/`qzeros`
resident alongside the kernel operands. A Paro CPU layer therefore held
both layouts at once: ~2.07x (fp16) / ~2.10x (bf16) the checkpoint bytes
after the first forward.

Reuse the existing `_fused_train_state` machinery (#4532/#4788) instead
of a second module. `ParoLinear` now rebinds `qweight`/`qzeros` to the
packed operands of the layout it is actually running and rebuilds the
checkpoint words for `state_dict()`/save from a verified inverse; the
AVX512 and ATen paths each own exactly one layout, and switching layouts,
`.to()`, `train()`, the dense fallback, a checkpoint load and a weight
cache clear all restore the checkpoint layout first. Scales and bias keep
their checkpoint dtype and are widened per call for the AVX512 kernel.

Resident bytes after the first forward (Qwen-like 512->256, g128, krot=2):
ATen 1.05x quantized / 1.17x total (fp16), 1.20x (bf16); AVX512 1.02x
quantized. The remaining ATen overhead is the FP32 `scales_and_zeros`
operand the op requires. `state_dict()` stays bit-exact for all four
tensors on both paths.
@Qubitium
Qubitium merged commit 9f2569c into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4814 branch November 17, 2026 16:27
sLaffey pushed a commit to sLaffey/GPTQModel that referenced this pull request Nov 18, 2026
…lock (ModelCloud#4885) (ModelCloud#4922)

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

ModelCloud#4832 (the fix of ModelCloud#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)

* test(paro): catch Exception in the CPU lock concurrency workers

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