Skip to content

fix(cpu): new cpu kernels regressed trainable api calls from peft - #3480

Merged
Qubitium merged 4 commits into
ModelCloud:mainfrom
jiqing-feng:fix-cpu-avx512-training-regression
Nov 2, 2026
Merged

Qubitium merged 4 commits into
ModelCloud:mainfrom
jiqing-feng:fix-cpu-avx512-training-regression

Conversation

@jiqing-feng

@jiqing-feng jiqing-feng commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Since #3408, PEFT LoRA fine-tuning of bf16 GPTQ/AWQ checkpoints on AVX512-BF16 CPUs fails as soon as model.train() is called:

NotImplementedError: TorchCpuAvx512Linear is an inference-only CPU kernel. Use BACKEND.GPTQ_TORCH_ATEN for trainable GPTQ layers.
NotImplementedError: TorchCpuAvx512AwqLinear is an inference-only CPU kernel. Use BACKEND.AWQ_TORCH_ATEN for trainable AWQ layers.

Reproducer:

import sys

import torch
from peft import LoraConfig, get_peft_model
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = sys.argv[1] if len(sys.argv) > 1 else "hugging-quants/Meta-Llama-3.1-8B-Instruct-GPTQ-INT4"
model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16, device_map="cpu")
print(type(model.model.layers[0].self_attn.q_proj).__name__)
model = get_peft_model(model, LoraConfig(r=8, task_type="CAUSAL_LM"))

inputs = AutoTokenizer.from_pretrained(model_id)("Hello world", return_tensors="pt")
model.train()
loss = model(**inputs, labels=inputs["input_ids"]).loss
loss.backward()
print("loss", loss.item())
python repro.py hugging-quants/Meta-Llama-3.1-8B-Instruct-GPTQ-INT4
python repro.py hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4

Cause

#3408 added TorchCpuAvx512Linear / TorchCpuAvx512AwqLinear with a higher BACKEND.AUTO priority than TorchAtenLinear / TorchAtenAwqLinear, but marked them SUPPORTS_TRAINING = False and made train(True) raise. Transformers loads GPTQ/AWQ checkpoints with BACKEND.AUTO, so these kernels are now picked by default on CPU, and the Trainer/PEFT model.train() call fails. Before #3408, AUTO picked the ATen kernels, which are trainable.

Note: PEFT's LoRA path never selects the kernel itself (it wraps an already-loaded model), and optimum passes HF's default backend="auto" through to hf_select_quant_linear_v2. So the fix has to live in the kernel and in AUTO selection, not in PEFT.

Fix

The forward path of both AVX512 kernels already handles training: the fused kernel is only used when not training and inputs do not require grad, and otherwise they fall back to a dequantized matmul, as the ATen kernels do. This PR:

  • sets SUPPORTS_TRAINING = True on both kernels and removes the train() overrides that raise;
  • resets the cached linear_mode dispatch on train/eval transitions, so an eval forward cannot pin later training forwards to the @torch.no_grad fused kernel;
  • adds the FP32 bias in place in the AWQ training fallback (out.add_(self.bias)), so the sum is rounded once to the activation dtype like TorchAtenLinear.

Safeguard

So a future inference-only CPU kernel cannot silently regress PEFT/Trainer again:

  • BACKEND.AUTO now prefers a trainable CPU GPTQ/AWQ kernel and only falls back to an inference-only one when no trainable kernel validates. Accelerator AUTO priority (Marlin/ExLlama/...) and non-GPTQ/AWQ methods are unchanged.
  • Switching an inference-only kernel to training now tells the user to reload with backend="auto_trainable".

Validation

On an AVX512-BF16 Xeon CPU (torch 2.14.1+cpu, transformers 5.23.0.dev0):

Kernel selected Reproducer
main TorchCpuAvx512Linear / TorchCpuAvx512AwqLinear NotImplementedError
this PR TorchCpuAvx512Linear / TorchCpuAvx512AwqLinear passes

With a fixed seed, the LoRA loss and gradient norm from this PR match the commit before #3408, which used the ATen kernels (GPTQ: loss 4.0020, grad 80.739; AWQ: loss 3.4318, grad 0.39097).

New tests cover eval-forward → train → backward for both kernels, biased AWQ training (including the single-rounding case), independent checkpoint-order dequantization oracles, and AUTO trainable preference/fallback on CPU.

ModelCloud#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 ModelCloud#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-ModelCloud#3408 TorchAtenLinear path.
@jiqing-feng
jiqing-feng marked this pull request as draft November 2, 2026 03:19
@jiqing-feng
jiqing-feng marked this pull request as ready for review November 2, 2026 03:31
@jiqing-feng

Copy link
Copy Markdown
Contributor Author

Hi @Qubitium . Would you please review it? Thanks!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Both AVX512 layers retain no-grad inference dispatch after switching back to training, dropping base-projection input gradients, while the AWQ training fallback can also incorrectly promote BF16 outputs to FP32.

3 open issues
What changed in this PR

This PR enables training for automatically selected AVX512-BF16 CPU kernels, addressing the GPTQ/AWQ PEFT LoRA regression introduced in #3408.

Changes:

  • Marks both kernels as trainable and removes overrides that reject train(True).
  • Replaces inference-only assertions with BF16 forward/backward checks for outputs and input gradients.
File Description
tests/​test_torch_cpu_avx512.py Adds GPTQ training forward/backward coverage.
tests/​test_torch_cpu_avx512_awq.py Adds AWQ training forward/backward coverage.
gptqmodel/​nn_modules/​qlinear/​torch_cpu_avx512.py Enables GPTQ training through the existing fallback.
gptqmodel/​nn_modules/​qlinear/​torch_cpu_avx512_awq.py Enables AWQ training through the existing fallback.

Review effort: Max

@Qubitium

Qubitium commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Hi @Qubitium . Would you please review it? Thanks!

@jiqing-feng Sorry. Will have a look and push some fixes. I will try to avoid having this happen again. I remember this is like the 2nd or 3rd time it has happened so I need to install some safe-guard so train calls from trainfer/peft can never fail with new kernels.

- 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.
@Qubitium
Qubitium requested a max review from Copilot November 2, 2026 04:23
@Qubitium Qubitium changed the title fix(cpu): keep AVX512-BF16 GPTQ/AWQ kernels trainable fix(cpu): new cpu kernels regressed trainable api calls from peft Nov 2, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The fallback prematurely rounds bias to BF16, producing incorrect outputs, while implementation-dependent references and loose tolerances can hide regressions.

2 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Use an independent dequantization oracle in the reference

tests/​test_torch_cpu_avx512.py:953

The expected weight comes from the same dequantize_weight() used by the forward under test, so both output and gradient assertions can pass with incorrect unpacking or channel order. Build the reference independently with _layer_dequantize_oracle from the original codes, zeros, scales, and group indices, including the checkpoint-dtype rounding before BF16 conversion. Tighten both comparisons to atol=rtol=2e-3; the current fused-kernel tolerances can hide errors in this Torch matmul fallback.

3 resolved since last review

Review effort: Max

Comment thread tests/test_torch_cpu_avx512_awq.py Resolved
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.
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
Qubitium merged commit 73cda73 into ModelCloud:main Nov 2, 2026
1 check passed
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.

3 participants