Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions helion/_compiler/cute/resident_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
}
Expand Down
40 changes: 37 additions & 3 deletions test/test_cute_resident_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)

Expand Down