Skip to content

fix(nf12): keep the decoded weight out of InferenceMode so grad-mode forwards work (#5211) - #5230

Merged
Qubitium merged 1 commit into
mainfrom
fix/nf12-grad-mode-forward-5211
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/nf12-grad-mode-forward-5211

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #5211.

Problem

NF12Linear.dequantize_weight() was decorated @torch.inference_mode(), and pack_original() / load_quant_result() installed the packed buffers inside one. The wide forward path (_forward_impl → torch.mm(x, weight.t())) therefore multiplied by an inference tensor, so any graph-tracking forward raised:

RuntimeError: Inference tensors cannot be saved for backward.

That hit every CPU forward (CPU always takes the wide path), CUDA fp16, and the in-session model right after quantize(NF12Config()).

Fix

  • Keep nf12_packed / nf12_escapes / nf12_exp_base / bias as normal tensors when they are installed: a helper clones them outside InferenceMode when the source carries the inference flag. This also covers a loader that installs a checkpoint inside torch.inference_mode().
  • Decode under torch.no_grad() instead of inference_mode, and normalize the decoded matrix if it still carries the flag. The packed weights carry no grad, so no numerics change; the operand can now be saved for backward.
  • Public dequantize_weight() now returns a normal tensor too (it is an export API; nothing installed it as a cache).

Verification

  • New tests/test_nf12_grad_mode_forward_5211.py (CPU): grad-mode forward after pack_original, grad-mode forward after a loader-style inference-mode buffer install, and an autograd torch.mm on dequantize_weight(). All 3 fail on unpatched main and pass here; outputs are bit-identical to the inference-mode forward.
  • pytest tests/test_nf12_codec.py tests/test_nf12_runtime_load.py tests/test_nf12_escape_bounds.py tests/test_nf12_abi_parity.py tests/test_offload_files.py -q → 153 passed, 11 skipped.
  • ruff check clean.

…forwards work (#5211)

NF12Linear.dequantize_weight() was decorated with torch.inference_mode() and
pack_original()/load_quant_result() installed the packed buffers inside one.
The wide forward path therefore multiplied by an inference tensor and raised
'Inference tensors cannot be saved for backward' whenever the activation
required grad -- every CPU forward, CUDA fp16, and the in-session model right
after quantize().

- Keep the installed nf12_packed/nf12_escapes/nf12_exp_base/bias buffers as
  normal tensors (a loader can install a checkpoint inside InferenceMode too).
- Decode under torch.no_grad() instead of inference_mode, and normalize the
  result if it still carries the inference flag, so the matmul operand can be
  saved for backward.
- Add CPU regression tests: grad-mode forward after pack, after a loader-style
  inference-mode install, and a dequantize_weight autograd matmul.
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