Repository navigation
Fix MatMul8bitFp backward for non-2D inputs - #2109
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
…2108) Signed-off-by: Anh Nguyen (Jerry) <92361793+anh-nguyenn@users.noreply.github.com>
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 #2108
Summary
MatMul8bitFp.backward()computes the weight gradient astorch.matmul(A.t(), grad_output).t().A.t()only accepts tensors with rank <= 2, so CPU/XPU training withLinear8bitLtcrashes for 1-D, 3-D, or 4-D inputs even though the forward pass (viaF.linear) accepts them.grad_outputwas also only flattened for exactly 3-D inputs, so 1-D activations reached the matmul with mismatched ranks.This flattens any non-2-D
grad_outputand reshapesAto 2-D before computing the weight gradient. The input-gradient path was already rank-independent and is unchanged.Verification
Added
tests/test_modules.py::test_linear8bitlt_cpu_backprop_non_2d, covering(32,),(2, 3, 32), and(2, 2, 2, 32)and comparing both input and weight gradients againsttorch.nn.Linear.Before the fix:
After the fix:
GPU validation
This change only touches the CPU/XPU
MatMul8bitFppath; CUDA usesMatMul8bitLtand is unaffected. The regression test runs on CPU, where the bug reproduces. The full CPU test run above was performed on an NVIDIA RTX A5000 host (SM86) using the CPU device.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,CPU.