Skip to main content

Module ndarray

Module ndarray 

Source
Expand description

Conversion between DLPack and ndarray, this module requires the ndarray feature to be enabled.

The following conversions are supported:

  • DLPackTensor => ndarray::Array (makes a copy of the data)
  • DLPackTensor => ndarray::ArcArray (makes a copy of the data)
  • DLPackTensorRef => ndarray::ArrayView
  • DLPackTensorRefMut => ndarray::ArrayViewMut
  • ndarray::Array => DLPackTensor
  • &ndarray::Array => DLPackTensorRef
  • &mut ndarray::Array => DLPackTensorRefMut
  • ndarray::ArrayView => DLPackTensorRef
  • ndarray::ArrayViewMut => DLPackTensorRefMut
  • ndarray::ArcArray => DLPackTensor (share data, but creates a read-only DLPackTensor)
  • &ndarray::ArcArray => DLPackTensorRef

§Examples

use dlpk::{DLPackTensor, DLPackTensorRef};

let tensor: DLPackTensor = get_tensor_from_somewhere();

// makes a copy of the data
let array: ndarray::ArrayD<f32> = tensor.try_into().unwrap();

// no copy, share data with the original tensor
let tensor: DLPackTensor = get_tensor_from_somewhere();
let tensor_ref: DLPackTensorRef = tensor.as_ref();
let reference: ndarray::ArrayView2<f32> = tensor_ref.try_into().unwrap();

// convert an ndarray array into a DLPack tensor
let array = ndarray::Array::from_elem((2, 3), 1.0f32);
let tensor: DLPackTensor = array.clone().try_into().unwrap();

let tensor_ref: DLPackTensorRef = (&array).try_into().unwrap();

Modules§

sync
This module provides conversions between Arc<Mutex<Array<T, D>>> and Arc<RwLock<Array<T, D>>> to DLPackTensor, allowing you to share data between Rust and DLPack while ensuring that the data is not modified by Rust while it’s used by DLPack and reciprocally. The locks will be held until the DLPackTensor is dropped, ensuring safe access to the data.

Enums§

DLPackNDarrayError
Possible error causes when converting between ndarray and DLPack

Traits§

DimFromVec
Internal trait that will convert a Vec<usize> into one of ndarray’s Dim type.