Skip to main content

SymbolicShape

Struct SymbolicShape 

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

A shape pattern that can contain fixed, symbolic, and dynamic dimensions.

Think of it as a “shape template” that can be resolved to a concrete Shape once all symbolic dimensions are bound.

§Examples

// Define a shape pattern for transformer input
let pattern = SymbolicShape::from(vec![
    SymDim::symbolic("Batch"),
    SymDim::symbolic("SeqLen"),
    SymDim::fixed(768),
]);

// Resolve with concrete bindings
let mut env = ShapeEnv::new();
env.bind("Batch", 32);
env.bind("SeqLen", 128);
let concrete = pattern.resolve(&env)?; // Shape([32, 128, 768])

// Or validate a concrete tensor shape
let guard = ShapeGuard::new(pattern);
assert!(guard.validate_shape(&Shape::new(vec![32, 128, 768]), &env));

Implementations§

Source§

impl SymbolicShape

Source

pub fn new(dims: Vec<SymDim>) -> Self

Create a new symbolic shape from a vector of SymDim.

Source

pub fn from_shape(shape: &Shape) -> Self

Create a fully-fixed symbolic shape from a concrete shape.

Source

pub fn rank(&self) -> usize

Number of dimensions.

Source

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

Get the dimension patterns.

Source

pub fn is_concrete(&self) -> bool

Check if all dimensions are fixed (fully concrete).

Source

pub fn has_symbolic(&self) -> bool

Check if any dimension is symbolic or dynamic.

Source

pub fn symbolic_names(&self) -> Vec<&str>

Get all symbolic dimension names used in this shape.

Source

pub fn resolve(&self, env: &ShapeEnv) -> Result<Shape>

Try to resolve this symbolic shape to a concrete Shape.

Returns an error if any symbolic dimension is not bound in the env, or if any dimension is Dynamic (cannot resolve without a value).

Source

pub fn resolve_with_default(&self, env: &ShapeEnv, default: usize) -> Shape

Try to resolve, falling back to a default for unresolved dims. Dynamic/unbound symbolic dims use the provided default value.

Source

pub fn matches(&self, shape: &Shape, env: &ShapeEnv) -> bool

Check if a concrete shape matches this pattern.

Returns true if the shapes have the same rank and each dimension matches (fixed dims must be equal, symbolic dims must match their binding if bound, dynamic dims match anything).

Source

pub fn unify(&self, shape: &Shape, env: &mut ShapeEnv) -> bool

Unify this pattern with a concrete shape, binding symbolic dims.

Returns true if unification succeeded (all dims compatible and symbolic bindings are consistent). On success, newly discovered bindings are added to env.

Source

pub fn broadcast(&self, other: &SymbolicShape) -> Option<SymbolicShape>

Compute the output shape of a broadcasting operation between two symbolic shapes. Returns None if shapes are incompatible.

Trait Implementations§

Source§

impl Clone for SymbolicShape

Source§

fn clone(&self) -> SymbolicShape

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SymbolicShape

Source§

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

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

impl Display for SymbolicShape

Source§

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

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

impl From<Shape> for SymbolicShape

Source§

fn from(shape: Shape) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<SymDim>> for SymbolicShape

Source§

fn from(dims: Vec<SymDim>) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for SymbolicShape

Source§

fn eq(&self, other: &SymbolicShape) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for SymbolicShape

Source§

impl StructuralPartialEq for SymbolicShape

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