Skip to content

fix(paro): keep training gradients in the module forward (#4958) - #4992

Merged
Qubitium merged 1 commit into
mainfrom
root/paro-autograd-4958
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
root/paro-autograd-4958

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4958. ParoLinear (and therefore ParoTritonLinear) advertises
SUPPORTS_TRAINING = True and is what AUTO/AUTO_TRAINABLE load, but on an
accelerator its train-mode forward cut the autograd graph and returned None
for the input and rotation-parameter gradients - silently, with only a one-time
PyTorch warning.

Two cut points were confirmed on this host (NVIDIA PG506, sm_80, torch
2.15.0.dev20260928+cu130, Triton 3.8.0):

  1. _rotate_inputs always called apply_paro_rotation, which dispatches to the
    fused gptqmodel_paro::rotate op. That op has no autograd kernel:
    x.grad, theta.grad and channel_scales.grad were all None.
  2. The native AWQ GEMM (gptqmodel_awq::gemm_forward /
    gemm_forward_fp32_reduce) has no autograd kernel either, so a
    differentiable rotation alone would still have lost dL/dx at the matmul.
    This is the NVIDIA half of the check the issue asked for; on ROCm the issue
    reporter saw the same None.

ParoTritonLinear needed no change: _select_plan already routes a
gradient-bearing operand to _forward_triton_dense.

What Changed

  • gptqmodel/nn_modules/qlinear/paro.py
    • new ParoLinear._rotation_needs_autograd(): torch.is_grad_enabled() and
      the input, theta or channel_scales requires grad - the same condition
      the Paro optimizer already uses in
      quantization/paro/optimization.py::_apply_rotation.
    • _rotate_inputs uses apply_paro_rotation_autograd under that condition;
      the vectorized CPU path is also gated on it (it would otherwise drop a
      theta gradient when only the rotation parameters train).
    • _forward_cuda_awq_kernel returns None for a gradient-bearing operand so
      the dense reference (_forward_dense, pure Torch) serves the call.
  • docs/paro.md: new "Module training path" subsection under "Fused rotation
    backward" with the contract table and the new tests.
  • tests/test_paro_training_gradients_4958.py (new, 9 nodes).

No API change. Inference is unchanged: the autograd wrapper's forward runs the
same fused kernel and is asserted bitwise identical to
apply_paro_rotation, and no_grad forwards keep the plain/vectorized rotation
and the native AWQ GEMM.

Tests

Red on unmodified origin/main (6fec8afe5), green on this branch:

export CUDA_HOME=/usr/local/cuda PATH=/usr/local/cuda/bin:$PATH \
  TORCH_CUDA_ARCH_LIST=8.0 TORCH_NO_COMPILER_WRAPPER=1 GPTQ_BYPASS_SAFETY_CHECKS=1 \
  LOGBAR_ANIMATION=0 CUDA_VISIBLE_DEVICES=5 HF_HUB_OFFLINE=1
# PYTHONPATH=<worktree>; /tmp/root-rt.py imports the real `kernels` package first
python /tmp/root-rt.py tests/test_paro_training_gradients_4958.py -q -o addopts= -o log_cli=false
Tree Result
unmodified origin/main + this test file 6 failed, 3 passed
this branch 9 passed

The three nodes that pass on main are the CPU ones, which matches the issue
report ("On CPU the extension is not used, so the reference rotation runs and
gradients are correct").

Regression suites (identical on main and this branch):

python /tmp/root-rt.py tests/test_paro.py tests/kernels/test_paro.py \
  tests/test_paro_bf16_rotation_4221.py tests/test_paro_rotation_pairs.py \
  tests/test_paro_single_rounding_4317.py tests/kernels/test_paro_cpu_lock_concurrency_4885.py \
  -q -o addopts= -o log_cli=false
# main:   307 passed, 4 skipped
# branch: 307 passed, 4 skipped

python /tmp/root-rt.py tests/test_paro_rotation_lifecycle.py tests/test_paro_capture_order.py \
  tests/test_paro_positional_replay.py tests/test_paro_branch_replay_masks.py \
  tests/test_awq_rank_and_empty.py -q -o addopts= -o log_cli=false
# branch: 80 passed

Gradients vs a dense FP32 reference (128x128 layer, non-identity rotation,
krot=8, group_size=128; relative error at max magnitude):

Device / dtype y x.grad theta.grad channel_scales.grad
CPU / FP32 exact exact exact exact
CUDA / FP16 1.2e-3 2.2e-3 1.5e-3 1.2e-3
CUDA / BF16 7.1e-4 8.9e-3 1.0e-2 5.9e-3

ParoTritonLinear produces the same numbers as ParoLinear (both take the
dense path in training).

  • Unit tests have been executed and passed.
  • I added a new simple/fast unit test for this change (9 nodes, ~10 s).

Model Support / Module Plan

Not applicable - no model, definition, MODEL_MAP, fusion or module-plan change.

`ParoLinear` declares `SUPPORTS_TRAINING = True` and is what
`AUTO`/`AUTO_TRAINABLE` load, but a train-mode forward on an accelerator
silently dropped its gradients at two points.

1. `_rotate_inputs` always called `apply_paro_rotation`, which dispatches to the
   fused `gptqmodel_paro::rotate` op. That op has no autograd kernel, so
   `x.grad`, `theta.grad` and `channel_scales.grad` were all `None` and PyTorch
   only emitted a one-time warning. The autograd-capable wrapper
   (`apply_paro_rotation_autograd` / `_ParoRotateTensorFunc`) already existed
   and was already used by the Paro optimizer.
2. The native AWQ GEMM (`gptqmodel_awq::gemm_forward` and
   `gemm_forward_fp32_reduce`) has no autograd kernel either, so even a
   differentiable rotation would have lost `dL/dx` at the matmul. The
   `ParoTritonLinear` plans were already covered by `_needs_dense_autograd`.

The rotation now takes the autograd wrapper under the same condition the Paro
optimizer uses (`torch.is_grad_enabled()` and the input, `theta` or
`channel_scales` requires grad), and `_forward_cuda_awq_kernel` returns `None`
for a gradient-bearing operand so the dense reference serves the call.

Inference is untouched: the wrapper's forward runs the same fused kernel and
returns a bitwise identical tensor (asserted in the tests), and the
plain/vectorized rotation and the native AWQ GEMM keep serving `no_grad`
forwards.

Gradients against a dense FP32 reference on a 128x128 layer with a
non-identity rotation (relative error at the max magnitude): CPU FP32 exact,
CUDA FP16 2.2e-3, CUDA BF16 8.9e-3 - all inside the layer's single output
rounding. Before the fix the CUDA paths returned `None`.
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