Skip to content

perf(cpu): tune and validate optimized quantized kernels - #3430

Merged
Qubitium merged 1 commit into
mainfrom
perf/cpu-kernel-validation
Oct 23, 2026
Merged

Qubitium merged 1 commit into
mainfrom
perf/cpu-kernel-validation

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

  • Adds the CPU-relevant BF16x3 and FP32-accumulation behavior from HF Kernels directly to GPTQModel's existing JIT extension path instead of adding a runtime dependency on HF Kernels.
  • Tunes the BnB INT8 tiled recompute path to the measured 64 <= M <= 256 window for 512x512 BF16 layers. The path uses bounded feature tiles and never retains a dense weight.
  • Makes GGUF fused CPU forward accumulate in FP32 under oneDNN BF16x3, adds bias inside the fused path, and fixes the per-shape autotune result propagation.
  • Keeps BnB packed 4-bit CPU inference opt-in and enforces a normalized-L2 accuracy gate of 2e-3.
  • Adds independent Torch oracles, persistent-storage regression tests, and reproducible GPTQ/AWQ, BnB, GGUF, ATen, torchao, and oneDNN benchmarks.
  • Qualifies the shared GPTQ/AWQ int4_qzero_group_scaled_cpu kernel for group sizes 32, 64, 128, 256, and 512.

Storage Guarantee

Inference retains only native packed quantized weights, scales, zero points, and bias. No dense, dequantized, expanded, or INT4-to-INT8 weight cache is retained after a forward, including in repeated calls, warmup, fallback paths, and compiled/JIT paths. BnB INT8 and GGUF fused CPU forward use temporary bounded chunks/tiles released after the consuming operation.

Regression coverage checks repeated forwards and verifies that the persistent buffers remain the packed qweight/bias representation. No additional dense weight bytes are retained.

Accuracy

Environment: AMD EPYC 9V33X, AVX512-BF16, 8 Torch intra-op threads, Torch 2.16.0.dev20261021+cpu, torchao 0.18.0+cpu, Python 3.14.4. Shape is 512x512; errors are normalized L2 against an independent FP32 Torch matmul of the same dequantized weight. Values are the maximum observed over M = 1, 7, 32, 128, 512.

Group GPTQ max rel err AWQ max rel err ATen GPTQ max ATen AWQ max Gate
32 1.6645e-3 1.7152e-3 1.9707e-3 2.0258e-3 PASS
64 1.6610e-3 1.6617e-3 1.9703e-3 1.9593e-3 PASS
128 1.6798e-3 1.6787e-3 2.0491e-3 1.9772e-3 PASS
256 1.7301e-3 1.6633e-3 2.0647e-3 1.9525e-3 PASS
512 1.6502e-3 1.6729e-3 n/a n/a PASS

ATen is an external comparison and is shown for context; its observed error is not the acceptance gate for this kernel. ATen supports group sizes 32/64/128/256, so group 512 has no ATen comparison.

BnB INT8 uses an independent A8W8 oracle for the native path and an A16 dequantize-and-matmul oracle for the tiled path:

M Native ms Shape-aware ms Speedup Dispatch Error vs oracle
1 0.074 0.080 0.92x native 1.7461e-3
7 0.115 0.122 0.95x native 1.7150e-3
32 0.204 0.214 0.95x native 1.7673e-3
64 0.316 0.246 1.28x tiled 1.6483e-3
128 0.401 0.335 1.20x tiled 1.6476e-3
256 0.441 0.393 1.12x tiled 1.6627e-3
512 0.406 0.413 0.98x native 1.7589e-3

BnB packed 4-bit CPU remains opt-in: its measured normalized error is 3.3e-3 to 3.8e-3, above the 2e-3 gate, so it is rejected rather than silently used.

Speed

GPTQ and AWQ vs ATen

Median per-call wall time, milliseconds, 512x512, 8 threads, 5 warmup, 20 iterations. ours/ATen shows the speedup.

Group M GPTQ ours GPTQ ATen GPTQ speedup AWQ ours AWQ ATen AWQ speedup
32 1 0.008 0.009 1.05x 0.015 0.016 1.04x
32 7 0.013 0.017 1.28x 0.021 0.024 1.15x
32 32 0.036 0.055 1.55x 0.043 0.063 1.47x
32 128 0.117 0.202 1.72x 0.126 0.209 1.66x
32 512 0.441 0.789 1.79x 0.450 0.797 1.77x
64 1 0.007 0.007 1.00x 0.014 0.014 0.99x
64 7 0.010 0.012 1.16x 0.017 0.019 1.12x
64 32 0.020 0.031 1.51x 0.028 0.038 1.37x
64 128 0.060 0.102 1.71x 0.067 0.110 1.65x
64 512 0.215 0.390 1.81x 0.225 0.402 1.79x
128 1 0.007 0.007 1.02x 0.014 0.014 1.01x
128 7 0.010 0.012 1.21x 0.017 0.018 1.10x
128 32 0.018 0.030 1.66x 0.025 0.037 1.50x
128 128 0.055 0.102 1.86x 0.062 0.110 1.78x
128 512 0.197 0.395 2.01x 0.205 0.398 1.94x
256 1 0.006 0.007 1.04x 0.014 0.014 1.02x
256 7 0.010 0.012 1.15x 0.017 0.019 1.09x
256 32 0.018 0.030 1.68x 0.026 0.037 1.46x
256 128 0.055 0.102 1.87x 0.061 0.109 1.80x
256 512 0.192 0.388 2.02x 0.199 0.397 1.99x
512 1 0.007 n/a n/a 0.015 n/a n/a
512 7 0.011 n/a n/a 0.018 n/a n/a
512 32 0.019 n/a n/a 0.026 n/a n/a
512 128 0.054 n/a n/a 0.061 n/a n/a
512 512 0.189 n/a n/a 0.197 n/a n/a

Other CPU paths at group 128, M=7/128/512

Path M7 ms M128 ms M512 ms Quantizer
GPTQ AVX512 int4_qzero_group_scaled_cpu 0.010 0.055 0.197 GPTQ affine group
ATen int4pack 0.012 0.102 0.395 ATen affine group
torchao A16W4 (Int4OpaqueTensor) 0.023 0.113 0.404 torchao affine group
oneDNN W8A8 0.021 0.039 0.099 INT8 W8A8

torchao A16W4 uses a different affine quantizer and calls ATen's _weight_int4pack_mm_for_cpu; its error is not GPTQ kernel error. oneDNN W8A8 is also a different precision/quantizer. Both are included as context, not as byte-identical implementations.

GGUF fused CPU, 512x512, M=7

Type Fused ms Dense ms Speedup Raw error
Q4_0 0.994 0.898 0.90x 1.6563e-3
Q8_0 0.513 0.590 1.15x 1.6930e-3
Q4_K 0.515 0.987 1.92x 1.6963e-3
Q5_K 0.581 1.051 1.81x 1.6845e-3
Q6_K 0.408 0.772 1.89x 1.6964e-3

The per-shape autotuner selects the dense path for Q4_0 where fused loses.

HF Kernels Compatibility

  • HF Kernels 0.17.2 is the latest PyPI stable release as of 2026-10-23; the validation environment also tested 0.18.0.dev0.
  • The CPU variants published for this environment cover Torch 2.10 through 2.15. There is no build variant for 2.16.0.dev20261021+cpu, so kernels.get_kernel cannot load a prebuilt CPU binary.
  • There is no free-threaded CPython 3.14 build. HF Kernels PR Add opt-in CPU JIT fallback for missing kernel builds huggingface/kernels#920 rejects free-threaded Python below 3.15 because the current extension ABI is unsafe there.
  • Direct consumption would therefore require a compatible wheel or building HF's extension against this Torch build. Porting the relevant CPU arithmetic into GPTQModel avoids that ABI/build-variant dependency and uses GPTQModel's existing JIT loader.
  • This PR does not claim that every HF CPU kernel was JIT-validated. The ported behavior is limited to the CPU paths covered by these tests.
  • mslk is not installed. PyPI currently exposes only the placeholder mslk 0.0.0, not a usable kernel package. torchao DA8W4 therefore remains unavailable; torchao A16W4 works without mslk.
  • ATen is part of PyTorch, not torchao. torchao A16W4 delegates to ATen's CPU int4pack op.

Validation

  • Focused main environment: 78 passed, 40 skipped, 1 failed; the single failure is the unrelated pre-existing test_gguf_triton_kernel_rejects_unsupported_formats, which raises ModuleNotFoundError: triton before reaching the format assertion.
  • GPTQ/AWQ AVX512 CPU suites: 80 passed, including group sizes 256 and 512.
  • Free-threaded Python 3.14.7t with Torch 2.14.1+cpu: 55 passed, 63 skipped, 1 failed with the same unrelated missing-Triton failure.
  • ruff, py_compile, and git diff --check pass.
  • BnB tests confirm the 64..256 gate and repeated-forward packed-storage retention.
  • GGUF tests cover Q4_0, Q8_0, Q4_K, Q5_K, Q6_K in FP32/BF16/FP16 and verify no dense cache is retained.

Notes

  • The CPU kernel name int4_qzero_group_scaled_cpu is intentionally format-specific: it describes the affine zero-point/group-scale format shared by GPTQ and AWQ, rather than all INT4 quantization algorithms.
  • Intel/HF upstream attribution is retained in the kernel header notes. No new license or copyright header is added.

@Qubitium
Qubitium merged commit 737d46a into main Oct 23, 2026
6 checks passed
@Qubitium
Qubitium deleted the perf/cpu-kernel-validation branch October 23, 2026 06:04
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