|
| 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 |
0 commit comments