|
| 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 | +# pyre-unsafe |
| 7 | + |
| 8 | +import typing |
| 9 | + |
| 10 | +import torch |
| 11 | +import torch.fx as fx |
| 12 | +from executorch.backends.arm._passes.arm_pass_utils import get_first_fake_tensor |
| 13 | +from executorch.backends.arm._passes.insert_table_ops import TableOps |
| 14 | +from executorch.exir.backend.utils import WhyNoPartitionReporter |
| 15 | + |
| 16 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 17 | +from torch.fx.passes.operator_support import OperatorSupportBase |
| 18 | + |
| 19 | + |
| 20 | +class EthosU55DtypeSupport(OperatorSupportBase): |
| 21 | + |
| 22 | + def __init__(self, reporter: WhyNoPartitionReporter): |
| 23 | + super().__init__() |
| 24 | + self.reporter = reporter |
| 25 | + |
| 26 | + targeted_ops_i8_i16_i32 = [ |
| 27 | + exir_ops.edge.aten.cat.default, |
| 28 | + exir_ops.edge.aten.repeat.default, |
| 29 | + exir_ops.edge.aten.constant_pad_nd.default, |
| 30 | + exir_ops.edge.aten.view.default, |
| 31 | + exir_ops.edge.aten.permute.default, |
| 32 | + ] |
| 33 | + |
| 34 | + target_ops_i8 = tuple(TableOps.included_ops()) |
| 35 | + |
| 36 | + def _try_determine_dtype(self, node: fx.Node) -> torch.dtype | None: |
| 37 | + """Attempt to figure out the quantized data type of node. On failure, return None.""" |
| 38 | + |
| 39 | + dtype = get_first_fake_tensor(node).dtype |
| 40 | + if not dtype.is_floating_point: |
| 41 | + return dtype |
| 42 | + |
| 43 | + if ( |
| 44 | + node.target |
| 45 | + is exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default |
| 46 | + ): |
| 47 | + return get_first_fake_tensor(node.all_input_nodes[0]).dtype |
| 48 | + |
| 49 | + if len(node.users) == 0: |
| 50 | + return None |
| 51 | + |
| 52 | + q_node = list(node.users)[0] |
| 53 | + if ( |
| 54 | + q_node.target |
| 55 | + is exir_ops.edge.quantized_decomposed.quantize_per_tensor.default |
| 56 | + ): |
| 57 | + return typing.cast(torch.dtype, q_node.args[-1]) |
| 58 | + |
| 59 | + # We can't easily figure out dtype, return None |
| 60 | + return None |
| 61 | + |
| 62 | + def is_node_supported( # noqa: C901 |
| 63 | + self, submodules: typing.Mapping[str, torch.nn.Module], node: fx.Node |
| 64 | + ) -> bool: |
| 65 | + |
| 66 | + dtype = self._try_determine_dtype(node) |
| 67 | + if dtype is None: |
| 68 | + # If we couldn't determine dtype, just return ok. |
| 69 | + return True |
| 70 | + |
| 71 | + if node.target in self.targeted_ops_i8_i16_i32: |
| 72 | + if dtype not in (torch.int8, torch.int16, torch.int32): |
| 73 | + self.reporter.report_reject( |
| 74 | + node, f"Unsupported dtype {dtype} (Supports i8, i16, i32)." |
| 75 | + ) |
| 76 | + return False |
| 77 | + |
| 78 | + if node.target in self.target_ops_i8: |
| 79 | + if dtype not in (torch.int8,): |
| 80 | + self.reporter.report_reject( |
| 81 | + node, f"Unsupported dtype {dtype} (Supports i8)." |
| 82 | + ) |
| 83 | + return False |
| 84 | + |
| 85 | + if node.target == exir_ops.edge.aten.convolution.default: |
| 86 | + ifm, weight = node.all_input_nodes[0:2] |
| 87 | + ifm_dtype = self._try_determine_dtype(ifm) |
| 88 | + if ifm_dtype is not None and ifm_dtype not in (torch.int8, torch.int16): |
| 89 | + self.reporter.report_reject( |
| 90 | + node, f"Unsupported input dtype {dtype} (Supports i8, i16)." |
| 91 | + ) |
| 92 | + return False |
| 93 | + weight_dtype = self._try_determine_dtype(weight) |
| 94 | + if weight_dtype is not None and weight_dtype not in (torch.int8,): |
| 95 | + self.reporter.report_reject( |
| 96 | + node, f"Unsupported weight dtype {dtype} (Supports i8)." |
| 97 | + ) |
| 98 | + return False |
| 99 | + if len(node.all_input_nodes) > 2: |
| 100 | + bias = node.all_input_nodes[2] |
| 101 | + bias_dtype = self._try_determine_dtype(bias) |
| 102 | + if bias_dtype is not None and bias_dtype not in (torch.int32,): |
| 103 | + self.reporter.report_reject( |
| 104 | + node, f"Unsupported bias dtype {dtype} (Supports i32)." |
| 105 | + ) |
| 106 | + return False |
| 107 | + |
| 108 | + if node.target in ( |
| 109 | + exir_ops.edge.aten.mm.default, |
| 110 | + exir_ops.edge.aten.bmm.default, |
| 111 | + ): |
| 112 | + for input_node in node.all_input_nodes: |
| 113 | + dtype = self._try_determine_dtype(input_node) |
| 114 | + if dtype is not None and dtype != torch.int8: |
| 115 | + self.reporter.report_reject( |
| 116 | + input_node, |
| 117 | + f"Input {input_node.name} has unsupported dtype {dtype} (Supports i8).", |
| 118 | + ) |
| 119 | + return False |
| 120 | + |
| 121 | + return True |
| 122 | + |
| 123 | + |
| 124 | +class EthosU55NotSupported(OperatorSupportBase): |
| 125 | + """ |
| 126 | + Certain operators are not supported on U55. These are listed in `unsupported_ops`. |
| 127 | + The comment mentions the unsupported TOSA operator that the aten operator maps to where it is not obvious. |
| 128 | + For unimplemented operators, this is the anticipated mapping, and it might be incorrect. |
| 129 | + """ |
| 130 | + |
| 131 | + unsupported_ops = [ |
| 132 | + exir_ops.edge.aten.any.default, # REDUCE_ANY |
| 133 | + exir_ops.edge.aten.any.dim, # REDUCE_ANY |
| 134 | + exir_ops.edge.aten.any.dims, # REDUCE_ANY |
| 135 | + exir_ops.edge.aten.bitwise_and.Tensor, |
| 136 | + exir_ops.edge.aten.bitwise_or.Tensor, |
| 137 | + exir_ops.edge.aten.bitwise_xor.Tensor, |
| 138 | + exir_ops.edge.aten.bitwise_not, |
| 139 | + exir_ops.edge.aten.logical_and.default, |
| 140 | + exir_ops.edge.aten.logical_or.default, |
| 141 | + exir_ops.edge.aten.logical_xor.default, |
| 142 | + exir_ops.edge.aten.logical_not.default, |
| 143 | + exir_ops.edge.aten.amax.default, # REDUCE_MAX |
| 144 | + exir_ops.edge.aten.amin.default, # REDUCE_MIN |
| 145 | + exir_ops.edge.aten.eq.Tensor, |
| 146 | + exir_ops.edge.aten.eq.Scalar, |
| 147 | + exir_ops.edge.aten.ge.Tensor, |
| 148 | + exir_ops.edge.aten.gt.Tensor, |
| 149 | + exir_ops.edge.aten.le.Tensor, |
| 150 | + exir_ops.edge.aten.lt.Tensor, |
| 151 | + exir_ops.edge.aten.flip.default, # REVERSE |
| 152 | + exir_ops.edge.aten.grid_sampler_2d, # GATHER |
| 153 | + exir_ops.edge.aten.scatter.src, |
| 154 | + exir_ops.edge.aten.scatter.value, |
| 155 | + exir_ops.edge.aten.select_scatter.default, |
| 156 | + exir_ops.edge.aten.scatter_reduce.two, |
| 157 | + exir_ops.edge.aten.scatter_add.default, |
| 158 | + exir_ops.edge.aten.upsample_nearest2d.vec, # RESIZE |
| 159 | + exir_ops.edge.aten.upsample_bilinear2d.vec, # RESIZE |
| 160 | + exir_ops.edge.aten.reflection_pad1d.default, # REVERSE |
| 161 | + exir_ops.edge.aten.reflection_pad2d.default, # REVERSE |
| 162 | + exir_ops.edge.aten.reflection_pad3d.default, # REVERSE |
| 163 | + ] |
| 164 | + |
| 165 | + def __init__(self, reporter: WhyNoPartitionReporter): |
| 166 | + self.reporter = reporter |
| 167 | + |
| 168 | + def is_node_supported( |
| 169 | + self, submodules: typing.Mapping[str, torch.nn.Module], node: fx.Node |
| 170 | + ) -> bool: |
| 171 | + |
| 172 | + if node.target in self.unsupported_ops: |
| 173 | + self.reporter.report_reject(node, "Op is not supported on U55.") |
| 174 | + return False |
| 175 | + |
| 176 | + return True |
0 commit comments