A pure Rust implementation of DLPack for zero-copy tensor exchange. It supports the legacy and versioned ABIs, Rust container adapters, the Python Array API protocol, and DLPack 1.3 C Exchange.
No features are enabled by default. Enable only the integrations you use:
cargo add dlpark --features "ndarray half"
cargo add dlpark --features "pyo3 image"
cargo add dlpark --features "cudarc"
cargo add dlpark --features "safetensors"A producer owns its container through the managed tensor's deleter. The versioned ABI is recommended for new code:
use dlpark::{allocation::dynamic, ffi::DLManagedTensorVersioned, versioned};
use ndarray::arr2;
let initialized: dynamic::Initialized<DLManagedTensorVersioned> =
Box::new(arr2(&[[1_i32, 2, 3], [4, 5, 6]])).try_into()?;
let dlpack: versioned::Dlpack = unsafe { initialized.finish() };
let tensor = dlpack.validate()?;
assert_eq!(tensor.shape(), &[2, 3]);
assert_eq!(unsafe { tensor.cpu_slice::<i32>()? }, &[1, 2, 3, 4, 5, 6]);
# Ok::<(), Box<dyn std::error::Error>>(())finish is unsafe because the caller must ensure that the completed
descriptor obeys the DLPack lifetime, pointer, layout, and flag requirements.
Imported descriptors should be checked with Managed::validate() before their
metadata is used.
| Feature | Integration | Data movement |
|---|---|---|
pyo3 |
Python capsule protocol and C Exchange API | zero-copy protocol layer |
image |
ImageBuffer producer and consumer |
zero-copy |
ndarray |
owned arrays and borrowed views | zero-copy |
half |
f16 and bf16 element types |
— |
candle |
CPU Tensor |
zero-copy export, copying import |
cudarc |
CudaSlice<T> |
zero-copy |
safetensors |
file/mmap export and serialization views | zero-copy |
Raw CUDA and Metal runtime policy remains application-specific. The
demos/cuda-python and demos/metal-python projects show complete native
buffer implementations.
- Ownership, validation, metadata, and versioning
- Python import, reusable export, streams, and C Exchange
- Interop backend behavior
- Development and binding regeneration
Runnable demos live under demos/:
cuda-python: CuPy and Torch exchange through a Rust-owned CUDA buffer.metal-python: MLX exchange through a sharedMTLBuffer.image-python: Torch exchange withimage::RgbImage.ndarray-candle: anndarray → DLPack → candle → DLPack → ndarrayround trip.
The API reference is available on docs.rs.