Skip to content

perf(cpu): add shared AVX512-BF16 GPTQ/AWQ inference kernel - #3408

Merged
Qubitium merged 5 commits into
mainfrom
feat/cpu-gptq-avx512-kernel
Oct 21, 2026
Merged

Qubitium merged 5 commits into
mainfrom
feat/cpu-gptq-avx512-kernel

Conversation

@Qubitium

@Qubitium Qubitium commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Adds the shared int4_qzero_group_scaled_cpu AVX512-BF16 W4A16 GEMM, ported from the HF Kernels quantization-gptq CPU kernel.
  • Uses one compute kernel for GPTQ and AWQ GEMM; each format has its own fused checkpoint-to-kernel packer.
  • Adds BACKEND.GPTQ_TORCH_CPU_AVX512 and BACKEND.AWQ_TORCH_CPU_AVX512, selected ahead of the ATen int4pack fallback for compatible Linux x86-64 hosts and layer shapes.
  • Supports group sizes 16, 32, 64, 128, 256, and 512.
  • Adds the fused GPTQ and AWQ int4 packers, XSAVE/AVX512 feature gating, identity-g_idx fast paths, channel-order-preserving dequantization, tests, and a benchmark.
  • Keeps HF Kernels out of the runtime dependency set.

Naming

The kernel is named int4_qzero_group_scaled_cpu because it implements INT4 zero-point plus group-scale quantization, not a generic INT4 format. The same name is used by the extension registry, torch.ops namespace, C++ namespace, Python kernel handles, and benchmark.

Why port instead of consuming HF Kernels directly?

HF kernels 0.17.2 does not publish a build variant compatible with torch 2.16.0.dev20261020+cpu. kernels.get_kernel therefore cannot load a prebuilt binary for this environment. Direct consumption would require a compatible wheel or building HF's extension against this torch build; porting avoids that ABI/build-variant dependency and lets GPT-QModel use its existing JIT extension loader.

HF Kernels also publishes no GIL-disabled (PYTHON_GIL=0) compatible builds, despite Python 3.14 introducing free-threaded builds more than a year ago. GPT-QModel can JIT-build the ported CPU kernel for the active interpreter and torch ABI instead of being blocked on an unavailable HF Kernels wheel variant.

Attribution

The upstream Intel/Hugging Face attribution is retained in the CPU kernel source headers and NOTICE, with the upstream commit pinned. The duplicate gptqmodel_ext/gptq_cpu/LICENSE file has been removed.

Performance

Measured on AMD EPYC 9V33X (Zen 5), 8 torch threads, 4096x4096 GPTQ group-128 layers:

  • AVX512 vs ATen int4pack: 1.77x at M=1, 1.92x at M=32, 1.92x at M=128, 1.88x at M=512.
  • Fused packing: 227-459x faster than the Python packer.
  • Real Llama 3.2 1B GPTQ warm forward: 0.0628 s vs 0.0902 s (1.44x), with top-1 logits matching.
  • Relative error versus an FP32 dequantized reference: approximately 1.6-2.3e-3.
  • AUTO priority is 120 for the AVX512 GPTQ/AWQ kernels versus 110 for the ATen int4pack kernels; selection tests assert that AVX512 is chosen first when available.

AVX512 vs ATen: speed and accuracy

Method: 4096x4096 layer, BF16 activations, 8 torch threads, 3 warmups, 10 timed iterations, median wall time. Accuracy is normalized L2 error versus an FP32 matmul of the same dequantized weight. ATen supports groups 16/32/64/128; groups 256/512 are AVX512-only.

GPTQ

Group M AVX512 ms ATen ms Speedup AVX512 rel L2 ATen rel L2
32 1 0.125 0.223 1.79x 1.6630e-3 1.9571e-3
32 8 0.472 0.790 1.68x 1.6485e-3 1.9509e-3
32 32 1.660 3.130 1.89x 2.2969e-3 1.9524e-3
32 128 6.274 12.556 2.00x 2.3006e-3 1.9606e-3
32 512 25.119 50.031 1.99x 2.2962e-3 1.9610e-3
64 1 0.066 0.117 1.77x 1.6772e-3 2.0058e-3
64 8 0.237 0.393 1.66x 1.6346e-3 1.9516e-3
64 32 0.805 1.547 1.92x 2.2956e-3 1.9552e-3
64 128 3.187 6.153 1.93x 2.3015e-3 1.9592e-3
64 512 12.772 24.811 1.94x 2.2937e-3 1.9586e-3
128 1 0.064 0.115 1.80x 1.6732e-3 1.9669e-3
128 8 0.234 0.389 1.67x 1.6723e-3 1.9825e-3
128 32 0.801 1.536 1.92x 2.2868e-3 1.9634e-3
128 128 3.177 6.106 1.92x 2.2913e-3 1.9614e-3
128 512 12.700 24.421 1.92x 2.2934e-3 1.9619e-3

AWQ

Group M AVX512 ms ATen ms Speedup AVX512 rel L2 ATen rel L2
32 1 0.080 0.130 1.62x 1.6106e-3 1.9071e-3
32 8 0.253 0.409 1.61x 1.6645e-3 1.9675e-3
32 32 0.817 1.587 1.94x 2.2881e-3 1.9618e-3
32 128 3.226 6.279 1.95x 2.2988e-3 1.9523e-3
32 512 12.793 25.220 1.97x 2.2976e-3 1.9568e-3
64 1 0.076 0.127 1.68x 1.6516e-3 1.9139e-3
64 8 0.247 0.404 1.64x 1.6608e-3 1.9770e-3
64 32 0.818 1.563 1.91x 2.2919e-3 1.9641e-3
64 128 3.201 6.184 1.93x 2.2993e-3 1.9586e-3
64 512 12.829 24.833 1.94x 2.3001e-3 1.9586e-3
128 1 0.075 0.125 1.67x 1.6564e-3 1.9991e-3
128 8 0.244 0.401 1.65x 1.6738e-3 1.9832e-3
128 32 0.817 1.551 1.90x 2.3207e-3 1.9479e-3
128 128 3.199 6.131 1.92x 2.2932e-3 1.9573e-3
128 512 12.717 24.502 1.93x 2.2980e-3 1.9567e-3

AVX512 max-abs errors in these runs ranged from 0.123 to 0.220 for GPTQ and 0.123 to 0.262 for AWQ; ATen ranged from 0.135 to 0.220 and 0.135 to 0.240, respectively.

Accuracy vs Torch oracle

The tests compare the kernel against an independent Torch oracle for GPTQ and AWQ at group sizes 32, 64, 128, 256, and 512. Dequantized weights are computed in FP32 before the matmul; this keeps the oracle independent of the kernel's BF16 path. This is a precision correction to the oracle, not a tolerance relaxation. The 256/512 cases exercise both the M<=24 tiny-GEMM path and the M>24 BRGEMM path.

Enforced limits:

  • BF16 output: atol=0.2, rtol=0.01, normalized L2 < 3e-3 for groups 32-128 and < 3.5e-3 for groups 256/512
  • FP16 activation path: atol=0.25, rtol=0.02
  • Dequantized weights: atol=2e-3, rtol=2e-3

Measured kernel vs Torch oracle over 10 seeds with M=1/7/64, K=1024, N=256, BF16, using synthetic random packed weights/zeros/scales:

Format Group Max normalized L2 Mean normalized L2 Max abs
GPTQ 32 2.547632e-3 8.378387e-4 0.125
GPTQ 64 2.567908e-3 8.320676e-4 0.125
GPTQ 128 2.570224e-3 8.302094e-4 0.125
GPTQ 256 2.570842e-3 8.326195e-4 0.250
GPTQ 512 2.600805e-3 8.241488e-4 0.250
AWQ 32 2.550820e-3 1.228687e-3 0.125
AWQ 64 2.544854e-3 1.179773e-3 0.125
AWQ 128 2.571644e-3 1.242516e-3 0.125
AWQ 256 3.285290e-3 1.662607e-3 0.125
AWQ 512 3.291246e-3 1.665776e-3 0.125

These numbers measure kernel numerical accuracy against the dequantized Torch reference, not end-to-end model perplexity.

Validation

  • Clean JIT rebuild with torch 2.16.0.dev20261020+cpu under the renamed namespace.
  • Focused GPTQ/AWQ CPU kernel and backend-selection suite: 137 passed, 51 skipped.
  • GPTQ and AWQ oracle coverage now includes group sizes 32, 64, 128, 256, and 512, including the tiny-GEMM and BRGEMM paths.
  • AUTO selection is tested for group sizes 256 and 512 for both GPTQ and AWQ.
  • git diff --check: clean.

Notes

  • oneDNN W8A8 is faster but uses a different quantized format, roughly doubles weight memory, and changes numerics, so it is not the default GPTQ/AWQ W4A16 path.
  • torchao's optimized Int4Tensor forward dispatches to MSLK's bf16i4bf16_rowwise; MSLK publishes CUDA/ROCm builds only, so there is no CPU kernel to benchmark. torchao's CPU int4 path uses ATen _weight_int4pack_mm_for_cpu, which is the ATen baseline in the tables above.

Port the HF Kernels GPTQ AVX512-BF16 W4A16 kernel into GPT-QModel and select it ahead of the ATen int4pack fallback when the host and layer shape are compatible.

Add a fused C++ packer, XSAVE/AVX512 feature gating, identity-g_idx fast paths, channel-order-preserving dequantization, tests, and a benchmark.
Rename the shared CPU kernel to int4_qzero_group_scaled_cpu across the extension registry, torch.ops namespace, C++ namespace, and Python handles. Add the fused AWQ-to-HF int4 packer and reuse the GPTQ compute kernel for AWQ GEMM layers. Keep Intel/Hugging Face attribution in the source headers and NOTICE while removing the duplicate license file.
@Qubitium Qubitium changed the title perf(cpu): add AVX512-BF16 GPTQ inference kernel perf(cpu): add shared AVX512-BF16 GPTQ/AWQ inference kernel Oct 21, 2026
@Qubitium
Qubitium merged commit d3c03fa into main Oct 21, 2026
6 checks passed
@Qubitium
Qubitium deleted the feat/cpu-gptq-avx512-kernel branch October 21, 2026 19:34
Qubitium added a commit to jiqing-feng/GPTQModel that referenced this pull request Nov 2, 2026
HF/optimum load GPTQ/AWQ with BACKEND.AUTO and PEFT then calls model.train(), so an inference-only CPU kernel picked by AUTO regresses fine-tuning (see ModelCloud#3408). AUTO now prefers trainable CPU GPTQ/AWQ kernels and falls back to an inference-only kernel only when no trainable one is valid; accelerator and non-GPTQ/AWQ priority orders are unchanged. Also point users at backend="auto_trainable" when an inference-only kernel is switched to training mode.
Qubitium added a commit that referenced this pull request Nov 2, 2026
)

* fix(cpu): keep AVX512-BF16 GPTQ/AWQ kernels trainable

#3408 made TorchCpuAvx512Linear / TorchCpuAvx512AwqLinear the top
BACKEND.AUTO choice on AVX512-BF16 CPUs but marked them inference-only and
made train(True) raise. HF Transformers/PEFT load GPTQ/AWQ checkpoints with
BACKEND.AUTO and then call model.train() for LoRA fine-tuning, so CPU
fine-tuning of bf16 GPTQ/AWQ models now fails with:

  NotImplementedError: TorchCpuAvx512Linear is an inference-only CPU kernel.

Before #3408 AUTO selected TorchAtenLinear, which trains through the
dequantized-matmul path. The AVX512 kernels already inherit/implement that
same path in forward() (fused kernel only when not training and no grad), so
mark them SUPPORTS_TRAINING and drop the raising train() overrides.

Seeded Llama-3.1-8B-GPTQ-INT4 + PEFT LoRA on CPU gives identical loss and
LoRA grad norm to the pre-#3408 TorchAtenLinear path.

* fix(cpu): reset AVX512 training dispatch and keep AWQ bias dtype

- Reset the cached fused-vs-dequantized dispatch when the AVX512 GPTQ/AWQ
  layers switch between train and eval. An eval forward previously pinned
  linear_mode to "inference", so later training forwards kept using the
  no-grad fused kernel and dropped input gradients through the base
  projection.
- Cast the AWQ training fallback bias to the activation dtype. The fused
  kernel stores bias in FP32, and out + bias promoted BF16 outputs to FP32.
- Cover eval-forward -> train -> backward for both kernels and bias=True for
  AWQ.

* fix(cpu): match ATen bias rounding in AVX512 AWQ training

Add the FP32 bias in place in the dequantized training fallback so the sum is rounded once to the activation dtype, matching TorchAtenLinear. Use independent checkpoint-order dequantization oracles in the GPTQ/AWQ training regression tests and tighten them to 2e-3.

* fix(auto): keep CPU AUTO on trainable GPTQ/AWQ kernels

HF/optimum load GPTQ/AWQ with BACKEND.AUTO and PEFT then calls model.train(), so an inference-only CPU kernel picked by AUTO regresses fine-tuning (see #3408). AUTO now prefers trainable CPU GPTQ/AWQ kernels and falls back to an inference-only kernel only when no trainable one is valid; accelerator and non-GPTQ/AWQ priority orders are unchanged. Also point users at backend="auto_trainable" when an inference-only kernel is switched to training mode.

---------

Co-authored-by: Qubitium <[email protected]>
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