You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(cpu): new cpu kernels regressed trainable api calls from peft - #3480
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.
#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.
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.
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
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
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.
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.
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.
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
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.
Problem
Since #3408, PEFT LoRA fine-tuning of bf16 GPTQ/AWQ checkpoints on AVX512-BF16 CPUs fails as soon as
model.train()is called:Reproducer:
Cause
#3408 added
TorchCpuAvx512Linear/TorchCpuAvx512AwqLinearwith a higherBACKEND.AUTOpriority thanTorchAtenLinear/TorchAtenAwqLinear, but marked themSUPPORTS_TRAINING = Falseand madetrain(True)raise. Transformers loads GPTQ/AWQ checkpoints withBACKEND.AUTO, so these kernels are now picked by default on CPU, and the Trainer/PEFTmodel.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
optimumpasses HF's defaultbackend="auto"through tohf_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:
SUPPORTS_TRAINING = Trueon both kernels and removes thetrain()overrides that raise;linear_modedispatch on train/eval transitions, so an eval forward cannot pin later training forwards to the@torch.no_gradfused kernel;out.add_(self.bias)), so the sum is rounded once to the activation dtype likeTorchAtenLinear.Safeguard
So a future inference-only CPU kernel cannot silently regress PEFT/Trainer again:
BACKEND.AUTOnow 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.backend="auto_trainable".Validation
On an AVX512-BF16 Xeon CPU (torch 2.14.1+cpu, transformers 5.23.0.dev0):
mainTorchCpuAvx512Linear/TorchCpuAvx512AwqLinearNotImplementedErrorTorchCpuAvx512Linear/TorchCpuAvx512AwqLinearWith 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.