blob: 1012a61520d68760eb7ca3d0e78f8ded17d6d1c1 [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
Patrik Gustavssonf1580f02021-09-01 12:43:02 +020041 memory_only_ops = set((Op.Reshape, Op.Transpose, Op.Concat, Op.SplitSliceRead,))
Patrik Gustavssonb081d672021-08-25 13:49:25 +020042 binary_elem_wise_add_mul_sub = set((Op.Add, Op.Mul, Op.RescaleMul, Op.Sub,))
Patrik Gustavsson46408a82021-09-20 10:47:47 +020043 elem_wise_ops = binary_elem_wise_add_mul_sub
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020044 type_conversion_ops = set((Op.Rescale,))
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +020045 relu_ops = set((Op.Clamp, Op.ReluN,))
Patrik Gustavssonf436ada2021-09-14 14:56:48 +020046 activation_ops = relu_ops | set((Op.Table,))
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +020047 pad_ops = set((Op.Pad,))
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020048
49 npu_post_ops = activation_ops
Patrik Gustavsson46408a82021-09-20 10:47:47 +020050
51 supported_operators = mac_main_ops | type_conversion_ops | npu_post_ops | memory_only_ops | elem_wise_ops | pad_ops
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020052
53 # Supported data types
54 # TODO will differ compared to TensorFlow Lite, currently set to the same
Patrik Gustavssonf1580f02021-09-01 12:43:02 +020055 supported_op_dtypes = set((DataType.uint8, DataType.int8, DataType.int16, DataType.int32)) # TODO add bool
Patrik Gustavssonf366fb12021-09-07 13:30:29 +020056 tens_dim_range = (1, 65535) # TODO HW limitation, that is to be resolved in SW
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020057
58 def __init__(self):
59 # Setup the generic constraints. Note: the order matters
60 self.generic_constraints = []
61 self.generic_constraints.append(TosaSupportedOperators.constraint_tens_dtype)
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +020062 self.generic_constraints.append(TosaSupportedOperators.constraint_tens_dimension) # TODO as not supported yet
63 self.generic_constraints.append(TosaSupportedOperators.constraint_rank) # TODO as not supported yet
64 self.generic_constraints.append(TosaSupportedOperators.constraint_batch) # TODO as not supported yet
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020065
66 # Setup specific constraints. Note: the order matters
67 self.specific_constraints = defaultdict(list)
68
Patrik Gustavssondf995102021-08-23 15:33:59 +020069 self.specific_constraints[Op.Transpose].append(TosaSupportedOperators.constraint_ifm_producer)
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +020070 self.specific_constraints[Op.Pad].append(TosaSupportedOperators.constraint_padding_producer)
Patrik Gustavssonf436ada2021-09-14 14:56:48 +020071 self.specific_constraints[Op.Table].append(TosaSupportedOperators.constraint_table_dtype)
72 self.specific_constraints[Op.Table].append(TosaSupportedOperators.constraint_table_producer)
Patrik Gustavssondf995102021-08-23 15:33:59 +020073
74 # Depthwise Conv specific checks:
75 for op_type in TosaSupportedOperators.depthwise_convolution_ops:
76 self.specific_constraints[op_type].append(TosaSupportedOperators.constraint_depth_multiplier)
77
Patrik Gustavssonf366fb12021-09-07 13:30:29 +020078 # Avgpool specific checks
79 for op_type in TosaSupportedOperators.avg_pooling_ops:
80 self.specific_constraints[op_type].append(TosaSupportedOperators.constraint_padding)
81
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020082 def is_operator_supported(self, op):
83 ext_type = optype_to_tosa_op_type(op.type)
84 if op.type not in TosaSupportedOperators.supported_operators:
85 if op.type not in (Op.Placeholder, Op.SubgraphInput, Op.Const):
86 print(f"Info: {ext_type} '{op.name}' is not a NPU op")
87 return False
88
89 for constraint in self.generic_constraints + self.specific_constraints[op.type]:
90 valid, extra = constraint(op)
91 if not valid:
92 print(f"Warning: {ext_type} '{op.name}' is not supported on the NPU")
93 print(f" - {constraint.__doc__}")
94 if extra:
95 print(f" {extra}")
96 return False
97
98 return True
99
100 # TODO this function is the same for TensorFlow Lite, but input might differ
101 @classmethod
102 @docstring_format_args([list_formatter(supported_op_dtypes)])
103 def constraint_tens_dtype(cls, op):
104 "Tensors must be of type: {}"
105 valid = True
106 extra = []
107 tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens]
108 if not tensors:
109 tensors = [tens for tens in op.inputs if tens]
110 for tens in tensors:
111 if tens.dtype not in cls.supported_op_dtypes:
112 valid = False
113 extra.append(f"Tensor '{tens.name}' has data type: {tens.dtype}")
114 return valid, ", ".join(extra)
Patrik Gustavssondf995102021-08-23 15:33:59 +0200115
Patrik Gustavssonf366fb12021-09-07 13:30:29 +0200116 # TODO Duplicates check present for TFLite. But it is only temporarily added
117 # This is for a HW limitation, that is to be resolved in SW later on
118 @classmethod
119 @docstring_format_args(tens_dim_range)
Patrik Gustavsson3f22ec22021-09-21 14:18:44 +0200120 def constraint_tens_dimension(self, op):
121 "Tensor dimensions must be in the range [{}, {}], if not elementwise"
122 tens_min, tens_max = self.tens_dim_range
Patrik Gustavssonf366fb12021-09-07 13:30:29 +0200123 valid = True
124 extra = []
Patrik Gustavsson3f22ec22021-09-21 14:18:44 +0200125 if op.type not in self.binary_elem_wise_add_mul_sub:
126 tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens]
127 if not tensors:
128 tensors = [tens for tens in op.inputs if tens]
129 for tens in tensors:
130 if not all(tens_min <= dim <= tens_max for dim in tens.shape):
131 valid = False
132 extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}")
Patrik Gustavssonf366fb12021-09-07 13:30:29 +0200133 return valid, ", ".join(extra)
134
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200135 # TODO This is for a HW limitation, that is to be resolved in SW later on
Patrik Gustavsson46408a82021-09-20 10:47:47 +0200136 @classmethod
137 def constraint_rank(self, op):
138 "Tensor rank must be <= 4, if not elementwise"
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200139 valid = True
140 extra = []
Patrik Gustavsson46408a82021-09-20 10:47:47 +0200141 if op.type not in self.binary_elem_wise_add_mul_sub:
142 tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens]
143 if not tensors:
144 tensors = [tens for tens in op.inputs if tens]
145 for tens in tensors:
146 rank = len(tens.shape)
147 if not rank <= 4:
148 valid = False
149 extra.append(f"Tensor '{tens.name}' has rank: {rank}")
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200150 return valid, ", ".join(extra)
151
152 # TODO This is for a HW limitation, that is to be resolved in SW later on
Patrik Gustavsson46408a82021-09-20 10:47:47 +0200153 @classmethod
154 def constraint_batch(self, op):
155 "If Tensor rank is 4 batch of ifms/ofm must be 1, if not elementwise"
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200156 valid = True
157 extra = []
Patrik Gustavsson46408a82021-09-20 10:47:47 +0200158 if op.type not in self.binary_elem_wise_add_mul_sub:
159 tensors = [tens for tens in op.get_ifm_ifm2_ofm() if tens]
160 if not tensors:
161 tensors = [tens for tens in op.inputs if tens]
162 for tens in tensors:
163 rank = len(tens.shape)
164 if rank == 4 and tens.shape[0] != 1:
165 valid = False
166 extra.append(f"Tensor '{tens.name}' has rank: 4 and N: {tens.shape[0]}")
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200167 return valid, ", ".join(extra)
168
Patrik Gustavssondf995102021-08-23 15:33:59 +0200169 @staticmethod
170 def constraint_ifm_producer(cls, op):
171 "Input must be constant data"
172 valid = op.ifm.ops and op.ifm.ops[0].type == Op.Const
173 return valid, "Op has ifm with non-constant data"
174
Patrik Gustavssonf366fb12021-09-07 13:30:29 +0200175 @staticmethod
176 def constraint_padding(op):
177 # TODO Only support for when global scaling can be used.
178 # That is when there is padding no padding
179 "Avgpool only supported for no padding"
180 top, left, _, _ = op.attrs["explicit_padding"]
181 valid = top == 0 and left == 0
182
183 return valid, "Avgpool with pad_top {top} and pad_left {left}"
184
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200185 # TODO limit padding to be const data for now.
186 # For TFLite it is assumed to be constant.
187 @staticmethod
188 def constraint_padding_producer(op):
189 "Input must be constant data"
190 valid = op.inputs[1].ops and op.inputs[1].ops[0].type == Op.Const
191 return valid, "PAD Op with non-constant data padding"
192
Patrik Gustavssonf366fb12021-09-07 13:30:29 +0200193 # TODO duplicates tflite_supported operators, but support for depth multiplier should be added at a later stage
Patrik Gustavssondf995102021-08-23 15:33:59 +0200194 @staticmethod
195 def constraint_depth_multiplier(op):
196 "For depth multipliers > 1, IFM channels must be 1 and OFM channels must be equal to the depth multiplier"
197 depth_multiplier = op.attrs.get("depth_multiplier", 1)
198 if depth_multiplier > 1:
199 ifm_channels = op.ifm.shape[3]
200 ofm_channels = op.ofm.shape[3]
201 valid = (ifm_channels == 1) and (ofm_channels == depth_multiplier)
202 extra = (
203 f"Op has ifm_channels={ifm_channels}, ofm_channels={ofm_channels}"
204 f" and depth_multiplier={depth_multiplier}"
205 )
206 return valid, extra
207 return True, "Op has depth_multiplier=1"
Patrik Gustavssonf436ada2021-09-14 14:56:48 +0200208
209 # TODO Table operator support limited to int8 for now.
210 # For TFLite it is assumed to be constant.
211 @staticmethod
212 def constraint_table_dtype(op):
213 "Only supported is int8"
214 valid = True
215 tensors = [op.ifm, op.ofm, op.inputs[1]]
216 for tens in tensors:
217 if tens.dtype != DataType.int8:
218 valid = False
219 return valid, "Table operator with non int8 tensor"
220
221 # TODO limit table to be constant data for now.
222 # Can it be non-constant?
223 @staticmethod
224 def constraint_table_producer(op):
225 "Input must be constant data"
226 valid = op.inputs[1].ops and op.inputs[1].ops[0].type == Op.Const
227 return valid, "Table Op with non-constant table input"