Repository navigation
[linalg][transfrom] A transform extension for tiling and fusing dependent reductions. - #297
Conversation
|
cc @tkarna |
There was a problem hiding this comment.
🟡 Changes recommended
Accepted payloads can produce incorrect numerical results or invalid intermediate representation.
8 open findings
Dynamic dimensions lack runtime slice size operands · New Projected output map is not preserved for permuted dimensions · New Unsafe substitution can produce zero-over-zero corrections · New Unstable exponential correction causes NaN and infinity · New Dependent operands may not dominate fused loop uses · New Scalar region captures can violate definition-before-use ordering · New Correction factor does not match nonlinear reduction contribution · New Non-floating constants produce invalid cloned IR · New
What changed in this PR
Introduces dependent-reduction fusion for tiled online reductions, advancing #274 without integrating it into existing schedules yet.
Changes:
- Adds and exports the fusion transform.
- Implements correction planning, loop rewriting, and legality checks.
- Adds structural and rejection tests.
| File | Description |
|---|---|
test/transform/test_fuse_dependent_reduction_ops.py |
Tests softmax, attention, mixed precision, and rejection behavior. |
test/transform/test_dependent_reduction_legality.py |
Tests unsupported correction substitutions. |
lighthouse/dialects/transform/transform_ext/utils/ir_rewrite.py |
Adds cloning, hoisting, and type utilities. |
lighthouse/dialects/transform/transform_ext/utils/dependent_reduction_legality.py |
Adds correction-body preflight checks. |
lighthouse/dialects/transform/transform_ext/utils/dependent_reduction_fusion.py |
Implements correction factors and fused-loop construction. |
lighthouse/dialects/transform/transform_ext/ops/fuse_dependent_reduction_ops.py |
Defines the transform operation and handle contract. |
lighthouse/dialects/transform/transform_ext/__init__.py |
Exports the transform and reduction-loop marker. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if isinstance(operand, ir.BlockArgument) and operand.owner == body: | ||
| neutral = plan.neutral_by_op[op_index] | ||
| assert neutral is not None | ||
| value_map[operand] = arith.constant(compute_type, neutral) | ||
| temporary.append(operand) |
There was a problem hiding this comment.
This is a real issue. currently I don't have good solution rather than assuming a already simplified form the IR (x+x -> 2x in this case). I imagine this can be solved by tracking provenance over the R1 input in E. only values that data-dependent on R1 needs such adjustment. I keep this for later improvements.
Adding a TODO:
| operand | ||
| for consumer in (e, r2) | ||
| for operand in consumer.operands | ||
| if not irr.depends_on_op(operand, r1_loop) |
There was a problem hiding this comment.
I think this is a corner case for now (other users between E and R2). I would keep this for future work.
| to_hoist = [ | ||
| operand | ||
| for consumer in (e, r2) | ||
| for operand in consumer.operands | ||
| if not irr.depends_on_op(operand, r1_loop) |
There was a problem hiding this comment.
same as above. I think LLVM have stronger support for cleaner hoisting. harder to in python. leaving for future work.

This PR introduces a new transform op
fuse_dependent_reduction_opsthat takes tiled reduction and an elementwise op followed by another reduction and, fuse them into a single tiled online reduction loop.Main motivation is is to capture
R1 -> E -> R2linalg op sequence where,R1is a generic tiled reduction with at least one result.Eis an elementwise generic that consumes at least one output ofR1and uses it broadcasted over the same reduction axis asR1(This recovers the rank ofR1's input)R2is a generic add reduction that usesE's single output and produce single output.And fuse them into a single tiled loop. This type of fusion is not always doable and legality is checked before applying the fusion.
PR does not integrate the op with any schedule yet and will be done in a follow-up PR.
This is part of landing #274
Assisted by: Claude, Codex