-
Notifications
You must be signed in to change notification settings - Fork 22
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
Follow recommendation on the interaction with numpy.ndarray
in binary ops
#2266
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
antonwolfy
requested review from
AlexanderKalistratov,
vlad-perevezentsev and
vtavana
as code owners
January 20, 2025 13:28
antonwolfy
force-pushed
the
follow-nep-13
branch
from
January 20, 2025 13:41
87624db
to
10532ee
Compare
antonwolfy
force-pushed
the
follow-nep-13
branch
from
January 20, 2025 13:42
10532ee
to
6150308
Compare
antonwolfy
commented
Jan 20, 2025
vlad-perevezentsev
approved these changes
Jan 20, 2025
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.
LGTM!
Thank you @antonwolfy
Array API standard conformance tests for dpnp=0.17.0dev4=py312he4f9c94_17 ran successfully. |
github-actions bot
added a commit
that referenced
this pull request
Jan 20, 2025
…ry ops (#2266) The PR proposes to follow NEP-13 [recommendations](https://numpy.org/neps/nep-0013-ufunc-overrides.html#behavior-in-combination-with-python-s-binary-operations) on how to interact with `numpy.ndarray` in binary the operations. It will set `__array_ufunc__ = None` which means that dpnp implements Python binary operations freely and so `numpy.ufuncs` called on this argument will raise `TypeError`: ```python a = numpy.ones(10) b = dpnp.ones(10) a += b --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[9], line 1 ----> 1 a += b TypeError: operand 'dpnp_array' does not support ufuncs (__array_ufunc__=None) ``` And an elementwise operation with `numpy.ndarray` will cause an explicit exception in dpnp: ```python a + b --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[4], line 1 ----> 1 a + b File ~/code/dpnp/dpnp/dpnp_array.py:518, in dpnp_array.__radd__(self, other) 516 def __radd__(self, other): 517 """Return ``value+self``.""" --> 518 return dpnp.add(other, self) File ~/code/dpnp/dpnp/dpnp_algo/dpnp_elementwise_common.py:314, in DPNPBinaryFunc.__call__(self, x1, x2, out, where, order, dtype, subok, **kwargs) 303 def __call__( 304 self, 305 x1, (...) 312 **kwargs, 313 ): --> 314 dpnp.check_supported_arrays_type( 315 x1, x2, scalar_type=True, all_scalars=False 316 ) 317 if kwargs: 318 raise NotImplementedError( 319 f"Requested function={self.name_} with kwargs={kwargs} " 320 "isn't currently supported." 321 ) File ~/code/dpnp/dpnp/dpnp_iface.py:400, in check_supported_arrays_type(scalar_type, all_scalars, *arrays) 397 if scalar_type and dpnp.isscalar(a): 398 continue --> 400 raise TypeError( 401 f"An array must be any of supported type, but got {type(a)}" 402 ) 404 if len(arrays) > 0 and not (all_scalars or any_is_array): 405 raise TypeError( 406 "At least one input must be of supported array type, " 407 "but got all scalars." 408 ) TypeError: An array must be any of supported type, but got <class 'numpy.ndarray'> ``` Previously it works as in a way: ```python a = numpy.ones(10) b = dpnp.ones(10) a + b # Out: # array([array(2.), array(2.), array(2.), array(2.), array(2.), array(2.), # array(2.), array(2.), array(2.), array(2.)], dtype=object) ``` Note, some updates in tests from #2260 have been mapped to that PR. 13816bd
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The PR proposes to follow NEP-13 recommendations on how to interact with
numpy.ndarray
in binary the operations.It will set
__array_ufunc__ = None
which means that dpnp implements Python binary operations freely and sonumpy.ufuncs
called on this argument will raiseTypeError
:And an elementwise operation with
numpy.ndarray
will cause an explicit exception in dpnp:Previously it works as in a way:
Note, some updates in tests from #2260 have been mapped to that PR.