Skip to content

BUG: validate contraction axes in tensordot#30521

Merged
ngoldbaum merged 5 commits intonumpy:mainfrom
vkverma9534:patch-1
Jan 20, 2026
Merged

BUG: validate contraction axes in tensordot#30521
ngoldbaum merged 5 commits intonumpy:mainfrom
vkverma9534:patch-1

Conversation

@vkverma9534
Copy link
Copy Markdown
Contributor

tensordot performs a tensor contraction where each axis corresponds to a distinct summation index. Duplicate contraction axes (e.g. axes=([1, 1], [0, 0])) are mathematically invalid but currently fail later during an internal transpose, raising ValueError: axes don't match array. This PR adds early validation to reject duplicate axes explicitly, aligning the behavior with the definition of tensor contraction and other NumPy axis-handling APIs.

Current behavior:
Supplying duplicate contraction axes (e.g. axes=([1, 1], [0, 0])) causes tensordot to fail later during an internal transpose, raising ValueError: axes don't match array.

Expected behavior:
Duplicate contraction axes should be rejected explicitly, since tensor contraction requires distinct summation axes, and a clear input-validation error should be raised before any internal reshaping or transposition.

tensordot performs a tensor contraction where each axis corresponds to a distinct summation index. Duplicate contraction axes (e.g. axes=([1, 1], [0, 0])) are mathematically invalid but currently fail later during an internal transpose, raising ValueError: axes don't match array. This PR adds early validation to reject duplicate axes explicitly, aligning the behavior with the definition of tensor contraction and other NumPy axis-handling APIs.

Current behavior:
Supplying duplicate contraction axes (e.g. axes=([1, 1], [0, 0])) causes tensordot to fail later during an internal transpose, raising ValueError: axes don't match array.

Expected behavior:
Duplicate contraction axes should be rejected explicitly, since tensor contraction requires distinct summation axes, and a clear input-validation error should be raised before any internal reshaping or transposition.
@vkverma9534
Copy link
Copy Markdown
Contributor Author

Exact case I used

a = np.ones((2, 3, 3))
b = np.ones((3, 3, 4))
np.tensordot(a, b, axes=([1, 1], [0, 0]))
ValueError: axes don't match array

@charris charris changed the title fix: validate contraction axes in tensordot BUG: validate contraction axes in tensordot Dec 29, 2025
@charris charris added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported labels Dec 29, 2025
@vkverma9534
Copy link
Copy Markdown
Contributor Author

Hello, following up with a gentle reminder on this PR. All checks are passing, and I’m open to any discussion or feedback. Thank you for your consideration.

@ngoldbaum
Copy link
Copy Markdown
Member

Seems fine to me.

Can you update the docstring to indicate that repeated entries in axes aren't allowed?

Also, IMO the explanation about axes in the note of the docstring is nigh-imcomprehensible. If you want to take a crack at making that more understandable by adding some examples that would be great.

@vkverma9534
Copy link
Copy Markdown
Contributor Author

@ngoldbaum Thanks for the review!

Yes, agreed ,I’ll update the docstring to explicitly state that repeated entries in axes are not allowed.

I’ll also try to clarify the explanatory note by adding a couple of small concrete examples to make the contraction semantics easier to follow. I’ll push an update shortly.

@vkverma9534
Copy link
Copy Markdown
Contributor Author

@ngoldbaum
I’ve added the docstring clarification and examples we talked about earlier and pushed the update. Please let me know if anything else would be helpful.

Copy link
Copy Markdown
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

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

Thanks!

@ngoldbaum ngoldbaum merged commit 4ca820d into numpy:main Jan 20, 2026
75 checks passed
charris pushed a commit to charris/numpy that referenced this pull request Jan 21, 2026
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jan 21, 2026
charris added a commit that referenced this pull request Jan 21, 2026
BUG: validate contraction axes in tensordot (#30521)
@vkverma9534 vkverma9534 deleted the patch-1 branch March 7, 2026 06:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants