Mojo struct
Coord
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
all_dims_known
comptime all_dims_known = #kgen.param_list.reduce(values, base=True, reducer=[PrevV: Bool, VA: KGENParamList[CoordLike], idx: __mlir_type.index] VA[idx].is_static_value and PrevV)
True if all dimensions are statically known at compile time.
contains_slices
comptime contains_slices = #kgen.param_list.reduce(values, base=False, reducer=[PrevV: Bool, VA: KGENParamList[CoordLike], idx: __mlir_type.index] _type_is_eq_parse_time[VA[idx], _All]() if _type_is_eq_parse_time[VA[idx], _All]() else PrevV)
If the Coord contains the All symbol.
DTYPE
comptime DTYPE = DType.invalid
The data type for runtime values, or invalid for compile-time values.
flat_rank
comptime flat_rank = TypeList[#kgen.param_list.reduce(#kgen.param_list.reduce(#kgen.param_list.reduce(#kgen.param_list.reduce(values, base=, reducer=[PrevV: KGENParamList[CoordLike], VA: KGENParamList[CoordLike], idx: __mlir_type.index] #kgen.param_list.concat(PrevV, VA[idx].ParamListType if VA[idx].is_tuple else VA[idx])), base=, reducer=[PrevV: KGENParamList[CoordLike], VA: KGENParamList[CoordLike], idx: __mlir_type.index] #kgen.param_list.concat(PrevV, VA[idx].ParamListType if VA[idx].is_tuple else VA[idx])), base=, reducer=[PrevV: KGENParamList[CoordLike], VA: KGENParamList[CoordLike], idx: __mlir_type.index] #kgen.param_list.concat(PrevV, VA[idx].ParamListType if VA[idx].is_tuple else VA[idx])), base=, reducer=[PrevV: KGENParamList[CoordLike], VA: KGENParamList[CoordLike], idx: __mlir_type.index] #kgen.param_list.concat(PrevV, VA[idx].ParamListType if VA[idx].is_tuple else VA[idx]))].size
The total number of leaf elements after flattening nested Coords.
is_flat
comptime is_flat = (Coord[element_types].rank == Coord[element_types].flat_rank)
If the Coord contains nested items.
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.
ParamListType
comptime ParamListType = values
The variadic element types of this Coord.
rank
comptime rank = TypeList[values].size
The number of top-level elements in this Coord.
static_product
comptime static_product = #kgen.param_list.reduce(values, base=1, reducer=[PrevV: Int, VA: KGENParamList[CoordLike], idx: __mlir_type.index] (VA[idx].static_value * PrevV))
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).
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.param_list.tabulate(index_list.rank, [idx: __mlir_type.index] RuntimeInt[dtype])]
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): TheIndexListto convert to aCoord.
Returns:
__init__(var *args: *element_types.values) -> Self
Construct tuple from variadic arguments.
Args:
- *args (
*element_types.values): Values for each element.
@implicit
__init__(var tuple: Tuple[*element_types.values]) -> Self
Construct from a Tuple with matching element types.
Args:
- tuple (
Tuple): The Tuple to construct from.
__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 otherCoord.
Args:
- other (
Coord): TheCoordto 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 otherCoord.
Args:
- other (
Coord): TheCoordto 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.
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.
__getitem_param__
__getitem_param__[idx: Int](ref self) -> ref[idx._storage] element_types.values[idx]
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.
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) -> Coord[*Coord[element_types].ParamListType]
Get this Coord as a tuple.
Returns:
Coord: This Coord (identity operation for tuple types).
reverse
reverse(var self) -> Coord[*#kgen.param_list.reduce(element_types.values, base=, reducer=[PrevV: KGENParamList[CoordLike], VA: KGENParamList[CoordLike], idx: __mlir_type.index] #kgen.param_list.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.param_list.concat(element_types.values, other_element_types.values)]
Concatenate this Coord with another.
Parameters:
- *other_element_types (
CoordLike): The element types of the otherCoord.
Args:
- other (
Coord): TheCoordto append.
Returns:
Coord: A new Coord containing elements from both Coords.
flatten
flatten(var self) -> Coord[*#kgen.param_list.reduce(#kgen.param_list.reduce(#kgen.param_list.reduce(#kgen.param_list.reduce(element_types.values, base=, reducer=[PrevV: KGENParamList[CoordLike], VA: KGENParamList[CoordLike], idx: __mlir_type.index] #kgen.param_list.concat(PrevV, VA[idx].ParamListType if VA[idx].is_tuple else VA[idx])), base=, reducer=[PrevV: KGENParamList[CoordLike], VA: KGENParamList[CoordLike], idx: __mlir_type.index] #kgen.param_list.concat(PrevV, VA[idx].ParamListType if VA[idx].is_tuple else VA[idx])), base=, reducer=[PrevV: KGENParamList[CoordLike], VA: KGENParamList[CoordLike], idx: __mlir_type.index] #kgen.param_list.concat(PrevV, VA[idx].ParamListType if VA[idx].is_tuple else VA[idx])), base=, reducer=[PrevV: KGENParamList[CoordLike], VA: KGENParamList[CoordLike], idx: __mlir_type.index] #kgen.param_list.concat(PrevV, VA[idx].ParamListType 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.param_list.reduce(element_types.values, base=, reducer=[PrevV: KGENParamList[CoordLike], VA: KGENParamList[CoordLike], idx: __mlir_type.index] #kgen.param_list.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 resultingRuntimeIntvalues.
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!