pub struct Tensor {
pub dtype: DataType,
pub shape: Vec<usize>,
pub layout: TensorLayout,
/* private fields */
}Expand description
An owned, host-resident, device-aware tensor (§5, §20.2).
Owns the DeviceBuffer that holds its elements and the EP that must free
it. On Phase-1 CPU the buffer is a host allocation, so Tensor::as_bytes
and the typed accessors read it directly; the design leaves room for
non-host devices (the accessors gate on host accessibility).
Fields§
§dtype: DataTypeElement type.
shape: Vec<usize>Logical shape (static dims).
layout: TensorLayoutPhysical layout of Tensor::buffer. Row-major contiguous for tensors
this crate produces.
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn from_raw(
dtype: DataType,
shape: Vec<usize>,
bytes: &[u8],
) -> Result<Self, SessionError>
pub fn from_raw( dtype: DataType, shape: Vec<usize>, bytes: &[u8], ) -> Result<Self, SessionError>
Build a tensor from raw little-endian bytes on the shared CPU device.
Sourcepub fn from_f32(shape: &[usize], data: &[f32]) -> Result<Self, SessionError>
pub fn from_f32(shape: &[usize], data: &[f32]) -> Result<Self, SessionError>
Build an f32 tensor from a dense row-major slice.
Sourcepub fn from_i64(shape: &[usize], data: &[i64]) -> Result<Self, SessionError>
pub fn from_i64(shape: &[usize], data: &[i64]) -> Result<Self, SessionError>
Build an i64 tensor from a dense row-major slice.
Sourcepub fn from_borrowed_parts_with_guard(
allocator: Arc<dyn ExecutionProvider>,
dtype: DataType,
shape: Vec<usize>,
layout: TensorLayout,
buffer: DeviceBuffer,
guard: Box<dyn Any + Send + Sync>,
) -> Self
pub fn from_borrowed_parts_with_guard( allocator: Arc<dyn ExecutionProvider>, dtype: DataType, shape: Vec<usize>, layout: TensorLayout, buffer: DeviceBuffer, guard: Box<dyn Any + Send + Sync>, ) -> Self
Wrap foreign, borrowed memory in a Tensor, with an opaque guard
that releases the foreign allocation when the tensor is dropped.
This is the zero-copy import constructor: buffer must be a
borrowed DeviceBuffer (built via
DeviceBuffer::from_borrowed_parts) aliasing memory owned by whatever
guard boxes up — for a DLPack import, guard owns the foreign
DLManagedTensor and its Drop calls that tensor’s deleter exactly
once. Because the buffer is borrowed, the owning EP’s deallocate is a
no-op for it, so the only thing that frees the aliased memory is the
guard.
§Ordering invariant
Drop deallocates buffer (a no-op for a borrowed buffer) and only
then drops the guard, so the guard’s deleter never runs while the
buffer still aliases the foreign memory. Do not rely on the guard freeing
anything the buffer still points at before drop completes.
§Panics
Panics (debug builds) if buffer is not borrowed — an owned buffer here
would be double-freed (once by the EP, once by the guard).
Sourcepub fn device_ptr(&self) -> *const c_void
pub fn device_ptr(&self) -> *const c_void
Base pointer of this tensor’s backing allocation.
For host-accessible devices (CPU, MLX) this is a dereferenceable host
pointer; for device memory (CUDA/ROCm) it is an opaque device address
only meaningful inside the owning EP’s context — never dereference it on
the host. This is the device-agnostic base the zero-copy DLPack export
path hands to a consumer, so a CUDA-resident output can be borrowed as a
kDLCUDA tensor without a host round-trip. Returns null for an empty
(zero-element) tensor.
Sourcepub fn sync(&self) -> Result<(), SessionError>
pub fn sync(&self) -> Result<(), SessionError>
Block until all pending work on the owning EP’s stream completes.
Device-agnostic: the CPU EP’s sync is a no-op, while the CUDA EP fully
synchronizes its compute stream. The DLPack export path calls this
before handing a kDLCUDA buffer to a foreign consumer, so the producer’s
device work is guaranteed complete (and thus the data valid) regardless of
which stream the consumer reads on — the conservative, always-correct end
of the DLPack stream handshake.
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Borrow the raw little-endian element bytes (host tensors only).
Sourcepub fn to_vec_f32(&self) -> Vec<f32>
pub fn to_vec_f32(&self) -> Vec<f32>
Copy out the elements as f32. Panics if the dtype is not Float32.
Sourcepub fn to_vec_i64(&self) -> Vec<i64>
pub fn to_vec_i64(&self) -> Vec<i64>
Copy out the elements as i64. Panics if the dtype is not Int64.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Tensor
impl !UnwindSafe for Tensor
impl Freeze for Tensor
impl Send for Tensor
impl Sync for Tensor
impl Unpin for Tensor
impl UnsafeUnpin for Tensor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more