SymbolicDim¶

class onnx_ir.SymbolicDim(value)¶

Immutable symbolic dimension that can be shared across multiple shapes.

SymbolicDim is used to represent a symbolic (non-integer) dimension in a tensor shape. It is immutable and can be compared or hashed. It supports SymPy expressions for symbolic arithmetic.

Example:

>>> import onnx_ir as ir
>>> # Simple symbolic dimension (backward compatible)
>>> batch = ir.SymbolicDim("batch")
>>> batch.value
'batch'
>>> # Arithmetic operations return new SymbolicDim with SymPy expression
>>> seq_plus_one = ir.SymbolicDim("seq_len") + 1
>>> seq_plus_one.value
'seq_len + 1'
>>> # Evaluate with concrete values
>>> seq_plus_one.evaluate({"seq_len": 128})
129
Parameters:

value (str | None)

display(*, page=False)¶

Pretty print the object.

Parameters:

page (bool) – Whether to page the output.

Return type:

None

evaluate(bindings)[source]¶

Evaluate the symbolic dimension with concrete values.

Parameters:

bindings (Mapping[str, int]) – A mapping from symbol names to integer values.

Returns:

The concrete integer value if fully evaluated, or a SymbolicDim containing the partially evaluated expression or None.

Return type:

int | SymbolicDim

Example:

>>> dim = ir.SymbolicDim("N") + 1
>>> dim.evaluate({"N": 10})
11
free_symbols()[source]¶

Return the set of free symbol names in this dimension.

Returns:

A frozenset of symbol names that appear in the expression.

Return type:

frozenset[str]

simplify()[source]¶

Return a new SymbolicDim with the expression simplified.

Uses SymPy’s simplify function to reduce the expression.

Returns:

A new SymbolicDim with simplified expression.

Return type:

SymbolicDim

property value: str | None¶

The value of the symbolic dimension as a string.

Returns the string representation, or None for unknown dimensions.