Skip to content

Fix broadcast output shape computation in tensor binary operations#125582

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-binary-operation-tensor-lengths
Draft

Fix broadcast output shape computation in tensor binary operations#125582
Copilot wants to merge 2 commits intomainfrom
copilot/fix-binary-operation-tensor-lengths

Conversation

Copy link
Contributor

Copilot AI commented Mar 15, 2026

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 output Tensor<TResult>) used a simple rank comparison to determine the output shape:

  • If x.Rank > y.Rank, it used x's lengths
  • Otherwise, it used y's lengths

This 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 pick y's shape [2, 1], losing the larger dimension from x.

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 stackalloc for small ranks (≤ 5) and ArrayPool for larger ranks, consistent with existing patterns in the codebase.

Tests

  • Uncommented previously disabled "double broadcast" test cases in TensorMultiplyTests (shapes [3] × [3, 1])
  • Added a new test case for the exact scenario reported in the issue ([2, 3] × [2, 1])
  • All 5389 existing tests pass with zero failures

Copilot AI and others added 2 commits March 15, 2026 14:59
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>
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics-tensors
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected Result when Binary Operation is Applied to Tensors with Different Lengths

2 participants