|
| 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 math |
| 8 | +from functools import partial |
| 9 | + |
| 10 | +import torch |
| 11 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 12 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 13 | + |
| 14 | +from .utils import copy_meta, get_const_node |
| 15 | + |
| 16 | + |
| 17 | +class DecomposeLogVariants(ExportPass): |
| 18 | + """ |
| 19 | + Decompose log variants [log10, log2, log1p] operations using the identities: |
| 20 | + log10(x) = log(x) / log(10) |
| 21 | + log2(x) = log(x) / log(2) |
| 22 | + log1p(x) = log(1 + x) |
| 23 | + """ |
| 24 | + |
| 25 | + _EDGE_OPS = { |
| 26 | + exir_ops.edge.aten.log10.default, |
| 27 | + exir_ops.edge.aten.log2.default, |
| 28 | + exir_ops.edge.aten.log1p.default, |
| 29 | + } |
| 30 | + |
| 31 | + def __init__(self) -> None: |
| 32 | + super().__init__() |
| 33 | + self._dispatcher = { |
| 34 | + # Edge dialect (post-to_edge) - FP |
| 35 | + exir_ops.edge.aten.log10.default: partial(self._decompose_log_n, n=10), |
| 36 | + exir_ops.edge.aten.log2.default: partial(self._decompose_log_n, n=2), |
| 37 | + exir_ops.edge.aten.log1p.default: partial(self._decompose_log_p, p=1), |
| 38 | + # ATen dialect (pre-to_edge) - Quantized |
| 39 | + torch.ops.aten.log10.default: partial(self._decompose_log_n, n=10), |
| 40 | + torch.ops.aten.log2.default: partial(self._decompose_log_n, n=2), |
| 41 | + torch.ops.aten.log1p.default: partial(self._decompose_log_p, p=1), |
| 42 | + } |
| 43 | + |
| 44 | + def _decompose_log_n(self, node, graph, graph_module, n): |
| 45 | + input_node = node.args[0] |
| 46 | + is_edge = node.target in self._EDGE_OPS |
| 47 | + |
| 48 | + if is_edge: |
| 49 | + log_op = exir_ops.edge.aten.log.default |
| 50 | + div_op = exir_ops.edge.aten.div.Tensor |
| 51 | + div_arg = get_const_node( |
| 52 | + graph, |
| 53 | + graph_module, |
| 54 | + f"_log_base_{n}_constant", |
| 55 | + math.log(n), |
| 56 | + node, |
| 57 | + ) |
| 58 | + |
| 59 | + else: |
| 60 | + log_op = torch.ops.aten.log.default |
| 61 | + div_op = torch.ops.aten.div.Tensor |
| 62 | + div_arg = math.log(n) |
| 63 | + |
| 64 | + with graph.inserting_after(input_node): |
| 65 | + log_node = graph.create_node("call_function", log_op, (input_node,)) |
| 66 | + log_node.meta = copy_meta(node.meta) |
| 67 | + |
| 68 | + with graph.inserting_after(log_node): |
| 69 | + div_node = graph.create_node( |
| 70 | + "call_function", div_op, (log_node, div_arg) |
| 71 | + ) |
| 72 | + div_node.meta = copy_meta(node.meta) |
| 73 | + |
| 74 | + for user in node.users.copy(): |
| 75 | + user.replace_input_with(node, div_node) |
| 76 | + |
| 77 | + def _decompose_log_p(self, node, graph, graph_module, p): |
| 78 | + input_node = node.args[0] |
| 79 | + is_edge = node.target in self._EDGE_OPS |
| 80 | + |
| 81 | + if is_edge: |
| 82 | + add_op = exir_ops.edge.aten.add.Tensor |
| 83 | + log_op = exir_ops.edge.aten.log.default |
| 84 | + add_arg = get_const_node( |
| 85 | + graph, |
| 86 | + graph_module, |
| 87 | + f"_log1p_addend_{p}_constant", |
| 88 | + p, |
| 89 | + node, |
| 90 | + ) |
| 91 | + |
| 92 | + else: |
| 93 | + add_op = torch.ops.aten.add.Tensor |
| 94 | + log_op = torch.ops.aten.log.default |
| 95 | + add_arg = p |
| 96 | + |
| 97 | + with graph.inserting_after(input_node): |
| 98 | + add_node = graph.create_node("call_function", add_op, (input_node, add_arg)) |
| 99 | + add_node.meta = copy_meta(node.meta) |
| 100 | + |
| 101 | + with graph.inserting_after(add_node): |
| 102 | + log_node = graph.create_node("call_function", log_op, (add_node,)) |
| 103 | + log_node.meta = copy_meta(node.meta) |
| 104 | + |
| 105 | + for user in node.users.copy(): |
| 106 | + user.replace_input_with(node, log_node) |
| 107 | + |
| 108 | + def call(self, graph_module: torch.fx.GraphModule) -> PassResult: |
| 109 | + graph = graph_module.graph |
| 110 | + |
| 111 | + for node in list(graph.nodes): |
| 112 | + if node.target in self._dispatcher: |
| 113 | + self._dispatcher[node.target](node, graph, graph_module) |
| 114 | + |
| 115 | + graph.eliminate_dead_code() |
| 116 | + graph_module.recompile() |
| 117 | + return PassResult(graph_module, True) |
0 commit comments