8000 BUG (I guess?): Make a @= b error out by njsmith · Pull Request #6000 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG (I guess?): Make a @= b error out #6000

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 1 commit into from
Jun 28, 2015

Conversation

njsmith
Copy link
Member
@njsmith njsmith commented Jun 22, 2015

Before this change, we defined a nb_matrix_multiply slot but not a
nb_inplace_matrix_multiply slot, which means that a statement like

  a @= b

would be silently expanded by the CPython interpreter to become

  a = a @ b

This is undesireable, because it produces unexpected memory
allocations, breaks view relationships, and so forth.

This commit adds a nb_inplace_matrix_multiply slot which simply errors
out, and suggests that users write a = a @ b explicitly if that's
what they want.

[Noticed this while working on a __numpy_ufunc__ patch, so here's a quick fix that I haven't even tested yet :-). Let's see what Travis says...]

@njsmith njsmith added this to the 1.10 blockers milestone Jun 22, 2015
@njsmith
Copy link
Member Author
njsmith commented Jun 22, 2015

Doh, right, there's actually no way to test this at all except on py3.5.

Our current .travis.yml is complicated enough that I'm quailing a little at the thought of trying to add a 3.5-pre check, but that does seem like something we need to do for this and other reasons. Anyone else feeling brave and inspired?

@njsmith
Copy link
Member Author
njsmith commented Jun 25, 2015

Just pushed a new version of this that's ready for review.

I tested 3859b2f locally against a recent python 3.5 build, and the new test passed.

# we avoid writing the token `exec` so as not to crash python 2's
# parser
exec_ = getattr(builtins, "exec")
assert_raises(TypeError, exec_, "a @= b", globals(), locals())
Copy link
Member

Choose a reason for hiding this comment

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

Could also use operator.imatmul probably, though I am not sure I care.

Copy link
Member Author

Choose a reason for hiding this comment

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

Eh, sure, I'll throw that in too :-)

Copy link
Member

Choose a reason for hiding this comment

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

I thought to avoid the exec dance ;p, but nvm. All is good in any case.

@seberg
Copy link
Member
seberg commented Jun 27, 2015

Anyway, seems the right thing to do to just not support it for the moment. For stacked matrices, it would probably be easy to support (and make sense to safe memory), since einsum supports it, and I think we currently use einsum (for non-stacked, a simple assignment using safe casting might do anyway)? But it is probably more flexible and reasonable to just not support it for the moment.

@seberg
Copy link
Member
seberg commented Jun 27, 2015

Nvm. it is not possible to support even with einsum, since it should be (at least almost) impossible to force correct/large enough buffering. (It might be that forcing buffering + iteration size would do, but I don't even think einsum exposes that).

@charris charris modified the milestones: 1.10.0 release, 1.10 blockers Jun 27, 2015
Before this change, we defined a nb_matrix_multiply slot but not a
nb_inplace_matrix_multiply slot, which means that a statement like

  a @= b

would be silently expanded by the CPython interpreter to become

  a = a @ b

This is undesireable, because it produces unexpected memory
allocations, breaks view relationships, and so forth.

This commit adds a nb_inplace_matrix_multiply slot which simply errors
out, and suggests that users write 'a = a @ b' explicitly if that's
what they want.
@njsmith njsmith force-pushed the inplace-matmul-error branch from 3859b2f to 1adcdf7 Compare June 28, 2015 02:21
@njsmith
Copy link
Member Author
njsmith commented Jun 28, 2015

Addressed all comments.

seberg added a commit that referenced this pull request Jun 28, 2015
@seberg seberg merged commit 23e10e1 into numpy:master Jun 28, 2015
@seberg
Copy link
Member
seberg commented Jun 28, 2015

Thanks.

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.

3 participants
0