Repository navigation
fix(paro): keep training gradients in the module forward (#4958) - #4992
Merged
Merged
Conversation
`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`.
This was referenced Nov 17, 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.
Summary
Fixes #4958.
ParoLinear(and thereforeParoTritonLinear) advertisesSUPPORTS_TRAINING = Trueand is whatAUTO/AUTO_TRAINABLEload, but on anaccelerator its train-mode forward cut the autograd graph and returned
Nonefor 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):
_rotate_inputsalways calledapply_paro_rotation, which dispatches to thefused
gptqmodel_paro::rotateop. That op has no autograd kernel:x.grad,theta.gradandchannel_scales.gradwere allNone.gptqmodel_awq::gemm_forward/gemm_forward_fp32_reduce) has no autograd kernel either, so adifferentiable rotation alone would still have lost
dL/dxat the matmul.This is the NVIDIA half of the check the issue asked for; on ROCm the issue
reporter saw the same
None.ParoTritonLinearneeded no change:_select_planalready routes agradient-bearing operand to
_forward_triton_dense.What Changed
gptqmodel/nn_modules/qlinear/paro.pyParoLinear._rotation_needs_autograd():torch.is_grad_enabled()andthe input,
thetaorchannel_scalesrequires grad - the same conditionthe Paro optimizer already uses in
quantization/paro/optimization.py::_apply_rotation._rotate_inputsusesapply_paro_rotation_autogradunder that condition;the vectorized CPU path is also gated on it (it would otherwise drop a
thetagradient when only the rotation parameters train)._forward_cuda_awq_kernelreturnsNonefor a gradient-bearing operand sothe dense reference (
_forward_dense, pure Torch) serves the call.docs/paro.md: new "Module training path" subsection under "Fused rotationbackward" 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, andno_gradforwards keep the plain/vectorized rotationand the native AWQ GEMM.
Tests
Red on unmodified
origin/main(6fec8afe5), green on this branch:origin/main+ this test fileThe three nodes that pass on
mainare the CPU ones, which matches the issuereport ("On CPU the extension is not used, so the reference rotation runs and
gradients are correct").
Regression suites (identical on
mainand this branch):Gradients vs a dense FP32 reference (128x128 layer, non-identity rotation,
krot=8,group_size=128; relative error at max magnitude):yx.gradtheta.gradchannel_scales.gradParoTritonLinearproduces the same numbers asParoLinear(both take thedense path in training).
Model Support / Module Plan
Not applicable - no model, definition,
MODEL_MAP, fusion or module-plan change.