Skip to content

Fix einsum failing with repeated inputs #1260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 10, 2025

Conversation

Abhinav-Khot
Copy link
Contributor

@Abhinav-Khot Abhinav-Khot commented Mar 2, 2025

Description

Fixed the issue where passing the same tensor multiple times as arguments to the einsum function would raise a ValueError. Implemented a function that internally converts duplicate objects into copies using copy().

Related Issue

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

📚 Documentation preview 📚: https://pytensor--1260.org.readthedocs.build/en/1260/

@Abhinav-Khot
Copy link
Contributor Author

Abhinav-Khot commented Mar 2, 2025

@ricardoV94 @twiecki

Hello maintainers,

My pull request seems to fail 2 tests even though these aren't directly linked to any changes I have made. This is my first time contributing, I would appreciate any help

@ricardoV94
Copy link
Member

It's unrelated and will be patched by a0d9ecf

Comment on lines 424 to 428
for i in range(len(elements)):
for j in range(i + 1, len(elements)):
if elements[i] == elements[j]:
elements[j] = elements[j].copy()
return elements
Copy link
Member

@ricardoV94 ricardoV94 Mar 3, 2025

Choose a reason for hiding this comment

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

Some small optimization. Don't assume a list is passed and reduce list access. Also is should be better than ==.

Suggested change
for i in range(len(elements)):
for j in range(i + 1, len(elements)):
if elements[i] == elements[j]:
elements[j] = elements[j].copy()
return elements
elements = list(elements)
n_elem = len(elements)
for i, elem1 in enumerate(elements[:-1]):
for j, elem2 in enumerate(elements[i + 1:], start=i+1):
if elem1 is elem2:
elements[j] = elem1.copy()
return elements

@ricardoV94
Copy link
Member

Can you add a regression test?

@ricardoV94 ricardoV94 added bug Something isn't working NumPy compatibility labels Mar 3, 2025
@ricardoV94 ricardoV94 changed the title fixed Einsum failing with repeated inputs Fix einsum failing with repeated inputs Mar 3, 2025
@ricardoV94
Copy link
Member

Failing test should go away with a rebase from main

@ricardoV94
Copy link
Member

You seem to have pushed a pyd file by mistake

@Abhinav-Khot
Copy link
Contributor Author

@ricardoV94 I have deleted that file, thank you for noticing that

@ricardoV94
Copy link
Member

Still needs a regression test

@Abhinav-Khot
Copy link
Contributor Author

Still needs a regression test

Just to clarify, does this mean I need to add a test to the test_einsum.py to check functionality? Again apologies, this is my first time contributing.

@ricardoV94
Copy link
Member

Still needs a regression test

Just to clarify, does this mean I need to add a test to the test_einsum.py to check functionality? Again apologies, this is my first time contributing.

Yes, a test that would fail before this PR. Try to make it a simple as possible

Co-authored-by: Ricardo Vieira <[email protected]>
Copy link

codecov bot commented Mar 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.99%. Comparing base (2a7f3e1) to head (30aaffc).
Report is 11 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1260      +/-   ##
==========================================
- Coverage   81.99%   81.99%   -0.01%     
==========================================
  Files         188      188              
  Lines       48553    48582      +29     
  Branches     8673     8688      +15     
==========================================
+ Hits        39812    39833      +21     
- Misses       6579     6585       +6     
- Partials     2162     2164       +2     
Files with missing lines Coverage Δ
pytensor/tensor/einsum.py 97.10% <100.00%> (+0.10%) ⬆️

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ricardoV94 ricardoV94 merged commit 00fea0e into pymc-devs:main Mar 10, 2025
73 checks passed
@ricardoV94
Copy link
Member

Thanks @Abhinav-Khot

@Abhinav-Khot Abhinav-Khot deleted the repeated-inputs-einsum branch March 12, 2025 09:15
@ricardoV94 ricardoV94 changed the title Fix einsum failing with repeated inputs Fix einsum failing with repeated inputs Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working NumPy compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Einsum fails with repeated inputs
2 participants