Repository navigation
Fix k-bit bias gradients for non-2D inputs - #2111
Open
anh-nguyenn wants to merge 1 commit into
Open
anh-nguyenn wants to merge 1 commit into
anh-nguyenn wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Fixes #2110
Summary
All three k-bit matmul autograd functions (
MatMul8bitLt,MatMul8bitFp,MatMul4Bit) computed the bias gradient asgrad_output.sum(0). For 1-D inputs this returns a scalar, so backward raised:For inputs with rank > 2 it returned a tensor with extra leading dimensions instead of an
(out_features,)vector; autograd happened to reduce it further during accumulation, but the returned gradient had the wrong shape.This reduces over every leading dimension explicitly:
This is identical for 2-D inputs, makes 1-D inputs work, and returns the documented vector shape for 3-D/4-D inputs.
Verification
Added
tests/test_modules.py::test_kbit_bias_grad_non_2d, parametrized overLinear8bitLt(has_fp16_weights=False)andLinearNF4, with input shapes(32,),(2, 3, 32), and(2, 2, 2, 32). It compares the bias gradient againsttorch.nn.Linearusing a random upstream gradient.Before the fix:
After the fix:
Regression runs:
GPU validation
CPU tests were run on a host with an NVIDIA RTX A5000 (SM86). The CUDA regression runs above were executed on that A5000 and cover the 4-bit bias-gradient path; the CUDA 1-D case is now fixed and the existing 2-D/3-D/4-D autograd tests pass.
AI assistance
I used an AI assistant (opencode) to help investigate, reproduce, and implement this change. I reviewed the reproducer, the patch, and the test results before submitting.
Suggested labels:
Bug,CUDA,x64 CPU.