Repository navigation
fix(cpu): keep gradients on a grad forward after a fused eval forward (#5314) - #5321
Merged
Merged
Conversation
…#5314) The CPU int4pack kernels latch linear_mode=inference on the first no_grad forward and then keep using the @torch.no_grad fused op, so later eval-mode grad forwards returned detached outputs. Check torch.is_grad_enabled() and x.requires_grad per call; when the fused layout is latched, restore the checkpoint layout and use the differentiable dequant path for that call. The next inference forward transforms again.
Qubitium
marked this pull request as ready for review
November 19, 2026 01:18
This was referenced Nov 19, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 19, 2026
… dense (#5392) (#5447) The CPU int4pack kernels (`TorchFusedLinear`/`TorchFusedAwqLinear`, `TorchAtenLinear`/`TorchAtenAwqLinear`, `TorchCpuAvx512Linear`/ `TorchCpuAvx512AwqLinear`) latch `linear_mode="train"` when a forward runs with `linear_mode is None`. After #5321, the first eval-mode grad forward restores the checkpoint layout and leaves `linear_mode=None`; the second grad forward then took that branch and pinned the module to the dense-dequant path for good. `model.eval()` returns early (`mode == old_train`), so only `train(); eval()` recovered, and later inference dequantized the whole weight on every call (measured 8-15x per layer, ~90x end to end). Only latch the dense path in training mode. An eval-mode grad forward takes the dense path for that call only, so the next no-grad forward transforms again. Also fix the GGUF CPU sibling the issue lists: `GGUFCppKernel` returned a detached tensor for `x.requires_grad` because the native ggml matmul has no autograd support. A grad-mode forward now takes the exact torch path for that call (same math as `GGUFTorchLinear`); inference forwards keep the native kernel.
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
Fixes #5314.
The CPU int4pack kernels chose fused-vs-dense once, on the first forward, and
latched
linear_mode=inferenceafter anyno_grad/inference_modecall. Everylater eval-mode forward then took the
@torch.no_gradfused op, even when gradwas enabled and
x.requires_gradwas true. The output was silently detached:LoRA params behind a quantized
o_proj/down_projgotgrad=None, and therest had wrong gradients, with no error.
The decision is now per call: if the fused inference layout is latched and the
call needs a gradient, the module restores the checkpoint layout with
restore_fused_train_state()and takes the differentiable dequant path. Thenext inference forward transforms again.
Affected GPTQ kernels:
TorchFusedLinear,TorchAtenLinear,TorchCpuAvx512Linear(inherits the ATen forward).Affected AWQ kernels:
TorchFusedAwqLinear,TorchAtenAwqLinear,TorchCpuAvx512AwqLinear.What Changed
gptqmodel/nn_modules/qlinear/_fused_train_state.py: newfused_autograd_needs_dense(module, x)helper. True only when grad is enabled,x.requires_grad, andlinear_modeis the fusedinferencestate.forward: computeforce_denseunder the existing transformlock, restore the checkpoint layout before the dense call, and skip the
one-time inference latch for that call. If the checkpoint layout cannot be
restored (no snapshot / suspended export), raise instead of returning a
detached tensor.
tests/test_cpu_trainable_kernels_grad_after_inference_5314.py: for eachkernel, run one
inference_modeforward (which latches the fused path), thena grad-mode forward with
requires_grad=True. Assert the output has agrad_fn,x.gradis notNone, andx.grad == grad_out @ W.Tfrom thedequantized weight. Also assert a later inference forward still works.
GGUFCppKernelis unchanged: it declaresSUPPORTS_TRAINING = False, so itdoes not promise gradients.
Tests
The four runnable kernel cases fail on the parent commit (
out.requires_gradis false); they pass with the fix. The AVX512 cases skip here and will run on
the x86 CI runner.
Model Support / Module Plan
Not applicable: no model, alias, or module-plan change.
Logic Flow Documentation
The change adds one decision inside each existing forward:
flowchart TD A[forward x] --> B{fused inference latched?} B -- no --> C[existing first-call selection] B -- yes --> D{grad enabled and x.requires_grad?} D -- no --> E[fused no_grad op] D -- yes --> F[restore_fused_train_state] F --> G[dense dequant matmul] G --> H[next inference forward re-transforms]No public control flow or ownership changes.
Kernel Accuracy Requirements
Not applicable: the fused kernel math is unchanged. The new test compares the
dense-path gradient with
grad_out @ W.Tfrom the dequantized weight.Notes
Restoring and re-transforming when a process alternates inference and gradient
calls costs one pack/unpack per switch. A pure training loop stays on the dense
path after the first restore; a pure inference loop keeps the fused path.