pub struct Shape { /* private fields */ }Expand description
A shape represents the dimensions of a tensor.
Internally backed by a SmallVec that stores up to 6 dimensions
on the stack without heap allocation. A scalar has rank 0 (no dimensions), a vector has
rank 1, a matrix has rank 2, and so on.
Shape can be created from tuples, slices, Vec<usize>, or the Shape::from_dims
constructor.
dims holds the per-axis bounds (a Scalar’s value, or a Range’s
max/capacity); dynamic sparsely marks which axes are bounded-symbolic.
dims() borrows dims unchanged; the symbolic view is extent(). The
common all-concrete shape has dynamic: None. Equality includes dynamic
(a symbolic shape is distinct from a concrete one with the same bounds), so
DynAxis entries are kept sorted by axis.
Implementations§
Source§impl Shape
impl Shape
Sourcepub fn rank(&self) -> usize
pub fn rank(&self) -> usize
The rank is the number of dimensions, 0 for a scalar value, 1 for a vector, etc.
Sourcepub fn into_dims(self) -> Vec<usize>
pub fn into_dims(self) -> Vec<usize>
Consumes the shape and returns its dimensions as a Vec<usize>.
Sourcepub fn dims(&self) -> &[usize]
pub fn dims(&self) -> &[usize]
The dimensions as a slice of usize — the per-axis bounds (a
scalar’s value, or a dynamic axis’s capacity/max). Unchanged contract:
this is what sizing, striding, and iteration want. The live value of a
dynamic axis is obtained separately via Shape::extent +
Extent::resolve (or Shape::resolve); it is never folded in here.
Sourcepub fn extent(&self, axis: usize) -> Extent
pub fn extent(&self, axis: usize) -> Extent
The Extent of axis: a Range if the axis is dynamic (carrying its
min, the dims() bound as max, and its sym), else a Scalar of
the bound. The symbolic view over dims(). Phase D step 1b.
Sourcepub fn has_dynamic(&self) -> bool
pub fn has_dynamic(&self) -> bool
Whether any axis is a bounded-symbolic Range.
Sourcepub fn with_dynamic_axis(self, axis: usize, min: usize, sym: SymId) -> Shape
pub fn with_dynamic_axis(self, axis: usize, min: usize, sym: SymId) -> Shape
Mark axis as a bounded-symbolic Range [min, dims[axis]] resolved
via sym. dims()[axis] stays the capacity bound; extent(axis) now
reports the Range. Replaces any prior dynamic mark on axis. The
KV-cache length axis uses this; K-length and V-length pass the same
sym to unify.
Sourcepub fn resolve(&self, env: &SymEnv) -> Result<Shape, Error>
pub fn resolve(&self, env: &SymEnv) -> Result<Shape, Error>
Resolve every dynamic axis through env, returning a fully-concrete
Shape (all Scalar). Reads env at call time, so it always reflects
the current bindings; never caches. Errors if a dynamic axis’s symbol
is unbound. A shape with no dynamic axes resolves to a clone.
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,
The dimension size for a specified dimension index.
Supports both forward indexing (usize) and backward indexing via D.
Sourcepub fn elem_count(&self) -> usize
pub fn elem_count(&self) -> usize
The total number of elements, this is the product of all dimension sizes.
For a scalar (rank 0), the element count is 1.
Sourcepub fn stride_contiguous(&self) -> SmallVec<[isize; 6]>
pub fn stride_contiguous(&self) -> SmallVec<[isize; 6]>
The strides given in number of elements for a contiguous n-dimensional
array using this shape. Returns signed strides
(crate::StrideVec) — a contiguous layout’s strides are
always non-negative, but the type is signed so callers can
uniformly handle negative-stride view ops (Flip, etc.).
Sourcepub fn is_contiguous(&self, stride: &[isize]) -> bool
pub fn is_contiguous(&self, stride: &[isize]) -> bool
Returns true if the strides are C contiguous (aka row major).
Sourcepub fn is_fortran_contiguous(&self, stride: &[isize]) -> bool
pub fn is_fortran_contiguous(&self, stride: &[isize]) -> bool
Returns true if the strides are Fortran contiguous (aka column major).
Sourcepub fn extend(self, additional_dims: &[usize]) -> Shape
pub fn extend(self, additional_dims: &[usize]) -> Shape
Modifies the shape by adding a list of additional dimensions at the end of the existing dimensions.
Trait Implementations§
impl Eq for Shape
impl StructuralPartialEq for Shape
Auto Trait Implementations§
impl Freeze for Shape
impl RefUnwindSafe for Shape
impl Send for Shape
impl Sync for Shape
impl Unpin for Shape
impl UnsafeUnpin for Shape
impl UnwindSafe for Shape
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