helion.language.grid
- helion.language.grid(begin_or_end, end_or_none=None, /, step=None)[source]
Iterate over individual indices of the given iteration space.
The key difference from
tile()
is thatgrid
gives you scalar integer indices (torch.SymInt
), whiletile
gives youTile
objects that load a slice of elements. Usetile
in most cases. Usegrid
when you need explicit control over the launch grid or when processing one element at a time.Semantics are equivalent to:
for i in hl.tile(...): # i is a Tile object, accesses multiple elements data = tensor[i] # loads slice of elements (1D tensor)
vs:
for i in hl.grid(...): # i is a scalar index, accesses single element data = tensor[i] # loads single element (0D scalar)
When used at the top level of a function, this becomes the grid of the kernel. Otherwise, it becomes a loop in the output kernel.
- Parameters:
begin_or_end (
int
|Tensor
|Sequence
[int
|Tensor
]) – If 2+ positional args provided, the start of iteration space. Otherwise, the end of iteration space.end_or_none (
int
|Tensor
|Sequence
[int
|Tensor
] |None
) – If 2+ positional args provided, the end of iteration space.step (
object
) – Step size for iteration (default: 1)
- Returns:
Iterator over scalar indices
- Return type:
Iterator[torch.SymInt] or Iterator[Sequence[torch.SymInt]]
See also
tile()
: For processing multiple elements at oncetile_index()
: For getting tile indicesarange()
: For creating index sequences
Note
Similar to
range()
with multiple forms:grid(end) iterates from 0 to end-1, step 1
grid(begin, end) iterates from begin to end-1, step 1
grid(begin, end, step) iterates from begin to end-1, given step
grid(end, step=step) iterates from 0 to end-1, given step
Use
tile
in most cases. Usegrid
when you need explicit control over the launch grid.