Mojo struct
Coord
@register_passable(trivial)
struct Coord[*element_types: CoordLike]
A struct representing tuple-like data with compile-time and runtime elements.
Parameters
- *element_types (
CoordLike): The variadic pack of element types that implementCoordLike.
Implemented traits
AnyType,
CoordLike,
Copyable,
Defaultable,
ImplicitlyCopyable,
ImplicitlyDestructible,
Movable,
RegisterPassable,
Sized,
TrivialRegisterPassable,
Writable
comptime members
__copy_ctor_is_trivial
comptime __copy_ctor_is_trivial = True
__del__is_trivial
comptime __del__is_trivial = True
__move_ctor_is_trivial
comptime __move_ctor_is_trivial = True
all_dims_known
comptime all_dims_known = #kgen.variadic.reduce(element_types, base=True, reducer=[PrevV: Variadic[Bool], VA: Variadic[CoordLike], idx: __mlir_type.index] PrevV[0] if VA[idx].is_static_value else VA[idx].is_static_value)[0]
True if all dimensions are statically known at compile time.
DTYPE
comptime DTYPE = DType.invalid
The data type for runtime values, or invalid for compile-time values.
flat_rank
comptime flat_rank = Variadic.size[CoordLike](#kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[CoordLike], VA: Variadic[CoordLike], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, VA[idx].VariadicType if VA[idx].is_tuple else VA[idx])))
The total number of leaf elements after flattening nested Coords.
is_static_value
comptime is_static_value = False
True if the value is known at compile time.
is_tuple
comptime is_tuple = True
True, indicating this is a tuple type.
is_value
comptime is_value = False
True if this is a scalar value, False for tuple types.
rank
comptime rank = Variadic.size[CoordLike](element_types)
The number of top-level elements in this Coord.
static_product
comptime static_product = #kgen.variadic.reduce(element_types, base=1, reducer=[PrevV: Variadic[Int], VA: Variadic[CoordLike], idx: __mlir_type.index] (VA[idx].static_value * PrevV[0]))[0]
The product of all static dimensions, or -1 if any are dynamic.
static_value
comptime static_value = -1
Always -1 for tuple types (value not applicable).
VariadicType
comptime VariadicType = element_types
The variadic element types of this Coord.
Methods
__init__
__init__() -> Self
Empty initialize a tensor with static dims.
__init__[rank: Int, dtype: DType](index_list: IndexList[index_list.rank, element_type=dtype]) -> Coord[#kgen.variadic.splat(RuntimeInt[dtype], index_list.rank._mlir_value)]
Construct a Coord from an IndexList.
Parameters:
- rank (
Int): The number of elements in the index list. - dtype (
DType): The data type of the index list elements.
Args:
- index_list (
IndexList): The IndexList to convert to a Coord.
Returns:
__init__(var *args: *element_types) -> Self
Construct tuple from variadic arguments.
Args:
- *args (
*element_types): Values for each element.
@implicit
__init__(var tuple: Tuple[element_types]) -> Self
Construct from a Tuple with matching element types.
Args:
- tuple (
Tuple): The Tuple to construct from.
__init__(*, var storage: VariadicPack[storage.is_owned, CoordLike, element_types]) -> Self
Construct from a low-level variadic pack.
Args:
- storage (
VariadicPack): The variadic pack storage to construct from.
__getitem__
__getitem__[idx: Int](ref self) -> ref[self_is_mut._storage] element_types[idx._mlir_value]
Get a reference to an element in the tuple.
Parameters:
- idx (
Int): The element index to access.
Returns:
ref: A reference to the specified element.
__eq__
__eq__[*other_types: CoordLike](self, other: Coord[other_types]) -> Bool
Check if this Coord equals another.
Parameters:
- *other_types (
CoordLike): The element types of the other Coord.
Args:
- other (
Coord): The Coord to compare with.
Returns:
Bool: True if all elements are equal, False otherwise.
__ne__
__ne__[*other_types: CoordLike](self, other: Coord[other_types]) -> Bool
Check if this Coord is not equal to another.
Parameters:
- *other_types (
CoordLike): The element types of the other Coord.
Args:
- other (
Coord): The Coord to compare with.
Returns:
Bool: True if any elements differ, False if all are equal.
size
static size() -> Int
Get the total number of elements including nested ones.
Returns:
Int: The total count of all elements.
__len__
static __len__() -> Int
Get the length of the tuple.
Returns:
Int: The number of elements in the tuple.
__len__(self) -> Int
Get the length of the tuple.
Returns:
Int: The number of elements in the tuple.
__repr__
__repr__(self) -> String
Get the string representation of this Coord.
Returns:
String: A string in the format "Coord(elem1, elem2, ...)".
write_repr_to
write_repr_to(self, mut writer: T)
Write the repr of this Coord to a writer.
Args:
- writer (
T): The writer to write the representation to.
product
product(self) -> Int
Calculate the product of all elements recursively.
Returns:
Int: The product of all leaf values in the Coord.
sum
sum(self) -> Int
Calculate the sum of all elements recursively.
Returns:
Int: The sum of all leaf values in the Coord.
value
value(self) -> Int
Get the value (not valid for Coord tuples).
Returns:
Int: Never returns; aborts at compile time.
inner_product
inner_product(self, t: IntTuple) -> Int
Calculate the inner product with an IntTuple.
Args:
- t (
IntTuple): The other value to compute inner product with.
Returns:
Int: The inner product of the two values.
inner_product[*other_types: CoordLike](self, other: Coord[other_types]) -> Int
Calculate the inner product with another CoordLike.
Parameters:
- *other_types (
CoordLike): The types of the other value.
Args:
- other (
Coord): The other value to compute inner product with.
Returns:
Int: The inner product of the two values.
tuple
tuple(var self) -> Self
Get this Coord as a tuple.
Returns:
Self: This Coord (identity operation for tuple types).
reverse
reverse(var self) -> Coord[#kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[CoordLike], VA: Variadic[CoordLike], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, VA[(add (mul idx, -1), len(VA), -1)]))]
Reverse the order of elements in this Coord.
Returns:
Coord: A new Coord with elements in reverse order.
concat
concat[*other_element_types: CoordLike](var self, var other: Coord[other_element_types]) -> Coord[#kgen.variadic.concat(element_types, other_element_types)]
Concatenate this Coord with another.
Parameters:
- *other_element_types (
CoordLike): The element types of the other Coord.
Args:
- other (
Coord): The Coord to append.
Returns:
Coord: A new Coord containing elements from both Coords.
flatten
flatten(var self) -> Coord[#kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[CoordLike], VA: Variadic[CoordLike], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, VA[idx].VariadicType if VA[idx].is_tuple else VA[idx]))]
Convert a nested Coord to a flattened Coord.
Examples:
from layout import Coord, Idx
var nested = Coord(
Idx[5](),
Coord(Idx[3](), Idx[2]()),
Idx(7)
)
var flat = nested.flatten()
# flat is Coord(Idx[5](), Idx[3](), Idx[2](), Idx(7))Returns:
Coord: A flattened Coord containing all leaf values in order.
make_dynamic
make_dynamic[dtype: DType](self) -> Coord[#kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[CoordLike], VA: Variadic[CoordLike], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, RuntimeInt[dtype]))]
Convert all elements to RuntimeInt[dtype].
Examples:
from layout.coord import Coord, ComptimeInt, RuntimeInt
var c = Coord(ComptimeInt[3](), RuntimeInt[DType.int32](5), ComptimeInt[7]())
var dynamic = c.make_dynamic[DType.int64]()
# dynamic is Coord(RuntimeInt[DType.int64](3), RuntimeInt[DType.int64](5), RuntimeInt[DType.int64](7))Parameters:
- dtype (
DType): The data type for the resulting RuntimeInt values.
Returns:
Coord: A new Coord where all elements are converted to RuntimeInt[dtype].
write_to
write_to(self, mut w: T)
Write this Coord to a Writer.
Args:
- w (
T): The writer to output to.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!