diff --git a/helion/_compiler/cute/resident_reductions.py b/helion/_compiler/cute/resident_reductions.py index 6b253bece..70e2a82fc 100644 --- a/helion/_compiler/cute/resident_reductions.py +++ b/helion/_compiler/cute/resident_reductions.py @@ -772,6 +772,18 @@ def _plan( # Never reorder an online reduction or a reduction feeding the row carry. if dependent & carries or any(marker.input_name in dependent for marker in markers): return None + # The rewrite sums a marker's input when the row's other statements have + # run for the feature, so the name has to hold then what the marker read. + # A row assigns a name once, and only a carry is read ahead of its + # assignment: a marker above ``carry = old_carry + increment`` sums the + # carry the row started with, where the rewrite would sum the updated one. + for position, statement in enumerate(row_body): + marker = _is_lane_reduce_marker_assign(statement) + if marker is not None and any( + _assignment(later) == marker.input_name + for later in row_body[position + 1 :] + ): + return None if any( _is_lane_reduce_marker_assign(statement) is not None for statement in [*prefix, *suffix] @@ -943,6 +955,12 @@ def _plan( varying = _tainted( [*replay, *producer, *consumer], {lane_name, layout.index_name, *state} ) + # Each feature keeps its own carry, which the feature loop loads and + # stores around the row's statements. An update that reads nothing of + # its feature (``carry = cutlass.Float32(row)``) would run once for the + # row, ahead of that loop, which then stores the carry's old value back. + if not carries <= varying: + return None producer_names = { name for stmt in producer if (name := _assignment(stmt)) is not None } diff --git a/test/test_cute_resident_reductions.py b/test/test_cute_resident_reductions.py index a17195f79..cc6d87fbc 100644 --- a/test/test_cute_resident_reductions.py +++ b/test/test_cute_resident_reductions.py @@ -380,6 +380,16 @@ def test_grouped_rows_keep_int32_endpoint_semantics( _assert_grouped_rows_match(original, rewritten, end - begin, group_rows) +def _lane_sum(value: np.float32, *args: object, **kwargs: object) -> np.float32: + """The lane reduction of a value that every feature holds, as the inputs + of ``_assert_grouped_rows_match`` give each name of a row: the reference + then executes the original serial schedule exactly.""" + total = np.float32(0) + for _ in range(_FEATURES): + total = total + value + return total + + def _assert_grouped_rows_match( original: str, rewritten: str, rows: int, group_rows: int, *, row_limit: int = 0 ) -> None: @@ -408,9 +418,7 @@ def _assert_grouped_rows_match( "initial": -0.0, "limit": _FEATURES, "row_limit": np.int32(row_limit), - # The zero weight makes both reduction inputs zero, so this - # reference executes the original serial schedule exactly. - "_helion_lane_reduce": lambda value, *args, **kwargs: np.float32(0), + "_helion_lane_reduce": _lane_sum, } ) with np.errstate(over="raise", invalid="raise"): @@ -439,6 +447,9 @@ def _assert_grouped_rows_match( ("w = (weight.iterator + cutlass.Int32(column)).load()", "w = weight[column]"), ("carry = cutlass.Float32(initial)", "carry = cutlass.Float64(initial)"), ("carry = old_carry + increment", "carry = old_carry + reduced_product"), + # An update that reads nothing of its feature: hoisted out of the + # feature loop, it would leave the carry its old value. + ("carry = old_carry + increment", "carry = cutlass.Float32(row)"), ( "result = centered_gradient * scale_value", "result = centered_gradient * carry", @@ -512,6 +523,29 @@ def test_unknown_effects_aliases_and_loop_carried_values_decline( assert ast.dump(ast.parse(rewritten)) == ast.dump(ast.parse(source)) +def test_a_marker_sums_the_carry_that_its_position_reads() -> None: + """The rewrite sums a marker's input when the row's other statements have + run for the feature. Below the carry's update that is the carry the + marker reads; above it the loop sums the carry the row started with, and + keeps its form.""" + marker = ( + " reduced_gradient = cutlass.Float32(_helion_lane_reduce(" + "weighted, 'sum', cutlass.Float32(0), threads_in_group=1))\n" + ) + update = " carry = old_carry + increment\n" + source = _source(rows=2) + assert marker in source and update in source + carried = marker.replace("(weighted,", "(carry,") + source = source.replace(marker, "") + above = source.replace(update, carried + update) + assert ast.dump(ast.parse(_rewrite(above))) == ast.dump(ast.parse(above)) + below = source.replace(update, update + carried) + for group_rows, pipelined in ((1, False), (2, False), (2, True)): + rewritten = _rewrite(below, group_rows=group_rows, pipelined=pipelined) + assert "resident_state" in rewritten + _assert_grouped_rows_match(below, rewritten, 2, group_rows) + + def test_resident_requires_nonaliasing_outputs() -> None: assert "resident_state" not in _rewrite(_source(), disjoint=False)