|
| 1 | +# Copyright (c) Qualcomm Innovation Center, Inc. |
| 2 | +# All rights reserved |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import torch |
| 8 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 9 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 10 | + |
| 11 | +from .utils import create_const_node, create_node |
| 12 | + |
| 13 | + |
| 14 | +class DecomposeHyperbolicVariants(ExportPass): |
| 15 | + """ |
| 16 | + Decompose hyperbolic functions into supported primitives: |
| 17 | + sinh(x) = 0.5 * (exp(x) - exp(-x)) |
| 18 | + cosh(x) = 0.5 * (exp(x) + exp(-x)) |
| 19 | + asinh(x) = log(x + sqrt(x*x + 1)) |
| 20 | + acosh(x) = log(x + sqrt(x*x - 1)) |
| 21 | + atanh(x) = 0.5 * log((1 + x) / (1 - x)) |
| 22 | + """ |
| 23 | + |
| 24 | + _EDGE_OPS = { |
| 25 | + exir_ops.edge.aten.sinh.default, |
| 26 | + exir_ops.edge.aten.cosh.default, |
| 27 | + exir_ops.edge.aten.asinh.default, |
| 28 | + exir_ops.edge.aten.acosh.default, |
| 29 | + exir_ops.edge.aten.atanh.default, |
| 30 | + } |
| 31 | + |
| 32 | + def __init__(self): |
| 33 | + super(DecomposeHyperbolicVariants, self).__init__() |
| 34 | + self._dispatcher = { |
| 35 | + # ATen dialect |
| 36 | + torch.ops.aten.sinh.default: self._decompose_sinh, |
| 37 | + torch.ops.aten.cosh.default: self._decompose_cosh, |
| 38 | + torch.ops.aten.asinh.default: self._decompose_asinh, |
| 39 | + torch.ops.aten.acosh.default: self._decompose_acosh, |
| 40 | + torch.ops.aten.atanh.default: self._decompose_atanh, |
| 41 | + # Edge dialect |
| 42 | + exir_ops.edge.aten.sinh.default: self._decompose_sinh, |
| 43 | + exir_ops.edge.aten.cosh.default: self._decompose_cosh, |
| 44 | + exir_ops.edge.aten.asinh.default: self._decompose_asinh, |
| 45 | + exir_ops.edge.aten.acosh.default: self._decompose_acosh, |
| 46 | + exir_ops.edge.aten.atanh.default: self._decompose_atanh, |
| 47 | + } |
| 48 | + |
| 49 | + def _get_ops(self, is_edge): |
| 50 | + if is_edge: |
| 51 | + return { |
| 52 | + "exp": exir_ops.edge.aten.exp.default, |
| 53 | + "neg": exir_ops.edge.aten.neg.default, |
| 54 | + "add": exir_ops.edge.aten.add.Tensor, |
| 55 | + "sub": exir_ops.edge.aten.sub.Tensor, |
| 56 | + "mul": exir_ops.edge.aten.mul.Tensor, |
| 57 | + "div": exir_ops.edge.aten.div.Tensor, |
| 58 | + "log": exir_ops.edge.aten.log.default, |
| 59 | + "sqrt": exir_ops.edge.aten.sqrt.default, |
| 60 | + } |
| 61 | + return { |
| 62 | + "exp": torch.ops.aten.exp.default, |
| 63 | + "neg": torch.ops.aten.neg.default, |
| 64 | + "add": torch.ops.aten.add.Tensor, |
| 65 | + "sub": torch.ops.aten.sub.Tensor, |
| 66 | + "mul": torch.ops.aten.mul.Tensor, |
| 67 | + "div": torch.ops.aten.div.Tensor, |
| 68 | + "log": torch.ops.aten.log.default, |
| 69 | + "sqrt": torch.ops.aten.sqrt.default, |
| 70 | + } |
| 71 | + |
| 72 | + def _decompose_exp_symmetry( |
| 73 | + self, node, graph, graph_module, const_cache, combine_op_key |
| 74 | + ): |
| 75 | + """Shared helper for sinh and cosh: (exp(x) ± exp(-x)) / 2.""" |
| 76 | + is_edge = node.target in self._EDGE_OPS |
| 77 | + ops = self._get_ops(is_edge) |
| 78 | + meta = node.meta |
| 79 | + input_node = node.args[0] |
| 80 | + |
| 81 | + if is_edge: |
| 82 | + half_name = "_half_constant" |
| 83 | + if half_name not in const_cache: |
| 84 | + const_cache[half_name] = create_const_node( |
| 85 | + graph, graph_module, half_name, 0.5, node |
| 86 | + ) |
| 87 | + half_arg = const_cache[half_name] |
| 88 | + else: |
| 89 | + half_arg = 0.5 |
| 90 | + |
| 91 | + with graph.inserting_before(node): |
| 92 | + exp_pos = create_node(graph, ops["exp"], (input_node,), meta) |
| 93 | + neg_x = create_node(graph, ops["neg"], (input_node,), meta) |
| 94 | + exp_neg = create_node(graph, ops["exp"], (neg_x,), meta) |
| 95 | + combine = create_node(graph, ops[combine_op_key], (exp_pos, exp_neg), meta) |
| 96 | + result = create_node(graph, ops["mul"], (combine, half_arg), meta) |
| 97 | + |
| 98 | + for user in node.users.copy(): |
| 99 | + user.replace_input_with(node, result) |
| 100 | + |
| 101 | + def _decompose_sinh(self, node, graph, graph_module, const_cache): |
| 102 | + self._decompose_exp_symmetry(node, graph, graph_module, const_cache, "sub") |
| 103 | + |
| 104 | + def _decompose_cosh(self, node, graph, graph_module, const_cache): |
| 105 | + self._decompose_exp_symmetry(node, graph, graph_module, const_cache, "add") |
| 106 | + |
| 107 | + def _decompose_asinh(self, node, graph, graph_module, const_cache): |
| 108 | + is_edge = node.target in self._EDGE_OPS |
| 109 | + ops = self._get_ops(is_edge) |
| 110 | + meta = node.meta |
| 111 | + input_node = node.args[0] |
| 112 | + |
| 113 | + if is_edge: |
| 114 | + one_name = "_one_constant" |
| 115 | + if one_name not in const_cache: |
| 116 | + const_cache[one_name] = create_const_node( |
| 117 | + graph, graph_module, one_name, 1.0, node |
| 118 | + ) |
| 119 | + one_arg = const_cache[one_name] |
| 120 | + else: |
| 121 | + one_arg = 1.0 |
| 122 | + |
| 123 | + with graph.inserting_before(node): |
| 124 | + x_sq = create_node(graph, ops["mul"], (input_node, input_node), meta) |
| 125 | + x_sq_plus_1 = create_node(graph, ops["add"], (x_sq, one_arg), meta) |
| 126 | + sqrt_node = create_node(graph, ops["sqrt"], (x_sq_plus_1,), meta) |
| 127 | + sum_node = create_node(graph, ops["add"], (input_node, sqrt_node), meta) |
| 128 | + result = create_node(graph, ops["log"], (sum_node,), meta) |
| 129 | + |
| 130 | + for user in node.users.copy(): |
| 131 | + user.replace_input_with(node, result) |
| 132 | + |
| 133 | + def _decompose_acosh(self, node, graph, graph_module, const_cache): |
| 134 | + is_edge = node.target in self._EDGE_OPS |
| 135 | + ops = self._get_ops(is_edge) |
| 136 | + meta = node.meta |
| 137 | + input_node = node.args[0] |
| 138 | + |
| 139 | + if is_edge: |
| 140 | + one_name = "_one_constant" |
| 141 | + if one_name not in const_cache: |
| 142 | + const_cache[one_name] = create_const_node( |
| 143 | + graph, graph_module, one_name, 1.0, node |
| 144 | + ) |
| 145 | + one_arg = const_cache[one_name] |
| 146 | + else: |
| 147 | + one_arg = 1.0 |
| 148 | + |
| 149 | + with graph.inserting_before(node): |
| 150 | + x_sq = create_node(graph, ops["mul"], (input_node, input_node), meta) |
| 151 | + x_sq_minus_1 = create_node(graph, ops["sub"], (x_sq, one_arg), meta) |
| 152 | + sqrt_node = create_node(graph, ops["sqrt"], (x_sq_minus_1,), meta) |
| 153 | + sum_node = create_node(graph, ops["add"], (input_node, sqrt_node), meta) |
| 154 | + result = create_node(graph, ops["log"], (sum_node,), meta) |
| 155 | + |
| 156 | + for user in node.users.copy(): |
| 157 | + user.replace_input_with(node, result) |
| 158 | + |
| 159 | + def _decompose_atanh(self, node, graph, graph_module, const_cache): |
| 160 | + is_edge = node.target in self._EDGE_OPS |
| 161 | + ops = self._get_ops(is_edge) |
| 162 | + meta = node.meta |
| 163 | + input_node = node.args[0] |
| 164 | + |
| 165 | + if is_edge: |
| 166 | + one_name = "_one_constant" |
| 167 | + if one_name not in const_cache: |
| 168 | + const_cache[one_name] = create_const_node( |
| 169 | + graph, graph_module, one_name, 1.0, node |
| 170 | + ) |
| 171 | + one_arg = const_cache[one_name] |
| 172 | + half_name = "_half_constant" |
| 173 | + if half_name not in const_cache: |
| 174 | + const_cache[half_name] = create_const_node( |
| 175 | + graph, graph_module, half_name, 0.5, node |
| 176 | + ) |
| 177 | + half_arg = const_cache[half_name] |
| 178 | + else: |
| 179 | + one_arg = 1.0 |
| 180 | + half_arg = 0.5 |
| 181 | + |
| 182 | + with graph.inserting_before(node): |
| 183 | + one_plus_x = create_node(graph, ops["add"], (one_arg, input_node), meta) |
| 184 | + one_minus_x = create_node(graph, ops["sub"], (one_arg, input_node), meta) |
| 185 | + ratio = create_node(graph, ops["div"], (one_plus_x, one_minus_x), meta) |
| 186 | + log_node = create_node(graph, ops["log"], (ratio,), meta) |
| 187 | + result = create_node(graph, ops["mul"], (half_arg, log_node), meta) |
| 188 | + |
| 189 | + for user in node.users.copy(): |
| 190 | + user.replace_input_with(node, result) |
| 191 | + |
| 192 | + def call(self, graph_module: torch.fx.GraphModule) -> PassResult: |
| 193 | + graph = graph_module.graph |
| 194 | + const_cache = {} |
| 195 | + |
| 196 | + for node in list(graph.nodes): |
| 197 | + if node.target in self._dispatcher: |
| 198 | + self._dispatcher[node.target](node, graph, graph_module, const_cache) |
| 199 | + |
| 200 | + graph.eliminate_dead_code() |
| 201 | + graph_module.recompile() |
| 202 | + return PassResult(graph_module, True) |
0 commit comments