Mojo struct
Dim
A tensor dimension.
Tensor dimensions can be
- Static, aka known size
- Dynamic, aka unknown size
- Symbolic, aka unknown size but named
In most cases you don't need to work with a Dim
directly, but can rely
on conversion constructors, for instance you can specify a tensor type as
from max.graph import Dim, TensorType
var tensor_type = TensorType(DType.int64, "batch", 10, Dim.dynamic())
from max.graph import Dim, TensorType
var tensor_type = TensorType(DType.int64, "batch", 10, Dim.dynamic())
will create a tensor type with 3 dimensions: a symbolic "batch" dimension, a static dimension of size 10, and a dynamic dimension.
You can still construct dimensions explicitly via helpers, eg.
```mojo
var some_dims = [
Dim.dynamic(),
Dim.symbolic("batch"),
Dim.static(5),
]
You can still construct dimensions explicitly via helpers, eg.
```mojo
var some_dims = [
Dim.dynamic(),
Dim.symbolic("batch"),
Dim.static(5),
]
Constraining tensor dimensions is one important way to improve model
performance. If tensors have unknown dimensions, we can't optimize them
as aggressively. Symoblic tensors allow the compiler to learn constraints
on a specific dimension (eg. if 2 inputs have the same batch
dimension)
which can be an important improvement over dynamic dimensions, but static
dims are the easiest to optimize and therefore the easiest to create
and work with.
Fields
- value (
Variant[DynamicDim, StaticDim, SymbolicDim]
): The dimension data.
Implemented traits
AnyType
,
CollectionElement
,
Copyable
,
Movable
,
Stringable
Methods
__init__
__init__(out self, dim: Int)
Int static dimension conversion constructor.
Args:
- dim (
Int
): The static size of the dimension.
__init__(out self, name: StringLiteral)
StringLiteral symbolic dimension conversion constructor.
Args:
- name (
StringLiteral
): The name of the symbolic dimension.
__eq__
__eq__(self, other: Self) -> Bool
Checks whether two dimensions are equal.
Dimensions are equal if they are the same dimension type (dynamic, symbolic, static). Additionally, static dimensions are only equal if their dimension is the same size, and symbolic dimensions are only equal if they have the same name.
Args: