|
| 1 | +# Copyright (c) Intel Corporation |
| 2 | +# |
| 3 | +# Licensed under the BSD License (the "License"); you may not use this file |
| 4 | +# except in compliance with the License. See the license file 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 | +# Ops to match |
| 12 | +DIV_TENSOR_MODE_OPS = { |
| 13 | + exir_ops.edge.aten.div.Tensor_mode, |
| 14 | + torch.ops.aten.div.Tensor_mode, |
| 15 | +} |
| 16 | + |
| 17 | +# Replacement op sets per dialect |
| 18 | +EDGE_OPS = { |
| 19 | + "div": exir_ops.edge.aten.div.Tensor, |
| 20 | + "floor": exir_ops.edge.aten.floor.default, |
| 21 | + "to_copy": exir_ops.edge.aten._to_copy.default, |
| 22 | +} |
| 23 | + |
| 24 | +ATEN_OPS = { |
| 25 | + "div": torch.ops.aten.div.Tensor, |
| 26 | + "floor": torch.ops.aten.floor.default, |
| 27 | + "to_copy": torch.ops.aten._to_copy.default, |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +def _get_opset(op): |
| 32 | + if op is exir_ops.edge.aten.div.Tensor_mode: |
| 33 | + return EDGE_OPS |
| 34 | + if op is torch.ops.aten.div.Tensor_mode: |
| 35 | + return ATEN_OPS |
| 36 | + raise RuntimeError(f"Unexpected op: {op}") |
| 37 | + |
| 38 | + |
| 39 | +def _node_dtype(node): |
| 40 | + """Return the dtype of a graph node's output, or None if unknown.""" |
| 41 | + if isinstance(node, torch.fx.Node): |
| 42 | + val = node.meta.get("val") |
| 43 | + if val is not None: |
| 44 | + return val.dtype |
| 45 | + return None |
| 46 | + |
| 47 | + |
| 48 | +class DecomposeFloorDividePass(ExportPass): |
| 49 | + """Decompose div with rounding_mode='floor' for correct semantics. |
| 50 | +
|
| 51 | + ExecuTorch decomposes floor_divide into aten.div.Tensor_mode with |
| 52 | + rounding_mode='floor'. OpenVINO implements this with truncation-toward-zero |
| 53 | + semantics instead of PyTorch's floor-toward-negative-infinity. |
| 54 | +
|
| 55 | + For float inputs, replaces div(x, y, rounding_mode='floor') with |
| 56 | + floor(div(x, y)). |
| 57 | +
|
| 58 | + For integer inputs, OpenVINO's integer division truncates toward zero, so |
| 59 | + floor(int_div(x, y)) still gives truncation semantics. Instead we cast to |
| 60 | + float32, divide, floor, then cast back: |
| 61 | + _to_copy(floor(div(_to_copy(x, float32), _to_copy(y, float32))), int_dtype) |
| 62 | + """ |
| 63 | + |
| 64 | + def call(self, graph_module: torch.fx.GraphModule) -> PassResult: |
| 65 | + graph = graph_module.graph |
| 66 | + |
| 67 | + for node in list(graph.nodes): |
| 68 | + if node.op != "call_function": |
| 69 | + continue |
| 70 | + if node.target not in DIV_TENSOR_MODE_OPS: |
| 71 | + continue |
| 72 | + |
| 73 | + rounding_mode = node.kwargs.get("rounding_mode") |
| 74 | + if rounding_mode != "floor": |
| 75 | + continue |
| 76 | + |
| 77 | + opset = _get_opset(node.target) |
| 78 | + a, b = node.args[0], node.args[1] |
| 79 | + |
| 80 | + a_dtype = _node_dtype(a) |
| 81 | + is_integer = a_dtype is not None and not a_dtype.is_floating_point |
| 82 | + |
| 83 | + with graph.inserting_before(node): |
| 84 | + if is_integer: |
| 85 | + a_f = graph.call_function( |
| 86 | + opset["to_copy"], (a,), {"dtype": torch.float32} |
| 87 | + ) |
| 88 | + b_f = graph.call_function( |
| 89 | + opset["to_copy"], (b,), {"dtype": torch.float32} |
| 90 | + ) |
| 91 | + div_node = graph.call_function(opset["div"], (a_f, b_f)) |
| 92 | + floored = graph.call_function(opset["floor"], (div_node,)) |
| 93 | + result = graph.call_function( |
| 94 | + opset["to_copy"], (floored,), {"dtype": a_dtype} |
| 95 | + ) |
| 96 | + else: |
| 97 | + div_node = graph.call_function(opset["div"], (a, b)) |
| 98 | + result = graph.call_function(opset["floor"], (div_node,)) |
| 99 | + |
| 100 | + node.replace_all_uses_with(result) |
| 101 | + graph.erase_node(node) |
| 102 | + |
| 103 | + graph.eliminate_dead_code() |
| 104 | + graph_module.recompile() |
| 105 | + graph_module = super().call(graph_module).graph_module |
| 106 | + return PassResult(graph_module, True) |
0 commit comments