Skip to main content

Shape

Struct Shape 

Source
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

Source

pub fn new(dims: impl Into<Vec<usize>>) -> Shape

Source

pub fn scalar() -> Shape

The scalar shape: rank 0, one element.

Source

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

Source

pub fn rank(&self) -> usize

Number of dimensions. Zero for a scalar.

Source

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.

Source

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.

Source

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.

Source

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§

Source§

impl Clone for Shape

Source§

fn clone(&self) -> Shape

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 Shape

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Shape

Source§

fn default() -> Shape

Returns the “default value” for a type. Read more
Source§

impl Display for Shape

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Eq for Shape

Source§

impl From<&[usize]> for Shape

Source§

fn from(dims: &[usize]) -> Shape

Converts to this type from the input type.
Source§

impl From<Vec<usize>> for Shape

Source§

fn from(dims: Vec<usize>) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<[usize; N]> for Shape

Source§

fn from(dims: [usize; N]) -> Shape

Converts to this type from the input type.
Source§

impl Hash for Shape

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Shape

Source§

fn eq(&self, other: &Shape) -> 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 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> 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<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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.