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
impl SymbolicShape
Sourcepub fn from_shape(shape: &Shape) -> Self
pub fn from_shape(shape: &Shape) -> Self
Create a fully-fixed symbolic shape from a concrete shape.
Sourcepub fn is_concrete(&self) -> bool
pub fn is_concrete(&self) -> bool
Check if all dimensions are fixed (fully concrete).
Sourcepub fn has_symbolic(&self) -> bool
pub fn has_symbolic(&self) -> bool
Check if any dimension is symbolic or dynamic.
Sourcepub fn symbolic_names(&self) -> Vec<&str>
pub fn symbolic_names(&self) -> Vec<&str>
Get all symbolic dimension names used in this shape.
Sourcepub fn resolve(&self, env: &ShapeEnv) -> Result<Shape>
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).
Sourcepub fn resolve_with_default(&self, env: &ShapeEnv, default: usize) -> Shape
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.
Sourcepub fn matches(&self, shape: &Shape, env: &ShapeEnv) -> bool
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).
Sourcepub fn unify(&self, shape: &Shape, env: &mut ShapeEnv) -> bool
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.
Sourcepub fn broadcast(&self, other: &SymbolicShape) -> Option<SymbolicShape>
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
impl Clone for SymbolicShape
Source§fn clone(&self) -> SymbolicShape
fn clone(&self) -> SymbolicShape
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more