Skip to content

[BUG] #5230 (fix of #5211) incomplete: NF12 bf16 forward on CUDA/RDNA4 runs nf12::linear / nf12_hip::linear with no autograd kernel - backward silently returns x.grad=None (LoRA/PEFT through NF12 loses gradients) #5305

Closed

Description

@Qubitium

Found on main @ c03416c, on an RTX 4090 (torch 2.15.0+cu132) and an AMD R9700 (ROCm 10.1, torch 2.16.0.dev).

What is wrong

#5230 (the fix of #5211) made NF12 grad-mode forwards stop raising. On the native bf16 GPU path, though, the backward now silently returns no input gradient.

NF12Linear._native_forward (gptqmodel/nn_modules/qlinear/nf12.py:456) sends every CUDA bf16 activation to nf12_runtime.linear (the nf12::linear custom op), and every RDNA4 bf16 activation to nf12_hip_runtime.linear (nf12_hip::linear). Neither op registers an Autograd kernel. The output does carry grad_fn=torch::autograd::WarnNotImplemented, so the forward looks differentiable. On backward, PyTorch prints a warning once:

nf12::linear: an autograd kernel was not registered to the Autograd key(s) but we are trying to backprop through it. This may lead to silently incorrect behavior.

It then returns None as the gradient for x.

bf16 is the NF12 storage dtype and the default way to run an NF12 model on GPU. So any LoRA/PEFT fine-tune, or any other gradient through an NF12 model on CUDA or RDNA4, silently drops the gradient through every NF12 projection. The loss still backpropagates through the residual stream, so nothing fails. Adapters in earlier layers get wrong gradients, with no error.

The other dtypes and paths go through dequant + torch.mm and are correct: fp16 CUDA, fp32, CPU, and HIP at M=300, which falls back.

Repro

import torch
from gptqmodel.nn_modules.qlinear.nf12 import NF12Linear
lin = torch.nn.Linear(1024, 512, bias=True, dtype=torch.bfloat16)
m = NF12Linear(bits=12, group_size=-1, sym=True, desc_act=False, in_features=1024,
               out_features=512, bias=True, register_buffers=True)
m.pack_original(lin, None, None); m = m.cuda(); m.post_init()
for M in (1, 16, 300):
    x = torch.randn(M, 1024, device="cuda", dtype=torch.bfloat16, requires_grad=True)
    y = m(x); y.float().sum().backward()
    print(M, y.grad_fn.name(), x.grad)

Output:

# CUDA (RTX 4090)
1   torch::autograd::WarnNotImplemented  None
16  torch::autograd::WarnNotImplemented  None
300 torch::autograd::WarnNotImplemented  None
# same module, fp16 / fp32 activations: grad_fn ViewBackward0 / AddmmBackward0, x.grad matches ones @ W to 4e-4
# ROCm R9700 (nf12_hip::linear)
1   torch::autograd::WarnNotImplemented  None
16  torch::autograd::WarnNotImplemented  None
300 ViewBackward0                         <tensor>   (falls back to decode + matmul)

The forward output is correct (max error vs the dense bf16 layer: 0 to 1.6e-2).

Suggested fix

In _native_forward, return None, so that the existing differentiable decode + matmul runs, whenever torch.is_grad_enabled() and x.requires_grad are both true (or the module has a trainable adapter). Alternatively, register an autograd formula for nf12::linear / nf12_hip::linear with grad_x = grad_out @ W from the exact decode. Do the same for any other NF12 native op used in forward, such as the MoE op.

Which test should have caught it

tests/test_nf12_grad_mode_forward_5211.py (#5230) has three tests. All three run on CPU, and they pass with no GPU visible. The CUDA bf16 native path, which #5211's title lists as "fp16 CUDA", is never exercised. A CUDA/HIP bf16 node that checks x.grad is not None and compares it to grad_out @ W would catch this.

Severity: high for anyone training through NF12, because gradients are silently wrong with no error. Inference is unaffected.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 19, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-root-5305-nf12-grad finished this issue.

    Pull request: #5308

    claiming: skip NF12 native CUDA/HIP bf16 linear in grad mode so backward gets a real x.grad, with a GPU regression test

  2. Qubitium commented on Nov 19, 2026

    @Qubitium
    CollaboratorAuthor

    Implemented in draft PR #5308: _native_forward now falls back to the differentiable decode + matmul whenever torch.is_grad_enabled() and x.requires_grad. Added a CUDA bf16 gradient test plus a deterministic dispatch test that covers the gate even without the NF12 extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions