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() and 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
Parameters:
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=False)[source]¶

Return a copy of the shape.

Parameters:

frozen (bool)

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. Call copy() to create a new shape with the same dimensions that can be modified.

get_denotation(index)[source]¶

Return the denotation of the dimension at the index.

Parameters:

index (int) – The index of the dimension.

Returns:

The denotation of the dimension.

Return type:

str | None

is_dynamic(dim=None)[source]¶
Return type:

bool

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:

bool

numpy()[source]¶
Return type:

tuple[int, …]

rank()[source]¶

The rank of the tensor this shape represents.

Return type:

int

set_denotation(index, denotation)[source]¶

Set the denotation of the dimension at the index.

Parameters:
  • index (int) – The index of the dimension.

  • denotation (str | None) – The denotation of the dimension.

Return type:

None