|
| 1 | +"""Ordered carry for jagged row tiles on the Pallas emit_pipeline path. |
| 2 | +
|
| 3 | +A ``hl.tile(s, e)`` with runtime bounds rounds its rows out to a sublane-aligned |
| 4 | +window and masks the extra. Neighbouring groups then share one boundary row, |
| 5 | +which a small VMEM ``carry`` stitches back together. Only valid when each |
| 6 | +output row comes from its own input row. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import dataclasses |
| 12 | +from typing import TYPE_CHECKING |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from helion._compiler.inductor_lowering import CodegenState |
| 16 | + |
| 17 | + |
| 18 | +@dataclasses.dataclass(frozen=True) |
| 19 | +class CarryRowTile: |
| 20 | + """What the block-spec, mask, and store code share about one carried tile.""" |
| 21 | + |
| 22 | + block_id: int |
| 23 | + begin_var: str # host var for the tile's runtime begin (e.g. ``s``) |
| 24 | + end_var: str # host var for the tile's runtime end (e.g. ``e``) |
| 25 | + sublane: int # S = backend.sublane_tiling(dtype) |
| 26 | + carry_scratch_name: str | None = None |
| 27 | + |
| 28 | + |
| 29 | +def is_dynamic_bound_tile(state: CodegenState, block_id: int) -> bool: |
| 30 | + """True for a jagged tile: its begin and end are runtime tensor values. |
| 31 | +
|
| 32 | + A static ``hl.tile(0, K)`` keeps a constant ``end_expr``; a ``hl.tile(s, e)`` |
| 33 | + with runtime bounds has both ``begin_expr`` and ``end_expr`` set to None. |
| 34 | + """ |
| 35 | + loops = state.codegen.active_device_loops.get(block_id) |
| 36 | + if not loops: |
| 37 | + return False |
| 38 | + info = loops[-1].block_id_to_info.get(block_id) |
| 39 | + if info is None: |
| 40 | + return False |
| 41 | + return info.begin_expr is None and info.end_expr is None |
| 42 | + |
| 43 | + |
| 44 | +def is_row_map_axis(state: CodegenState, block_id: int) -> bool: |
| 45 | + """Whether each output row comes only from its own input row (a map axis). |
| 46 | +
|
| 47 | + True only when the row is written straight back (row i to output row i), |
| 48 | + never summed, scattered, or shifted; the over-read and carry are only |
| 49 | + correct in that case. |
| 50 | + """ |
| 51 | + # TODO(implement): the map-axis check above; for now reject every jagged tile. |
| 52 | + return False |
| 53 | + |
| 54 | + |
| 55 | +def needs_ordered_carry(state: CodegenState, block_id: int) -> bool: |
| 56 | + """Whether this dynamic row tile needs sublane carry stitching. |
| 57 | +
|
| 58 | + Carry stitches the shared boundary tile of a per-row map store, so it |
| 59 | + applies only when this jagged tile is a straight map axis (is_row_map_axis) |
| 60 | + *and* it feeds a 2-D store whose leading dim is the tile and whose trailing |
| 61 | + (column) dim is tiled -- that tiled column is the block the carry folds and |
| 62 | + saves into. An untiled ``:`` column (a BlockSpec store) cannot carry and |
| 63 | + does not need to; a higher-rank ``[L, H, D]`` store has no shared row; a |
| 64 | + reduction or scatter axis is not a map and never carries. |
| 65 | + """ |
| 66 | + from helion._compiler.pallas.plan_tiling import TilePattern |
| 67 | + from helion.language.memory_ops import store |
| 68 | + |
| 69 | + if not is_row_map_axis(state, block_id): |
| 70 | + return False |
| 71 | + for ginfo in state.codegen.codegen_graphs: |
| 72 | + for node in ginfo.graph.nodes: |
| 73 | + if node.target is not store: |
| 74 | + continue |
| 75 | + patterns = node.meta.get("indexing_patterns") |
| 76 | + if not patterns or len(patterns) != 2: |
| 77 | + continue |
| 78 | + row_pat, col_pat = patterns |
| 79 | + if getattr(row_pat, "block_id", None) != block_id: |
| 80 | + continue |
| 81 | + if isinstance(row_pat, TilePattern) and ( |
| 82 | + getattr(col_pat, "block_id", None) is not None |
| 83 | + ): |
| 84 | + return True |
| 85 | + return False |
| 86 | + |
| 87 | + |
| 88 | +def begin_end_vars(state: CodegenState, block_id: int) -> tuple[str, str] | None: |
| 89 | + """Return the (begin_var, end_var) host names for a dynamic row tile.""" |
| 90 | + loops = state.codegen.active_device_loops.get(block_id) |
| 91 | + if not loops: |
| 92 | + return None |
| 93 | + info = loops[-1].block_id_to_info.get(block_id) |
| 94 | + if info is None or info.begin_var_name is None: |
| 95 | + return None |
| 96 | + end_var = info.end_var_name |
| 97 | + if end_var is None: |
| 98 | + return None |
| 99 | + return info.begin_var_name, end_var |
| 100 | + |
| 101 | + |
| 102 | +def emit_carry_store( |
| 103 | + state: CodegenState, |
| 104 | + tensor: object, |
| 105 | + subscript: list[object] | tuple[object, ...], |
| 106 | + name: str, |
| 107 | + idx_str: str, |
| 108 | + value: object, |
| 109 | +) -> bool: |
| 110 | + """Store to a jagged row tile with the boundary carry. |
| 111 | +
|
| 112 | + Not implemented yet. Returns False for a normal store; for a jagged-row |
| 113 | + store it raises, because writing it directly would overwrite the previous |
| 114 | + group's data in the row they share. |
| 115 | + """ |
| 116 | + fn = state.device_function |
| 117 | + patterns = state.fx_node.meta.get("indexing_patterns") if state.fx_node else None |
| 118 | + if not patterns: |
| 119 | + return False |
| 120 | + for pat in patterns: |
| 121 | + bid = getattr(pat, "block_id", None) |
| 122 | + if bid is not None and bid in fn.carry_tiles: |
| 123 | + raise NotImplementedError( |
| 124 | + "Pallas ordered carry store for a jagged row tile is not " |
| 125 | + "implemented yet." |
| 126 | + ) |
| 127 | + return False |
0 commit comments