Skip to main content

TensorTrait

Trait TensorTrait 

Source
pub trait TensorTrait<T>: Send + Sync
where T: Num + Clone + Debug,
{
Show 19 methods // Required methods fn new(shape: &[usize], name: Option<&str>) -> Result<Self> where Self: Sized; fn from_fd(fd: OwnedFd, shape: &[usize], name: Option<&str>) -> Result<Self> where Self: Sized; fn clone_fd(&self) -> Result<OwnedFd>; fn memory(&self) -> TensorMemory; fn name(&self) -> String; fn shape(&self) -> &[usize]; fn reshape(&mut self, shape: &[usize]) -> Result<()>; fn map_with(&self, access: CpuAccess) -> Result<TensorMap<T>>; fn buffer_identity(&self) -> &BufferIdentity; // Provided methods fn len(&self) -> usize { ... } fn is_empty(&self) -> bool { ... } fn size(&self) -> usize { ... } fn capacity_bytes(&self) -> usize { ... } fn set_logical_shape(&mut self, shape: &[usize]) -> Result<()> { ... } fn map(&self) -> Result<TensorMap<T>> { ... } fn map_read(&self) -> Result<TensorMap<T>> { ... } fn map_write(&self) -> Result<TensorMap<T>> { ... } fn map_mut(&self) -> Result<TensorMap<T>> { ... } fn view(&self, offset_bytes: usize, shape: &[usize]) -> Result<Self> where Self: Sized { ... }
}

Required Methods§

Source

fn new(shape: &[usize], name: Option<&str>) -> Result<Self>
where Self: Sized,

Create a new tensor with the given shape and optional name. If no name is given, a random name will be generated.

Source

fn from_fd(fd: OwnedFd, shape: &[usize], name: Option<&str>) -> Result<Self>
where Self: Sized,

Create a new tensor using the given file descriptor, shape, and optional name. If no name is given, a random name will be generated.

On Linux: Inspects the fd to determine DMA vs SHM based on device major/minor. On other Unix (macOS): Always creates SHM tensor.

Source

fn clone_fd(&self) -> Result<OwnedFd>

Clone the file descriptor associated with this tensor.

Source

fn memory(&self) -> TensorMemory

Get the memory type of this tensor.

Source

fn name(&self) -> String

Get the name of this tensor.

Source

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

Get the shape of this tensor.

Source

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

Reshape this tensor to the given shape. The total number of elements must remain the same.

Source

fn map_with(&self, access: CpuAccess) -> Result<TensorMap<T>>

Map the tensor into memory with the given access direction and return a TensorMap for accessing the data.

access selects the platform mapping mode (read-only IOSurface lock, dma-buf sync direction, AHardwareBuffer lock usage) and the map’s mutability: a map obtained with CpuAccess::Read rejects as_mut_slice. CpuAccess::None is not a mappable direction and returns Error::InvalidArgument.

Prefer the typed wrappers map_read / map_write / map_mut.

Source

fn buffer_identity(&self) -> &BufferIdentity

Get the buffer identity for cache keying and liveness tracking.

Provided Methods§

Source

fn len(&self) -> usize

Get the number of elements in this tensor.

Source

fn is_empty(&self) -> bool

Check if the tensor is empty.

Source

fn size(&self) -> usize

Get the size in bytes of this tensor.

Source

fn capacity_bytes(&self) -> usize

Bytes of the underlying allocation (>= the current logical size()). Defaults to the logical size for storages without spare capacity.

Source

fn set_logical_shape(&mut self, shape: &[usize]) -> Result<()>

Set the logical shape to any shape whose byte size fits the allocation capacity, without the equal-size constraint of reshape.

Source

fn map(&self) -> Result<TensorMap<T>>

Map the tensor read-write (equivalent to map_with(CpuAccess::ReadWrite) — the historical map() behavior).

Source

fn map_read(&self) -> Result<TensorMap<T>>

Map the tensor for CPU reading only. The returned map rejects as_mut_slice; on macOS this takes the read-only IOSurface lock (skips the unlock flush), on Linux the dma-buf read-direction sync.

Source

fn map_write(&self) -> Result<TensorMap<T>>

Map the tensor for CPU writing (fill-only: reading through a write map may see write-combined memory — do not read the slice).

Source

fn map_mut(&self) -> Result<TensorMap<T>>

Map the tensor read-write (alias of map with the intent spelled out).

Source

fn view(&self, offset_bytes: usize, shape: &[usize]) -> Result<Self>
where Self: Sized,

Create a zero-copy sub-region view of this backing that shares the underlying allocation and BufferIdentity.

The window is [offset_bytes, offset_bytes + shape.product() * size_of::<T>()) measured from this tensor’s own logical start, so a sub-view of a sub-view composes by adding offsets. Sharing the parent’s identity is the contract that lets identity-keyed caches (e.g. the GL EGLImage import cache) treat offset-distinct windows as one buffer rather than unrelated allocations — view must never mint a fresh identity.

Defaults to Error::NotImplemented; every backend that supports sub-views overrides it (Mem, Shm, Linux DMA, macOS IOSurface, Pbo).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T> TensorTrait<T> for PboTensor<T>
where T: Num + Clone + Debug + Send + Sync,

Source§

impl<T> TensorTrait<T> for Tensor<T>
where T: Num + Clone + Debug + Send + Sync,