|
| 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.dialects.edge._ops import EdgeOpOverload |
| 10 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 11 | + |
| 12 | +from .utils import copy_meta |
| 13 | + |
| 14 | + |
| 15 | +class DecomposeDivMode(ExportPass): |
| 16 | + """ |
| 17 | + Decompose aten.div.Tensor_mode into supported primitives. |
| 18 | +
|
| 19 | + div(x, y, rounding_mode=None) -> div(x, y) |
| 20 | + div(x, y, rounding_mode="trunc") -> trunc(div(x, y)) |
| 21 | + div(x, y, rounding_mode="floor") -> floor(div(x, y)) |
| 22 | +
|
| 23 | + Note: div.Scalar_mode is handled by LiftConstantScalarOperands which converts it to div.Tensor_mode before this pass runs. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self): |
| 27 | + super(DecomposeDivMode, self).__init__() |
| 28 | + self.targets = { |
| 29 | + torch.ops.aten.div.Tensor_mode, |
| 30 | + exir_ops.edge.aten.div.Tensor_mode, |
| 31 | + } |
| 32 | + |
| 33 | + def call(self, graph_module: torch.fx.GraphModule): |
| 34 | + graph = graph_module.graph |
| 35 | + |
| 36 | + for node in list(graph.nodes): |
| 37 | + if node.op == "call_function" and node.target in self.targets: |
| 38 | + is_edge = isinstance(node.target, EdgeOpOverload) |
| 39 | + meta = node.meta |
| 40 | + |
| 41 | + x_node = node.args[0] |
| 42 | + y_node = node.args[1] |
| 43 | + |
| 44 | + rounding_mode = node.kwargs.get("rounding_mode", None) |
| 45 | + if rounding_mode is None and len(node.args) > 2: |
| 46 | + rounding_mode = node.args[2] |
| 47 | + |
| 48 | + div_op = ( |
| 49 | + exir_ops.edge.aten.div.Tensor |
| 50 | + if is_edge |
| 51 | + else torch.ops.aten.div.Tensor |
| 52 | + ) |
| 53 | + |
| 54 | + with graph.inserting_before(node): |
| 55 | + # Step 1: div_result = div(x, y) |
| 56 | + div_node = graph.create_node( |
| 57 | + "call_function", div_op, (x_node, y_node) |
| 58 | + ) |
| 59 | + div_node.meta = copy_meta(meta) |
| 60 | + |
| 61 | + # Step 2: Apply rounding mode if needed |
| 62 | + if rounding_mode == "trunc": |
| 63 | + trunc_op = ( |
| 64 | + exir_ops.edge.aten.trunc.default |
| 65 | + if is_edge |
| 66 | + else torch.ops.aten.trunc.default |
| 67 | + ) |
| 68 | + result_node = graph.create_node( |
| 69 | + "call_function", trunc_op, (div_node,) |
| 70 | + ) |
| 71 | + result_node.meta = copy_meta(meta) |
| 72 | + elif rounding_mode == "floor": |
| 73 | + floor_op = ( |
| 74 | + exir_ops.edge.aten.floor.default |
| 75 | + if is_edge |
| 76 | + else torch.ops.aten.floor.default |
| 77 | + ) |
| 78 | + result_node = graph.create_node( |
| 79 | + "call_function", floor_op, (div_node,) |
| 80 | + ) |
| 81 | + result_node.meta = copy_meta(meta) |
| 82 | + else: |
| 83 | + # rounding_mode=None: plain division |
| 84 | + result_node = div_node |
| 85 | + |
| 86 | + for user in node.users.copy(): |
| 87 | + user.replace_input_with(node, result_node) |
| 88 | + |
| 89 | + graph.eliminate_dead_code() |
| 90 | + graph_module.recompile() |
| 91 | + return PassResult(graph_module, True) |
0 commit comments