Skip to content

Commit 8e9ce62

Browse files
authored
[transform] Filter elementwise ops (llvm#241)
Adds a utility transform op that allow to opaquely target all Linalg elementwise operations for further processing. Also, registers missing transform_ext ops for easier importing. Assisted-by: Claude
1 parent 87637e7 commit 8e9ce62

3 files changed

Lines changed: 95 additions & 9 deletions

File tree

‎lighthouse/dialects/transform/transform_ext/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .ops.update_address_space import update_address_space
1515
from .ops.replace_with_fused_attention import replace_with_fused_attention
1616
from .ops.filter_num_loops import filter_num_loops
17+
from .ops.filter_elementwise import filter_elementwise
1718
from .ops.get_leading_unit_tile_sizes import get_leading_unit_tile_sizes
1819
from .ops.move_offsets_to_subview import move_offsets_to_subview
1920

@@ -22,6 +23,7 @@
2223
"assign_tile_sizes",
2324
"convert_func_results_to_args",
2425
"extract_handle",
26+
"filter_elementwise",
2527
"filter_num_loops",
2628
"get_leading_unit_tile_sizes",
2729
"get_named_attribute",
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from mlir import ir
2+
from mlir.dialects import ext, transform, linalg
3+
from mlir.dialects.transform import DiagnosedSilenceableFailure
4+
5+
from lighthouse.dialects.transform.transform_ext import TransformExtensionDialect
6+
from lighthouse.utils.mlir import indexing_maps, linalg_inputs
7+
8+
9+
def _all_loops_parallel(op: ir.OpView) -> bool:
10+
"""Check whether all iterator types of a (generic) op are parallel."""
11+
build = ir.AttrBuilder.get("linalg.IteratorTypeEnum")
12+
parallel = build(linalg.IteratorType.parallel, context=op.context)
13+
return all(it == parallel for it in op.iterator_types)
14+
15+
16+
def is_elementwise(op: ir.Operation | ir.OpView) -> bool:
17+
"""Check whether the op is an elementwise linalg op.
18+
19+
NOTE: Mimics corresponding Linalg util as it is not exposed
20+
in the Python bindings yet.
21+
"""
22+
ov = op.opview if isinstance(op, ir.Operation) else op
23+
maps = indexing_maps(ov)
24+
if maps is None:
25+
return False
26+
if isinstance(ov, linalg.GenericOp) and not _all_loops_parallel(ov):
27+
return False
28+
if not all(m.is_projected_permutation for m in maps):
29+
return False
30+
num_inputs = len(linalg_inputs(ov))
31+
return all(m.is_permutation for m in maps[num_inputs:])
32+
33+
34+
class FilterElementwiseOp(
35+
TransformExtensionDialect.Operation, name="filter_elementwise"
36+
):
37+
"""
38+
Returns the target ops that are elementwise linalg ops.
39+
40+
Targets that are not elementwise linalg ops are dropped.
41+
42+
Args:
43+
target: Handle to target op(s).
44+
Returns:
45+
Handle to the elementwise subset of `target`.
46+
"""
47+
48+
target: ext.Operand[transform.AnyOpType]
49+
ops: ext.Result[transform.AnyOpType[()]] = ext.infer_result()
50+
51+
@classmethod
52+
def attach_interface_impls(cls, ctx=None):
53+
cls.TransformOpInterfaceModel.attach(cls.OPERATION_NAME, context=ctx)
54+
cls.MemoryEffectsOpInterfaceModel.attach(cls.OPERATION_NAME, context=ctx)
55+
56+
class TransformOpInterfaceModel(transform.TransformOpInterface):
57+
@staticmethod
58+
def apply(
59+
op: "FilterElementwiseOp",
60+
_rewriter: transform.TransformRewriter,
61+
results: transform.TransformResults,
62+
state: transform.TransformState,
63+
) -> DiagnosedSilenceableFailure:
64+
targets = state.get_payload_ops(op.target)
65+
matching_ops = [t for t in targets if is_elementwise(t)]
66+
results.set_ops(op.ops, matching_ops)
67+
return DiagnosedSilenceableFailure.Success
68+
69+
@staticmethod
70+
def allow_repeated_handle_operands(_op: "FilterElementwiseOp") -> bool:
71+
return False
72+
73+
class MemoryEffectsOpInterfaceModel(ir.MemoryEffectsOpInterface):
74+
@staticmethod
75+
def get_effects(op: ir.Operation, effects):
76+
transform.only_reads_handle(op.op_operands, effects)
77+
transform.produces_handle(op.results, effects)
78+
transform.only_reads_payload(effects)
79+
80+
81+
def filter_elementwise(target: ir.Value[transform.AnyOpType]) -> ir.Value:
82+
"""
83+
snake_case wrapper to create a FilterElementwiseOp.
84+
85+
Args:
86+
target: Handle to target op(s).
87+
Returns:
88+
Handle to the elementwise subset of `target`.
89+
"""
90+
return FilterElementwiseOp(target=target).ops

‎test/transform/test_tile_size_ops.py‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@
66

77
import lighthouse.dialects as lh_dialects
88
from lighthouse import transform as lh_transform
9-
from lighthouse.dialects.transform.transform_ext.ops.assign_tile_sizes import (
10-
assign_tile_sizes,
11-
)
12-
from lighthouse.dialects.transform.transform_ext.ops.get_leading_unit_tile_sizes import (
13-
get_leading_unit_tile_sizes,
14-
)
15-
from lighthouse.dialects.transform.transform_ext.ops.get_tile_sizes import (
16-
get_tile_sizes,
17-
)
9+
from lighthouse.dialects.transform.transform_ext import assign_tile_sizes
10+
from lighthouse.dialects.transform.transform_ext import get_leading_unit_tile_sizes
11+
from lighthouse.dialects.transform.transform_ext import get_tile_sizes
1812
from lighthouse.schedule.builders import schedule_boilerplate
1913

2014

0 commit comments

Comments
 (0)