Skip to content

Commit 34296a8

Browse files
Updated from_dlpack docstring
Closes gh-1917. The change to docstring documents that `tensor.from_dlpack` may return numpy.ndarray or tensor.usm_ndarray and stipulates when that may happen.
1 parent 631d4c3 commit 34296a8

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

dpctl/tensor/_dlpack.pyx

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -965,41 +965,66 @@ cdef object _create_device(object device, object dl_device):
965965
def from_dlpack(x, /, *, device=None, copy=None):
966966
"""from_dlpack(x, /, *, device=None, copy=None)
967967
968-
Constructs :class:`dpctl.tensor.usm_ndarray` instance from a Python
969-
object ``x`` that implements ``__dlpack__`` protocol.
968+
Constructs :class:`dpctl.tensor.usm_ndarray` or :class:`numpy.ndarray` instance
969+
from a Python object ``x`` that implements ``__dlpack__`` protocol.
970970
971971
Args:
972972
x (object):
973973
A Python object representing an array that supports
974974
``__dlpack__`` protocol.
975975
device (Optional[str, :class:`dpctl.SyclDevice`, :class:`dpctl.SyclQueue`, :class:`dpctl.tensor.Device`, tuple([:class:`enum.IntEnum`, int])])):
976-
Array API concept of a device where the output array is to be placed.
977-
``device`` can be ``None``, a oneAPI filter selector
978-
string, an instance of :class:`dpctl.SyclDevice` corresponding to
979-
a non-partitioned SYCL device, an instance of
980-
:class:`dpctl.SyclQueue`, a :class:`dpctl.tensor.Device` object
981-
returned by :attr:`dpctl.tensor.usm_ndarray.device`, or a
982-
2-tuple matching the format of the output of the ``__dlpack_device__``
983-
method, an integer enumerator representing the device type followed by
984-
an integer representing the index of the device. The only supported
985-
:class:`dpctl.tensor.DLDeviceType` types are "kDLCPU" and
986-
"kDLOneAPI".
976+
Device where the output array is to be placed. ``device`` keyword values can be:
977+
978+
* ``None``
979+
The data remains on the same device. If the data backing up
980+
input object ``x`` resides on ``"kDLCPU"`` device, the return
981+
type would be :class:`numpy.ndarray`, otherwise the return
982+
type would be :class:`dpctl.tensor.usm_ndarray`.
983+
* oneAPI filter selector string
984+
SYCL device selected by :ref:`filter selector string <filter_selector_string>`.
985+
* :class:`dpctl.SyclDevice`
986+
explicit SYCL device that must correspond to
987+
a non-partitioned SYCL device.
988+
* :class:`dpctl.SyclQueue`
989+
implies SYCL device targeted by the SYCL queue.
990+
* :class:`dpctl.tensor.Device`
991+
implies SYCL device `device.sycl_queue`. The `Device` object
992+
is obtained via :attr:`dpctl.tensor.usm_ndarray.device`.
993+
* ``(device_type, device_id)``
994+
2-tuple matching the format of the output of the ``__dlpack_device__``
995+
method: an integer enumerator representing the device type followed by
996+
an integer representing the index of the device.
997+
The only supported :class:`dpctl.tensor.DLDeviceType` device types
998+
are ``"kDLCPU"`` and ``"kDLOneAPI"``. If ``"kDLCPU"`` requested, the
999+
output type is :class:`numpy.ndarray`, otherwise it is
1000+
:class:`dpctl.tensor.usm_ndarray`.
1001+
9871002
Default: ``None``.
1003+
1004+
.. note::
1005+
1006+
If the return type if :class:`dpctl.tensor.usm_ndarray`, the associated
1007+
SYCL queue is derived from the ``device`` keyword. When ``device``
1008+
keyword value has type :class:`dpctl.SyclQueue`, the explicit queue
1009+
instance is used, when ``device`` keyword value has type :class:`dpctl.tensor.Device`,
1010+
the ``device.sycl_queue`` is used. In all other cases, the cached
1011+
SYCL queue corresponding the implied SYCL device is used.
1012+
9881013
copy (bool, optional)
9891014
Boolean indicating whether or not to copy the input.
9901015
9911016
* If ``copy`` is ``True``, the input will always be
992-
copied.
1017+
copied.
9931018
* If ``False``, a ``BufferError`` will be raised if a
994-
copy is deemed necessary.
1019+
copy is deemed necessary.
9951020
* If ``None``, a copy will be made only if deemed
996-
necessary, otherwise, the existing memory buffer will
997-
be reused.
1021+
necessary, otherwise, the existing memory buffer will
1022+
be reused.
9981023
9991024
Default: ``None``.
10001025
10011026
Returns:
1002-
usm_ndarray:
1027+
Alternative[usm_ndarray, numpy.ndarray]:
10031028
An array containing the data in ``x``. When ``copy`` is
10041029
``None`` or ``False``, this may be a view into the original
10051030
memory.
@@ -1008,7 +1033,7 @@ def from_dlpack(x, /, *, device=None, copy=None):
10081033
TypeError:
10091034
if ``x`` does not implement ``__dlpack__`` method
10101035
ValueError:
1011-
if the input array resides on an unsupported device
1036+
if data of the input object resides on an unsupported device
10121037
10131038
See https://dmlc.github.io/dlpack/latest/ for more details.
10141039
@@ -1031,7 +1056,9 @@ def from_dlpack(x, /, *, device=None, copy=None):
10311056
return self._array.__dlpack_device__()
10321057
10331058
C = Container(dpt.linspace(0, 100, num=20, dtype="int16"))
1059+
# create usm_ndarray view
10341060
X = dpt.from_dlpack(C)
1061+
# migrate content of the container to device of type kDLCPU
10351062
Y = dpt.from_dlpack(C, device=(dpt.DLDeviceType.kDLCPU, 0))
10361063
10371064
"""

0 commit comments

Comments
 (0)