Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame^] | 1 | # Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved. |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the License); you may |
| 6 | # not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | |
| 18 | # Description: |
| 19 | # The SupportedOperators class which is a collection of all supported operators and parameter checks. |
| 20 | |
| 21 | from .data_type import BaseType |
| 22 | |
| 23 | |
| 24 | class SupportedOperators: |
| 25 | def __init__(self): |
| 26 | # Categorised lists of supported operators |
| 27 | self.npu_pre_ops = set(("QuantizedResizeBilinear", "SplitSliceRead")) |
| 28 | self.convolution_ops = set(("Conv2DBiasAct", "Conv2D", "QuantizedConv2D", "Conv2DBackpropInputSwitched")) |
| 29 | self.depthwise_convolution_ops = set( |
| 30 | ("DepthwiseConv2dBiasAct", "DepthwiseConv2dNative", "QuantizedDepthwiseConv2D") |
| 31 | ) |
| 32 | self.max_pooling_ops = set(("QuantizedMaxPool", "MaxPool", "MaxPoolAct")) |
| 33 | self.avg_pooling_ops = set(("QuantizedAvgPool", "AvgPool", "AvgPoolAct")) |
| 34 | self.pooling_ops = self.max_pooling_ops | self.avg_pooling_ops |
| 35 | self.fc_vector_products = set(("QuantizedMatMul", "MatMul", "FullyConnectedAct")) |
| 36 | self.mac_main_ops = ( |
| 37 | # convolutions |
| 38 | self.convolution_ops |
| 39 | # depth-wise convolutions |
| 40 | | self.depthwise_convolution_ops |
| 41 | # pooling |
| 42 | | self.pooling_ops |
| 43 | # FC layers |
| 44 | | self.fc_vector_products |
| 45 | # RNN/LSTM/GRU |
| 46 | | set(("BlockLSTM")) |
| 47 | ) |
| 48 | self.elem_wise_main_ops = set( |
| 49 | ( |
| 50 | # element-wise |
| 51 | "AddAct", |
| 52 | "MulAct", |
| 53 | "SubAct", |
| 54 | "QuantizedAdd", |
| 55 | "QuantizedSub", |
| 56 | "QuantizedMul", |
| 57 | "Mul", |
| 58 | "Add", |
| 59 | "Sub", |
| 60 | "Minimum", |
| 61 | "Maximum", |
| 62 | ) |
| 63 | ) |
| 64 | self.activation_ops = set( |
| 65 | ("QuantizedRelu", "QuantizedRelu1", "QuantizedRelu6", "Relu", "Relu6", "ReluN1To1", "Sigmoid", "Tanh") |
| 66 | ) |
| 67 | self.npu_post_ops = ( |
| 68 | # activation functions |
| 69 | self.activation_ops |
| 70 | # concatenation write direction |
| 71 | | set(("ConcatSliceWrite")) |
| 72 | # bias add and batch norm |
| 73 | | set(("QuantizedBiasAdd", "Requantize", "QuantizedBatchNorm", "BiasAdd", "FusedBatchNorm")) |
| 74 | ) |
| 75 | self.split_ops = set(("Split", "StridedSlice", "Slice", "UnpackReshaped", "Unpack")) |
| 76 | self.concat_ops = set(("Concat", "ConcatV2", "QuantizedConcat", "ConcatTFLite", "PackReshaped", "Pack")) |
| 77 | self.memory_only_ops = ( |
| 78 | set(("Squeeze", "Reshape", "QuantizedReshape", "ExpandDims")) | self.concat_ops | self.split_ops |
| 79 | ) |
| 80 | self.supported_fused_activations = set(("Relu", "Relu6", "ReluN1To1", "Tanh", "Sigmoid")) |
| 81 | self.supported_operators = ( |
| 82 | self.npu_pre_ops | self.mac_main_ops | self.elem_wise_main_ops | self.npu_post_ops | self.memory_only_ops |
| 83 | ) |
| 84 | # Setup supported operator restriction checkers |
| 85 | self.supported_operator_restrictions = {} |
| 86 | self.supported_operator_restrictions.update( |
| 87 | {op: self.check_convolution_restrictions for op in self.convolution_ops} |
| 88 | ) |
| 89 | self.supported_operator_restrictions.update( |
| 90 | {op: self.check_depthwise_convolution_restrictions for op in self.depthwise_convolution_ops} |
| 91 | ) |
| 92 | self.supported_operator_restrictions.update({op: self.check_pooling_restrictions for op in self.pooling_ops}) |
| 93 | self.supported_operator_restrictions.update( |
| 94 | {op: self.check_vector_product_restrictions for op in self.fc_vector_products} |
| 95 | ) |
| 96 | self.supported_operator_restrictions.update( |
| 97 | {op: self.check_element_wise_restrictions for op in self.elem_wise_main_ops} |
| 98 | ) |
| 99 | self.supported_operator_restrictions.update( |
| 100 | {op: self.check_memory_only_restrictions for op in self.memory_only_ops} |
| 101 | ) |
| 102 | |
| 103 | def is_operator_supported(self, op): |
| 104 | if op.type not in self.supported_operators: |
| 105 | return False |
| 106 | if not self.check_generic_restrictions(op): |
| 107 | return False |
| 108 | if op.type in self.supported_operator_restrictions: |
| 109 | return self.supported_operator_restrictions[op.type](op) |
| 110 | return True |
| 111 | |
| 112 | def check_generic_restrictions(self, op): |
| 113 | # check fully defined shapes |
| 114 | for t in op.inputs + op.outputs: |
| 115 | if not t.has_fully_defined_shape(): |
| 116 | print("Warning:", op, "has inputs/outputs of undefined shape, placing on CPU") |
| 117 | return False |
| 118 | |
| 119 | # check data type |
| 120 | tensors = [t for t in op.get_ifm_ifm2_weights_ofm() if t is not None] |
| 121 | if not tensors: |
| 122 | tensors = op.inputs |
| 123 | for t in tensors: |
| 124 | if not (t.dtype.type & BaseType.Int): |
| 125 | return False |
| 126 | if t.element_size() > 2 and op.type != "Requantize": |
| 127 | return False |
| 128 | # check size |
| 129 | if any(dim > 65536 for dim in t.shape): |
| 130 | return False |
| 131 | |
| 132 | # check fused activations |
| 133 | if ( |
| 134 | "fused_activation_function" in op.attrs |
| 135 | and op.attrs["fused_activation_function"] is not None |
| 136 | and op.attrs["fused_activation_function"] not in self.supported_fused_activations |
| 137 | ): |
| 138 | return False |
| 139 | return True |
| 140 | |
| 141 | def check_convolution_restrictions(self, op): |
| 142 | # check stride |
| 143 | if op.attrs["stride_w"] > 2 or op.attrs["stride_h"] > 2: |
| 144 | return False |
| 145 | |
| 146 | # check dilation |
| 147 | dilation_w_factor = op.attrs.get("dilation_w_factor", 1) |
| 148 | dilation_h_factor = op.attrs.get("dilation_h_factor", 1) |
| 149 | if dilation_w_factor > 2 or dilation_h_factor > 2: |
| 150 | return False |
| 151 | |
| 152 | # check data type |
| 153 | ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm() |
| 154 | if weight_tensor.element_size() > 1: |
| 155 | return False |
| 156 | |
| 157 | # check kernel size |
| 158 | dilated_weight_w = weight_tensor.shape[0] + (weight_tensor.shape[0] - 1) * (dilation_w_factor - 1) |
| 159 | dilated_weight_h = weight_tensor.shape[1] + (weight_tensor.shape[1] - 1) * (dilation_h_factor - 1) |
| 160 | if ( |
| 161 | dilated_weight_w > 64 |
| 162 | or dilated_weight_h > 64 |
| 163 | or dilated_weight_w * dilated_weight_h * weight_tensor.shape[2] > 127 * 65536 |
| 164 | ): |
| 165 | return False |
| 166 | |
| 167 | # check batch size |
| 168 | if ifm_tensor.shape[0] != 1: |
| 169 | return False |
| 170 | return True |
| 171 | |
| 172 | def check_depthwise_convolution_restrictions(self, op): |
| 173 | # check depth |
| 174 | ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm() |
| 175 | if op.attrs["depth_multiplier"] > 1 and not ( |
| 176 | (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"]) |
| 177 | ): |
| 178 | return False |
| 179 | return self.check_convolution_restrictions(op) |
| 180 | |
| 181 | def check_pooling_restrictions(self, op): |
| 182 | # check stride |
| 183 | if op.attrs["stride_w"] > 2 or op.attrs["stride_h"] > 2: |
| 184 | return False |
| 185 | |
| 186 | # check data type |
| 187 | ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm() |
| 188 | if ifm_tensor.dtype != ofm_tensor.dtype: |
| 189 | return False |
| 190 | |
| 191 | # check batch size |
| 192 | if ifm_tensor.shape[0] != 1: |
| 193 | return False |
| 194 | |
| 195 | if op.type in self.avg_pooling_ops: |
| 196 | # check kernel size |
| 197 | if op.attrs["padding"] == b"SAME" and (op.attrs["filter_width"] > 8 or op.attrs["filter_height"] > 8): |
| 198 | return False |
| 199 | if op.attrs["padding"] == b"VALID" and (op.attrs["filter_width"] > 256 or op.attrs["filter_height"] > 256): |
| 200 | return False |
| 201 | |
| 202 | if op.type in self.max_pooling_ops: |
| 203 | # check data type |
| 204 | if not ifm_tensor.dtype == ofm_tensor.dtype: |
| 205 | return False |
| 206 | # check kernel size |
| 207 | if op.attrs["filter_width"] > 256 or op.attrs["filter_height"] > 256: # any padding |
| 208 | return False |
| 209 | return True |
| 210 | |
| 211 | def check_vector_product_restrictions(self, op): |
| 212 | # check data type |
| 213 | ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm() |
| 214 | if weight_tensor.element_size() > 1: |
| 215 | return False |
| 216 | |
| 217 | return True |
| 218 | |
| 219 | def check_element_wise_restrictions(self, op): |
| 220 | # check data type |
| 221 | ifm_tensor, ifm2_tensor, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm() |
| 222 | if op.type in ("Minimum", "Maximum") and ifm_tensor.dtype != ofm_tensor.dtype: |
| 223 | return False |
| 224 | |
| 225 | # check batch size |
| 226 | if (len(ifm_tensor.shape) > 2 and ifm_tensor.shape[0] != 1) or ( |
| 227 | len(ifm2_tensor.shape) > 2 and ifm2_tensor.shape[0] != 1 |
| 228 | ): |
| 229 | return False |
| 230 | |
| 231 | # check scalar size |
| 232 | if (hasattr(ifm_tensor.values, "__len__") and len(ifm_tensor.values) > 1) or ( |
| 233 | hasattr(ifm2_tensor.values, "__len__") and len(ifm2_tensor.values) > 1 |
| 234 | ): |
| 235 | return False |
| 236 | return True |
| 237 | |
| 238 | def check_memory_only_restrictions(self, op): |
| 239 | # check stride size |
| 240 | if op.type == "StridedSlice": |
| 241 | if len(op.inputs) > 3 and any(stride != 1 for stride in op.inputs[3].values): |
| 242 | return False |
| 243 | return True |