Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: Fix einsum optimize logic for singleton dimensions #10352

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 4 commits into from
Feb 5, 2018

Conversation

larsoner
Copy link
Contributor

@larsoner larsoner commented Jan 9, 2018

Closes #10343.

I don't know if it's the correct fix because these functions have a lot of logic going on that I have never looked at before (and haven't looked at too closely). Feel free to close if the right solution is a bit more involved.

@mhvk mhvk added this to the 1.15.0 release milestone Jan 10, 2018
@mhvk
Copy link
Contributor

mhvk commented Jan 10, 2018

@larsoner - thanks for the PR. It took me a long time to see what the heck is going on in the parsing, but now I understand that, basically, the optimize path did not include broadcasting -- I must add it surprised me that broadcasting was even allowed on identical indices (rather than just on the ..., but that seems to be the case before too).

Anyway, I think this PR is correct. @dgasmith - could you have a look?

@dgasmith
Copy link
Contributor

I need to think on it a bit, that parsing tech is fairly complex as folks want quite the variety of input possibilities. I would consider adding more tests that cover the "double broadcasting" cases where 1-dims are mixed with ellipses like in the original issue.

@dgasmith
Copy link
Contributor

This might hit issues in the BLAS calls. Please try the following two cases:

a = np.random.rand(1, 5)
b = np.random.rand(5, 5)

np.einsum("...ij,...jk->...ik", a, a)
np.einsum("...ij,...jk->...ik", a, b)

I suspect we may have to go in and change the BLAS logic to bounce broadcasting cases as well.

@larsoner
Copy link
Contributor Author

Rebased due to conflict and test added. I'm not on my dev machine now so I can't easily run the new tests so I'll let the CIs do the work :)

@larsoner
Copy link
Contributor Author

@dgasmith @mhvk tests are indeed a bit broken now. It might take me a while to fix this, so feel free to take over, or I can find time in the coming weeks to work on it.

@dgasmith
Copy link
Contributor

Yup, ill need to go in and really take a look at whats going on. Ill get to it in the next few days.

@larsoner larsoner changed the title FIX: Fix einsum optimize logic for singleton dimensions MRG: Fix einsum optimize logic for singleton dimensions Jan 18, 2018
@larsoner
Copy link
Contributor Author

Okay @dgasmith I deduplicated the code a bit, feel free to look to see if you're happy.

CIs are happy, ready for review/merge from my end.

Copy link
Contributor

@dgasmith dgasmith left a comment

Choose a reason for hiding this comment

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

Looks good to me, the NumPy maintainers might have a few convention thoughts.

@eric-wieser
Copy link
Member

Can you loop me in on what the MRG prefix means? I've seen it used a bunch in matplotlib too

left_dims = {dim: size for dim, size in
zip(input_left, tmp_operands[0].shape)}
right_dims = {dim: size for dim, size in
zip(input_right, tmp_operands[1].shape)}
Copy link
Member

Choose a reason for hiding this comment

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

Why not use idx_rm here? The other entries in this dict are being ignored

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here input_left is e.g. 'ik', so this seems like a straightforward / quick way to get the indices to line up. Are you thinking something like this should be done instead?

if any(tmp_operands[0].shape[input_left.index(ind)] != tmp_operands[1].shape[input_right.index(ind)] for ind in idx_rm)

Copy link
Member

@eric-wieser eric-wieser Jan 18, 2018

Choose a reason for hiding this comment

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

You're right, what you had was better and probably faster too

@larsoner
Copy link
Contributor Author

Can you loop me in on what the MRG prefix means? I've seen it used a bunch in matplotlib too

It's a way for me (contributor) to signal to you (reviewers) that the PR is ready for review / merge from my perspective. In other projects I work on, maintainers historically have then changed the title as they approve to e.g. MRG+1, MRG+2 etc., though GitHub recently came out with the "approve" / "request changes" stuff so now that mechanism gets used more than additional title changes from the reviewer end.

if 1 in tmp_operands[0] or 1 in tmp_operands[1]:
if any(tmp_operands[0].shape[input_left.index(ind)] !=
tmp_operands[1].shape[input_right.index(ind)]
for ind in idx_rm):
Copy link
Member

Choose a reason for hiding this comment

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

To reiterate my now-hidden comment, I think what you had before was better - sorry!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fine, easy enough to remove the commit

@charris charris changed the title MRG: Fix einsum optimize logic for singleton dimensions BUG: Fix einsum optimize logic for singleton dimensions Jan 28, 2018
@charris charris merged commit a113731 into numpy:master Feb 5, 2018
@charris
Copy link
Member

charris commented Feb 5, 2018

Merged, thanks @larsoner .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

einsum broadcast regression (with optimize=True)
5 participants