from_dlpack — Python array API standard 2024.12 documentation (original) (raw)

from_dlpack(x: object, /, *, device: device | None = None, copy: bool | None = None) → array

Returns a new array containing the data from another (array) object with a __dlpack__ method.

Parameters:

Returns:

out (array) – an array containing the data in x.

Raises:

Notes

See array.__dlpack__() for implementation suggestions for from_dlpack in order to handle DLPack versioning correctly.

A way to move data from two array libraries to the same device (assumed supported by both libraries) in a library-agnostic fashion is illustrated below:

def func(x, y): xp_x = x.array_namespace() xp_y = y.array_namespace()

# Other functions than `from_dlpack` only work if both arrays are from the same library. So if
# `y` is from a different one than `x`, let's convert `y` into an array of the same type as `x`:
if not xp_x == xp_y:
    y = xp_x.from_dlpack(y, copy=True, device=x.device)

# From now on use `xp_x.xxxxx` functions, as both arrays are from the library `xp_x`
...

Changed in version 2023.12: Required exceptions to address unsupported use cases.

Changed in version 2023.12: Added device and copy support.