Repository navigation
perf(cpu): add shared AVX512-BF16 GPTQ/AWQ inference kernel - #3408
Merged
Merged
Conversation
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
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]>
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
int4_qzero_group_scaled_cpuAVX512-BF16 W4A16 GEMM, ported from the HF Kernelsquantization-gptqCPU kernel.BACKEND.GPTQ_TORCH_CPU_AVX512andBACKEND.AWQ_TORCH_CPU_AVX512, selected ahead of the ATen int4pack fallback for compatible Linux x86-64 hosts and layer shapes.g_idxfast paths, channel-order-preserving dequantization, tests, and a benchmark.Naming
The kernel is named
int4_qzero_group_scaled_cpubecause it implements INT4 zero-point plus group-scale quantization, not a generic INT4 format. The same name is used by the extension registry,torch.opsnamespace, C++ namespace, Python kernel handles, and benchmark.Why port instead of consuming HF Kernels directly?
HF
kernels0.17.2 does not publish a build variant compatible withtorch 2.16.0.dev20261020+cpu.kernels.get_kerneltherefore 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 duplicategptqmodel_ext/gptq_cpu/LICENSEfile has been removed.Performance
Measured on AMD EPYC 9V33X (Zen 5), 8 torch threads,
4096x4096GPTQ group-128 layers:1.77xat M=1,1.92xat M=32,1.92xat M=128,1.88xat M=512.227-459xfaster than the Python packer.0.0628 svs0.0902 s(1.44x), with top-1 logits matching.1.6-2.3e-3.120for the AVX512 GPTQ/AWQ kernels versus110for the ATen int4pack kernels; selection tests assert that AVX512 is chosen first when available.AVX512 vs ATen: speed and accuracy
Method:
4096x4096layer, 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
AWQ
AVX512 max-abs errors in these runs ranged from
0.123to0.220for GPTQ and0.123to0.262for AWQ; ATen ranged from0.135to0.220and0.135to0.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:
atol=0.2,rtol=0.01, normalized L2< 3e-3for groups 32-128 and< 3.5e-3for groups 256/512atol=0.25,rtol=0.02atol=2e-3,rtol=2e-3Measured kernel vs Torch oracle over 10 seeds with
M=1/7/64,K=1024,N=256, BF16, using synthetic random packed weights/zeros/scales:2.547632e-38.378387e-40.1252.567908e-38.320676e-40.1252.570224e-38.302094e-40.1252.570842e-38.326195e-40.2502.600805e-38.241488e-40.2502.550820e-31.228687e-30.1252.544854e-31.179773e-30.1252.571644e-31.242516e-30.1253.285290e-31.662607e-30.1253.291246e-31.665776e-30.125These numbers measure kernel numerical accuracy against the dequantized Torch reference, not end-to-end model perplexity.
Validation
torch 2.16.0.dev20261020+cpuunder the renamed namespace.137 passed, 51 skipped.git diff --check: clean.Notes
Int4Tensorforward dispatches to MSLK'sbf16i4bf16_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.