Skip to main content

Layout

Struct Layout 

Source
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§

Source§

impl Layout

Source

pub fn contiguous(shape: Shape) -> Self

Create a new contiguous layout (row-major/C-order).

Source

pub fn new(shape: Shape, strides: Vec<isize>, start_offset: usize) -> Self

Create a layout with explicit strides.

Source

pub fn shape(&self) -> &Shape

The shape of the tensor.

Source

pub fn strides(&self) -> &[isize]

The strides in elements (can be negative for flipped dimensions).

Source

pub fn start_offset(&self) -> usize

The start offset for views/slices.

Source

pub fn num_dims(&self) -> usize

Number of dimensions.

Source

pub fn num_elements(&self) -> usize

Total number of elements.

Source

pub fn is_contiguous(&self) -> bool

Check if this layout is contiguous (row-major, positive strides).

Source

pub fn contiguous_offsets(&self) -> Option<(usize, usize)>

If contiguous, return (start, end) offsets for direct slice access.

Source

pub fn transpose(&self, dim1: usize, dim2: usize) -> Self

Transpose: swap two dimensions (zero-copy, metadata only).

Source

pub fn permute(&self, axes: &[usize]) -> Self

Permute: reorder dimensions according to axes (zero-copy, metadata only).

axes must be a permutation of 0..ndim.

Source

pub fn flip(&self, axes: &[usize]) -> Self

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.

Source

pub fn narrow(&self, dim: usize, start: usize, len: usize) -> Self

Narrow/slice along a dimension (zero-copy, metadata only).

Source

pub fn slice(&self, slices: &[Slice]) -> (Self, bool)

Apply slices to create a new layout.

Returns (new_layout, needs_copy):

  • needs_copy = false: Can use zero-copy view with new layout
  • needs_copy = true: Has negative steps requiring data reordering
Source

pub fn reshape(&self, new_shape: Shape) -> Option<Self>

Reshape to a new shape. Only works if contiguous with zero offset.

Returns None if not contiguous or has non-zero offset (would require data copy).

Source

pub fn index(&self, indices: &[usize]) -> usize

Compute linear index from multi-dimensional indices.

Source

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).

Source

pub fn has_contiguous_inner(&self) -> bool

Check if innermost dimension is contiguous (|stride| == 1). This enables efficient vectorized inner loops.

Source

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.

Source

pub fn has_positive_strides(&self) -> bool

Check if all strides are non-negative.

Source

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§

Source§

impl Clone for Layout

Source§

fn clone(&self) -> Layout

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Layout

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Layout

Source§

fn eq(&self, other: &Layout) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Layout

Source§

impl StructuralPartialEq for Layout

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.