-
Notifications
You must be signed in to change notification settings - Fork 30
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
Tensor asarray support for usm ndarray protocol #1959
Tensor asarray support for usm ndarray protocol #1959
Conversation
`asarray` supports objects that implement `__sycl_usm_array_interface__`. It can create a view into USM allocation owned by input object, hence maintains a reference to it. Asynchronous deallocation of such objects in dpctl.tensor functions require manipulating Python object reference counters, and hold GIL. This is a source of dead-locks, and affects performance. This PR adds support for ingesting Python objects that implement __usm_ndarray__ attribute (property) that returns dpt.usm_ndarray object with such a view directly. It is trivial for `dpnp.ndarray` to implement such a property, e.g,. ``` @Property def __usm_ndarray__(self): return self._array_obj ``` With this definition, `dpt.asarray(dpnp_array)` will recognize that the underlying USM allocation is managed by the smart pointer, and asynchronous deallocation will not involve Python objects, avoiding dead-locks and improving performance.
8d89f85
to
e8fe0e0
Compare
Deleted rendered PR docs from intelpython.github.com/dpctl, latest should be updated shortly. 🤞 |
Array API standard conformance tests for dpctl=0.19.0dev0=py310h93fe807_428 ran successfully. |
1 similar comment
Array API standard conformance tests for dpctl=0.19.0dev0=py310h93fe807_428 ran successfully. |
Drop in coverage level has to do with some issue with coverage itself. Running locally, I see
For example, lines 66-68 are newly added lines, but when I add a print statement there, I see the output during test execution. |
A brief thought: NumPy specifically outlines how the I'm not sure we need to require these arguments, but we should add some documentation for |
It turns me to the off-topic question if dpctl and dpnp have to provide support of Since today you can pass usm_ndarray to import numpy, dpctl, dpctl.tensor as dpt
a = dpt.ones(10)
numpy.asarray(a)
# Out:
# array([usm_ndarray(1.), usm_ndarray(1.), usm_ndarray(1.), usm_ndarray(1.),
# usm_ndarray(1.), usm_ndarray(1.), usm_ndarray(1.), usm_ndarray(1.),
# usm_ndarray(1.), usm_ndarray(1.)], dtype=object) but if we add support of class A:
def __init__(self, obj):
self.obj = obj
def __array__(self):
return dpt.asnumpy(self.obj)
b = A(a)
numpy.asarray(b)
# Out: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) or alternatively we can add an exception raising inside I can create a separate issue if there is something to do or discuss. |
@antonwolfy I opened gh-1964 to implement |
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.
Additional documentation can be saved for a follow-up PR, this LGTM
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 checked the changes with dpnp#2261 locally and all dpnp tests passed. It seems everything works properly.
Thank you @oleksandr-pavlyk
This PR changes
dpctl.tensor.asarray
function to recognize objects that implement__usm_ndarray__
protocol and expect this property to returndpctl.tensor.usm_ndarray
instance corresponding to the content of the object.This property is intended to speed-up conversion from
dpnp.ndarray
todpt.usm_ndarray
inx = dpt.asarray(dpnp_array_obj)
.The input object that implements
__usm_ndarray__
is recognized as owner of USM allocation that is managed by a smart pointer, and asynchronous deallocation ofx
need not involve GIL.