pub struct Shape(/* private fields */);Expand description
The dimensions of a tensor, outermost first.
A rank-0 shape (no dimensions) is a scalar and has exactly one element — not zero. That is the convention every tensor library converges on, and getting it wrong makes reductions (which produce scalars) special-cased everywhere.
Shape owns its dimensions in a Vec. Inference shapes are small (rank
4 at most, in practice) and created once per tensor, not per element, so
the allocation is not on any hot path — and a fixed-size inline array
would impose an arbitrary maximum rank for no measurable gain.
Implementations§
Source§impl Shape
impl Shape
pub fn new(dims: impl Into<Vec<usize>>) -> Shape
pub fn dims(&self) -> &[usize]
Sourcepub fn elem_count(&self) -> usize
pub fn elem_count(&self) -> usize
Total number of elements: the product of all dimensions.
An empty product is 1, so a scalar correctly reports one element. A shape containing a zero dimension correctly reports zero.
Sourcepub fn strides(&self) -> Vec<usize>
pub fn strides(&self) -> Vec<usize>
Row-major (C-order) strides, in elements, for this shape.
Row-major means the last dimension is contiguous — walking it steps one element at a time. This is the layout GGUF and SafeTensors both store weights in, so choosing it as the default avoids transposing every tensor at load time.
Sourcepub fn reshape(&self, dims: impl Into<Vec<usize>>) -> Result<Shape, Error>
pub fn reshape(&self, dims: impl Into<Vec<usize>>) -> Result<Shape, Error>
Reinterprets this shape as dims, which must describe the same number
of elements.
This is the shape-level half of a reshape; it says nothing about whether the underlying storage is contiguous enough to allow the reinterpretation without copying. That check belongs to the tensor.
Sourcepub fn broadcast(&self, other: &Shape) -> Result<Shape, Error>
pub fn broadcast(&self, other: &Shape) -> Result<Shape, Error>
The shape resulting from broadcasting self against other, or an
error if the two are not broadcast-compatible.
Follows the standard NumPy rule: align shapes from the right; two dimensions are compatible when they are equal or one of them is 1; the result takes the larger of each pair. Missing leading dimensions on the shorter shape are treated as 1.
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
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.