Skip to main content

Layout

Struct Layout 

Source
pub struct Layout { /* private fields */ }
Expand description

Layout describes how a tensor’s logical shape maps to flat storage.

Implementations§

Source§

impl Layout

Source

pub fn contiguous(shape: Shape) -> Self

Create a new contiguous layout for the given shape. Strides are computed as row-major (C-order).

Source

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

Create a layout with explicit strides and offset (for views).

Source

pub fn shape(&self) -> &Shape

Source

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

Source

pub fn offset(&self) -> usize

Source

pub fn rank(&self) -> usize

Source

pub fn dims(&self) -> &[usize]

Source

pub fn elem_count(&self) -> usize

Source

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.

Source

pub fn transpose(&self, dim0: usize, dim1: usize) -> Result<Layout>

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]

Source

pub fn narrow(&self, dim: usize, start: usize, len: usize) -> Result<Layout>

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]

Source

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

Source

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§

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 Eq for Layout

Source§

impl PartialEq for Layout

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
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<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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.