fix(broadcast_tensors): handle zero-size dims in broadcast shape computation - #6024
Merged
huangyiqun merged 2 commits intoSep 8, 2026
Merged
Conversation
…utation _compute_broadcast_shape used max(dim_size, shape[i]) to resolve each broadcast dimension. A size of 1 is broadcastable, but max() treats 0 as smaller than 1, so a zero-size dimension paired with a singleton (1) collapsed to 1 instead of 0. This violates PyTorch broadcasting semantics, where a non-1 size (including 0) wins over a 1, and only two distinct non-1 sizes are incompatible. The shape0 (empty-tensor) cases in test_smooth_l1_loss relied on the inputs being broadcast to a zero-size output and failed because the result wrongly became size 1. Resolve each dim by taking the unique non-1 size (or 1 if every input is 1), and raise a RuntimeError on two distinct non-1 sizes, matching torch.broadcast_shapes behavior. Co-Authored-By: Claude Code <noreply@anthropic.com>
huangyiqun
approved these changes
Sep 8, 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.
Summary
_compute_broadcast_shapeinbroadcast_tensors.pycomputed each output dimension asmax(dim_size, shape[i]). This is wrong for the broadcasting rule "a size of 1 is broadcastable":max()treats0 < 1, so a zero-size dimension paired with a singleton (1) collapsed to1instead of staying0. This violates PyTorch broadcasting semantics, where any non-1 size (including0) wins over a1, and only two distinct non-1 sizes are incompatible.Concretely,
torch.broadcast_shapes([0], [1])should give(0,), but the old code returned(1,). This broke theshape=(0,)(empty-tensor) cases intest_smooth_l1_loss, where the loss inputs must broadcast to a zero-size output.Fix
Resolve each dimension by taking the unique non-1 size:
RuntimeError(matchingtorch.broadcast_shapes's "The size of tensor a (...) must match the size of tensor b (...) at non-singleton dimension" message),This now matches
torch.broadcast_shapesbehavior, including for zero-size dims:Test
The
shape=(0,)cases intests/test_smooth_l1_loss.py(test_smooth_l1_lossandtest_smooth_l1_loss_backward) were previously failing because of this; they now pass.🤖 Generated with Claude Code