blob: 729d435a72e6d55e04071e672b771718cca374c7 [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# 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.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# The SupportedOperators class which is a collection of all supported operators and parameter checks.
Fredrik Svedberg388e9c22020-05-25 16:32:00 +020018from .data_type import BaseType, DataType
Tim Hall79d07d22020-04-27 18:20:16 +010019
20
21class SupportedOperators:
22 def __init__(self):
23 # Categorised lists of supported operators
24 self.npu_pre_ops = set(("QuantizedResizeBilinear", "SplitSliceRead"))
25 self.convolution_ops = set(("Conv2DBiasAct", "Conv2D", "QuantizedConv2D", "Conv2DBackpropInputSwitched"))
26 self.depthwise_convolution_ops = set(
27 ("DepthwiseConv2dBiasAct", "DepthwiseConv2dNative", "QuantizedDepthwiseConv2D")
28 )
29 self.max_pooling_ops = set(("QuantizedMaxPool", "MaxPool", "MaxPoolAct"))
30 self.avg_pooling_ops = set(("QuantizedAvgPool", "AvgPool", "AvgPoolAct"))
31 self.pooling_ops = self.max_pooling_ops | self.avg_pooling_ops
Dwight Lidman42fed942020-05-29 09:37:03 +020032 self.resizing_ops = set(("ResizeBilinear",))
Tim Hall79d07d22020-04-27 18:20:16 +010033 self.fc_vector_products = set(("QuantizedMatMul", "MatMul", "FullyConnectedAct"))
34 self.mac_main_ops = (
35 # convolutions
36 self.convolution_ops
37 # depth-wise convolutions
38 | self.depthwise_convolution_ops
39 # pooling
40 | self.pooling_ops
Dwight Lidman42fed942020-05-29 09:37:03 +020041 # resizing/upscaling
42 | self.resizing_ops
Tim Hall79d07d22020-04-27 18:20:16 +010043 # FC layers
44 | self.fc_vector_products
45 # RNN/LSTM/GRU
46 | set(("BlockLSTM"))
47 )
Dwight Lidmanf995db72020-04-27 11:15:12 +020048 self.unary_elem_wise_main_ops = set(("LeakyRelu", "Abs"))
Fredrik Svedberg388e9c22020-05-25 16:32:00 +020049 self.binary_elem_wise_min_max_ops = set(("Minimum", "Maximum"))
50 self.binary_elem_wise_add_mul_sub = set(
Tim Hall79d07d22020-04-27 18:20:16 +010051 (
Tim Hall79d07d22020-04-27 18:20:16 +010052 "AddAct",
53 "MulAct",
54 "SubAct",
55 "QuantizedAdd",
56 "QuantizedSub",
57 "QuantizedMul",
58 "Mul",
59 "Add",
60 "Sub",
Tim Hall79d07d22020-04-27 18:20:16 +010061 )
62 )
Fredrik Svedberg388e9c22020-05-25 16:32:00 +020063 self.binary_elem_wise_main_ops = self.binary_elem_wise_min_max_ops | self.binary_elem_wise_add_mul_sub
Dwight Lidmanf995db72020-04-27 11:15:12 +020064 self.elem_wise_main_ops = self.binary_elem_wise_main_ops | self.unary_elem_wise_main_ops
Tim Hall79d07d22020-04-27 18:20:16 +010065 self.activation_ops = set(
66 ("QuantizedRelu", "QuantizedRelu1", "QuantizedRelu6", "Relu", "Relu6", "ReluN1To1", "Sigmoid", "Tanh")
67 )
68 self.npu_post_ops = (
69 # activation functions
70 self.activation_ops
71 # concatenation write direction
72 | set(("ConcatSliceWrite"))
73 # bias add and batch norm
74 | set(("QuantizedBiasAdd", "Requantize", "QuantizedBatchNorm", "BiasAdd", "FusedBatchNorm"))
75 )
Charles Xu53d47522020-05-04 11:32:05 +020076 self.split_ops = set(("Split", "SplitV", "StridedSlice", "Slice", "UnpackReshaped", "Unpack"))
Tim Hall79d07d22020-04-27 18:20:16 +010077 self.concat_ops = set(("Concat", "ConcatV2", "QuantizedConcat", "ConcatTFLite", "PackReshaped", "Pack"))
78 self.memory_only_ops = (
79 set(("Squeeze", "Reshape", "QuantizedReshape", "ExpandDims")) | self.concat_ops | self.split_ops
80 )
81 self.supported_fused_activations = set(("Relu", "Relu6", "ReluN1To1", "Tanh", "Sigmoid"))
82 self.supported_operators = (
83 self.npu_pre_ops | self.mac_main_ops | self.elem_wise_main_ops | self.npu_post_ops | self.memory_only_ops
84 )
85 # Setup supported operator restriction checkers
86 self.supported_operator_restrictions = {}
87 self.supported_operator_restrictions.update(
88 {op: self.check_convolution_restrictions for op in self.convolution_ops}
89 )
90 self.supported_operator_restrictions.update(
91 {op: self.check_depthwise_convolution_restrictions for op in self.depthwise_convolution_ops}
92 )
93 self.supported_operator_restrictions.update({op: self.check_pooling_restrictions for op in self.pooling_ops})
Dwight Lidman42fed942020-05-29 09:37:03 +020094 self.supported_operator_restrictions.update({op: self.check_resize_restrictions for op in self.resizing_ops})
Tim Hall79d07d22020-04-27 18:20:16 +010095 self.supported_operator_restrictions.update(
96 {op: self.check_vector_product_restrictions for op in self.fc_vector_products}
97 )
98 self.supported_operator_restrictions.update(
99 {op: self.check_element_wise_restrictions for op in self.elem_wise_main_ops}
100 )
101 self.supported_operator_restrictions.update(
102 {op: self.check_memory_only_restrictions for op in self.memory_only_ops}
103 )
104
105 def is_operator_supported(self, op):
106 if op.type not in self.supported_operators:
107 return False
108 if not self.check_generic_restrictions(op):
109 return False
110 if op.type in self.supported_operator_restrictions:
111 return self.supported_operator_restrictions[op.type](op)
112 return True
113
114 def check_generic_restrictions(self, op):
115 # check fully defined shapes
116 for t in op.inputs + op.outputs:
117 if not t.has_fully_defined_shape():
118 print("Warning:", op, "has inputs/outputs of undefined shape, placing on CPU")
119 return False
120
121 # check data type
122 tensors = [t for t in op.get_ifm_ifm2_weights_ofm() if t is not None]
123 if not tensors:
124 tensors = op.inputs
125 for t in tensors:
126 if not (t.dtype.type & BaseType.Int):
127 return False
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200128 if t.element_size() > 2 and op.type not in ("Requantize") | self.binary_elem_wise_add_mul_sub:
Tim Hall79d07d22020-04-27 18:20:16 +0100129 return False
130 # check size
131 if any(dim > 65536 for dim in t.shape):
132 return False
133
134 # check fused activations
135 if (
136 "fused_activation_function" in op.attrs
137 and op.attrs["fused_activation_function"] is not None
138 and op.attrs["fused_activation_function"] not in self.supported_fused_activations
139 ):
140 return False
141 return True
142
143 def check_convolution_restrictions(self, op):
144 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200145 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100146 return False
147
148 # check dilation
149 dilation_w_factor = op.attrs.get("dilation_w_factor", 1)
150 dilation_h_factor = op.attrs.get("dilation_h_factor", 1)
151 if dilation_w_factor > 2 or dilation_h_factor > 2:
152 return False
153
154 # check data type
155 ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm()
156 if weight_tensor.element_size() > 1:
157 return False
158
159 # check kernel size
160 dilated_weight_w = weight_tensor.shape[0] + (weight_tensor.shape[0] - 1) * (dilation_w_factor - 1)
161 dilated_weight_h = weight_tensor.shape[1] + (weight_tensor.shape[1] - 1) * (dilation_h_factor - 1)
162 if (
163 dilated_weight_w > 64
164 or dilated_weight_h > 64
165 or dilated_weight_w * dilated_weight_h * weight_tensor.shape[2] > 127 * 65536
166 ):
167 return False
168
169 # check batch size
170 if ifm_tensor.shape[0] != 1:
171 return False
172 return True
173
174 def check_depthwise_convolution_restrictions(self, op):
175 # check depth
176 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
177 if op.attrs["depth_multiplier"] > 1 and not (
178 (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"])
179 ):
180 return False
181 return self.check_convolution_restrictions(op)
182
183 def check_pooling_restrictions(self, op):
184 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200185 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100186 return False
187
188 # check data type
189 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
190 if ifm_tensor.dtype != ofm_tensor.dtype:
191 return False
192
193 # check batch size
194 if ifm_tensor.shape[0] != 1:
195 return False
196
197 if op.type in self.avg_pooling_ops:
198 # check kernel size
199 if op.attrs["padding"] == b"SAME" and (op.attrs["filter_width"] > 8 or op.attrs["filter_height"] > 8):
200 return False
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200201 if (op.attrs["padding"] == b"VALID" and
202 (op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256)):
Tim Hall79d07d22020-04-27 18:20:16 +0100203 return False
204
205 if op.type in self.max_pooling_ops:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200206 # check kernel size (any padding)
207 if op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256:
Tim Hall79d07d22020-04-27 18:20:16 +0100208 return False
209 return True
210
Dwight Lidman42fed942020-05-29 09:37:03 +0200211 def check_resize_restrictions(self, op):
212 # check unsupported upscaling factor
213 if op.type == "ResizeBilinear":
214 upscaled_shape = [op.inputs[0].shape[1] * 2, op.inputs[0].shape[2] * 2]
215 out_shape = op.outputs[0].shape[1:3]
216 if not op.attrs["align_corners"] and out_shape != upscaled_shape:
217 return False
218 elif op.attrs["align_corners"] and out_shape != [upscaled_shape[0] - 1, upscaled_shape[1] - 1]:
219 return False
220 return True
221
Tim Hall79d07d22020-04-27 18:20:16 +0100222 def check_vector_product_restrictions(self, op):
223 # check data type
224 ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm()
225 if weight_tensor.element_size() > 1:
226 return False
227
228 return True
229
230 def check_element_wise_restrictions(self, op):
231 # check data type
232 ifm_tensor, ifm2_tensor, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200233 # input and output datatype must match for these operators
234 if (op.type in self.binary_elem_wise_min_max_ops | self.unary_elem_wise_main_ops and
235 ifm_tensor.dtype != ofm_tensor.dtype):
Tim Hall79d07d22020-04-27 18:20:16 +0100236 return False
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200237 if (op.type in self.binary_elem_wise_add_mul_sub):
238 # both inputs must have same type
239 if (ifm_tensor.dtype != ifm2_tensor.dtype):
240 return False
241 # signed input check
242 if (ifm_tensor.dtype.type & BaseType.Signed):
243 # output must be signed
244 if (ofm_tensor.dtype.type & BaseType.Unsigned):
245 return False
246 # and 8, 16 or 32-bit
247 if (ofm_tensor.element_size() not in (1, 2, 4)):
248 return False
249 # unsigned input check, output must be same type or int32
250 if (ifm_tensor.dtype.type & BaseType.Unsigned and not
251 (ifm_tensor.dtype == ofm_tensor.dtype or
252 ofm_tensor.dtype == DataType.int32)):
253 return False
Tim Hall79d07d22020-04-27 18:20:16 +0100254
255 # check batch size
Dwight Lidmanf995db72020-04-27 11:15:12 +0200256 if len(ifm_tensor.shape) > 2 and ifm_tensor.shape[0] != 1:
257 return False
258 if op.type in self.binary_elem_wise_main_ops: # if op type is unary, ifm2_tensor is None
259 if len(ifm2_tensor.shape) > 2 and ifm2_tensor.shape[0] != 1:
260 return False
Tim Hall79d07d22020-04-27 18:20:16 +0100261 return True
262
263 def check_memory_only_restrictions(self, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100264 if op.type == "StridedSlice":
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200265 # check stride size
Tim Hall79d07d22020-04-27 18:20:16 +0100266 if len(op.inputs) > 3 and any(stride != 1 for stride in op.inputs[3].values):
267 return False
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200268 # check ellipsis_mask
269 if op.attrs["ellipsis_mask"] != 0:
270 return False
271 # check if both new_axis_mask and shrink_axis_mask have bit set
272 if op.attrs["new_axis_mask"] != 0 and op.attrs["shrink_axis_mask"] != 0:
273 return False
Tim Hall79d07d22020-04-27 18:20:16 +0100274 return True