|  | 
|  | 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 | +import logging | 
|  | 7 | +from math import pi | 
|  | 8 | + | 
|  | 9 | +from executorch.backends.arm._passes import ArmPass | 
|  | 10 | +from executorch.exir.dialects._ops import ops as exir_ops | 
|  | 11 | + | 
|  | 12 | + | 
|  | 13 | +edge_atan = exir_ops.edge.aten.atan.default  # MI case | 
|  | 14 | + | 
|  | 15 | + | 
|  | 16 | +def _get_atan_ops(op): | 
|  | 17 | +    """Return the primitive ops required..""" | 
|  | 18 | +    if op is not edge_atan: | 
|  | 19 | +        raise RuntimeError(f"Can't decompose atan for op {op}") | 
|  | 20 | + | 
|  | 21 | +    return ( | 
|  | 22 | +        exir_ops.edge.aten.mul.Tensor, | 
|  | 23 | +        exir_ops.edge.aten.mul.Scalar, | 
|  | 24 | +        exir_ops.edge.aten.add.Tensor, | 
|  | 25 | +        exir_ops.edge.aten.add.Scalar, | 
|  | 26 | +        exir_ops.edge.aten.sub.Tensor, | 
|  | 27 | +        exir_ops.edge.aten.abs.default, | 
|  | 28 | +        exir_ops.edge.aten.gt.Scalar, | 
|  | 29 | +        exir_ops.edge.aten.reciprocal.default, | 
|  | 30 | +        exir_ops.edge.aten.where.self, | 
|  | 31 | +        exir_ops.edge.aten.neg.default, | 
|  | 32 | +    ) | 
|  | 33 | + | 
|  | 34 | + | 
|  | 35 | +class DecomposeAtanPass(ArmPass): | 
|  | 36 | +    """Decomposes the atan operator into a rational (Padé) approximation.""" | 
|  | 37 | + | 
|  | 38 | +    def _rational_approximation(self, z, ops, meta): | 
|  | 39 | +        """Creates a (2,1) Padé approximation for atan(x) on [-1, 1].""" | 
|  | 40 | + | 
|  | 41 | +        op_mul, op_mul_scalar, op_add, op_add_scalar, _, _, _, op_recip, _, _ = ops | 
|  | 42 | + | 
|  | 43 | +        # Coefficients calculated using minimax on the interval [-1, 1]. | 
|  | 44 | +        a1 = 0.3529666667 | 
|  | 45 | +        a2 = -0.0287666667 | 
|  | 46 | +        b1 = 0.6863 | 
|  | 47 | + | 
|  | 48 | +        z2 = super().call_operator(op_mul, (z, z), {}, meta, updated=True) | 
|  | 49 | +        z4 = super().call_operator(op_mul, (z2, z2), {}, meta, updated=True) | 
|  | 50 | + | 
|  | 51 | +        num1 = super().call_operator(op_mul_scalar, (z2, a1), {}, meta, updated=True) | 
|  | 52 | +        num2 = super().call_operator(op_mul_scalar, (z4, a2), {}, meta, updated=True) | 
|  | 53 | +        num = super().call_operator(op_add_scalar, (num1, 1.0), {}, meta, updated=True) | 
|  | 54 | +        num = super().call_operator(op_add, (num, num2), {}, meta, updated=True) | 
|  | 55 | + | 
|  | 56 | +        den1 = super().call_operator(op_mul_scalar, (z2, b1), {}, meta, updated=True) | 
|  | 57 | +        den = super().call_operator(op_add_scalar, (den1, 1.0), {}, meta, updated=True) | 
|  | 58 | + | 
|  | 59 | +        inv_den = super().call_operator(op_recip, (den,), {}, meta, updated=True) | 
|  | 60 | + | 
|  | 61 | +        prod = super().call_operator(op_mul, (num, inv_den), {}, meta, updated=True) | 
|  | 62 | +        return super().call_operator(op_mul, (z, prod), {}, meta, updated=True) | 
|  | 63 | + | 
|  | 64 | +    def call_operator(self, op, args, kwargs, meta): | 
|  | 65 | +        if op is not edge_atan: | 
|  | 66 | +            return super().call_operator(op, args, kwargs, meta, updated=False) | 
|  | 67 | + | 
|  | 68 | +        logging.info( | 
|  | 69 | +            f"Approximating atan. This may introduce small numerical errors. For details, see {__file__}." | 
|  | 70 | +        ) | 
|  | 71 | + | 
|  | 72 | +        ops = _get_atan_ops(op) | 
|  | 73 | +        ( | 
|  | 74 | +            _, | 
|  | 75 | +            op_mul_scalar, | 
|  | 76 | +            _, | 
|  | 77 | +            op_add_scalar, | 
|  | 78 | +            op_sub, | 
|  | 79 | +            op_abs, | 
|  | 80 | +            op_gt, | 
|  | 81 | +            op_recip, | 
|  | 82 | +            op_where, | 
|  | 83 | +            op_neg, | 
|  | 84 | +        ) = ops | 
|  | 85 | + | 
|  | 86 | +        x = args[0] | 
|  | 87 | + | 
|  | 88 | +        # |x| > 1 is reduced to [0, 1] using atan(x) = pi/2 - atan(1/x) and atan(-x) = -atan(x). | 
|  | 89 | + | 
|  | 90 | +        abs_x = super().call_operator(op_abs, (x,), {}, meta, updated=True) | 
|  | 91 | +        mask_hi = super().call_operator(op_gt, (abs_x, 1.0), {}, meta, updated=True) | 
|  | 92 | + | 
|  | 93 | +        inv_x = super().call_operator(op_recip, (abs_x,), {}, meta, updated=True) | 
|  | 94 | +        z = super().call_operator( | 
|  | 95 | +            op_where, (mask_hi, inv_x, abs_x), {}, meta, updated=True | 
|  | 96 | +        ) | 
|  | 97 | + | 
|  | 98 | +        atan_z = self._rational_approximation(z, ops, meta) | 
|  | 99 | + | 
|  | 100 | +        zero_tensor = super().call_operator( | 
|  | 101 | +            op_mul_scalar, (x, 0.0), {}, meta, updated=True | 
|  | 102 | +        ) | 
|  | 103 | +        half_pi_tensor = super().call_operator( | 
|  | 104 | +            op_add_scalar, (zero_tensor, pi / 2), {}, meta, updated=True | 
|  | 105 | +        ) | 
|  | 106 | + | 
|  | 107 | +        diff = super().call_operator( | 
|  | 108 | +            op_sub, (half_pi_tensor, atan_z), {}, meta, updated=True | 
|  | 109 | +        ) | 
|  | 110 | +        atan_abs = super().call_operator( | 
|  | 111 | +            op_where, (mask_hi, diff, atan_z), {}, meta, updated=True | 
|  | 112 | +        ) | 
|  | 113 | + | 
|  | 114 | +        mask_pos = super().call_operator(op_gt, (x, 0.0), {}, meta, updated=True) | 
|  | 115 | +        neg_val = super().call_operator(op_neg, (atan_abs,), {}, meta, updated=True) | 
|  | 116 | + | 
|  | 117 | +        return super().call_operator( | 
|  | 118 | +            op_where, (mask_pos, atan_abs, neg_val), {}, meta, updated=True | 
|  | 119 | +        ) | 
0 commit comments