Skip to content

Repository files navigation

dlpark

Tests Clippy Miri Crates.io docs.rs

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.

Installation

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"

Quick start

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.

Features

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.

Guides

Runnable demos live under demos/:

  • cuda-python: CuPy and Torch exchange through a Rust-owned CUDA buffer.
  • metal-python: MLX exchange through a shared MTLBuffer.
  • image-python: Torch exchange with image::RgbImage.
  • ndarray-candle: an ndarray → DLPack → candle → DLPack → ndarray round trip.

The API reference is available on docs.rs.

About

Pure Rust DLPack implementation for zero-copy tensor interoperability

Topics

Resources

Stars

63 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages