Fix aten_linear: squeeze trailing dim after 1D-weight MatMul - #2983
Draft
justinchuby with Copilot wants to merge 2 commits into
Draft
Fix aten_linear: squeeze trailing dim after 1D-weight MatMul#2983justinchuby with Copilot wants to merge 2 commits into
justinchuby with Copilot wants to merge 2 commits into
Conversation
When weight is 1D, the previous code unsqueezed it to (in_features, 1) for MatMul but never squeezed the resulting trailing dim back off. This caused the output to have rank input.rank instead of the correct input.rank - 1 (matching PyTorch eager dot-product semantics). The fix squeezes axis [-1] immediately after the MatMul, so both the no-bias and with-bias code paths return the correctly-shaped result. Also adds a new OpInfo 'nn.functional.linear_1d_weight' with sample inputs that specifically exercise the 1D-weight path (no-bias and with-bias for 1-D, 2-D, and 3-D inputs), closing the coverage gap noted in the issue. Closes #2982
Copilot
AI
changed the title
[WIP] Fix aten_linear behavior when bias is None
Fix aten_linear: squeeze trailing dim after 1D-weight MatMul
Jul 30, 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.
When
weightis 1D,aten_linearunsqueezed it to(in_features, 1)for MatMul but never squeezed the resulting trailing dim back off. This produced output rankinput.rankinstead of the correctinput.rank - 1, mismatching PyTorch eager's dot-product contraction semantics.Changes
onnxscript/function_libs/torch_lib/ops/nn.py: Applyop.Squeeze(..., [-1])immediately after the 1D-weight MatMul, before thebias is Nonebranch, so both paths return the correctly-ranked result:tests/function_libs/torch_lib/extra_opinfo.py/ops_test_data.py: Addnn.functional.linear_1d_weightOpInfo withsample_inputs_linear_1d_weight, covering 1D/2D/3D inputs with and without bias. The 2D-input+bias combination is intentionally excluded — PyTorch eager itself rejects it.