-
Notifications
You must be signed in to change notification settings - Fork 32
Disallow numeric scalar conversation for non-0D arrays #2223
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
base: master
Are you sure you want to change the base?
Conversation
|
View rendered docs @ https://intelpython.github.io/dpctl/pulls/2223/index.html |
|
Array API standard conformance tests for dpctl=0.22.0dev0=py310h93fe807_103 ran successfully. |
|
Array API standard conformance tests for dpctl=0.22.0dev0=py310h93fe807_108 ran successfully. |
| cdef inline void _check_0d_scalar_conversion(object usm_ary) except *: | ||
| "Raise TypeError if array cannot be converted to a Python scalar" | ||
| if (usm_ary.ndim != 0): | ||
| raise TypeError( |
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.
| raise TypeError( | |
| raise ValueError( |
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 guess NumPy raises a TypeError but this really seems like a ValueError to me
| assert not X.__le__(-1) | ||
| assert not X.__eq__(-1) | ||
| assert X.__ne__(-1) | ||
| assert dpt.all(X.__gt__(-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.
instead of adding these additional kernel launches, we can just either strip the dimension off of X or we can use dpt.asarray above, so that X is an array scalar
| x2 = dpt.ones([1], dtype="i1") | ||
| r2 = dpt.argsort(x2, kind="radixsort") | ||
| assert r2 == 0 | ||
| assert dpt.all(r2 == 0) |
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.
similar to the other test, we can save a kernel launch by stripping the dimension off of r2. It's not too bad of a hack I feel.
ndgrigorian
left a comment
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.
aside from minor comments this LGTM
This PR partially aligns
usm_ndarrayscalar conversion semantics with the Python Array API by allowing numeric scalar conversion only for 0D usm_ndarraysConversions to Python numeric scalars via
int,float,complexand their corresponding methods (__int__,_float__,__complex__) now raiseTypeErrorwhen applied to usm_ndarrays withndim !=0Note:
For full compliance with the Python Array API, the same check should be used for truth values
__bool__method and integer indexing index methodBut these changes would impact dpnp compatibility with Numpy 2.4 which currently disallows only numeric scalar conversation (a minor behavioral difference I think)
In addition several
dpctl testsrely on implicit truth conversion for non-0D arrays and must be updated accordingly