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
impl Layout
Sourcepub fn new(
shape: Shape,
stride: SmallVec<[isize; 6]>,
start_offset: usize,
) -> Layout
pub fn new( shape: Shape, stride: SmallVec<[isize; 6]>, start_offset: usize, ) -> Layout
Creates a layout with explicit shape, strides, and start offset.
Sourcepub fn contiguous_with_offset<S>(shape: S, start_offset: usize) -> Layout
pub fn contiguous_with_offset<S>(shape: S, start_offset: usize) -> Layout
Creates a contiguous (row-major) layout with the given start offset.
Sourcepub fn contiguous<S>(shape: S) -> Layout
pub fn contiguous<S>(shape: S) -> Layout
Creates a contiguous (row-major) layout with zero start offset.
Sourcepub fn dim<D>(&self, dim: D) -> Result<usize, Error>where
D: Dim,
pub fn dim<D>(&self, dim: D) -> Result<usize, Error>where
D: Dim,
Returns the size of a specific dimension.
Sourcepub fn has_dynamic(&self) -> bool
pub fn has_dynamic(&self) -> bool
Whether the layout’s shape has any bounded-symbolic (Range) axis.
Phase D step 1b.
Sourcepub fn resolve(&self, env: &SymEnv) -> Result<Layout, Error>
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.
Sourcepub fn stride(&self) -> &[isize]
pub fn stride(&self) -> &[isize]
Returns the strides as a slice (in elements, not bytes). Signed — see struct-level docs.
Sourcepub fn stride_unsigned(&self) -> SmallVec<[usize; 6]>
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.
Sourcepub fn start_offset(&self) -> usize
pub fn start_offset(&self) -> usize
Returns the start offset into the underlying storage.
Sourcepub fn contiguous_offsets(&self) -> Option<(usize, usize)>
pub fn contiguous_offsets(&self) -> Option<(usize, usize)>
Returns (start, end) offsets if the layout is contiguous, None otherwise.
Sourcepub fn is_contiguous(&self) -> bool
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.
Sourcepub fn is_fortran_contiguous(&self) -> bool
pub fn is_fortran_contiguous(&self) -> bool
Returns true if the layout is Fortran-contiguous (column-major).
Sourcepub 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>
Returns a narrowed layout selecting len elements starting at start on dimension dim.
Sourcepub fn transpose(&self, dim1: usize, dim2: usize) -> Result<Layout, Error>
pub fn transpose(&self, dim1: usize, dim2: usize) -> Result<Layout, Error>
Returns a layout with two dimensions swapped.
Sourcepub fn permute(&self, idxs: &[usize]) -> Result<Layout, Error>
pub fn permute(&self, idxs: &[usize]) -> Result<Layout, Error>
Returns a layout with dimensions reordered according to idxs.
Sourcepub fn unsqueeze(&self, dim: usize) -> Result<Layout, Error>
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).
Sourcepub fn squeeze(&self, dim: usize) -> Result<Layout, Error>
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.
Sourcepub fn flip(&self, dim: usize) -> Result<Layout, Error>
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.
Sourcepub fn broadcast_as<S>(&self, shape: S) -> Result<Layout, Error>
pub fn broadcast_as<S>(&self, shape: S) -> Result<Layout, Error>
Returns a layout broadcast to the given shape.
Dimensions of size 1 in self are stretched to match the target shape.
pub fn strided_index(&self) -> StridedIndex<'_> ⓘ
pub fn strided_blocks(&self) -> StridedBlocks<'_>
pub fn offsets_b(&self) -> Option<ContiguousOffsetsWithBroadcast>
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,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§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