Dlpack Module
Rust module: cuvs::dlpack
Source: rust/cuvs/src/dlpack.rs
DLPack tensor interop.
cuVS exchanges tensors with the C library through the DLPack ABI. This crate
never owns tensor storage: every entry point borrows a value that exposes a
view through the [AsDlTensor] / [AsDlTensorMut] traits:
- [
DLTensorView] — a read-only view, for inputs the C API only reads (datasets, queries). - [
DLTensorViewMut] — a writable view, for outputs the C API writes (neighbors, distances).
A view is non-owning. The traits take &self / &mut self, so the compiler
ties the view’s lifetime to the borrow of the value that owns the underlying
buffer. The view materializes a stack-local [DLManagedTensor] only for the
duration of each FFI call.
The crate ships no public tensor type. To hand your own GPU (or host) buffer
to cuVS, implement [AsDlTensor] / [AsDlTensorMut] for it on top of
[DLTensorView::from_raw_parts]. Most algorithms require search inputs and
outputs to live in device memory. See examples/cagra.rs for a complete,
runnable adapter built on the raw CUDA runtime.
ffi::{DLDataType, DLDataTypeCode, DLDevice, DLDeviceType, DLManagedTensor, DLTensor}
Source: rust/cuvs/src/dlpack.rs:34
AsDlTensor
Borrows a tensor as a read-only [DLTensorView] for tensor inputs.
Implement this for your own tensor type by calling
[DLTensorView::from_raw_parts] inside a small unsafe block, upholding its
safety contract.
Examples
A minimal adapter for a row-major matrix in device memory:
Source: rust/cuvs/src/dlpack.rs:79
AsDlTensorMut
Borrows a tensor as a writable [DLTensorViewMut] for tensor outputs.
In addition to the [DLTensorView::from_raw_parts] invariants, writable
adapters must guarantee exclusive access to the data region. The &mut self
receiver makes the compiler enforce that exclusivity for the borrow.
Source: rust/cuvs/src/dlpack.rs:88
DType
Maps a Rust element type to a DLPack [DLDataType].
Source: rust/cuvs/src/dlpack.rs:93
DLPackError
Error when converting an external tensor to a DLPack view.
Source: rust/cuvs/src/dlpack.rs:121
DLTensorView
A non-owning, read-only DLPack tensor view.
Methods
from_raw_parts
Construct a DLPack view from raw tensor metadata.
Safety
The caller must guarantee that:
datapoints to initialized storage matchingshape,strides, anddtype, residing on the device described bydevice;- that storage remains valid for the lifetime
'a; - the C API consumes the resulting [
DLManagedTensor] (including itsshape/stridespointers) only for the duration of the FFI call and does not retain it afterward — cuVS upholds this.
Source: rust/cuvs/src/dlpack.rs:176
ndim
Number of dimensions.
Source: rust/cuvs/src/dlpack.rs:226
shape
Shape of the tensor.
Source: rust/cuvs/src/dlpack.rs:231
strides
Strides, if non-contiguous. None means row-major contiguous.
Source: rust/cuvs/src/dlpack.rs:236
dtype
Element data type.
Source: rust/cuvs/src/dlpack.rs:241
device
Device where the data resides.
Source: rust/cuvs/src/dlpack.rs:246
Source: rust/cuvs/src/dlpack.rs:155
DLTensorViewMut
A non-owning, writable DLPack tensor view.
Methods
from_raw_parts
Construct a writable DLPack view from raw tensor metadata.
Safety
In addition to the [DLTensorView::from_raw_parts] invariants, the
caller must guarantee the storage is exclusively writable for 'a.
Source: rust/cuvs/src/dlpack.rs:274
Source: rust/cuvs/src/dlpack.rs:262