Repository navigation
fix(nf12): keep input gradients on the CUDA/RDNA4 bf16 forward (#5305) - #5308
Merged
Merged
Conversation
…radient (#5305) nf12::linear and nf12_hip::linear register no Autograd formula, so a bf16 grad-mode forward returned None for x.grad while still exposing a WarnNotImplemented grad_fn. Gate the native dispatch on torch.is_grad_enabled() and x.requires_grad; inference keeps the fused kernel.
Qubitium
marked this pull request as ready for review
November 19, 2026 00:53
Qubitium
marked this pull request as draft
November 19, 2026 00:53
Qubitium
marked this pull request as ready for review
November 19, 2026 00:54
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 #5305.
NF12Linear._native_forwardsent every CUDA/RDNA4 bf16 activation tonf12::linear/nf12_hip::linear. Neither op registers an Autograd formula,so the output carried
grad_fn=torch::autograd::WarnNotImplementedandbackward silently returned
Noneforx.grad(after a one-time warning). AnyLoRA/PEFT pass through an NF12 projection lost that gradient while the loss
still backpropagated through the residual stream.
The layer now takes the existing differentiable decode + matmul path whenever
the activation needs a gradient. Inference (
no_grad/inference_mode) keepsthe fused kernel.
What Changed
gptqmodel/nn_modules/qlinear/nf12.py:_native_forwardreturnsNonewhen
torch.is_grad_enabled()andx.requires_grad, before any nativedispatch. The fallback path already produces the correct
grad_fnandx.grad.tests/test_nf12_native_grad_5305.py:x.gradis notNone, has a real backward node,and matches
grad_out @ Wwithin bf16 tolerance.nf12_runtimeso the gate iscovered even on hosts where the NF12 extension is unavailable: the native
call is skipped in grad mode and still used under
no_grad.No API or behavior change for inference. Training through NF12 now has a
correct (slower) fallback rather than silently wrong gradients.
Tests
The dispatch test fails on the parent commit (
calls == [True]) and passeswith the fix. The gradient test is a no-op on hosts where
nf12_runtime.available()isFalse; it exercises the native path on asupported CUDA GPU.
Model Support / Module Plan
Not applicable: no model, alias, or module-plan change.
Logic Flow Documentation
Not applicable: the change adds one early return to an existing dispatch; no
resolution order or lifecycle changes.
Kernel Accuracy Requirements
Not applicable: no kernel math change. The fallback decode is the existing
exact NF12 codec; the test compares its gradient against
grad_out @ W.Notes
This intentionally does not add an Autograd formula to the custom ops. The
fallback is the safe path for training; a fused backward kernel can be added
later without changing this contract.