Repository navigation
fix(nf12): keep the decoded weight out of InferenceMode so grad-mode forwards work (#5211) - #5230
Merged
Merged
Conversation
…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.
This was referenced Nov 18, 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 #5211.
Problem
NF12Linear.dequantize_weight()was decorated@torch.inference_mode(), andpack_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:That hit every CPU forward (CPU always takes the wide path), CUDA fp16, and the in-session model right after
quantize(NF12Config()).Fix
nf12_packed/nf12_escapes/nf12_exp_base/biasas 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 insidetorch.inference_mode().torch.no_grad()instead ofinference_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.dequantize_weight()now returns a normal tensor too (it is an export API; nothing installed it as a cache).Verification
tests/test_nf12_grad_mode_forward_5211.py(CPU): grad-mode forward afterpack_original, grad-mode forward after a loader-style inference-mode buffer install, and an autogradtorch.mmondequantize_weight(). All 3 fail on unpatchedmainand 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 checkclean.