Skip to content

Use the first dim of size 3 when torch.cross has no dim - #2810

Open
LeSingh1 wants to merge 1 commit into
apple:mainfrom
LeSingh1:torch-cross-default-dim
Open

Use the first dim of size 3 when torch.cross has no dim#2810
LeSingh1 wants to merge 1 commit into
apple:mainfrom
LeSingh1:torch-cross-default-dim

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

dim is optional in aten::cross. Omitted, torch uses the first dimension of size 3, which is not dim 0 in general. The converter passed the argument straight to mb.gather, where the missing dim fell back to axis 0.

class M(nn.Module):
    def forward(self, x, y):
        return torch.cross(x, y)          # no dim

a = b = torch.rand(2, 3)

torch returns shape (2, 3); Core ML returns (3, 3) — wrong shape and wrong values, with nothing raised. Passing dim=1 explicitly has always been correct, which is why the existing test_cross (which always passes dim) never caught it.

The fix resolves the default the way torch does, and raises when no dim has size 3 rather than gathering along a dim that cannot hold a cross product.

Testing

TestCross::test_cross_default_dim over shapes (2, 3), (3, 4), (3, 3), (2, 3, 4). The (2, 3) and (2, 3, 4) cases fail on main; (3, 4) and (3, 3) pass either way and are there as controls, since for those the first dim of size 3 is dim 0. Existing test_cross is unchanged and still passes.

This machine has a broken scikit-learn install that makes every TorchFrontend.TORCHEXPORT case error out, including pre-existing ones, so I could only exercise the TorchScript frontend locally.

dim is optional in aten::cross. Omitted, torch uses the first dimension of
size 3, which is not dim 0 in general. The converter passed the argument
straight to mb.gather, where a missing dim fell back to axis 0, so
torch.cross(a, b) on a (2, 3) input gathered along the batch axis and
produced a (3, 3) output where torch produces (2, 3) — a wrong shape and
wrong values, with nothing raised.

Resolve the default the way torch does, and raise if no dim has size 3
rather than silently gathering along a dim that cannot hold a cross
product.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant