Repository navigation
Fix contracting dim match and batch broadcasting in broadcast_to_matmul - #3103
Open
Hardik K (hardikkaurani) wants to merge 1 commit into
Open
Hardik K (hardikkaurani) wants to merge 1 commit into
Hardik K (hardikkaurani) wants to merge 1 commit into
Conversation
In _broadcast_to_matmul.py, check_if_not_need_reshape checked reversed dimensions using if dim_from_a not in {1, dim_from_b}:.
- At idx == 0 (the contracting dimension K), dim_from_a and dim_from_b must match exactly (dim_from_a == dim_from_b). Allowing dim_from_a == 1 when dim_from_b != 1 permitted rewriting graphs into invalid MatMul operations with incompatible contracting dimensions.
- At idx > 0 (the batch dimensions), two dimensions are broadcastable if dim_from_a == dim_from_b, dim_from_a == 1, or dim_from_b == 1. The previous check rejected valid broadcasting when dim_from_b == 1 and dim_from_a > 1.
This change enforces exact equality on contracting dimensions at idx == 0 and symmetric broadcasting on batch dimensions at idx > 0. Adds regression unit tests covering both cases.
Author
|
Hello G. Ramalingam (@gramalingam), I have submitted this pull request to resolve two correctness issues in
All 18 unit tests in Please review when convenient and let me know if any adjustments are desired. |
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.
Summary
In
_broadcast_to_matmul.py,check_if_not_need_reshapeinspected reversed input dimensions with:This introduced two issues:
idx == 0):At
idx == 0,dim_from_aanddim_from_bare the contracting dimensionsMatMul,dim_from_a == 1whendim_from_b != 1erroneously permitted rewriting valid reshape graphs into invalidMatMulnodes with mismatched contracting dimensions that fail ONNX shape inference.idx > 0):At
idx > 0,dim_from_aanddim_from_bare batch dimensions. Two dimensions are broadcastable ifChanges
idx == 0(dim_from_a == dim_from_b).idx > 0(dim_from_a == dim_from_b or dim_from_a == 1 or dim_from_b == 1)._broadcast_to_matmul_test.py:test_reshape_matmul_reshape_replace_when_second_input_batch_dim_is_one: confirms redundant reshapes are removed when inputtest_reshape_matmul_reshape_remain_when_contracting_dimension_mismatches: confirms the rewrite is safely declined when contracting dimensions mismatch.Validation
_broadcast_to_matmul_test.pypass.ruff checkandruff format --checkpass with zero warnings or errors.