Shape¶

class onnx_ir.Shape(dims: Iterable[int | SupportsInt | SymbolicDim | str | None], /, denotations: Iterable[str | None] | None = None, frozen: bool = False)¶

The shape of a tensor, including its dimensions and optional denotations.

The Shape stores the dimensions of a tensor, which can be integers, None (unknown), or symbolic dimensions.

A shape can be compared to another shape or plain Python list.

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 :method:`freeze` to freeze the shape.

To update the dimension of a frozen shape, call :method:`copy` to create a new shape with the same dimensions that can be modified.

Use :method:`get_denotation` and :method:`set_denotation` to access and modify the denotations.

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

copy(frozen: bool = False)[source]¶

Return a copy of the shape.

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: bool = False) None¶

Pretty print the object.

Parameters:

page – Whether to page the output.

freeze() None[source]¶

Freeze the shape.

When the shape is frozen, it cannot be unfrozen, making it suitable to be shared.

property frozen: bool¶

Whether the shape is frozen.

When the shape is frozen, it cannot be unfrozen, making it suitable to be shared. Call :method:`freeze` to freeze the shape. Call :method:`copy` to create a new shape with the same dimensions that can be modified.

get_denotation(index: int) str | None[source]¶

Return the denotation of the dimension at the index.

Parameters:

index – The index of the dimension.

Returns:

The denotation of the dimension.

is_dynamic(dim=None) bool[source]¶
is_static(dim=None) bool[source]¶

Return True if the dimension is static. If dim is None, return True if all dimensions are static.

numpy() tuple[int, ...][source]¶
rank() int[source]¶

The rank of the tensor this shape represents.

set_denotation(index: int, denotation: str | None) None[source]¶

Set the denotation of the dimension at the index.

Parameters:
  • index – The index of the dimension.

  • denotation – The denotation of the dimension.