|
| 1 | +# Copyright 2025 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +from executorch.backends.arm._passes import ArmPass |
| 7 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 8 | + |
| 9 | +edge_elu_ops = (exir_ops.edge.aten.elu.default,) |
| 10 | + |
| 11 | + |
| 12 | +def get_elu_decomposition(op) -> tuple: |
| 13 | + """ |
| 14 | + Returns the decomposition of the given aten.elu operation into |
| 15 | + its equivalent TOSA-supported operations |
| 16 | +
|
| 17 | + This handles both edge dialect ops and core PyTorch ops. The decomposition strategy |
| 18 | + is: |
| 19 | + elu(x, y) → where(greater_or_eq(x, 0), (exp(x)-1), x) |
| 20 | +
|
| 21 | + Returns: |
| 22 | + A tuple (expm1_op, ge_op, where_op, mul_op) corresponding to the appropriate operator |
| 23 | + overloads for the input op. |
| 24 | +
|
| 25 | + Raises: |
| 26 | + RuntimeError: If the provided operator is not a supported elu variant. |
| 27 | + """ |
| 28 | + |
| 29 | + if op in edge_elu_ops: |
| 30 | + return ( |
| 31 | + exir_ops.edge.aten.expm1.default, |
| 32 | + exir_ops.edge.aten.ge.Scalar, |
| 33 | + exir_ops.edge.aten.where.self, |
| 34 | + exir_ops.edge.aten.mul.Scalar, |
| 35 | + ) |
| 36 | + |
| 37 | + raise RuntimeError(f"Can't get elu decomposition for op {op}") |
| 38 | + |
| 39 | + |
| 40 | +class DecomposeEluPass(ArmPass): |
| 41 | + """ |
| 42 | + A transformation pass that decomposes unsupported 'aten.elu' operations |
| 43 | + into a combination of supported TOSA-equivalent operations. |
| 44 | +
|
| 45 | + Since TOSA does not provide a native ELU operator, this pass rewrites: |
| 46 | + elu(x) → where(greater_or_eq(x, 0), (alpha*(exp(x)-1)), x) |
| 47 | +
|
| 48 | + Supported input ops: |
| 49 | + - exir_ops.edge.aten.elu.Tensor(x) |
| 50 | +
|
| 51 | + These are replaced with: |
| 52 | + - exir_ops.edge.aten.expm1.default |
| 53 | + - exir_ops.edge.aten.ge.Scalar |
| 54 | + - exir_ops.edge.aten.where.self |
| 55 | + - exir_ops.edge.aten.mul.Scalar |
| 56 | + """ |
| 57 | + |
| 58 | + def call_operator(self, op, args, kwargs, meta): |
| 59 | + if op not in edge_elu_ops: |
| 60 | + return super().call_operator(op, args, kwargs, meta, updated=False) |
| 61 | + |
| 62 | + ( |
| 63 | + expm1_op, |
| 64 | + ge_op, |
| 65 | + where_op, |
| 66 | + mul_op, |
| 67 | + ) = get_elu_decomposition(op) |
| 68 | + |
| 69 | + input = args[0] |
| 70 | + alpha = args[1] if len(args) > 1 else 1.0 |
| 71 | + |
| 72 | + if alpha == 0: |
| 73 | + relu_op = exir_ops.edge.aten.relu.default |
| 74 | + return super().call_operator(relu_op, (input,), {}, meta, updated=True) |
| 75 | + |
| 76 | + expm1_node = super().call_operator(expm1_op, (input,), {}, meta, updated=True) |
| 77 | + mul_node = super().call_operator( |
| 78 | + mul_op, (expm1_node, alpha), {}, meta, updated=True |
| 79 | + ) |
| 80 | + ge_node = super().call_operator(ge_op, (input, 0.0), {}, meta, updated=True) |
| 81 | + where_node = super().call_operator( |
| 82 | + where_op, (ge_node, input, mul_node), {}, meta, updated=True |
| 83 | + ) |
| 84 | + |
| 85 | + return where_node |
0 commit comments