SunDoge/dlpark
Acerca de
Pure Rust DLPack implementation for zero-copy tensor interoperability
IA y aprendizaje automáticoWhat you can do
- 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 - —
Crecimiento de estrellas medido
- in 24h
- 0
- in 7 days
- -1
- in 15 days
- 0
- in 1 month
- +1
Cambios netos en períodos que se solapan, no totales diarios. Los datos no disponibles se dejan en blanco. Las estrellas indican interés, no calidad.
README.md
Originaldlpark
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:
The core library supports Rust 1.88 and later. Optional integrations may require a newer compiler.
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:
cargo add dlpark --features ndarray
cargo add [email protected]
use dlpark::{allocation::dynamic, ffi::DLManagedTensorVersioned, versioned};
use ndarray::arr2;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let initialized: dynamic::Initialized<DLManagedTensorVersioned> =
Box::new(arr2(&[[1_i32, 2, 3], [4, 5, 6]])).try_into()?;
// SAFETY: the ndarray adapter supplies the descriptor and its owner.
// No fields have been overridden.
let dlpack: versioned::Dlpack = unsafe { initialized.finish() };
let tensor = dlpack.validate()?;
assert_eq!(tensor.shape(), &[2, 3]);
// SAFETY: the owned CPU array remains alive and is not mutated.
let values = unsafe { tensor.cpu_slice::<i32>()? };
assert_eq!(values, &[1, 2, 3, 4, 5, 6]);
Ok(())
}
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.
Used by
Guides
- 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.
Explorar proyectos relacionados
Cargando…