Skip to content

Commit e067321

Browse files
committed
Handle different result types
1 parent 006cc54 commit e067321

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

dpnp/dpnp_iface_arraycreation.py

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,8 +2050,8 @@ def fromstring(
20502050

20512051
def from_dlpack(x, /, *, device=None, copy=None):
20522052
"""
2053-
Create a dpnp array from a Python object implementing the ``__dlpack__``
2054-
protocol.
2053+
Constructs :class:`dpnp.ndarray` or :class:`numpy.ndarray` instance from
2054+
a Python object `x` that implements ``__dlpack__`` protocol.
20552055
20562056
For full documentation refer to :obj:`numpy.from_dlpack`.
20572057
@@ -2060,16 +2060,27 @@ def from_dlpack(x, /, *, device=None, copy=None):
20602060
x : object
20612061
A Python object representing an array that implements the ``__dlpack__``
20622062
and ``__dlpack_device__`` methods.
2063-
device : {None, tuple, SyclDevice, SyclQueue, Device}, optional
2064-
Array API concept of a device where the output array is to be placed.
2065-
``device`` can be ``None``, an oneAPI filter selector string,
2066-
an instance of :class:`dpctl.SyclDevice` corresponding to
2067-
a non-partitioned SYCL device, an instance of :class:`dpctl.SyclQueue`,
2068-
a :class:`dpctl.tensor.Device` object returned by
2069-
:attr:`dpctl.tensor.usm_ndarray.device`, or a 2-tuple matching
2070-
the format of the output of the ``__dlpack_device__`` method,
2071-
an integer enumerator representing the device type followed by
2072-
an integer representing the index of the device.
2063+
device : {None, string, tuple, device}, optional
2064+
Device where the output array is to be placed. `device` keyword values
2065+
can be:
2066+
2067+
* ``None`` : The data remains on the same device.
2068+
* oneAPI filter selector string : SYCL device selected by filter
2069+
selector string.
2070+
* :class:`dpctl.SyclDevice` : Explicit SYCL device that must correspond
2071+
to a non-partitioned SYCL device.
2072+
* :class:`dpctl.SyclQueue` : Implies SYCL device targeted by the SYCL
2073+
queue.
2074+
* :class:`dpctl.tensor.Device` : Implies SYCL device
2075+
``device.sycl_queue``. The `device` object is obtained via
2076+
:attr:`dpctl.tensor.usm_ndarray.device`.
2077+
* ``(device_type, device_id)`` : 2-tuple matching the format of the
2078+
output of the ``__dlpack_device__`` method: an integer enumerator
2079+
representing the device type followed by an integer representing
2080+
the index of the device. The only supported
2081+
:class:`dpctl.tensor.DLDeviceType` device types are ``"kDLCPU"``
2082+
and ``"kDLOneAPI"``.
2083+
20732084
Default: ``None``.
20742085
copy : {bool, None}, optional
20752086
Boolean indicating whether or not to copy the input.
@@ -2084,16 +2095,30 @@ def from_dlpack(x, /, *, device=None, copy=None):
20842095
20852096
Returns
20862097
-------
2087-
out : dpnp.ndarray
2088-
Returns a new dpnp array containing the data from another array `obj`
2089-
with the ``__dlpack__`` method on the same device as object.
2098+
out : {dpnp.ndarray, numpy.ndarray}
2099+
An array containing the data in `x`. When `copy` is ``None`` or
2100+
``False``, this may be a view into the original memory.
2101+
The type of the returned object depends on where the data backing up
2102+
input object `x` resides. If it resides in a USM allocation on a SYCL
2103+
device, the type :class:`dpnp.ndarray` is returned, otherwise if it
2104+
resides on ``"kDLCPU"`` device the type is :class:`numpy.ndarray`, and
2105+
otherwise an exception is raised.
20902106
20912107
Raises
20922108
------
2093-
TypeError:
2109+
TypeError
20942110
if `obj` does not implement ``__dlpack__`` method
2095-
ValueError:
2096-
if the input array resides on an unsupported device
2111+
ValueError
2112+
if data of the input object resides on an unsupported device
2113+
2114+
Notes
2115+
-----
2116+
If the return type is :class:`dpnp.ndarray`, the associated SYCL queue is
2117+
derived from the `device` keyword. When `device` keyword value has type
2118+
:class:`dpctl.SyclQueue`, the explicit queue instance is used, when `device`
2119+
keyword value has type :class:`dpctl.tensor.Device`, the
2120+
``device.sycl_queue`` is used. In all other cases, the cached SYCL queue
2121+
corresponding to the implied SYCL device is used.
20972122
20982123
Examples
20992124
--------
@@ -2105,8 +2130,10 @@ def from_dlpack(x, /, *, device=None, copy=None):
21052130
21062131
"""
21072132

2108-
usm_res = dpt.from_dlpack(x, device=device, copy=copy)
2109-
return dpnp_array._create_from_usm_ndarray(usm_res)
2133+
result = dpt.from_dlpack(x, device=device, copy=copy)
2134+
if isinstance(result, dpt.usm_ndarray):
2135+
return dpnp_array._create_from_usm_ndarray(result)
2136+
return result
21102137

21112138

21122139
def full(

0 commit comments

Comments
 (0)