Skip to main content

from_dlpack

Function from_dlpack 

Source
pub fn from_dlpack(
    py: Python<'_>,
    capsule: &Bound<'_, PyAny>,
) -> PyResult<Py<PyAny>>
Expand description

Convert a DLPack capsule (from PyTorch, JAX, CuPy, TensorFlow, …) into a scirs2 NumPy-compatible array.

§Parameters

capsule : PyCapsule A PyCapsule object whose name is "dltensor". Anything that implements __dlpack__() can produce such an object.

§Returns

numpy.ndarray A NumPy array (matching the source tensor’s shape and dimensionality) whose contents are copied from the DLPack tensor. CPU tensors of dtype int8/16/32/64, uint8/16/32/64, float32, or float64 are supported; all other dtypes raise TypeError.

§Notes

GPU tensors raise TypeError until an optional gpu feature is enabled. The capsule is renamed to "used_dltensor" after consumption to prevent double-frees, consistent with the DLPack 1.0 spec.

Stride handling: the source tensor is not assumed to be C-contiguous. A non-null strides field (e.g. from a transposed PyTorch tensor’s t().__dlpack__(), a reversed/negative-stride view, or any other sliced/strided view) is read using genuine N-dimensional strided iteration, so the copied data is always logically correct regardless of the producer’s physical memory layout. A null strides field is treated as the DLPack-spec default of C-contiguous.