Skip to content

Fix einsum ellipsis lowering to MIL - #2706

Merged
TobyRoseman merged 2 commits into
apple:mainfrom
eyupcanakman:fix/parse-einsum-ellipsis-2644
May 22, 2026
Merged

TobyRoseman merged 2 commits into
apple:mainfrom
eyupcanakman:fix/parse-einsum-ellipsis-2644

Conversation

@eyupcanakman

Copy link
Copy Markdown
Contributor

torch.einsum("...,f->...f", x, y) fails to convert with perm should have the same length as rank(x): 3 != 2. Any equation with ... hits the same path.

parse_einsum_equation maps each character to a unique int. Every . in ... becomes index 0, so solve_diagonal_einsum reads them as three duplicate indices and produces a perm longer than the input rank.

The fix replaces ... with fresh single-character labels before that call, using each input var's rank to count how many dims it covers. The replacement happens in build_einsum_mil, which already receives the input vars. Equations without ... are unaffected.

test_einsum_with_ellipsis covers the reported ...,f->...f shape and fails on main without the fix.

Fixes #2644

parse_einsum_equation maps each character to a unique int, so every
"." in "..." becomes index 0 and solve_diagonal_einsum reads them as
three duplicate indices, producing a perm longer than the input rank.
Expand "..." to fresh single-character labels in build_einsum_mil
before parsing, using each input var's rank to count how many dims
its ellipsis covers.

Fixes apple#2644

@TobyRoseman TobyRoseman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this change looks good. Two minor comments.

@TobyRoseman

Copy link
Copy Markdown
Collaborator

@TobyRoseman
TobyRoseman merged commit 82c89a8 into apple:main May 22, 2026
@TobyRoseman

Copy link
Copy Markdown
Collaborator

Thanks for your contribution @eyupcanakman

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.

CoreMLTools PyTorch converter fails on rotary-attention einsum (BeatThis model) with rank/perm mismatch

2 participants