Skip to content
المشاريع

حول

Pure Rust DLPack implementation for zero-copy tensor interoperability

الذكاء الاصطناعي وتعلّم الآلة

كيف تُقاس هذه الأرقام

ما يمكنك فعله

  • 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 - —

النمو المقاس للنجوم

خلال 24 ساعة
٠
خلال 7 أيام
؜-١
خلال 15 يومًا
٠
خلال شهر
+١

تغيّر صافي النجوم خلال فترات متداخلة، وليس مجموعًا يوميًا. تبقى القياسات المفقودة فارغة. النجوم تقيس الاهتمام لا جودة البرمجيات.

README.md

الأصل

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:

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

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.

استكشف مشاريع مشابهة ومرتبطة

جارٍ التحميل…