-
Notifications
You must be signed in to change notification settings - Fork 30
usm_ndarray object special methods, and other changes #586
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
Conversation
This file contains hidden or 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
89ca598
to
bde29a2
Compare
ed4c5b4
to
a3e209f
Compare
public keyword amounted to Cython exposing private struct members to Python (such usm_ndarray.typenum_, usm_ndarray.nd_, etc) Keeping only api (no public), these members are not longer exposed. Added __pyx_capi__ function to access these private members. Added tests to test __pyx_capi__ functions
47f384b
to
e60a9f8
Compare
Implemented copy based on Python API to _Memory.memcpy (slow) as a reference implementation. Implemented `dpctl.tensor.reshape` and `dpctl.tensor.astype`, and usm_ndarray.shape setter. Full signature follows NumPy: ``` dpt.astype(usm_ary, new_type, order="K", casting="unsage", copy=True) ``` order can be "K", "C", "F" or "A" casting can be what is accepted by np.can_cast copy: True/False (if True, a copy is always made) Added member attribute of the array storing array namespace, which can be set by dpctl.tensor.usm_ndarray._set_namespace. This namespace is used to relay special method calls, e.g. __abs__ translates into namespace.abs(). Presently, namespace defaults to None, which is interpreted as use `dpnp` is loaded. If no function to relay to is available, NotImplemented object is returned. added internal attribute `._pointer` to get array pointer corresponding to zero indexes. Implemented `dpct.tensor.to_numpy(usm_ary)` and `dpctl.tensor.from_numpy(np_ary, usm_type="device", queue=None)`. Added tests to ensure coverage of > 80%. ``` In [1]: import dpnp Running on: Intel(R) UHD Graphics [0x9bca] DPCtrl SYCL queue used SYCL kernels link time: 5.94e-07 (sec.) Math backend version: Intel(R) oneAPI Math Kernel Library Version 2021.3-Product Build 20210617 for Intel(R) 64 architecture applications In [2]: import dpctl.tensor as dpt In [3]: import numpy as np In [4]: X = dpt.from_numpy(np.array([1.2, 0.3, 2.4], dtype='d')) In [5]: Y = dpt.from_numpy(np.array([-0.8, 0.6, -1.8], dtype='d')) In [6]: X + Y # works by falling back to dpnp.add Out[6]: <dpctl.tensor._usmarray.usm_ndarray at 0x7f8be3b3ead0> In [7]: dpt.to_numpy(_) Out[7]: array([0.4, 0.9, 0.6]) ``` Modularized __pyx_capi__, also added tests for exported constants. Closes #591 Closes #592 N.B.: Cython version 0.* generates its extensions with quirky behavior of special methods as documented in https://stackoverflow.com/questions/33218006/correct-override-of-rmul-in-cython-extensions http://docs.cython.org/en/latest/src/userguide/special_methods.html#arithmetic-methods Essentially, even if such Cython extension implemented __rmul__ special method, it never gets called by CPython, instead __mul__ is called with swapped arguments. Therefore, ``def __mul__(self, other)`` can not assume that `self` is an instance of implementor. This behavior changes in Cython 3 (currently in alpha). There, `__rmul__` will get called, and first argument of `__mul__` would be guaranteed to be an instance of the class implementing the special method.
e60a9f8
to
936bd69
Compare
Alexander-Makaryev
approved these changes
Sep 23, 2021
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.
👍
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.
Use of
public
keyword when declaringusm_ndarray
Cython class amounted to Cython exposing private struct members to Python (suchusm_ndarray.typenum_
,usm_ndarray.nd_
, etc)Keeping only
api
(nopublic
), these members are not longer exposed, while the python object and its type remain exposed.Added
__pyx_capi__
functions to access these private members, paving the road to using these classes in C/C++ extensions, e.g. built with help of pybind11.Added tests to test these
__pyx_capi__
functions.This PR has also grown to add
__setitem__
special method tousm_ndarray
class, as well asdpctl.tensor.astype(array, new_dtype, order="K", copy=True)
anddpctl.tensor.reshape(array, new_shape, order="C")
.usm_ndarray
can also be reshaped usingshape
setter if this can be done as a view.