Skip to main content

TensorLike

Trait TensorLike 

Source
pub trait TensorLike:
    Send
    + Sync
    + Debug {
Show 17 methods // Required methods fn as_any(&self) -> &dyn Any; fn shape(&self) -> &[usize]; fn dtype(&self) -> DataType; fn device(&self) -> Device; fn is_contiguous(&self) -> bool; fn view(&self, start: &[usize], end: &[usize]) -> Result<TensorRef>; fn reshape(&self, shape: &[usize]) -> Result<TensorRef>; fn to_cpu(&self) -> Result<TensorRef>; fn to_device(&self, device: &Device) -> Result<TensorRef>; fn to_dtype(&self, dtype: DataType) -> Result<TensorRef>; // Provided methods fn numel(&self) -> usize { ... } fn ndim(&self) -> usize { ... } fn is_scalar(&self) -> bool { ... } fn size_bytes(&self) -> usize { ... } fn to_vec_f32(&self) -> Result<Vec<f32>> { ... } fn to_vec_u32(&self) -> Result<Vec<u32>> { ... } fn argmax_last_dim_u32(&self) -> Result<u32> { ... }
}
Expand description

Core tensor trait for zero-copy, device-aware operations

Required Methods§

Source

fn as_any(&self) -> &dyn Any

Downcast support for backend-specific fast paths

Source

fn shape(&self) -> &[usize]

Get tensor shape

Source

fn dtype(&self) -> DataType

Get tensor data type

Source

fn device(&self) -> Device

Get device where tensor resides

Source

fn is_contiguous(&self) -> bool

Check if tensor is contiguous in memory

Source

fn view(&self, start: &[usize], end: &[usize]) -> Result<TensorRef>

Create a view/slice of this tensor

Source

fn reshape(&self, shape: &[usize]) -> Result<TensorRef>

Reshape tensor to new shape (must have same number of elements)

Source

fn to_cpu(&self) -> Result<TensorRef>

Convert tensor to CPU device

Source

fn to_device(&self, device: &Device) -> Result<TensorRef>

Convert tensor to specific device

Source

fn to_dtype(&self, dtype: DataType) -> Result<TensorRef>

Convert tensor to specific data type

Provided Methods§

Source

fn numel(&self) -> usize

Get total number of elements

Source

fn ndim(&self) -> usize

Get number of dimensions

Source

fn is_scalar(&self) -> bool

Check if tensor is scalar (0-dimensional)

Source

fn size_bytes(&self) -> usize

Get size in bytes for this tensor

Source

fn to_vec_f32(&self) -> Result<Vec<f32>>

Extract tensor data as Vec (for logits sampling) This is a convenience method for backends that need to extract data

Source

fn to_vec_u32(&self) -> Result<Vec<u32>>

Extract tensor data as Vec (for token IDs) This is a convenience method for backends that need to extract token data

Source

fn argmax_last_dim_u32(&self) -> Result<u32>

Fast path: argmax over the last dimension, returning the selected token id.

Backends may override this to avoid transferring full logits to CPU.

Implementors§