@@ -965,41 +965,66 @@ cdef object _create_device(object device, object dl_device):
965
965
def from_dlpack (x , /, *, device = None , copy = None ):
966
966
""" from_dlpack(x, /, *, device=None, copy=None)
967
967
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.
970
970
971
971
Args:
972
972
x (object):
973
973
A Python object representing an array that supports
974
974
``__dlpack__`` protocol.
975
975
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
+
987
1002
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
+
988
1013
copy (bool, optional)
989
1014
Boolean indicating whether or not to copy the input.
990
1015
991
1016
* If ``copy`` is ``True``, the input will always be
992
- copied.
1017
+ copied.
993
1018
* If ``False``, a ``BufferError`` will be raised if a
994
- copy is deemed necessary.
1019
+ copy is deemed necessary.
995
1020
* 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.
998
1023
999
1024
Default: ``None``.
1000
1025
1001
1026
Returns:
1002
- usm_ndarray:
1027
+ Alternative[ usm_ndarray, numpy.ndarray] :
1003
1028
An array containing the data in ``x``. When ``copy`` is
1004
1029
``None`` or ``False``, this may be a view into the original
1005
1030
memory.
@@ -1008,7 +1033,7 @@ def from_dlpack(x, /, *, device=None, copy=None):
1008
1033
TypeError:
1009
1034
if ``x`` does not implement ``__dlpack__`` method
1010
1035
ValueError:
1011
- if the input array resides on an unsupported device
1036
+ if data of the input object resides on an unsupported device
1012
1037
1013
1038
See https://dmlc.github.io/dlpack/latest/ for more details.
1014
1039
@@ -1031,7 +1056,9 @@ def from_dlpack(x, /, *, device=None, copy=None):
1031
1056
return self._array.__dlpack_device__()
1032
1057
1033
1058
C = Container(dpt.linspace(0, 100, num=20, dtype="int16"))
1059
+ # create usm_ndarray view
1034
1060
X = dpt.from_dlpack(C)
1061
+ # migrate content of the container to device of type kDLCPU
1035
1062
Y = dpt.from_dlpack(C, device=(dpt.DLDeviceType.kDLCPU, 0))
1036
1063
1037
1064
"""
0 commit comments