dlpark
A pure Rust implementation of dmlc/dlpack.
This implementation focuses on transferring tensor from Rust to Python and vice versa.
What is DLPack?
DLPack is a common in-memory tensor structure that enables sharing tensor data between different deep learning frameworks. It provides a standardized way to exchange tensor data without copying, making it efficient for framework interoperability.
Key features of DLPack:
- Zero-copy tensor sharing between frameworks
- Support for various data types and devices (CPU, GPU, etc.)
- Memory management through deleter functions
- Versioned ABI for compatibility
Implementation Details
Versioning
The library implements both legacy and versioned DLPack structures:
SafeManagedTensor: Legacy implementation without versioningSafeManagedTensorVersioned: Versioned implementation (current standard) with:- Major version: 1
- Minor version: 1
- Additional flags for tensor properties (read-only, copied, sub-byte type padding)
Safe Abstractions
The library provides safe Rust abstractions over the C-style DLPack structures:
-
SafeManagedTensorandSafeManagedTensorVersioned:- Safe wrappers around raw
DLPacktensors - RAII-style memory management
- Automatic cleanup through deleter functions
- Safe conversion between different tensor types
- Safe wrappers around raw
-
Key Features:
- Memory safety through Rust's ownership system
- Zero-cost abstractions
- Support for various tensor types (ndarray, image, standard containers)
- Python interoperability through PyO3
Features
| Feature | Description | Status |
|---|---|---|
pyo3 |
Enable Python bindings with pyo3 | ✅ |
image |
Enable image support | ✅ |
ndarray |
Enable ndarray support | ✅ |
Quick Start
We provide a simple example of how to transfer image::RgbImage to Python and torch.Tensor to Rust.
Usage Examples
Converting between Rust and Python
use *;
// Rust to Python
// Python to Rust
Working with ndarray
use *;
use ArrayD;
let arr = from_shape_vec?;
let tensor = new?;
let view = try_from?;
Image Processing
use *;
use ;
let img = from_vec?;
let tensor = new?;
let img2 = try_from?;