You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
k-bit linear bias gradient is invalid for 1-D inputs (and wrong rank for >2-D) #2110
Debian Linux x86_64, Python 3.12.3, PyTorch 2.8.0+cu128
Reproduced on CPU and on an NVIDIA RTX A5000 (SM86)
Summary
Linear4bit and Linear8bitLt fail during backward for 1-D input tensors when the bias requires a gradient. All three k-bit matmul autograd functions compute the bias gradient as grad_output.sum(0). For a 1-D output this is a scalar, so autograd rejects the returned gradient:
RuntimeError: Function MatMul4BitBackward returned an invalid gradient at index 2
- got [] but expected shape compatible with [64]
nn.Linear supports 1-D inputs, and the bitsandbytes forward pass already returns the correct 1-D output, so this only fails in backward. For ranks above 2, sum(0) also returns a tensor with extra leading dimensions instead of a vector; PyTorch happens to reduce it further during accumulation, but the returned gradient has the wrong documented shape.
Both layers raise MatMul4BitBackward/MatMul8bitFpBackward returned an invalid gradient ... got [] but expected shape [64]. The same 4-bit failure occurs on CUDA.
Expected behavior
The bias gradient should be the reduction of grad_output over every leading dimension, i.e. grad_output.reshape(-1, grad_output.shape[-1]).sum(0), which is a (out_features,) vector for 1-D, 2-D, 3-D, and 4-D inputs.
Activity