Shape¶
- class onnx_ir.Shape(dims, /, denotations=None, frozen=False)¶
Represents the shape of a tensor, including its dimensions and optional denotations.
The
Shape
class stores the dimensions of a tensor, which can be integers, None (unknown), or symbolic dimensions. It provides methods for querying and manipulating the shape, as well as for comparing shapes to other shapes or plain Python lists.A shape can be frozen (made immutable). When the shape is frozen, it cannot be unfrozen, making it suitable to be shared across tensors or values. Call
freeze()
to freeze the shape.To update the dimension of a frozen shape, call
copy()
to create a new shape with the same dimensions that can be modified.Use
get_denotation()
andset_denotation()
to access and modify the denotations.Note
Two shapes can be compared for equality. Be careful when comparing shapes with unknown dimensions (
None
), as they may not be considered semantically equal even if all dimensions are the same. You can usehas_unknown_dim()
to check if a shape has any unknown dimensions.Example:
>>> import onnx_ir as ir >>> shape = ir.Shape(["B", None, 3]) >>> shape.rank() 3 >>> shape.is_static() False >>> shape.is_dynamic() True >>> shape.is_static(dim=2) True >>> shape[0] = 1 >>> shape[1] = 2 >>> shape.dims (1, 2, 3) >>> shape == [1, 2, 3] True >>> shape.frozen False >>> shape.freeze() >>> shape.frozen True
- Parameters:
dims (Iterable[int | SupportsInt | SymbolicDim | str | None])
denotations (Iterable[str | None] | None)
frozen (bool)
- dims¶
A tuple of dimensions representing the shape. Each dimension can be an integer, None, or a
SymbolicDim
.
- frozen¶
Indicates whether the shape is immutable. When frozen, the shape cannot be modified or unfrozen.
- property dims: tuple[int | SymbolicDim, ...]¶
All dimensions in the shape.
This property is read-only. Use __getitem__ and __setitem__ to modify the shape or create a new shape.
- display(*, page=False)¶
Pretty print the object.
- Parameters:
page (bool) – Whether to page the output.
- Return type:
None
- freeze()[source]¶
Freeze the shape.
When the shape is frozen, it cannot be unfrozen, making it suitable to be shared.
- Return type:
None
- property frozen: bool¶
Whether the shape is frozen.
When the shape is frozen, it cannot be unfrozen, making it suitable to be shared. Call
freeze()
to freeze the shape. Callcopy()
to create a new shape with the same dimensions that can be modified.
- has_unknown_dim()[source]¶
Return True if any dimension is unknown (None).
You can use
is_unknown_dim()
to check if a specific dimension is unknown.Added in version 0.1.10.
- Return type:
- is_static(dim=None)[source]¶
Return True if the dimension is static. If dim is None, return True if all dimensions are static.
- Return type: