|
| 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 torch |
| 7 | +from executorch.backends.arm._passes.arm_pass_utils import create_node |
| 8 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 9 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 10 | + |
| 11 | +DTYPE_RANK = { |
| 12 | + torch.bool: 0, |
| 13 | + torch.uint8: 1, |
| 14 | + torch.int8: 2, |
| 15 | + torch.int16: 3, |
| 16 | + torch.int32: 4, |
| 17 | + torch.int64: 5, |
| 18 | + torch.float16: 6, |
| 19 | + torch.float32: 7, |
| 20 | + torch.float64: 8, |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +def get_largest_dtype(dtype_1, dtype_2): |
| 25 | + """Find the largest dtype.""" |
| 26 | + return dtype_1 if DTYPE_RANK[dtype_1] > DTYPE_RANK[dtype_2] else dtype_2 |
| 27 | + |
| 28 | + |
| 29 | +class MatchWhereSelfDtypePass(ExportPass): |
| 30 | + """Pass to match data types of non-condition input tensors. |
| 31 | +
|
| 32 | + Edge dialect allows different data types for non-condition tensors, while TOSA |
| 33 | + does not. In cases where they differ a TOSA CAST operator is inserted. |
| 34 | +
|
| 35 | + There is an edge case where one input is `boolean`, which cannot be directly cast |
| 36 | + to, for example, float32. When this occurs two CAST operators are added to first |
| 37 | + cast to int8 and then to the correct target data type. |
| 38 | +
|
| 39 | + """ |
| 40 | + |
| 41 | + def call(self, graph_module: torch.fx.GraphModule): |
| 42 | + modified_graph = False |
| 43 | + graph = graph_module.graph |
| 44 | + node_list = graph.find_nodes( |
| 45 | + op="call_function", target=exir_ops.edge.aten.where.self |
| 46 | + ) |
| 47 | + for node in node_list: |
| 48 | + cond, input_, other_ = node.args |
| 49 | + |
| 50 | + input_dtype = input_.meta["val"].dtype |
| 51 | + other_dtype = other_.meta["val"].dtype |
| 52 | + target_dtype = torch.float32 |
| 53 | + if input_dtype != other_dtype: |
| 54 | + target_dtype = get_largest_dtype(input_dtype, other_dtype) |
| 55 | + |
| 56 | + for arg in node.args[1:]: |
| 57 | + arg_dtype = arg.meta["val"].dtype |
| 58 | + |
| 59 | + if arg_dtype != target_dtype: |
| 60 | + if arg_dtype == torch.bool: |
| 61 | + # Bool is an edge case which cannot necessarily be directly |
| 62 | + # converted to the target data type. |
| 63 | + with graph.inserting_after(arg): |
| 64 | + replace_node_int8 = create_node( |
| 65 | + graph, |
| 66 | + exir_ops.edge.dim_order_ops._to_dim_order_copy.default, |
| 67 | + ) |
| 68 | + replace_node_int8.args = (arg,) |
| 69 | + replace_node_int8.kwargs = {"dtype": torch.int8} |
| 70 | + |
| 71 | + with graph.inserting_after(replace_node_int8): |
| 72 | + replace_node_fp32 = create_node( |
| 73 | + graph, |
| 74 | + exir_ops.edge.dim_order_ops._to_dim_order_copy.default, |
| 75 | + ) |
| 76 | + replace_node_fp32.args = (replace_node_int8,) |
| 77 | + replace_node_fp32.kwargs = {"dtype": target_dtype} |
| 78 | + node.replace_input_with(arg, replace_node_fp32) |
| 79 | + else: |
| 80 | + with graph.inserting_after(arg): |
| 81 | + replace_node = create_node( |
| 82 | + graph, |
| 83 | + exir_ops.edge.dim_order_ops._to_dim_order_copy.default, |
| 84 | + ) |
| 85 | + replace_node.args = (arg,) |
| 86 | + replace_node.kwargs = {"dtype": target_dtype} |
| 87 | + node.replace_input_with(arg, replace_node) |
| 88 | + |
| 89 | + modified_graph = True |
| 90 | + |
| 91 | + if modified_graph: |
| 92 | + graph_module.recompile() |
| 93 | + graph_module = super().call(graph_module).graph_module |
| 94 | + |
| 95 | + return PassResult(graph_module, modified_graph) |
0 commit comments