Skip to main content

Layout

Struct Layout 

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

Describes the memory layout of a tensor: shape, strides, and start offset.

Strides are in units of elements (not bytes) and are signed — metadata-only view ops (notably Op::Flip) reverse iteration along a dim by negating that dim’s stride and adjusting start_offset, so a stride can be negative even though sizes can’t. A contiguous (row-major) tensor of shape [a, b, c] has strides [b*c, c, 1] (all positive); a Flip-on-dim-0 of that has stride [-b*c, c, 1] with start_offset set to the byte offset of the (originally) last element along dim 0.

start_offset is the byte offset of the layout’s first element in iteration order — by construction always non-negative, even for negative-stride layouts.

Implementations§

Source§

impl Layout

Source

pub fn new( shape: Shape, stride: SmallVec<[isize; 6]>, start_offset: usize, ) -> Layout

Creates a layout with explicit shape, strides, and start offset.

Source

pub fn contiguous_with_offset<S>(shape: S, start_offset: usize) -> Layout
where S: Into<Shape>,

Creates a contiguous (row-major) layout with the given start offset.

Source

pub fn contiguous<S>(shape: S) -> Layout
where S: Into<Shape>,

Creates a contiguous (row-major) layout with zero start offset.

Source

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

Returns the dimension sizes as a slice.

Source

pub fn dim<D>(&self, dim: D) -> Result<usize, Error>
where D: Dim,

Returns the size of a specific dimension.

Source

pub fn shape(&self) -> &Shape

Returns a reference to the Shape.

Source

pub fn has_dynamic(&self) -> bool

Whether the layout’s shape has any bounded-symbolic (Range) axis. Phase D step 1b.

Source

pub fn resolve(&self, env: &SymEnv) -> Result<Layout, Error>

Resolve the layout’s shape through env (every dynamic axis → concrete), keeping strides and offset unchanged: the physical walk of a fixed-capacity buffer is the same; only the logical live extent becomes concrete. Errors if a dynamic axis’s symbol is unbound. Phase D step 1b.

Source

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

Returns the strides as a slice (in elements, not bytes). Signed — see struct-level docs.

Source

pub fn stride_unsigned(&self) -> SmallVec<[usize; 6]>

Returns strides cast to usize, debug-asserting non-negative. Convenience for kernels that haven’t been updated to handle signed strides yet (most of them, since auto-Contiguize guarantees positive strides on kernel input today). One stack allocation via DimVec; effectively zero cost for rank ≤ 6.

Don’t use this if your kernel needs to support negative strides (e.g. is the consumer of an Op::Flip view): iterate via crate::StridedIndex which handles the sign correctly.

Source

pub fn start_offset(&self) -> usize

Returns the start offset into the underlying storage.

Source

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

Returns (start, end) offsets if the layout is contiguous, None otherwise.

Source

pub fn is_contiguous(&self) -> bool

Returns true if the layout is C-contiguous (row-major).

This does not require the start offset to be 0.

Source

pub fn is_fortran_contiguous(&self) -> bool

Returns true if the layout is Fortran-contiguous (column-major).

Source

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

Returns a narrowed layout selecting len elements starting at start on dimension dim.

Source

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

Returns a layout with two dimensions swapped.

Source

pub fn permute(&self, idxs: &[usize]) -> Result<Layout, Error>

Returns a layout with dimensions reordered according to idxs.

Source

pub fn unsqueeze(&self, dim: usize) -> Result<Layout, Error>

Returns a layout with a size-1 dimension inserted at position dim. dim must be in 0..=rank — dim == rank appends to the end. Pure metadata: the underlying bytes are unchanged.

Stride for the inserted size-1 axis is set to 0. This stride value never affects indexing (the axis only ever has coordinate 0, and 0 * stride == 0), so any value would be correct. Choosing 0 matches the convention used by broadcast_as for size-1 axes broadcast to larger sizes — keeps the convention uniform and Shape::is_contiguous stays unaffected (it already skips dims of size 1).

Source

pub fn squeeze(&self, dim: usize) -> Result<Layout, Error>

Drop a size-1 dimension at position dim. Inverse of Self::unsqueeze. The pruned dim’s stride is discarded — it’s irrelevant for indexing because the dim has size 1. Errors if dim is out of bounds or its size isn’t 1.

Source

pub fn flip(&self, dim: usize) -> Result<Layout, Error>

Returns a layout that iterates dim in reverse — pure metadata, zero copy. The stride at dim is negated; start_offset is shifted forward by (size-1) * |stride| so iteration starts at what was the last element along dim.

Size-0 and size-1 axes are no-ops (no element to flip; the new start_offset shift is 0). Successive flip calls on the same dim are involutive: stride re-negates, offset cancels, you get the original layout back.

Source

pub fn broadcast_as<S>(&self, shape: S) -> Result<Layout, Error>
where S: Into<Shape>,

Returns a layout broadcast to the given shape.

Dimensions of size 1 in self are stretched to match the target shape.

Source

pub fn strided_index(&self) -> StridedIndex<'_> ⓘ

Source

pub fn strided_blocks(&self) -> StridedBlocks<'_>

Source

pub fn offsets_b(&self) -> Option<ContiguousOffsetsWithBroadcast>

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<(), Error>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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

Checks if this value is equivalent to the given key. 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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more