Expand description
§pyo3-dlpack
Zero-copy DLPack tensor interop for PyO3.
This crate provides a safe and ergonomic way to exchange tensor data between Rust and Python ML frameworks (PyTorch, JAX, TensorFlow, CuPy, etc.) using the DLPack protocol.
§Features
- Zero-copy: Tensors are shared directly without copying data
- PyO3 0.23+: Uses the modern
IntoPyObjecttrait (no deprecation warnings) - Bidirectional: Import tensors from Python and export tensors to Python
- Device-agnostic: Works with CPU, CUDA, ROCm, and other devices
§Example: Importing a PyTorch tensor
ⓘ
use pyo3::prelude::*;
use pyo3_dlpack::PyTensor;
#[pyfunction]
fn process_tensor(tensor: PyTensor) -> PyResult<()> {
// Access tensor metadata
println!("Shape: {:?}", tensor.shape());
println!("Device: {:?}", tensor.device());
// Get the raw data pointer (for GPU tensors, this is a device pointer)
let ptr = tensor.data_ptr();
Ok(())
}§Example: Exporting a tensor to Python
ⓘ
use pyo3::prelude::*;
use pyo3_dlpack::{ExportConfig, IntoDLPack};
struct MyGpuTensor {
ptr: *mut f32,
shape: Vec<i64>,
device_id: i32,
}
impl IntoDLPack for MyGpuTensor {
// ... implement the trait
}
#[pyfunction]
fn create_tensor(py: Python<'_>) -> PyResult<PyObject> {
let tensor = MyGpuTensor { /* ... */ };
tensor.into_dlpack(py)
}Structs§
- DLData
Type - Data type descriptor specifying the element type of a tensor.
- DLDevice
- A device descriptor specifying where tensor data resides.
- DLManaged
Tensor - A managed tensor with ownership semantics.
- DLTensor
- The core DLTensor structure describing a tensor’s data and layout.
- PyTensor
- A tensor imported from Python via the DLPack protocol.
- Tensor
Info - Information about a tensor for DLPack export.
Enums§
- DLData
Type Code - Data type codes as defined by DLPack.
- DLDevice
Type - Device type codes as defined by DLPack.
Constants§
- DLPACK_
CAPSULE_ NAME - The DLPack capsule name for tensor exchange
- DLPACK_
CAPSULE_ NAME_ USED - The DLPack capsule name after consumption (to prevent double-free)
Traits§
- IntoDL
Pack - Trait for types that can be exported as DLPack tensors.
Functions§
- cpu_
device - Create a DLDevice for CPU.
- cuda_
device - Create a DLDevice for CUDA with the specified device ID.
- dtype_
bf16 - Create a DLDataType for bf16 (bfloat16).
- dtype_
bool - Create a DLDataType for bool.
- dtype_
f16 - Create a DLDataType for f16 (half precision float).
- dtype_
f32 - Create a DLDataType for f32 (single precision float).
- dtype_
f64 - Create a DLDataType for f64 (double precision float).
- dtype_
i8 - Create a DLDataType for i8 (signed 8-bit integer).
- dtype_
i16 - Create a DLDataType for i16 (signed 16-bit integer).
- dtype_
i32 - Create a DLDataType for i32 (signed 32-bit integer).
- dtype_
i64 - Create a DLDataType for i64 (signed 64-bit integer).
- dtype_
u8 - Create a DLDataType for u8 (unsigned 8-bit integer).
- dtype_
u16 - Create a DLDataType for u16 (unsigned 16-bit integer).
- dtype_
u32 - Create a DLDataType for u32 (unsigned 32-bit integer).
- dtype_
u64 - Create a DLDataType for u64 (unsigned 64-bit integer).
- metal_
device - Create a DLDevice for Metal (Apple GPU) with the specified device ID.