Skip to content

fix(nf12): keep input gradients on the CUDA/RDNA4 bf16 forward (#5305) - #5308

Merged
Qubitium merged 1 commit into
mainfrom
fix/nf12-native-grad-5305
Nov 19, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/nf12-native-grad-5305

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5305.

NF12Linear._native_forward sent every CUDA/RDNA4 bf16 activation to
nf12::linear / nf12_hip::linear. Neither op registers an Autograd formula,
so the output carried grad_fn=torch::autograd::WarnNotImplemented and
backward silently returned None for x.grad (after a one-time warning). Any
LoRA/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) keeps
the fused kernel.

What Changed

  • gptqmodel/nn_modules/qlinear/nf12.py: _native_forward returns None
    when torch.is_grad_enabled() and x.requires_grad, before any native
    dispatch. The fallback path already produces the correct grad_fn and
    x.grad.
  • tests/test_nf12_native_grad_5305.py:
    • CUDA bf16 forward checks x.grad is not None, has a real backward node,
      and matches grad_out @ W within bf16 tolerance.
    • A deterministic dispatch test patches nf12_runtime so the gate is
      covered 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

PYTHONPATH=$PWD GPTQ_BYPASS_SAFETY_CHECKS=1 CUDA_VISIBLE_DEVICES=0 \
  /root/gptqmodel-test-venv/bin/python -m pytest \
  tests/test_nf12_native_grad_5305.py tests/test_nf12_grad_mode_forward_5211.py -q
# 5 passed

PYTHONPATH=$PWD GPTQ_BYPASS_SAFETY_CHECKS=1 CUDA_VISIBLE_DEVICES=0 \
  /root/gptqmodel-test-venv/bin/python -m pytest \
  tests/test_nf12_codec.py tests/test_nf12_runtime_load.py tests/test_nf12_escape_bounds.py -q
# 62 passed, 10 skipped

ruff check gptqmodel/nn_modules/qlinear/nf12.py tests/test_nf12_native_grad_5305.py
# All checks passed!

The dispatch test fails on the parent commit (calls == [True]) and passes
with the fix. The gradient test is a no-op on hosts where
nf12_runtime.available() is False; it exercises the native path on a
supported 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.

…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
Qubitium marked this pull request as ready for review November 19, 2026 00:53
@Qubitium
Qubitium marked this pull request as draft November 19, 2026 00:53
@Qubitium
Qubitium marked this pull request as ready for review November 19, 2026 00:54
@Qubitium
Qubitium merged commit f1f2aca into main Nov 19, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/nf12-native-grad-5305 branch November 19, 2026 00:57
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