|
| 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 | + |
| 10 | +edge_expm1_ops = (exir_ops.edge.aten.expm1.default,) # MI case |
| 11 | + |
| 12 | + |
| 13 | +def _get_expm1_decomposition(op) -> tuple: |
| 14 | + """ |
| 15 | + Returns the decomposition of the given aten.expm1 operation into |
| 16 | + its equivalent TOSA-supported operations |
| 17 | +
|
| 18 | + This handles both edge dialect ops and core PyTorch ops. The decomposition strategy |
| 19 | + is: |
| 20 | + expm1(x) → where(and(ge(x, -0.35), le(x, 0.35)), {taylor_series_expansion}, (exp(x)-1)) |
| 21 | +
|
| 22 | + where {taylor_series_expansion} = x + (x^2/2) + (x^3/6) + (x^4/24) |
| 23 | +
|
| 24 | + Returns: |
| 25 | + A tuple (op_pow, op_div, op_add, op_exp, op_sub, op_ge, op_where, op_le, op_and) |
| 26 | + corresponding to the appropriate operator overloads for the input op. |
| 27 | +
|
| 28 | + Raises: |
| 29 | + RuntimeError: If the provided operator is not a supported elu variant. |
| 30 | + """ |
| 31 | + if op in edge_expm1_ops: |
| 32 | + return ( |
| 33 | + exir_ops.edge.aten.pow.Tensor_Scalar, |
| 34 | + exir_ops.edge.aten.div.Scalar, |
| 35 | + exir_ops.edge.aten.add.Tensor, |
| 36 | + exir_ops.edge.aten.exp.default, |
| 37 | + exir_ops.edge.aten.sub.Scalar, |
| 38 | + exir_ops.edge.aten.ge.Scalar, |
| 39 | + exir_ops.edge.aten.where.self, |
| 40 | + exir_ops.edge.aten.le.Scalar, |
| 41 | + exir_ops.edge.aten.logical_and.default, |
| 42 | + ) |
| 43 | + |
| 44 | + raise RuntimeError(f"Can't get expm1 decomposition for op {op}") |
| 45 | + |
| 46 | + |
| 47 | +class DecomposeExpm1Pass(ArmPass): |
| 48 | + """ |
| 49 | + A transformation pass that decomposes unsupported 'aten.expm1' operations |
| 50 | + into a combination of supported TOSA-equivalent operations. |
| 51 | +
|
| 52 | + Since TOSA does not provide a native expm1 operator, this pass rewrites: |
| 53 | + expm1(x) → where(and(ge(x, -0.35), le(x, 0.35)), {taylor_series_expansion}, (exp(x)-1)) |
| 54 | + where {taylor_series_expansion} = x + (x^2/2) + (x^3/6) + (x^4/24) |
| 55 | +
|
| 56 | + Supported input ops: |
| 57 | + - exir_ops.edge.aten.expm1.default(x) |
| 58 | +
|
| 59 | + These are replaced with: |
| 60 | + - exir_ops.edge.aten.pow.Tensor_Scalar, |
| 61 | + - exir_ops.edge.aten.div.Scalar, |
| 62 | + - exir_ops.edge.aten.add.Tensor, |
| 63 | + - exir_ops.edge.aten.exp.default, |
| 64 | + - exir_ops.edge.aten.sub.Scalar, |
| 65 | + - exir_ops.edge.aten.ge.Scalar, |
| 66 | + - exir_ops.edge.aten.where.self, |
| 67 | + - exir_ops.edge.aten.le.Scalar, |
| 68 | + - exir_ops.edge.aten.logical_and.default |
| 69 | + """ |
| 70 | + |
| 71 | + def call_operator(self, op, args, kwargs, meta): |
| 72 | + if op not in edge_expm1_ops: |
| 73 | + return super().call_operator(op, args, kwargs, meta, updated=False) |
| 74 | + |
| 75 | + ( |
| 76 | + op_pow, |
| 77 | + op_div, |
| 78 | + op_add, |
| 79 | + op_exp, |
| 80 | + op_sub, |
| 81 | + op_ge, |
| 82 | + op_where, |
| 83 | + op_le, |
| 84 | + op_and, |
| 85 | + ) = _get_expm1_decomposition(op) |
| 86 | + |
| 87 | + input = args[0] |
| 88 | + |
| 89 | + cutlo = -0.35 |
| 90 | + cuthi = 0.35 |
| 91 | + |
| 92 | + taylor_term_2_numerator = super().call_operator( |
| 93 | + op_pow, (input, 2), {}, meta, updated=False |
| 94 | + ) |
| 95 | + taylor_term_3_numerator = super().call_operator( |
| 96 | + op_pow, (input, 3), {}, meta, updated=False |
| 97 | + ) |
| 98 | + taylor_term_4_numerator = super().call_operator( |
| 99 | + op_pow, (input, 4), {}, meta, updated=False |
| 100 | + ) |
| 101 | + |
| 102 | + taylor_term_2 = super().call_operator( |
| 103 | + op_div, (taylor_term_2_numerator, 2), {}, meta, updated=False |
| 104 | + ) |
| 105 | + taylor_term_3 = super().call_operator( |
| 106 | + op_div, (taylor_term_3_numerator, 6), {}, meta, updated=False |
| 107 | + ) |
| 108 | + taylor_term_4 = super().call_operator( |
| 109 | + op_div, (taylor_term_4_numerator, 24), {}, meta, updated=False |
| 110 | + ) |
| 111 | + |
| 112 | + add_terms_1_2 = super().call_operator( |
| 113 | + op_add, (input, taylor_term_2), {}, meta, updated=False |
| 114 | + ) |
| 115 | + add_term_3 = super().call_operator( |
| 116 | + op_add, (add_terms_1_2, taylor_term_3), {}, meta, updated=False |
| 117 | + ) |
| 118 | + taylor_expansion = super().call_operator( |
| 119 | + op_add, (add_term_3, taylor_term_4), {}, meta, updated=False |
| 120 | + ) |
| 121 | + |
| 122 | + decomp_exp = super().call_operator(op_exp, (input,), {}, meta, updated=False) |
| 123 | + decomp_sub = super().call_operator( |
| 124 | + op_sub, (decomp_exp, 1.0), {}, meta, updated=False |
| 125 | + ) |
| 126 | + |
| 127 | + ge = super().call_operator(op_ge, (input, cutlo), {}, meta, updated=False) |
| 128 | + le = super().call_operator(op_le, (input, cuthi), {}, meta, updated=False) |
| 129 | + |
| 130 | + cond_and = super().call_operator(op_and, (ge, le), {}, meta, updated=False) |
| 131 | + where = super().call_operator( |
| 132 | + op_where, (cond_and, taylor_expansion, decomp_sub), {}, meta, updated=True |
| 133 | + ) |
| 134 | + |
| 135 | + return where |
0 commit comments