Struct Layout
pub struct Layout { /* private fields */ }Expand description
Layout describes how to interpret a linear buffer as an N-dimensional tensor.
Stores shape, strides (in elements, can be negative for flipped dimensions), and a start offset for views/slices.
Implementations§
§impl Layout
impl Layout
pub fn contiguous(shape: Shape) -> Layout
pub fn contiguous(shape: Shape) -> Layout
Create a new contiguous layout (row-major/C-order).
pub fn new(shape: Shape, strides: Vec<isize>, start_offset: usize) -> Layout
pub fn new(shape: Shape, strides: Vec<isize>, start_offset: usize) -> Layout
Create a layout with explicit strides.
pub fn start_offset(&self) -> usize
pub fn start_offset(&self) -> usize
The start offset for views/slices.
pub fn num_elements(&self) -> usize
pub fn num_elements(&self) -> usize
Total number of elements.
pub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Check if this layout is contiguous (row-major, positive strides).
pub fn is_dense_unique_storage(&self) -> bool
pub fn is_dense_unique_storage(&self) -> bool
Returns true if this layout represents contiguous, non-broadcast storage.
pub fn contiguous_offsets(&self) -> Option<(usize, usize)>
pub fn contiguous_offsets(&self) -> Option<(usize, usize)>
If contiguous, return (start, end) offsets for direct slice access.
pub fn transpose(&self, dim1: usize, dim2: usize) -> Layout
pub fn transpose(&self, dim1: usize, dim2: usize) -> Layout
Transpose: swap two dimensions (zero-copy, metadata only).
pub fn permute(&self, axes: &[usize]) -> Layout
pub fn permute(&self, axes: &[usize]) -> Layout
Permute: reorder dimensions according to axes (zero-copy, metadata only).
axes must be a permutation of 0..ndim.
pub fn flip(&self, axes: &[usize]) -> Layout
pub fn flip(&self, axes: &[usize]) -> Layout
Flip: reverse elements along specified axes (zero-copy, metadata only).
For each flipped axis, negates the stride and adjusts start_offset to point to the last element along that dimension.
pub fn narrow(&self, dim: usize, start: usize, len: usize) -> Layout
pub fn narrow(&self, dim: usize, start: usize, len: usize) -> Layout
Narrow/slice along a dimension (zero-copy, metadata only).
pub fn slice(&self, slices: &[Slice]) -> (Layout, bool)
pub fn slice(&self, slices: &[Slice]) -> (Layout, bool)
Apply slices to create a new layout.
Returns (new_layout, needs_copy):
needs_copy = false: Can use zero-copy view with new layoutneeds_copy = true: Has negative steps requiring data reordering
pub fn reshape(&self, new_shape: Shape) -> Option<Layout>
pub fn reshape(&self, new_shape: Shape) -> Option<Layout>
Reshape to a new shape. Only works if contiguous.
Preserves start_offset for zero-copy view of contiguous slices. Returns None if not contiguous (would require data copy).
pub fn inner_stride(&self) -> usize
pub fn inner_stride(&self) -> usize
Get stride of the innermost (last) dimension. Returns 1 for contiguous tensors, larger values for transposed. Returns absolute value (ignores flip).
pub fn has_contiguous_inner(&self) -> bool
pub fn has_contiguous_inner(&self) -> bool
Check if innermost dimension is contiguous (|stride| == 1). This enables efficient vectorized inner loops.
pub fn as_2d_strides(&self) -> Option<(usize, usize, isize, isize)>
pub fn as_2d_strides(&self) -> Option<(usize, usize, isize, isize)>
For 2D layouts, get (outer_size, inner_size, outer_stride, inner_stride). Returns None if not 2D.
pub fn has_positive_strides(&self) -> bool
pub fn has_positive_strides(&self) -> bool
Check if all strides are non-negative.
pub fn strided_blocks(&self) -> StridedBlocks<'_>
pub fn strided_blocks(&self) -> StridedBlocks<'_>
Compute strided blocks for efficient iteration.
Returns (block_len, num_blocks, block_stride) where:
- block_len: number of contiguous elements in each block
- num_blocks: total number of blocks
- block_stride: stride between consecutive blocks (0 if single block)
For contiguous tensors: single block covering all elements. For transposed/strided: multiple blocks of contiguous data.
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 UnsafeUnpin 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
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,
§impl<K, Q> Comparable<Q> for K
impl<K, Q> Comparable<Q> for K
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<K, Q> Equivalent<Q> for K
impl<K, Q> Equivalent<Q> for K
§fn equivalent(&self, key: &Q) -> bool
fn equivalent(&self, key: &Q) -> bool
key and return true if they are equal.impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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