blob: 15e156954e56efa80c7add5de7e8993e7fc2945d [file] [log] [blame]
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +02001# Copyright (C) 2021 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# Description:
17# The TosaSupportedOperators class which is a collection of all supported operators and parameter checks.
18from collections import defaultdict
19
20from .data_type import DataType
21from .operation import Op
22from .supported_operators_util import docstring_format_args
23from .supported_operators_util import list_formatter
24from .tosa_mapping import optype_to_tosa_op_type
25
26
27class TosaSupportedOperators:
28 # TODO currently sparsely populated
29 # Categorised lists of supported operators
30 convolution_ops = set((Op.Conv2DBias,))
Patrik Gustavssondf995102021-08-23 15:33:59 +020031 depthwise_convolution_ops = set((Op.DepthwiseConv2DBias,))
32 convolution_like_ops = convolution_ops | depthwise_convolution_ops
33
34 # TODO depending on what will be committed
Patrik Gustavssonc74682c2021-08-17 14:26:38 +020035 max_pooling_ops = Op.op_set(Op.is_maxpool_op)
36 avg_pooling_ops = Op.op_set(Op.is_avgpool_op)
Patrik Gustavssondf995102021-08-23 15:33:59 +020037 pooling_ops = max_pooling_ops | avg_pooling_ops
38 fc_vector_products = set((Op.FullyConnected,))
Patrik Gustavssonc74682c2021-08-17 14:26:38 +020039
Patrik Gustavssondf995102021-08-23 15:33:59 +020040 mac_main_ops = convolution_like_ops | pooling_ops | fc_vector_products
Jonas Ohlssond8575072022-03-30 10:30:25 +020041 memory_only_ops = set(
42 (
43 Op.Reshape,
44 Op.Transpose,
45 Op.Concat,
46 Op.SplitSliceRead,
47 )
48 )
49 binary_elem_wise_add_mul_sub = set(
50 (
51 Op.Add,
52 Op.Mul,
53 Op.RescaleMul,
54 Op.Sub,
55 )
56 )
Patrik Gustavsson46408a82021-09-20 10:47:47 +020057 elem_wise_ops = binary_elem_wise_add_mul_sub
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020058 type_conversion_ops = set((Op.Rescale,))
Jonas Ohlssond8575072022-03-30 10:30:25 +020059 relu_ops = set(
60 (
61 Op.Clamp,
62 Op.ReluN,
63 )
64 )
Patrik Gustavssonf436ada2021-09-14 14:56:48 +020065 activation_ops = relu_ops | set((Op.Table,))
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +020066 pad_ops = set((Op.Pad,))
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020067
Patrik Gustavssonb4936ad2021-10-05 13:53:34 +020068 rank_unlimited_ops = set((Op.Concat, Op.Reshape, Op.Identity, Op.Pad))
Patrik Gustavssonc2b129d2021-09-23 13:52:34 +020069 rank6_limited_ops = elem_wise_ops
Patrik Gustavsson008cd102021-09-24 13:46:42 +020070 batch_enabled_ops = rank6_limited_ops | rank_unlimited_ops
71 large_tens_dims_enabled_ops = batch_enabled_ops | set((Op.SplitSliceRead,))
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020072 npu_post_ops = activation_ops
Patrik Gustavsson46408a82021-09-20 10:47:47 +020073
Patrik Gustavssonef3ebdd2021-10-01 11:10:25 +020074 supported_operators = (
75 mac_main_ops
76 | type_conversion_ops
77 | npu_post_ops
78 | memory_only_ops
79 | elem_wise_ops
80 | pad_ops
81 | set((Op.Identity,))
82 )
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020083
84 # Supported data types
85 # TODO will differ compared to TensorFlow Lite, currently set to the same
Patrik Gustavssonf1580f02021-09-01 12:43:02 +020086 supported_op_dtypes = set((DataType.uint8, DataType.int8, DataType.int16, DataType.int32)) # TODO add bool
Patrik Gustavssonf366fb12021-09-07 13:30:29 +020087 tens_dim_range = (1, 65535) # TODO HW limitation, that is to be resolved in SW
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020088
89 def __init__(self):
90 # Setup the generic constraints. Note: the order matters
91 self.generic_constraints = []
92 self.generic_constraints.append(TosaSupportedOperators.constraint_tens_dtype)
Patrik Gustavsson008cd102021-09-24 13:46:42 +020093 self.generic_constraints.append(TosaSupportedOperators.constraint_tens_dimension) # TODO not supported yet
94 self.generic_constraints.append(TosaSupportedOperators.constraint_rank) # TODO not supported for all ops yet
95 self.generic_constraints.append(TosaSupportedOperators.constraint_batch) # TODO not supported for all ops yet
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020096
97 # Setup specific constraints. Note: the order matters
98 self.specific_constraints = defaultdict(list)
99
Patrik Gustavssondf995102021-08-23 15:33:59 +0200100 self.specific_constraints[Op.Transpose].append(TosaSupportedOperators.constraint_ifm_producer)
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200101 self.specific_constraints[Op.Pad].append(TosaSupportedOperators.constraint_padding_producer)
Patrik Gustavssonf436ada2021-09-14 14:56:48 +0200102 self.specific_constraints[Op.Table].append(TosaSupportedOperators.constraint_table_dtype)
103 self.specific_constraints[Op.Table].append(TosaSupportedOperators.constraint_table_producer)
Patrik Gustavssondf995102021-08-23 15:33:59 +0200104
105 # Depthwise Conv specific checks:
106 for op_type in TosaSupportedOperators.depthwise_convolution_ops:
107 self.specific_constraints[op_type].append(TosaSupportedOperators.constraint_depth_multiplier)
108
Patrik Gustavssonf366fb12021-09-07 13:30:29 +0200109 # Avgpool specific checks
110 for op_type in TosaSupportedOperators.avg_pooling_ops:
111 self.specific_constraints[op_type].append(TosaSupportedOperators.constraint_padding)
112
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200113 def is_operator_supported(self, op):
114 ext_type = optype_to_tosa_op_type(op.type)
115 if op.type not in TosaSupportedOperators.supported_operators:
116 if op.type not in (Op.Placeholder, Op.SubgraphInput, Op.Const):
117 print(f"Info: {ext_type} '{op.name}' is not a NPU op")
118 return False
119
120 for constraint in self.generic_constraints + self.specific_constraints[op.type]:
121 valid, extra = constraint(op)
122 if not valid:
123 print(f"Warning: {ext_type} '{op.name}' is not supported on the NPU")
124 print(f" - {constraint.__doc__}")
125 if extra:
126 print(f" {extra}")
127 return False
128
129 return True
130
131 # TODO this function is the same for TensorFlow Lite, but input might differ
132 @classmethod
133 @docstring_format_args([list_formatter(supported_op_dtypes)])
134 def constraint_tens_dtype(cls, op):
135 "Tensors must be of type: {}"
136 valid = True
137 extra = []
138 tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens]
139 if not tensors:
140 tensors = [tens for tens in op.inputs if tens]
141 for tens in tensors:
142 if tens.dtype not in cls.supported_op_dtypes:
143 valid = False
144 extra.append(f"Tensor '{tens.name}' has data type: {tens.dtype}")
145 return valid, ", ".join(extra)
Patrik Gustavssondf995102021-08-23 15:33:59 +0200146
Patrik Gustavssonf366fb12021-09-07 13:30:29 +0200147 # TODO Duplicates check present for TFLite. But it is only temporarily added
148 # This is for a HW limitation, that is to be resolved in SW later on
149 @classmethod
150 @docstring_format_args(tens_dim_range)
Patrik Gustavsson3f22ec22021-09-21 14:18:44 +0200151 def constraint_tens_dimension(self, op):
Patrik Gustavssonc2b129d2021-09-23 13:52:34 +0200152 "Tensor dimensions must be in the range [{}, {}]"
Patrik Gustavsson3f22ec22021-09-21 14:18:44 +0200153 tens_min, tens_max = self.tens_dim_range
Patrik Gustavssonf366fb12021-09-07 13:30:29 +0200154 valid = True
155 extra = []
Patrik Gustavssonc2b129d2021-09-23 13:52:34 +0200156 if op.type not in self.large_tens_dims_enabled_ops:
Patrik Gustavsson3f22ec22021-09-21 14:18:44 +0200157 tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens]
158 if not tensors:
159 tensors = [tens for tens in op.inputs if tens]
160 for tens in tensors:
161 if not all(tens_min <= dim <= tens_max for dim in tens.shape):
162 valid = False
163 extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}")
Patrik Gustavssonf366fb12021-09-07 13:30:29 +0200164 return valid, ", ".join(extra)
165
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200166 # TODO This is for a HW limitation, that is to be resolved in SW later on
Patrik Gustavsson46408a82021-09-20 10:47:47 +0200167 @classmethod
168 def constraint_rank(self, op):
Patrik Gustavssonc2b129d2021-09-23 13:52:34 +0200169 "Tensor rank must be <= 6 or <= 4 depending on operator"
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200170 valid = True
171 extra = []
Patrik Gustavssonc2b129d2021-09-23 13:52:34 +0200172 if op.type not in self.rank_unlimited_ops:
173 if op.type in self.rank6_limited_ops:
174 rank_limit = 6
175 else:
176 rank_limit = 4
Patrik Gustavsson46408a82021-09-20 10:47:47 +0200177 tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens]
178 if not tensors:
179 tensors = [tens for tens in op.inputs if tens]
180 for tens in tensors:
181 rank = len(tens.shape)
Patrik Gustavssonc2b129d2021-09-23 13:52:34 +0200182 if not rank <= rank_limit:
Patrik Gustavsson46408a82021-09-20 10:47:47 +0200183 valid = False
Patrik Gustavsson008cd102021-09-24 13:46:42 +0200184 extra.append(
185 f"Tensor '{tens.name}' has rank: {rank}, rank limit is currently {rank_limit}"
186 f" for op of type {op.type}"
187 )
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200188 return valid, ", ".join(extra)
189
190 # TODO This is for a HW limitation, that is to be resolved in SW later on
Patrik Gustavsson46408a82021-09-20 10:47:47 +0200191 @classmethod
192 def constraint_batch(self, op):
Patrik Gustavssonc2b129d2021-09-23 13:52:34 +0200193 "If Tensor rank is 4 batch of ifms/ofm must be 1"
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200194 valid = True
195 extra = []
Patrik Gustavssonc2b129d2021-09-23 13:52:34 +0200196 if op.type not in self.batch_enabled_ops:
Patrik Gustavsson46408a82021-09-20 10:47:47 +0200197 tensors = [tens for tens in op.get_ifm_ifm2_ofm() if tens]
198 if not tensors:
199 tensors = [tens for tens in op.inputs if tens]
200 for tens in tensors:
201 rank = len(tens.shape)
202 if rank == 4 and tens.shape[0] != 1:
203 valid = False
204 extra.append(f"Tensor '{tens.name}' has rank: 4 and N: {tens.shape[0]}")
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200205 return valid, ", ".join(extra)
206
Patrik Gustavssondf995102021-08-23 15:33:59 +0200207 @staticmethod
208 def constraint_ifm_producer(cls, op):
209 "Input must be constant data"
210 valid = op.ifm.ops and op.ifm.ops[0].type == Op.Const
211 return valid, "Op has ifm with non-constant data"
212
Patrik Gustavssonf366fb12021-09-07 13:30:29 +0200213 @staticmethod
214 def constraint_padding(op):
215 # TODO Only support for when global scaling can be used.
216 # That is when there is padding no padding
217 "Avgpool only supported for no padding"
218 top, left, _, _ = op.attrs["explicit_padding"]
219 valid = top == 0 and left == 0
220
221 return valid, "Avgpool with pad_top {top} and pad_left {left}"
222
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200223 # TODO limit padding to be const data for now.
224 # For TFLite it is assumed to be constant.
225 @staticmethod
226 def constraint_padding_producer(op):
227 "Input must be constant data"
228 valid = op.inputs[1].ops and op.inputs[1].ops[0].type == Op.Const
229 return valid, "PAD Op with non-constant data padding"
230
Patrik Gustavssonf366fb12021-09-07 13:30:29 +0200231 # TODO duplicates tflite_supported operators, but support for depth multiplier should be added at a later stage
Patrik Gustavssondf995102021-08-23 15:33:59 +0200232 @staticmethod
233 def constraint_depth_multiplier(op):
234 "For depth multipliers > 1, IFM channels must be 1 and OFM channels must be equal to the depth multiplier"
235 depth_multiplier = op.attrs.get("depth_multiplier", 1)
236 if depth_multiplier > 1:
237 ifm_channels = op.ifm.shape[3]
238 ofm_channels = op.ofm.shape[3]
239 valid = (ifm_channels == 1) and (ofm_channels == depth_multiplier)
240 extra = (
241 f"Op has ifm_channels={ifm_channels}, ofm_channels={ofm_channels}"
242 f" and depth_multiplier={depth_multiplier}"
243 )
244 return valid, extra
245 return True, "Op has depth_multiplier=1"
Patrik Gustavssonf436ada2021-09-14 14:56:48 +0200246
247 # TODO Table operator support limited to int8 for now.
248 # For TFLite it is assumed to be constant.
249 @staticmethod
250 def constraint_table_dtype(op):
251 "Only supported is int8"
252 valid = True
253 tensors = [op.ifm, op.ofm, op.inputs[1]]
254 for tens in tensors:
255 if tens.dtype != DataType.int8:
256 valid = False
257 return valid, "Table operator with non int8 tensor"
258
259 # TODO limit table to be constant data for now.
260 # Can it be non-constant?
261 @staticmethod
262 def constraint_table_producer(op):
263 "Input must be constant data"
264 valid = op.inputs[1].ops and op.inputs[1].ops[0].type == Op.Const
265 return valid, "Table Op with non-constant table input"