Skip to content

fix(cpu): eval-mode grad forwards must not latch the fused kernels to dense (#5392) - #5447

Merged
Qubitium merged 1 commit into
mainfrom
fix/awq-cpu-fused-eval-grad-latch-5392
Nov 19, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/awq-cpu-fused-eval-grad-latch-5392

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #5392.

Problem

The CPU int4pack kernels latch linear_mode="train" whenever a forward runs with linear_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) / TorchCpuAvx512AwqLinear

After #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, so model.eval() returns early (mode == old_train) and the #4949 reset never runs; only train(); eval() recovers. Every later no_grad/inference_mode forward 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.forward 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 (_forward_torch_cpu, the same math as GGUFTorchLinear); inference forwards keep the native ggml kernel. Raising instead, as the issue suggests, would break plain model(input_ids) calls outside no_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):

after two eval-mode grad forwards linear_mode forward
before this PR train 1.86 ms -> 18.92 ms (10.1x)
after this PR None (re-transforms on the next forward) 1.88 ms -> 1.92 ms (1.0x)

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), assert linear_mode != "train", then an inference forward and assert linear_mode == "inference" again. Fails on main for the four kernels that run on this host.
  • test_training_mode_still_latches_until_eval: a training-mode forward still latches "train" and eval() clears it (guards the other half of the condition).
  • test_gguf_cpp_grad_forward_uses_the_exact_path: GGUFCppKernel now returns a grad-carrying output, x.grad is populated, and the result matches GGUFTorchLinear on the same bytes. Fails on main.

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.py has two pre-existing feature-detection failures on this aarch64 host (identical with the change reverted). bash format/check.sh passes.

Not verified locally

The AVX512 variants cannot execute on this aarch64 host (the existing test_cpu_trainable_kernels_grad_after_inference_5314.py skips them too); their change is the same one-line condition in torch_aten_kernel.py (inherited) and torch_cpu_avx512_awq.py.

… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant