Skip to content

perf(cpu): optimize quantization kernels and runtime caches - #3412

Merged
Qubitium merged 3 commits into
mainfrom
perf/cpu-quant-kernel-audit
Oct 22, 2026
Merged

Qubitium merged 3 commits into
mainfrom
perf/cpu-quant-kernel-audit

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

This PR builds on the merged shared GPTQ/AWQ kernel work in #3408 and completes the CPU kernel/pipeline audit across the exposed quantization methods.

  • Adds shape-safe inference caches for Torch, AWQ, ParoQuant, QQQ, FP8, and VPTQ, with invalidation on weight mutation, load_state_dict, dtype/device changes, and training-mode changes.
  • Extends the shared int4_qzero_group_scaled_cpu GPTQ/AWQ kernel to group sizes 16/32/64/128/256/512.
  • Keeps one shared INT4 zero-point/group-scale compute kernel for GPTQ and AWQ; each format keeps its own checkpoint packer.
  • Vectorizes AWQ INT4 packing and the ParoQuant CPU rotation path.
  • Routes QQQ CPU inference through torch._int_mm with a shape-scoped FP32 fallback.
  • Adds fused GGUF Q4_0/Q8_0 block matmul plus the existing K-quant paths, with per-shape autotuning between fused and dense dequant+GEMM.
  • Adds accuracy, cache-invalidation, packing, and benchmark coverage.

The kernel remains named int4_qzero_group_scaled_cpu because it specifically implements INT4 zero-point plus group-scale quantization, not a generic INT4 format.

Host and methodology

  • CPU: AMD EPYC 9V33X (Zen 5), AVX512-BF16/VNNI/VBMI, no AMX.
  • Threads: 8 PyTorch threads, 1 inter-op thread.
  • Python: 3.14.4, GIL enabled.
  • PyTorch: 2.16.0.dev20261021+cpu (latest CPU nightly at test time).
  • torchao: 0.18.0+cpu.
  • oneDNN: 3.13.3.
  • HF kernels: 0.18.0.dev0.
  • GPTQ/AWQ speed table: 512x512, group 128, BF16, M=1/7/25, 5 warmups, 20 timed iterations, median wall time.
  • Accuracy: normalized L2 against an FP32 Torch matmul of the same dequantized weight. raw compares against the unrounded FP32 oracle; visible rounds the oracle to the output dtype first.

GPTQ/AWQ accuracy vs Torch oracle

The BF16 AVX512 tests enforce normalized L2 < 2e-3 plus atol=0.2, rtol=0.01. FP16 activation coverage uses atol=0.25, rtol=0.02. Dequantized-weight comparisons use atol=2e-3, rtol=2e-3.

Each group size is tested independently. Groups 16/32/64/128/256/512 all pass.

Format Groups Enforced gate Observed raw rel L2 Visible BF16 rel L2
GPTQ 4-bit 16, 32, 64, 128, 256, 512 < 2e-3 1.620e-3 - 1.680e-3 0 - 1.115e-4
AWQ 4-bit 16, 32, 64, 128, 256, 512 < 2e-3 1.651e-3 - 1.734e-3 0 - 2.62e-5
ATen int4pack, FP16 path 32, 64, 128 n/a 2.44e-4 - 2.54e-4 n/a
ATen int4pack, BF16 fallback 32, 64, 128 n/a 2.50e-3 - 2.69e-3 n/a

Notes:

  • FP16 GPTQ/AWQ activations use native ATen int4pack.
  • BF16 activations use the ported AVX512-BF16 kernel.
  • ATen has no group 256/512 support, so those groups are AVX512-only.
  • The accuracy numbers measure kernel numerics against a dequantized Torch reference, not end-to-end model perplexity.

GPTQ/AWQ speed vs ATen, dense, torchao, and oneDNN

512x512, group 128, BF16:

M GPTQ AVX512 GPTQ ATen AWQ AVX512 AWQ ATen Dense BF16 torchao W4A16 oneDNN W8A8
1 0.008 ms 0.008 ms 0.015 ms 0.015 ms 0.017 ms 0.017 ms 0.021 ms
7 0.013 ms 0.018 ms 0.020 ms 0.025 ms 0.024 ms 0.025 ms 0.023 ms
25 0.027 ms 0.048 ms 0.033 ms 0.055 ms 0.034 ms 0.057 ms 0.029 ms

Speedups versus ATen:

  • GPTQ: 1.07x / 1.40x / 1.77x at M=1/7/25.
  • AWQ: 1.01x / 1.22x / 1.68x at M=1/7/25.

The AVX512 kernel is selected at AUTO priority 120, above ATen int4pack at 110, when the host and layer shape are compatible.

ATen here means PyTorch's native CPU INT4 op (_weight_int4pack_mm_for_cpu), not torchao. torchao's legacy CPU W4A16 path dispatches to the same ATen op on this host; no separate optimized CPU INT4 kernel was available. oneDNN W8A8 is context only: it uses a different quantization format and roughly doubles weight memory.

Packing speed

Across 1024x1024, 2048x1024, and 1024x2048:

Format Fused C++ Previous Python Speedup
GPTQ INT4 0.026 - 0.052 ms 2.968 - 7.866 ms 72x - 203x
AWQ INT4 0.101 - 0.183 ms 2.461 - 4.784 ms 18.5x - 27.1x

Other exposed CPU methods

512x512, M=1/7/25/64/128:

Method CPU path Speed vs baseline Accuracy vs method FP32 oracle
QQQ torch._int_mm + cached weight 1.92x - 4.28x exact (0)
ParoQuant vectorized rotation + AVX512 BF16 1.03x - 1.07x raw 1.64e-3 - 1.79e-3; visible 0 - 5.5e-5
FP8 cached dequant 6.23x - 16.86x ~3.1e-3
VPTQ cached dense dequant 43.4x - 154.3x ~1.65e-3
GGUF Q4_K fused block matmul 1.57x - 1.68x format precision 8.7e-3 - 1.05e-2
GGUF Q5_K fused block matmul 1.44x - 1.66x format precision 8.7e-3 - 1.08e-2
GGUF Q6_K fused block matmul 1.53x - 1.93x format precision ~3.4e-3
GGUF Q8_0 fused block matmul 0.94x - 1.29x format precision ~3.0e-3 - 3.5e-3
GGUF Q4_0 fused block matmul 0.72x - 1.02x format precision ~2.7e-3 - 3.4e-3

GGUF Q4_0/Q8_0 fused kernels are not consistently faster than dense dequant+GEMM. The production path now caches the autotune decision per activation shape, so it selects dense where fused loses. On this host Q4_0 selected dense for M=1/7/25/64 and fused at M=128; Q8_0 was mixed. K-quant paths consistently selected fused in the audited range.

GGUF raw errors are format precision versus the method's own dequantized FP32 oracle. They are not GPTQ/AWQ kernel error and should not be compared to the 2e-3 GPTQ gate.

Packing/loading at 512x512:

Method Median
QQQ 2.619 ms
ParoQuant 0.230 ms
FP8 0.248 ms
VPTQ 2.760 ms
GGUF Q4_0 1.313 ms
GGUF Q8_0 0.876 ms
GGUF Q4_K 1.876 ms
GGUF Q5_K 2.131 ms
GGUF Q6_K 1.982 ms

VPTQ packing time loads a precomputed quantization result and excludes k-means training.

HF Kernels: consume or port?

HF kernels latest stable is 0.17.2. It publishes CPU build variants only through torch 2.15; there is no torch 2.16 build, so kernels.get_kernel cannot load a compatible binary for this environment. There are also no GIL-disabled (PYTHON_GIL=0) builds, despite Python 3.14 introducing free-threaded builds more than a year ago.

The relevant upstream CPU-kernel discussion is:

https://github.com/huggingface/kernels/pull/920

That PR is closed. Direct consumption would require a compatible wheel or rebuilding HF's extension against the active torch ABI. GPT-QModel instead ports the kernel into its existing JIT extension loader, which can build for the active interpreter and torch version. This avoids a hard dependency on an unavailable HF Kernels build variant.

Attribution

The upstream Intel/Hugging Face author headers and repository attribution remain in the CPU kernel headers and NOTICE. The duplicate gptqmodel_ext/gptq_cpu/LICENSE file was removed in #3408.

Validation

/root/.venvs/gptqmodel-cpu/bin/python3 -m pytest -q \
  tests/test_torch.py tests/test_torch_cpu_avx512.py \
  tests/test_torch_cpu_avx512_awq.py tests/kernels/test_gguf_torch_cpu.py \
  tests/test_qqq_jit.py tests/kernels/test_fp8_kernel.py \
  tests/kernels/test_paroquant.py tests/test_vptq.py \
  tests/test_awq_pack_vectorized.py -k "not cuda and not triton"

Result: 322 passed, 10 skipped, 118 deselected.

git diff --check is clean. Ruff passes on the changed GGUF files with the repository config.

Notes and caveats

  • mslk 2026.9.9+cu130 is installed, but mslk.so fails to load against the CPU-only torch build. No MSLK comparison is claimed.
  • torchao's optimized Int4Tensor forward dispatches to MSLK's bf16i4bf16_rowwise; MSLK publishes CUDA/ROCm builds only. On CPU, torchao's legacy int4 path uses ATen _weight_int4pack_mm_for_cpu.
  • HF Kernels cannot be consumed directly on this torch build; the port is the supported path here.
  • Q4_0 fused is opportunistic and autotuned per shape, not a guaranteed speedup.

@Qubitium
Qubitium merged commit 6c4d3b6 into main Oct 22, 2026
6 checks passed
@Qubitium
Qubitium deleted the perf/cpu-quant-kernel-audit branch October 22, 2026 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant