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
BUG: Fix the implementation of numpy.array_api.vecdot #21928
Conversation
Seems OK, I think I slightly prefer the
Plus the wrapping/unwrapping stuff. Or use |
OK, made it use moveaxis + matmul instead of einsum. |
ndim = max(x1.ndim, x2.ndim) | ||
x1_shape = (1,)*(ndim - x1.ndim) + tuple(x1.shape) | ||
x2_shape = (1,)*(ndim - x2.ndim) + tuple(x2.shape) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will go away with the change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this can be simplified, but without this, the shape checking is not correct (because size 1 dimensions broadcast).
Also, the broadcasting is necessary because, assuming I am understanding the idea in the spec correctly, nonnegative axis
should refer to the broadcasted dimensions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can move the checking to later and use -1
as axis. That probably gives you better errors for bad axis values anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I'm not following your suggestion here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am saying that you can do the check after the broadcast+moveaxis at the point where the axis is properly normalized to -1
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the liberty to do that change, unless you disagree will merge next time around (or earlier with feedback).
numpy/array_api/linalg.py
Outdated
x1_ = np.moveaxis(x1_, axis, -1) | ||
x2_ = np.moveaxis(x2_, axis, -1) | ||
|
||
if x1_shape[-1] != x2_shape[-1]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You missed some dots here.
I guess the spec's a little ambiguous here, but my understanding is that vecdot should not broadcast along the specified We should probably figure out which behavior we want here and update the spec to be clearer. |
Just to note that we ignore the lint errors in the |
9d61d5e
to
285084a
Compare
Yeah, I missed the boardcasting part, lets go with the previous version for now. |
Thanks @asmeurer, sorry I had lost track of this, lets put it in. |
* Fix the implementation of numpy.array_api.vecdot See https://data-apis.org/array-api/latest/API_specification/generated/signatures.linear_algebra_functions.vecdot.html * Use moveaxis + matmul instead of einsum in vecdot
See https://data-apis.org/array-api/latest/API_specification/generated/signatures.linear_algebra_functions.vecdot.html