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] QQQTorchLinear (CPU AUTO) caches its AVX512 kernel tensors as inference tensors: after any inference_mode forward/generate(), every grad-mode forward fails 'Inference tensors cannot be saved for backward' #5197
Found on main @ 396b5f0, CPU (x86, AVX512) host with RTX 4090, torch 2.15. Also on 7b8f127, so not a regression. Found while checking the grad-mode contract of #5152 (fix of #5089).
What is wrong
QQQTorchLinear (CPU AUTO kernel for QQQ) builds its AVX512 kernel cache lazily on the first forward: _packed_weight_for_cpu() (gptqmodel/nn_modules/qlinear/qqq.py:1069-1125) stores _torch_packed_cache = (packed, s_group, s_channel) and drops the checkpoint B/s_channel. If that first forward runs under torch.inference_mode() (every generate(), every inference_mode eval loop), the cached tensors are inference tensors. A later ordinary (grad-mode) forward then fails at qqq.py:1252:
The activations require grad (the embedding is a normal parameter), so autograd tries to save the inference-mode s_channel for backward. The order matters: grad-mode first builds a normal cache, and all later forwards (both modes) work.
The same applies in-session after quantize(): the cancelled-run and completed-run contracts (#4572, #5012, #5152) require a plain grad-mode forward to work, and the in-session QQQ model on CPU fails it after any inference-mode forward.
TorchLinear already guards the same thing for g_idx (torch.py:399: if self.g_idx.is_inference(): with torch.inference_mode(False): self.g_idx = self.g_idx.clone()). QQQTorchLinear's cache has no such guard.
Repro
Qwen2.5-0.5B-Instruct truncated to 4 layers, QQQConfig(bits=4, group_size=128), saved, then:
importtorchfromgptqmodelimportGPTQModel, BACKENDq=GPTQModel.load(QQQ_CKPT, device="cpu", dtype=torch.float16) # AUTO -> QQQTorchLinearids=q.tokenizer("Machine learning is a field of study in artificial intelligence. "*8, return_tensors="pt").input_idswithtorch.inference_mode():
print(q.model(ids, labels=ids).loss.item()) # 13.952040672302246print(q.model(ids, labels=ids).loss.item()) # RuntimeError: Inference tensors cannot be saved for backward
order of forwards (CPU, QQQTorchLinear)
result
inference_mode, then grad mode
RuntimeError at qqq.py:1252
inference_mode, then no_grad
OK
grad mode, inference_mode, grad mode
OK (13.952 each)
same checkpoint on CUDA (QQQ_TORCH), inference_mode then grad
OK
GPTQ TorchLinear / AWQ AwqTorchLinear in-session on CPU, inference_mode then grad
OK
Suggested fix
Build the cache outside inference mode, as TorchLinear.post_init does for g_idx. Either wrap the body of _packed_weight_for_cpu in torch.inference_mode(False) (and torch.no_grad()), or clone the cached tensors when cached[i].is_inference() and inference mode is off. Check the other lazily built CPU kernel caches for the same pattern.
Test that should have caught it
The QQQ CPU tests run in a single mode. Add a node that runs one forward under torch.inference_mode() and then one grad-mode forward on QQQTorchLinear (CPU), and assert that both give the same output.
Severity
Low to medium. This is a loud failure, but common call sequences hit it: generate() (or any inference_mode evaluation) followed by a plain model(ids, labels=ids) loss computation or a PEFT/LoRA step fails on every CPU QQQ model until the model is reloaded.
Agent claim:agent-4e3a0639ecaf is working on this issue now.
Started 2026-11-18T12:06:15Z. The claim expires 4.0h after this comment is last edited (heartbeat). Other agents: do not start this issue while the claim is live.
Scope: build the QQQTorchLinear AVX512 packed-weight cache (and sibling lazily built CPU kernel caches) outside inference mode, so a grad-mode forward after any inference_mode forward/generate works; add a CPU two-mode regression test
Agent claim released:agent-4e3a0639ecaf finished this issue.
PR: #5238 — base main, head fc4caf0d7 (branch fix/5197-qqq-cpu-cache), 2 files, +132.
Fix
QQQTorchLinear._packed_weight_for_cpu() now builds the packed AVX512 cache under @torch.inference_mode(False) + @torch.no_grad(), so the cached (packed, s_group, s_channel) are ordinary tensors even when the first forward is generate()/an eval loop. A grad-mode forward afterwards works in both orderings, including in-session after quantize(). Sibling CPU kernels audited: QQQ is the only one caching a tensor tuple read by an autograd-tracked op.
Evidence
RED new test file (pristine tree): 2 failed, 1 passed — RuntimeError: Inference tensors cannot be saved for backward at qqq.py:1252 plus assert not True on the cached is_inference().
GREEN: 3 passed; repro max|diff| = 0 for both orderings; repro_quantize.py cache flags [False, False, False].
CPU QQQ subset: 101 passed, 19 skipped; full QQQ family 261 passed, 61 skipped, 9 failed (the 9 are CUDA-only tests failing without CUDA; identical on the pristine tree, which has 11 failures = those 9 + the 2 new red tests).
Activity
Qubitium commentedon Nov 18, 2026
Agent claim:
agent-4e3a0639ecafis working on this issue now.Started 2026-11-18T12:06:15Z. The claim expires 4.0h after this comment is last edited (heartbeat). Other agents: do not start this issue while the claim is live.
Scope: build the QQQTorchLinear AVX512 packed-weight cache (and sibling lazily built CPU kernel caches) outside inference mode, so a grad-mode forward after any inference_mode forward/generate works; add a CPU two-mode regression test
fix(qqq): build the CPU packed cache outside inference mode (#5197) (#…
Qubitium commentedon Nov 18, 2026
Agent claim released:
agent-4e3a0639ecaffinished this issue.PR: #5238 — base
main, headfc4caf0d7(branchfix/5197-qqq-cpu-cache), 2 files, +132.Fix
QQQTorchLinear._packed_weight_for_cpu()now builds the packed AVX512 cache under@torch.inference_mode(False)+@torch.no_grad(), so the cached(packed, s_group, s_channel)are ordinary tensors even when the first forward isgenerate()/an eval loop. A grad-mode forward afterwards works in both orderings, including in-session afterquantize(). Sibling CPU kernels audited: QQQ is the only one caching a tensor tuple read by an autograd-tracked op.Evidence
RuntimeError: Inference tensors cannot be saved for backwardatqqq.py:1252plusassert not Trueon the cachedis_inference().max|diff| = 0for both orderings;repro_quantize.pycache flags[False, False, False].uvx [email protected] check gptqmodel docs/eora tests setup.pyclean.