Repository navigation
perf(cpu): optimize quantization kernels and runtime caches - #3412
Merged
Merged
Conversation
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
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.
load_state_dict, dtype/device changes, and training-mode changes.int4_qzero_group_scaled_cpuGPTQ/AWQ kernel to group sizes16/32/64/128/256/512.torch._int_mmwith a shape-scoped FP32 fallback.The kernel remains named
int4_qzero_group_scaled_cpubecause it specifically implements INT4 zero-point plus group-scale quantization, not a generic INT4 format.Host and methodology
2.16.0.dev20261021+cpu(latest CPU nightly at test time).0.18.0+cpu.kernels:0.18.0.dev0.512x512, group 128, BF16, M=1/7/25, 5 warmups, 20 timed iterations, median wall time.rawcompares against the unrounded FP32 oracle;visiblerounds the oracle to the output dtype first.GPTQ/AWQ accuracy vs Torch oracle
The BF16 AVX512 tests enforce normalized L2
< 2e-3plusatol=0.2,rtol=0.01. FP16 activation coverage usesatol=0.25,rtol=0.02. Dequantized-weight comparisons useatol=2e-3,rtol=2e-3.Each group size is tested independently. Groups
16/32/64/128/256/512all pass.< 2e-31.620e-3 - 1.680e-30 - 1.115e-4< 2e-31.651e-3 - 1.734e-30 - 2.62e-52.44e-4 - 2.54e-42.50e-3 - 2.69e-3Notes:
GPTQ/AWQ speed vs ATen, dense, torchao, and oneDNN
512x512, group 128, BF16:0.008 ms0.008 ms0.015 ms0.015 ms0.017 ms0.017 ms0.021 ms0.013 ms0.018 ms0.020 ms0.025 ms0.024 ms0.025 ms0.023 ms0.027 ms0.048 ms0.033 ms0.055 ms0.034 ms0.057 ms0.029 msSpeedups versus ATen:
1.07x / 1.40x / 1.77xat M=1/7/25.1.01x / 1.22x / 1.68xat M=1/7/25.The AVX512 kernel is selected at AUTO priority
120, above ATen int4pack at110, when the host and layer shape are compatible.ATenhere 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, and1024x2048:0.026 - 0.052 ms2.968 - 7.866 ms72x - 203x0.101 - 0.183 ms2.461 - 4.784 ms18.5x - 27.1xOther exposed CPU methods
512x512, M=1/7/25/64/128:torch._int_mm+ cached weight1.92x - 4.28x0)1.03x - 1.07x1.64e-3 - 1.79e-3; visible0 - 5.5e-56.23x - 16.86x~3.1e-343.4x - 154.3x~1.65e-31.57x - 1.68x8.7e-3 - 1.05e-21.44x - 1.66x8.7e-3 - 1.08e-21.53x - 1.93x~3.4e-30.94x - 1.29x~3.0e-3 - 3.5e-30.72x - 1.02x~2.7e-3 - 3.4e-3GGUF 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/64and 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-3GPTQ gate.Packing/loading at
512x512:2.619 ms0.230 ms0.248 ms2.760 ms1.313 ms0.876 ms1.876 ms2.131 ms1.982 msVPTQ packing time loads a precomputed quantization result and excludes k-means training.
HF Kernels: consume or port?
HF
kernelslatest stable is0.17.2. It publishes CPU build variants only through torch 2.15; there is no torch 2.16 build, sokernels.get_kernelcannot 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/920That 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 duplicategptqmodel_ext/gptq_cpu/LICENSEfile 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 --checkis clean. Ruff passes on the changed GGUF files with the repository config.Notes and caveats
mslk 2026.9.9+cu130is installed, butmslk.sofails to load against the CPU-only torch build. No MSLK comparison is claimed.Int4Tensorforward dispatches to MSLK'sbf16i4bf16_rowwise; MSLK publishes CUDA/ROCm builds only. On CPU, torchao's legacy int4 path uses ATen_weight_int4pack_mm_for_cpu.