dpnp.tensor.from_dlpack¶
- dpnp.tensor.from_dlpack(x, /, *, device=None, copy=None)¶
Constructs
dpnp.tensor.usm_ndarrayornumpy.ndarrayinstance from a Python objectxthat implements__dlpack__protocol.- Parameters:
x (object) -- A Python object representing an array that supports
__dlpack__protocol.device ({None, str, dpctl.SyclDevice, dpctl.SyclQueue,) --
dpnp.tensor.Device, tuple}, optional Device where the output array is to be placed.
devicekeyword values can be:NoneThe data remains on the same device.
- oneAPI filter selector string
SYCL device selected by filter selector string.
dpctl.SyclDeviceexplicit SYCL device that must correspond to a non-partitioned SYCL device.
dpctl.SyclQueueimplies SYCL device targeted by the SYCL queue.
dpnp.tensor.Deviceimplies SYCL device
device.sycl_queue. TheDeviceobject is obtained viadpnp.tensor.usm_ndarray.device.
(device_type, device_id)2-tuple matching the format of the output of the
__dlpack_device__method: an integer enumerator representing the device type followed by an integer representing the index of the device. The only supporteddpnp.tensor.DLDeviceTypedevice types are"kDLCPU"and"kDLOneAPI".
Default:
None.copy ({None, bool}, optional) --
Boolean indicating whether or not to copy the input.
If
copyisTrue, the input will always be copied.If
False, aBufferErrorwill be raised if a copy is deemed necessary.If
None, a copy will be made only if deemed necessary, otherwise, the existing memory buffer will be reused.
Default:
None.
- Returns:
out -- An array containing the data in
x. WhencopyisNoneorFalse, this may be a view into the original memory.The type of the returned object depends on where the data backing up input object
xresides. If it resides in a USM allocation on a SYCL device, the typedpnp.tensor.usm_ndarrayis returned, otherwise if it resides on"kDLCPU"device the type isnumpy.ndarray, and otherwise an exception is raised.Note
If the return type is
dpnp.tensor.usm_ndarray, the associated SYCL queue is derived from thedevicekeyword. Whendevicekeyword value has typedpctl.SyclQueue, the explicit queue instance is used, whendevicekeyword value has typedpnp.tensor.Device, thedevice.sycl_queueis used. In all other cases, the cached SYCL queue corresponding to the implied SYCL device is used.- Return type:
{usm_ndarray, numpy.ndarray}
- Raises:
TypeError -- if
xdoes not implement__dlpack__method.ValueError -- if data of the input object resides on an unsupported device.
See also
https//dmlc.github.io/dlpack/latest/ : DLPack specification.
Examples
import dpctl import dpnp.tensor as dpt class Container: "Helper class implementing `__dlpack__` protocol" def __init__(self, array): self._array = array def __dlpack__(self, stream=None): return self._array.__dlpack__(stream=stream) def __dlpack_device__(self): return self._array.__dlpack_device__() C = Container(dpt.linspace(0, 100, num=20, dtype="int16")) # create usm_ndarray view X = dpt.from_dlpack(C) # migrate content of the container to device of type kDLCPU Y = dpt.from_dlpack(C, device=(dpt.DLDeviceType.kDLCPU, 0))