Struct Layout
pub struct Layout { /* private fields */ }Expand description
Re-export core types. Layout describes how a tensor’s logical shape maps to flat storage.
Implementations§
§impl Layout
impl Layout
pub fn contiguous(shape: Shape) -> Layout
pub fn contiguous(shape: Shape) -> Layout
Create a new contiguous layout for the given shape. Strides are computed as row-major (C-order).
pub fn new(shape: Shape, strides: Vec<usize>, offset: usize) -> Layout
pub fn new(shape: Shape, strides: Vec<usize>, offset: usize) -> Layout
Create a layout with explicit strides and offset (for views).
pub fn shape(&self) -> &Shape
pub fn strides(&self) -> &[usize]
pub fn offset(&self) -> usize
pub fn rank(&self) -> usize
pub fn dims(&self) -> &[usize]
pub fn elem_count(&self) -> usize
pub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Check if this layout is contiguous (row-major, no gaps). A tensor is contiguous if its strides equal the default strides for its shape AND offset is 0.
pub fn transpose(&self, dim0: usize, dim1: usize) -> Result<Layout, Error>
pub fn transpose(&self, dim0: usize, dim1: usize) -> Result<Layout, Error>
Transpose two dimensions. Returns a new layout with swapped shape/strides. This is a “free” operation — no data is copied.
Example: [2, 3, 4] transpose(0, 2) → [4, 3, 2] strides [12, 4, 1] → [1, 4, 12]
pub fn narrow(
&self,
dim: usize,
start: usize,
len: usize,
) -> Result<Layout, Error>
pub fn narrow( &self, dim: usize, start: usize, len: usize, ) -> Result<Layout, Error>
Narrow (slice) along a dimension. Returns a new layout that is a view into the same storage with adjusted shape and offset.
Example: tensor of shape [4, 6], narrow(dim=1, start=2, len=3) → shape [4, 3], offset += 2 * stride[1]
pub fn flat_index(&self, index: &[usize]) -> usize
pub fn flat_index(&self, index: &[usize]) -> usize
Compute the flat index into storage for a given multi-dimensional index. This is the core formula: flat_index = offset + sum(index[i] * stride[i])
pub fn strided_indices(&self) -> StridedIter
pub fn strided_indices(&self) -> StridedIter
Iterator over all flat indices of this layout, in logical order. This handles non-contiguous layouts correctly by walking through multi-dimensional indices and converting via strides.
Trait Implementations§
impl Eq for Layout
impl StructuralPartialEq for Layout
Auto Trait Implementations§
impl Freeze for Layout
impl RefUnwindSafe for Layout
impl Send for Layout
impl Sync for Layout
impl Unpin for Layout
impl UnwindSafe for Layout
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
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