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
[BUG] NF12Linear forward uses an @torch.inference_mode() dequantize_weight() as the matmul operand: every grad-mode forward (fp16 CUDA, CPU, in-session after quantize) raises 'Inference tensors cannot be saved for backward' #5211
Found on main @ 396b5f0, RTX 4090 (sm_89) and x86 CPU, torch 2.15. Present since NF12 was added (#3573). Not a regression.
What is wrong
NF12Linear.dequantize_weight() is decorated @torch.inference_mode() (gptqmodel/nn_modules/qlinear/nf12.py:364). The forward's wide path calls it for the matmul operand (_forward_impl, nf12.py:~521-539: weight = self.dequantize_weight(...) -> torch.mm(x, weight.t())). The operand is therefore always an inference tensor. When autograd is on and the activation requires grad (it always does after the trainable embed_tokens), torch.mm must save weight for backward and raises:
RuntimeError: Inference tensors cannot be saved for backward. Please do not use Tensors created in inference mode in computation tracked by autograd.
So every plain grad-mode forward of an NF12 model fails, e.g. model(ids, labels=ids).loss without torch.no_grad(). This holds in every case that takes the wide path:
CUDA with fp16 activations, which is the common dtype=torch.float16 load (fp16 cannot hold BF16 weights exactly, so wide=True);
CPU, which is always wide.
CUDA with bf16 activations uses the non-wide fused path and works.
The same happens in-session right after quantize() with NF12Config. That breaks the in-session "plain grad-mode forward works" contract that #4572, #5012 and #5152 pinned for the other methods.
SUPPORTS_TRAINING = False is about training the quantized weights. A grad-mode forward (loss evaluation, a PEFT adapter on top, any code that does not wrap calls in no_grad) works for every other kernel I checked: GPTQ Torch/Marlin, AWQ Torch/GEMM, EXL3, FP8, GGUF Torch/Triton, Paro, MACaQ, QQQ (CUDA).
Repro
Qwen2.5-0.5B-Instruct, 4 layers, NF12Config(), saved and reloaded:
importtorchfromgptqmodelimportGPTQModel, BACKENDq=GPTQModel.load(NF12_CKPT, backend=BACKEND.NF12, dtype=torch.float16, device="cuda:0")
ids=q.tokenizer("Machine learning is a field of study in artificial intelligence. "*8, return_tensors="pt").input_ids.cuda()
withtorch.inference_mode(): print(q.model(ids, labels=ids).loss.item()) # 13.6312print(q.model(ids, labels=ids).loss.item()) # RuntimeError: Inference tensors cannot be saved for backward
case
result
CUDA, fp16, grad-mode forward (with or without an earlier inference_mode call)
RuntimeError at nf12.py:536 (torch.mm)
CPU, bf16, grad-mode forward
RuntimeError
CUDA, bf16, grad / inference / grad
OK (13.6159)
in-session after quantize(NF12Config()), fp16
RuntimeError
Suggested fix
Make the forward's operand a normal tensor. Either:
split the export API from the forward: keep @torch.inference_mode() on the public dequantize_weight() export, and have _forward_impl call an undecorated internal decode under torch.no_grad(); or
decorate with torch.no_grad() instead of torch.inference_mode().
Either way the result is a non-inference tensor that autograd can save. The packed buffers carry no grad, so nothing else changes. pack_original / load_quant_result have the same decorator. Check that the registered buffers are not left as inference tensors when they are installed outside quantize().
Test that should have caught it
The NF12 kernel tests run forwards only under inference_mode/no_grad. Add a grad-mode forward on CUDA fp16 and on CPU (activation requires_grad=True) and compare it with the inference output. Also extend the in-session grad-mode contract tests (#5012/#5152) to NF12Config.
Severity
Medium. The failure is loud, but every NF12 model loaded in fp16 or on CPU fails any grad-mode forward.
Activity
Qubitium commentedon Nov 18, 2026
Agent claim released:
agent-fb5145b84190is no longer working on this issue.The issue is free to claim.
Fixed by #5230.
fix(nf12): keep the decoded weight out of InferenceMode so grad-mode …