Fix broadcast output shape computation in tensor binary operations#125582
Draft
Fix broadcast output shape computation in tensor binary operations#125582
Conversation
The ValidateCompatibility method used rank comparison to pick the output shape for binary tensor operations, which was incorrect when both tensors had the same rank but different dimension sizes (e.g., [2,3] * [2,1] produced [2,1] instead of [2,3]). Now computes the broadcast output shape as element-wise max of each dimension, matching NumPy broadcasting semantics. Also uncomments and adds test cases for this scenario. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
stephentoub
March 15, 2026 15:33
View session
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics-tensors |
This was referenced Mar 15, 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.
Fixes #114ary
Summary
Tensor binary operations (add, subtract, multiply, divide, etc.) yielded incorrect results when applied to tensors with broadcast-compatible shapes of different sizes. The output tensor was truncated to the wrong shape instead of being broadcast correctly.
For example, multiplying tensors of shapes
[2, 3]and[2, 1]produced a result with shape[2, 1]instead of the expected[2, 3].Root Cause
TensorOperation.ValidateCompatibility(the overload that creates the outputTensor<TResult>) used a simple rank comparison to determine the output shape:x.Rank > y.Rank, it usedx's lengthsy's lengthsThis logic was incorrect when both tensors had the same rank but different dimension sizes (e.g.,
[2, 3]and[2, 1]). In that case, it would arbitrarily picky's shape[2, 1], losing the larger dimension fromx.Fix
The method now computes the broadcast output shape as the element-wise maximum of each aligned dimension, matching NumPy broadcasting semantics. Dimensions are aligned from the right (trailing dimensions), and missing leading dimensions are treated as size 1.
The implementation uses
stackallocfor small ranks (≤ 5) andArrayPoolfor larger ranks, consistent with existing patterns in the codebase.Tests
TensorMultiplyTests(shapes[3]×[3, 1])[2, 3]×[2, 1])