Repository navigation
fix(cpu): eval-mode grad forwards must not latch the fused kernels to dense (#5392) - #5447
Merged
Merged
Conversation
… dense (#5392) 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 was referenced Nov 19, 2026
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.
Fixes #5392.
Problem
The CPU int4pack kernels latch
linear_mode="train"whenever a forward runs withlinear_mode is None:TorchFusedLinear/TorchFusedAwqLinear(torch_fused.py,torch_fused_awq.py)TorchAtenLinear/TorchAtenAwqLinear(torch_aten_kernel.py,torch_aten_kernel_awq.py)TorchCpuAvx512Linear(inherits the ATen forward) /TorchCpuAvx512AwqLinearAfter #5321, the first eval-mode grad forward restores the checkpoint layout and leaves
linear_mode=None. The second grad forward then hits that latch and pins the module to the dense-dequant path for good. The module is still in eval mode, somodel.eval()returns early (mode == old_train) and the #4949 reset never runs; onlytrain(); eval()recovers. Every laterno_grad/inference_modeforward dequantizes the full weight.Change
Only latch the dense path in training mode:
elif not force_dense and self.linear_mode is None and self.training. An eval-mode grad forward takes the dense path for that call only (as it already did for the first one), so the next no-grad forward transforms again.Also fix the GGUF CPU sibling the issue lists:
GGUFCppKernel.forwardreturned a detached tensor forx.requires_gradbecause the native ggml matmul has no autograd support. A grad-mode forward now takes the exact torch path for that call (_forward_torch_cpu, the same math asGGUFTorchLinear); inference forwards keep the native ggml kernel. Raising instead, as the issue suggests, would break plainmodel(input_ids)calls outsideno_grad(the embedding output requires grad whenever the embedding weights do), so the differentiable fallback is used.Measured
TorchFusedLinear, 4096x4096, g128, bf16, M=1,torch.inference_mode()forward, median of 15 (shared host, so absolute numbers move between runs; the ratio is stable within a run):linear_modetrainNone(re-transforms on the next forward)Tests
New
tests/test_cpu_fused_eval_grad_latch_5392.py:test_two_eval_grad_forwards_do_not_latch_the_dense_path(all six builders): inference forward, two eval-mode grad forwards (both differentiable, grads checked), assertlinear_mode != "train", then an inference forward and assertlinear_mode == "inference"again. Fails onmainfor the four kernels that run on this host.test_training_mode_still_latches_until_eval: a training-mode forward still latches"train"andeval()clears it (guards the other half of the condition).test_gguf_cpp_grad_forward_uses_the_exact_path:GGUFCppKernelnow returns a grad-carrying output,x.gradis populated, and the result matchesGGUFTorchLinearon the same bytes. Fails onmain.5 of the new nodes fail with the source change reverted; the training-mode guard and the AVX512 variants (skipped: this host is aarch64) are unchanged-behaviour guards.
Neighbouring suites:
test_cpu_trainable_kernels_grad_after_inference_5314.py,test_gguf_cpp_matmul_accuracy_4316.py,test_awq_torch_fused_mode_5018.py,test_cpu_int4pack_dispatch_contracts.py,test_q4_torch_fused.py,test_torch_fused_asym_qzeros.py,test_torch_aten_kernel_import_guard.py-> 46 passed, 10 skipped.test_torch_cpu_avx512.pyhas two pre-existing feature-detection failures on this aarch64 host (identical with the change reverted).bash format/check.shpasses.Not verified locally
The AVX512 variants cannot execute on this aarch64 host (the existing
test_cpu_trainable_kernels_grad_after_inference_5314.pyskips them too); their change is the same one-line condition intorch_aten_kernel.py(inherited) andtorch_cpu_avx512_awq.py.