|
| 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_atanh = exir_ops.edge.aten.atanh.default # MI case |
| 11 | + |
| 12 | + |
| 13 | +def _get_atanh_ops(op): |
| 14 | + """Return the primitive ops required..""" |
| 15 | + if op is not edge_atanh: |
| 16 | + raise RuntimeError(f"Can't decompose atanh for op {op}") |
| 17 | + return ( |
| 18 | + exir_ops.edge.aten.mul.Tensor, |
| 19 | + exir_ops.edge.aten.mul.Scalar, |
| 20 | + exir_ops.edge.aten.add.Scalar, |
| 21 | + exir_ops.edge.aten.reciprocal.default, |
| 22 | + exir_ops.edge.aten.log.default, |
| 23 | + exir_ops.edge.aten.neg.default, |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +class DecomposeAtanhPass(ArmPass): |
| 28 | + """ |
| 29 | + Decomposes the atanh operator into primitive ops. |
| 30 | + atanh(x) = 0.5 * log((1 + x) / (1 - x)) |
| 31 | + """ |
| 32 | + |
| 33 | + def call_operator(self, op, args, kwargs, meta): |
| 34 | + if op is not edge_atanh: |
| 35 | + return super().call_operator(op, args, kwargs, meta, updated=False) |
| 36 | + |
| 37 | + ops = _get_atanh_ops(op) |
| 38 | + ( |
| 39 | + op_mul_tensor, |
| 40 | + op_mul_scalar, |
| 41 | + op_add_scalar, |
| 42 | + op_reciprocal, |
| 43 | + op_log, |
| 44 | + op_neg, |
| 45 | + ) = ops |
| 46 | + |
| 47 | + x = args[0] |
| 48 | + |
| 49 | + nom = super().call_operator(op_add_scalar, (x, 1.0), {}, meta, updated=True) |
| 50 | + |
| 51 | + neg_x = super().call_operator(op_neg, (x,), {}, meta, updated=True) |
| 52 | + denom = super().call_operator( |
| 53 | + op_add_scalar, (neg_x, 1.0), {}, meta, updated=True |
| 54 | + ) |
| 55 | + recip = super().call_operator(op_reciprocal, (denom,), {}, meta, updated=True) |
| 56 | + |
| 57 | + log_input = super().call_operator( |
| 58 | + op_mul_tensor, (nom, recip), {}, meta, updated=True |
| 59 | + ) |
| 60 | + log = super().call_operator(op_log, (log_input,), {}, meta, updated=True) |
| 61 | + |
| 62 | + return super().call_operator(op_mul_scalar, (log, 0.5), {}, meta, updated=True) |
0 commit comments