|
| 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 | +# pyre-unsafe |
| 7 | + |
| 8 | +from executorch.backends.arm._passes import ArmPass |
| 9 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 10 | + |
| 11 | +# For MI case |
| 12 | +edge_acosh_op = exir_ops.edge.aten.acosh.default |
| 13 | + |
| 14 | + |
| 15 | +class DecomposeAcoshPass(ArmPass): |
| 16 | + """ |
| 17 | + Decomposes acosh to supported TOSA-operations. |
| 18 | + This decomposition is based on the mathematical identity: |
| 19 | + acosh(x) = log(x + sqrt((x-1)(x+1)) |
| 20 | + """ |
| 21 | + |
| 22 | + def call_operator(self, op, args, kwargs, meta, updated=False): |
| 23 | + |
| 24 | + if op is not edge_acosh_op: |
| 25 | + return super().call_operator(op, args, kwargs, meta, updated) |
| 26 | + |
| 27 | + log_op, sqrt_op, mul_op, sub_op, add_op, add_op_scalar = ( |
| 28 | + exir_ops.edge.aten.log.default, |
| 29 | + exir_ops.edge.aten.sqrt.default, |
| 30 | + exir_ops.edge.aten.mul.Tensor, |
| 31 | + exir_ops.edge.aten.sub.Scalar, |
| 32 | + exir_ops.edge.aten.add.Tensor, |
| 33 | + exir_ops.edge.aten.add.Scalar, |
| 34 | + ) |
| 35 | + |
| 36 | + x = args[0] |
| 37 | + |
| 38 | + # (x-1)(x+1) |
| 39 | + sub = super().call_operator(sub_op, (x, 1.0), {}, meta, True) |
| 40 | + add = super().call_operator(add_op_scalar, (x, 1.0), {}, meta, True) |
| 41 | + mul = super().call_operator(mul_op, (sub, add), {}, meta, True) |
| 42 | + |
| 43 | + # sqrt((x-1)(x+1)) |
| 44 | + sqrt = super().call_operator(sqrt_op, (mul,), {}, meta, True) |
| 45 | + |
| 46 | + # x + sqrt((x-1)(x+1)) |
| 47 | + add = super().call_operator(add_op, (x, sqrt), {}, meta, True) |
| 48 | + |
| 49 | + # out = ln(x + sqrt((x-1)(x+1)) |
| 50 | + out = super().call_operator(log_op, (add,), {}, meta, True) |
| 51 | + |
| 52 | + return out |
0 commit comments