dpctl.tensor.from_dlpack¶
- dpctl.tensor.from_dlpack(x, /, *, device=None, copy=None)¶
Constructs
dpctl.tensor.usm_ndarrayinstance from a Python objectxthat implements__dlpack__protocol.- Parameters:
x (object) – A Python object representing an array that supports
__dlpack__protocol.device (Optional[str,
dpctl.SyclDevice,dpctl.SyclQueue,dpctl.tensor.Device, tuple([enum.IntEnum, int])])) – Array API concept of a device where the output array is to be placed.devicecan beNone, a oneAPI filter selector string, an instance ofdpctl.SyclDevicecorresponding to a non-partitioned SYCL device, an instance ofdpctl.SyclQueue, adpctl.tensor.Deviceobject returned bydpctl.tensor.usm_ndarray.device, or a 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 supporteddpctl.tensor.DLDeviceTypetypes are “kDLCPU” and “kDLOneAPI”. Default:None.copy (bool, optional) –
Boolean indicating whether or not to copy the input.
- If
copyisTrue, the input will always be copied.
- If
- If
False, aBufferErrorwill be raised if a copy is deemed necessary.
- If
- If
None, a copy will be made only if deemed necessary, otherwise, the existing memory buffer will be reused.
- If
Default:
None.
- Returns:
An array containing the data in
x. WhencopyisNoneorFalse, this may be a view into the original memory.- Return type:
- Raises:
TypeError – if
xdoes not implement__dlpack__methodValueError – if the input array resides on an unsupported device
See https://dmlc.github.io/dlpack/latest/ for more details.
- Example:
import dpctl import dpctl.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")) X = dpt.from_dlpack(C) Y = dpt.from_dlpack(C, device=(dpt.DLDeviceType.kDLCPU, 0))