Tensor asarray support for usm ndarray protocol#1959
Tensor asarray support for usm ndarray protocol#1959oleksandr-pavlyk merged 3 commits intomasterfrom
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 |
ndgrigorian
left a comment
There was a problem hiding this comment.
Additional documentation can be saved for a follow-up PR, this LGTM
There was a problem hiding this comment.
I checked the changes with dpnp#2261 locally and all dpnp tests passed. It seems everything works properly.
Thank you @oleksandr-pavlyk
The PR is intended to adopt to dpctl changes implemented in [dpctl#1959](IntelPython/dpctl#1959). It implements support of `__usm_ndarray__` protocol for `dpnp.ndarray` and returns a property with `dpctl.tensor.usm_ndarray` instance corresponding to the content of the array object. This property is intended to speed-up conversion from `dpnp.ndarray` to `dpt.usm_ndarray` in `x=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 of `x` need not involve GIL.
The PR is intended to adopt to dpctl changes implemented in [dpctl#1959](IntelPython/dpctl#1959). It implements support of `__usm_ndarray__` protocol for `dpnp.ndarray` and returns a property with `dpctl.tensor.usm_ndarray` instance corresponding to the content of the array object. This property is intended to speed-up conversion from `dpnp.ndarray` to `dpt.usm_ndarray` in `x=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 of `x` need not involve GIL. 9ad1bb5
This PR changes
dpctl.tensor.asarrayfunction to recognize objects that implement__usm_ndarray__protocol and expect this property to returndpctl.tensor.usm_ndarrayinstance corresponding to the content of the object.This property is intended to speed-up conversion from
dpnp.ndarraytodpt.usm_ndarrayinx = 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 ofxneed not involve GIL.