dpctl.tensor.from_dlpack

dpctl.tensor.from_dlpack(x, /, *, device=None, copy=None)

Constructs dpctl.tensor.usm_ndarray instance from a Python object x that implements __dlpack__ protocol.

Parameters:
  • x (Python object) – A Python object representing an array that supports __dlpack__ protocol.

  • (Optional[str (device) –

    dpctl.SyclDevice,

    dpctl.SyclQueue, dpctl.tensor.Device, tuple([enum.Enum, int])])):

    Array API concept of a device where the output array is to be placed. device can be None, a oneAPI filter selector string, an instance of dpctl.SyclDevice corresponding to a non-partitioned SYCL device, an instance of dpctl.SyclQueue, a dpctl.tensor.Device object returned by dpctl.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. Default: None.

:paramdpctl.SyclDevice,

dpctl.SyclQueue, dpctl.tensor.Device, tuple([enum.Enum, int])])):

Array API concept of a device where the output array is to be placed. device can be None, a oneAPI filter selector string, an instance of dpctl.SyclDevice corresponding to a non-partitioned SYCL device, an instance of dpctl.SyclQueue, a dpctl.tensor.Device object returned by dpctl.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. Default: None.

Parameters:

copy (bool, optional) –

Boolean indicating whether or not to copy the input.

  • If copy is True, the input will always be

    copied.

  • If False, a BufferError will 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:

An array containing the data in x. When copy is None or False, this may be a view into the original memory.

Return type:

usm_ndarray

Raises:
  • TypeError – if x does not implement __dlpack__ method

  • ValueError – 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)