Skip to main content

Python class

Dim

Dim

class max.graph.Dim(value)

source

Bases: object

A tensor dimension.

Tensor dimensions can be one of three types:

  • Static: Known size
  • Symbolic: Unknown size but named
  • Algebraic: Unknown size has an algebraic expression

In most cases, you don’t need to work with a Dim directly. Instead, use conversion constructors:

from max.graph import Dim, TensorType, DeviceRef

tensor_type = TensorType(DType.int64, ("batch", 10), device=DeviceRef.CPU())

This creates a tensor type with two dimensions:

  • A symbolic “batch” dimension
  • A static dimension of size 10

For explicit dimension construction, use the following helpers:

from max.graph import Dim

some_dims = [
    SymbolicDim("batch"),
    StaticDim(5),
    AlgebraicDim(Dim("batch") + 1),
]

Constraining tensor dimensions is one important way to improve model performance. If tensors have unknown dimensions, we can’t optimize them as aggressively. Symbolic tensors allow the compiler to learn constraints on a specific dimension (eg. if 2 inputs have the same batch dimension), but static dims are the easiest to optimize and therefore the easiest to create and work with.

Converts valid input values to Dim.

Parameters:

value (DimLike)

from_mlir()

static from_mlir(attr)

source

Constructs a dimension from an mlir.Attribute.

Parameters:

attr (TypedAttr) – The MLIR Attribute to parse into a dimension.

Returns:

The dimension represented by the MLIR Attr value.

Return type:

Dim

parameters

property parameters: Iterable[SymbolicDim]

source

Lists the symbolic dimension names on which this dim depends.

to_mlir()

to_mlir()

source

Creates an mlir.Attribute representing this dimension.

This is used internally when constructing tensor MLIR types.

Returns:

An mlir.Attribute in the context representing the dimension.

Return type:

TypedAttr