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. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 16 | # Description: |
| 17 | # The SupportedOperators class which is a collection of all supported operators and parameter checks. |
Charles Xu | 87c1350 | 2020-08-06 12:17:26 +0200 | [diff] [blame] | 18 | import numpy as np |
| 19 | |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 20 | from .data_type import BaseType |
| 21 | from .data_type import DataType |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 22 | from .numeric_util import is_integer |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 23 | from .operation import get_slice_offsets |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 24 | from .operation import Op |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 25 | |
| 26 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 27 | # Custom decorator function to allow formatting docstrings containing "{}" |
| 28 | def docstring_format_args(args): |
| 29 | def docstring(func): |
| 30 | func.__doc__ = func.__doc__.format(*args) |
| 31 | return func |
| 32 | |
| 33 | return docstring |
| 34 | |
| 35 | |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 36 | def warn_cpu(op, msg): |
| 37 | print("Warning: {} {}, placing on CPU".format(op.type, msg)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 38 | |
| 39 | |
| 40 | class SupportedOperators: |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 41 | # Categorised lists of supported operators |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 42 | npu_pre_ops = set((Op.SplitSliceRead,)) |
| 43 | convolution_ops = set((Op.Conv2DBias, Op.Conv2D, Op.QuantizedConv2D,)) |
| 44 | depthwise_convolution_ops = set((Op.DepthwiseConv2DBias,)) |
| 45 | transpose_convolution_ops = set((Op.Conv2DBackpropInput,)) |
| 46 | max_pooling_ops = Op.op_set(Op.is_maxpool_op) |
| 47 | avg_pooling_ops = Op.op_set(Op.is_avgpool_op) |
| 48 | pooling_ops = set((Op.ReduceSum,)) | max_pooling_ops | avg_pooling_ops |
| 49 | resizing_ops = set((Op.ResizeBilinear,)) |
| 50 | fc_vector_products = set((Op.QuantizedMatMul, Op.MatMul, Op.FullyConnected,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 51 | mac_main_ops = ( |
| 52 | # RNN/LSTM/GRU |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 53 | set((Op.BlockLSTM,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 54 | # convolutions |
| 55 | | convolution_ops |
| 56 | # depth-wise convolutions |
| 57 | | depthwise_convolution_ops |
| 58 | # transpose convolutions |
| 59 | | transpose_convolution_ops |
| 60 | # pooling |
| 61 | | pooling_ops |
| 62 | # resizing/upscaling |
| 63 | | resizing_ops |
| 64 | # FC layers |
| 65 | | fc_vector_products |
| 66 | ) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 67 | unary_elem_wise_main_ops = Op.op_set(Op.is_unary_elementwise_op) |
| 68 | binary_elem_wise_min_max_ops = set((Op.Minimum, Op.Maximum,)) |
| 69 | binary_elem_wise_shift_ops = set((Op.SHL, Op.SHR,)) |
| 70 | binary_elem_wise_add_mul_sub = set((Op.Add, Op.Mul, Op.Sub,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 71 | binary_elem_wise_main_ops = binary_elem_wise_min_max_ops | binary_elem_wise_add_mul_sub | binary_elem_wise_shift_ops |
| 72 | elem_wise_main_ops = binary_elem_wise_main_ops | unary_elem_wise_main_ops |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 73 | supported_int32_tensor_ops = ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 74 | set((Op.ReduceSum, Op.CLZ,)) | binary_elem_wise_add_mul_sub | binary_elem_wise_shift_ops |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 75 | ) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 76 | activation_ops = set((Op.Relu, Op.Relu6, Op.ReluN1To1, Op.Sigmoid, Op.Tanh, Op.Softmax,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 77 | npu_post_ops = ( |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 78 | # activation functions |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 79 | activation_ops |
| 80 | # concatenation write direction |
| 81 | | set((Op.ConcatSliceWrite,)) |
| 82 | # Quantization |
| 83 | | set((Op.Quantize,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 84 | ) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 85 | split_ops = set((Op.Split, Op.SplitV, Op.StridedSlice, Op.Slice, Op.UnpackReshaped, Op.Unpack,)) |
| 86 | concat_ops = set((Op.Concat, Op.ConcatTFLite, Op.PackReshaped, Op.Pack,)) |
| 87 | memory_only_ops = set((Op.Squeeze, Op.Reshape, Op.QuantizedReshape, Op.ExpandDims,)) | concat_ops | split_ops |
| 88 | shapeless_input_ops = binary_elem_wise_main_ops | set((Op.Split, Op.SplitV,)) |
| 89 | supported_fused_activations = set((Op.Relu, Op.Relu6, Op.ReluN1To1, Op.Tanh, Op.Sigmoid, Op.LUT,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 90 | supported_operators = npu_pre_ops | mac_main_ops | elem_wise_main_ops | npu_post_ops | memory_only_ops |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 91 | supported_dtypes = set((DataType.uint8, DataType.int8, DataType.int16, DataType.int32)) |
| 92 | # Defined ranges for allowed values: |
| 93 | tens_dim_range = (1, 65535) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 94 | |
Fredrik Svedberg | 880e735 | 2020-08-25 11:31:47 +0200 | [diff] [blame] | 95 | def __init__(self): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 96 | # Setup supported operator restriction checkers |
| 97 | self.supported_operator_restrictions = {} |
| 98 | self.supported_operator_restrictions.update( |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 99 | {op: self.check_convolution_restrictions for op in SupportedOperators.convolution_ops} |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 100 | ) |
| 101 | self.supported_operator_restrictions.update( |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 102 | {op: self.check_depthwise_convolution_restrictions for op in SupportedOperators.depthwise_convolution_ops} |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 103 | ) |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 104 | self.supported_operator_restrictions.update( |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 105 | {op: self.check_transpose_convolution_restrictions for op in SupportedOperators.transpose_convolution_ops} |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 106 | ) |
| 107 | self.supported_operator_restrictions.update( |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 108 | {op: self.check_pooling_restrictions for op in SupportedOperators.pooling_ops} |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 109 | ) |
| 110 | self.supported_operator_restrictions.update( |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 111 | {op: self.check_resize_restrictions for op in SupportedOperators.resizing_ops} |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 112 | ) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 113 | self.supported_operator_restrictions.update( |
| 114 | {op: self.check_vector_product_restrictions for op in SupportedOperators.fc_vector_products} |
| 115 | ) |
| 116 | self.supported_operator_restrictions.update( |
| 117 | {op: self.check_element_wise_restrictions for op in SupportedOperators.elem_wise_main_ops} |
| 118 | ) |
| 119 | self.supported_operator_restrictions.update( |
| 120 | {op: self.check_memory_only_restrictions for op in SupportedOperators.memory_only_ops} |
| 121 | ) |
| 122 | self.supported_operator_restrictions.update( |
| 123 | {op: self.check_activation_ops for op in SupportedOperators.activation_ops} |
| 124 | ) |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 125 | # Setup the generic constraints. Note: the order matters |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 126 | self.generic_constraints = [] |
| 127 | self.generic_constraints.append(SupportedOperators.constraint_tens_defined_shape) |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 128 | self.generic_constraints.append(SupportedOperators.constraint_tens_output_shapeless) |
| 129 | self.generic_constraints.append(SupportedOperators.constraint_tens_input_shapeless) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 130 | self.generic_constraints.append(SupportedOperators.constraint_tens_shape_size) |
| 131 | self.generic_constraints.append(SupportedOperators.constraint_tens_dtype) |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 132 | self.generic_constraints.append(SupportedOperators.constraint_tens_int32_ops) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 133 | self.generic_constraints.append(SupportedOperators.constraint_tens_dimension) |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 134 | self.generic_constraints.append(SupportedOperators.constraint_tens_quant_none_check) |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 135 | self.generic_constraints.append(SupportedOperators.constraint_tens_quant_scale) |
| 136 | self.generic_constraints.append(SupportedOperators.constraint_faf) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 137 | |
| 138 | def is_operator_supported(self, op): |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 139 | if op.type not in SupportedOperators.supported_operators: |
Louis Verhaard | 5f2ea2f | 2020-10-15 08:39:44 +0200 | [diff] [blame^] | 140 | if op.type not in (Op.Placeholder, Op.SubgraphInput, Op.Const): |
| 141 | print("Info: {} '{}' is not supported on the NPU. Placing on CPU instead".format(op.type, op.name)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 142 | return False |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 143 | for constraint in self.generic_constraints: |
| 144 | valid, extra = constraint(op) |
| 145 | if not valid: |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 146 | print("Warning: {} '{}' is not supported on the NPU. Placing on CPU instead".format(op.type, op.name)) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 147 | print(" - {}".format(constraint.__doc__)) |
| 148 | if extra: |
| 149 | print(" {}".format(extra)) |
| 150 | return False |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 151 | if op.type in self.supported_operator_restrictions: |
| 152 | return self.supported_operator_restrictions[op.type](op) |
| 153 | return True |
| 154 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 155 | @staticmethod |
| 156 | def constraint_tens_defined_shape(op): |
| 157 | "Input(s) and Output Tensors must have a defined shape" |
| 158 | valid = True |
| 159 | extra = [] |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 160 | tensors = [tens for tens in op.inputs + op.outputs if tens] |
| 161 | for tens in tensors: |
| 162 | if not tens.has_fully_defined_shape(): |
| 163 | valid = False |
| 164 | extra.append("Tensor '{}' has shape: {}".format(tens.name, tens.shape)) |
| 165 | return valid, ", ".join(extra) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 166 | |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 167 | @staticmethod |
| 168 | def constraint_tens_output_shapeless(op): |
| 169 | "Scalar or Broadcasting Tensors are only valid for Input Tensors" |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 170 | valid = True |
| 171 | extra = [] |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 172 | for tens in op.outputs: |
| 173 | if tens.shape == []: |
| 174 | valid = False |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 175 | extra.append("Output Tensor '{}' is shapeless".format(tens.name)) |
| 176 | return valid, ", ".join(extra) |
| 177 | |
| 178 | @classmethod |
| 179 | @docstring_format_args([shapeless_input_ops]) |
| 180 | def constraint_tens_input_shapeless(cls, op): |
| 181 | "Scalar or Broadcasting Input Tensors are only valid for op type: {}" |
| 182 | valid = True |
| 183 | extra = [] |
| 184 | tensors = [tens for tens in op.inputs if tens] |
| 185 | for tens in tensors: |
| 186 | if (tens.shape == []) and (op.type not in cls.shapeless_input_ops): |
| 187 | valid = False |
| 188 | extra.append(tens.name) |
| 189 | extra = "Op '{}' has shapeless input tensor(s): {}".format(op.name, ", ".join(extra)) |
| 190 | return valid, extra |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 191 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 192 | @staticmethod |
| 193 | def constraint_tens_shape_size(op): |
| 194 | "Input(s) and Output Tensors must not be greater than 4D" |
| 195 | valid = True |
| 196 | extra = [] |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 197 | tensors = [tens for tens in op.inputs + op.outputs if tens] |
| 198 | for tens in tensors: |
| 199 | if len(tens.shape) > 4: |
| 200 | valid = False |
| 201 | extra.append("Tensor '{}' has shape: {}".format(tens.name, tens.shape)) |
| 202 | return valid, ", ".join(extra) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 203 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 204 | @classmethod |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 205 | @docstring_format_args([supported_dtypes]) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 206 | def constraint_tens_dtype(cls, op): |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 207 | "Tensors must be of type: {}" |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 208 | valid = True |
| 209 | extra = [] |
| 210 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 211 | tensors = tensors if tensors else op.inputs |
| 212 | for tens in tensors: |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 213 | if tens.dtype not in cls.supported_dtypes: |
| 214 | valid = False |
| 215 | extra.append("Tensor '{}' has data type: {}".format(tens.name, tens.dtype)) |
| 216 | return valid, ", ".join(extra) |
| 217 | |
| 218 | @classmethod |
| 219 | @docstring_format_args([supported_int32_tensor_ops]) |
| 220 | def constraint_tens_int32_ops(cls, op): |
| 221 | "Tensors which are int32 are only valid when op type is: {}" |
| 222 | valid = True |
| 223 | extra = [] |
| 224 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 225 | tensors = tensors if tensors else op.inputs |
| 226 | for tens in tensors: |
| 227 | if (tens.dtype == DataType.int32) and (op.type not in cls.supported_int32_tensor_ops): |
| 228 | valid = False |
| 229 | extra.append(tens.name) |
| 230 | extra = "Op '{}' has int32 tensor(s): {}".format(op.name, ", ".join(extra)) |
| 231 | return valid, extra |
Andreas Nevalainen | eadb166 | 2020-09-01 15:36:26 +0200 | [diff] [blame] | 232 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 233 | @classmethod |
| 234 | @docstring_format_args(tens_dim_range) |
| 235 | def constraint_tens_dimension(cls, op): |
| 236 | "Tensor dimensions must be in the range {}-{} (inclusive)" |
| 237 | tens_min, tens_max = cls.tens_dim_range |
| 238 | valid = True |
| 239 | extra = [] |
| 240 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 241 | tensors = tensors if tensors else op.inputs |
| 242 | for tens in tensors: |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 243 | if not all(tens_min <= dim <= tens_max for dim in tens.shape): |
| 244 | valid = False |
| 245 | extra.append("Tensor '{}' has shape: {}".format(tens.name, tens.shape)) |
| 246 | return valid, ", ".join(extra) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 247 | |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 248 | @staticmethod |
| 249 | def constraint_tens_quant_none_check(op): |
| 250 | "Tensors must have quantization parameters" |
| 251 | valid = True |
| 252 | extra = [] |
| 253 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 254 | for tens in tensors: |
| 255 | if tens.quantization is None: |
| 256 | valid = False |
| 257 | extra.append("Tensor '{}' has no quantization parameters".format(tens.name)) |
| 258 | return valid, ", ".join(extra) |
| 259 | |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 260 | @staticmethod |
| 261 | def constraint_tens_quant_scale(op): |
| 262 | "Tensors with quantization scales must be finite" |
| 263 | valid = True |
| 264 | extra = [] |
| 265 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 266 | for tens in tensors: |
| 267 | if (tens.quantization.scale_f32 is not None) and np.isinf(tens.quantization.scale_f32).any(): |
| 268 | valid = False |
| 269 | extra.append("Tensor '{}' has quantization scale: {}".format(tens.name, tens.quantization.scale_f32)) |
| 270 | return valid, ", ".join(extra) |
| 271 | |
| 272 | @classmethod |
| 273 | @docstring_format_args([supported_fused_activations]) |
| 274 | def constraint_faf(cls, op): |
| 275 | "The fused activation function (if present) must be one of type: {}" |
| 276 | faf = op.activation |
| 277 | valid = (faf is None) or (faf in cls.supported_fused_activations) |
| 278 | extra = "Op '{}' has its fused activation function as: {}".format(op.name, faf) |
| 279 | return valid, extra |
| 280 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 281 | @classmethod |
| 282 | def check_convolution_restrictions(cls, op): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 283 | # check stride |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 284 | stride_w, stride_h = op.attrs["stride_w"], op.attrs["stride_h"] |
| 285 | if not is_integer(stride_w) or not is_integer(stride_h): |
| 286 | print("Warning:", op.type, "has non-integer stride, placing on CPU") |
| 287 | return False |
| 288 | if not 1 <= stride_w <= 3 or not 1 <= stride_h <= 3: |
| 289 | print( |
| 290 | "Warning: {} has stride ({}, {}), only strides in range [1, 3] are allowed. Placing on CPU".format( |
| 291 | op.type, stride_w, stride_h |
| 292 | ) |
| 293 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 294 | return False |
| 295 | |
| 296 | # check dilation |
| 297 | dilation_w_factor = op.attrs.get("dilation_w_factor", 1) |
| 298 | dilation_h_factor = op.attrs.get("dilation_h_factor", 1) |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 299 | if not is_integer(dilation_w_factor) or not is_integer(dilation_h_factor): |
| 300 | print("Warning:", op.type, "has non-integer dilation factor, placing on CPU") |
| 301 | return False |
| 302 | if not 1 <= dilation_w_factor <= 2 or not 1 <= dilation_h_factor <= 2: |
| 303 | print( |
| 304 | "Warning:", |
| 305 | op.type, |
| 306 | "has dilation factors ({}, {}), only factors in range [1, 2] are allowed. Placing on CPU".format( |
| 307 | dilation_w_factor, dilation_h_factor |
| 308 | ), |
| 309 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 310 | return False |
| 311 | |
| 312 | # check data type |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 313 | ifm_tensor, _, weight_tensor, bias_tensor, _ = op.get_ifm_ifm2_weights_biases_ofm() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 314 | if weight_tensor.element_size() > 1: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 315 | print("Warning: only 8-bit weights are supported, placing on CPU") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 316 | return False |
| 317 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 318 | if not cls.check_bias_restrictions(bias_tensor): |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 319 | return False |
| 320 | |
Andreas Nevalainen | f0c59bf | 2020-08-26 10:56:23 +0200 | [diff] [blame] | 321 | # check kernel size [HWIO] |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 322 | dilated_weight_w = (weight_tensor.shape[1] - 1) * dilation_w_factor + 1 |
| 323 | dilated_weight_h = (weight_tensor.shape[0] - 1) * dilation_h_factor + 1 |
Andreas Nevalainen | f0c59bf | 2020-08-26 10:56:23 +0200 | [diff] [blame] | 324 | |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 325 | # kernel limits |
| 326 | if not 1 <= dilated_weight_h <= 64: |
| 327 | print("Warning:", op.type, "has kernel height outside of range [1, 64], placing on CPU") |
| 328 | return False |
| 329 | if not 1 <= dilated_weight_w * dilated_weight_h <= 64 * 64: |
| 330 | print( |
| 331 | "Warning: product of kernel width and height must be >= 1 and not exceed 64 * 64 ({}),".format(64 * 64), |
| 332 | "placing on CPU", |
| 333 | ) |
Andreas Nevalainen | f0c59bf | 2020-08-26 10:56:23 +0200 | [diff] [blame] | 334 | return False |
| 335 | |
Andreas Nevalainen | 8854dc9 | 2020-09-24 13:43:00 +0200 | [diff] [blame] | 336 | # check non const weights |
| 337 | if weight_tensor.values is None: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 338 | print("Warning:", op.type, "has non-constant weights, placing on CPU") |
Andreas Nevalainen | 8854dc9 | 2020-09-24 13:43:00 +0200 | [diff] [blame] | 339 | return False |
| 340 | |
Andreas Nevalainen | f0c59bf | 2020-08-26 10:56:23 +0200 | [diff] [blame] | 341 | # check weight sums over [HWI] |
| 342 | zero_point = weight_tensor.quantization.zero_point |
| 343 | quant_weights = weight_tensor.quant_values.astype(np.int64) |
| 344 | weights = quant_weights - zero_point |
| 345 | totals = np.sum(np.absolute(weights), axis=(0, 1, 2)) |
| 346 | |
| 347 | if np.amax(totals) > 127 * 65536: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 348 | print("Warning: sum of weights exceeds 127 * 65536 ({}), placing on CPU".format(127 * 65536)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 349 | return False |
| 350 | |
| 351 | # check batch size |
| 352 | if ifm_tensor.shape[0] != 1: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 353 | print("Warning: only batch sizes of 1 are supported, placing on CPU") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 354 | return False |
Andreas Nevalainen | d8c032d | 2020-09-11 10:25:09 +0200 | [diff] [blame] | 355 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 356 | return True |
| 357 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 358 | @classmethod |
| 359 | def check_depthwise_convolution_restrictions(cls, op): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 360 | # check depth |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 361 | ifm_tensor, ofm_tensor = op.get_ifm_ofm() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 362 | if op.attrs["depth_multiplier"] > 1 and not ( |
| 363 | (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"]) |
| 364 | ): |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 365 | print( |
| 366 | "Warning: for depth multipliers > 1,", |
| 367 | "number of input channels must be 1 and number of output channels must be equal to depth multiplier.", |
| 368 | "Placing on CPU", |
| 369 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 370 | return False |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 371 | return cls.check_convolution_restrictions(op) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 372 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 373 | @classmethod |
| 374 | def check_transpose_convolution_restrictions(cls, op): |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 375 | # check stride |
| 376 | stride_h, stride_w = op.attrs["stride_h"], op.attrs["stride_w"] |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 377 | if stride_h != 2 or stride_w != 2: |
| 378 | print("Warning: stride must be equal to 2, placing on CPU") |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 379 | return False |
| 380 | |
| 381 | # check output dimensions |
| 382 | ifm_tensor, weight_tensor, _, ofm_tensor = op.get_ifm_weights_biases_ofm() |
| 383 | ifm_h, ifm_w = ifm_tensor.shape[1], ifm_tensor.shape[2] |
| 384 | ofm_h, ofm_w = ofm_tensor.shape[1], ofm_tensor.shape[2] |
| 385 | if op.attrs["padding"] == b"SAME": |
| 386 | if (ofm_h != ifm_h * stride_h) or (ofm_w != ifm_w * stride_w): |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 387 | print( |
| 388 | "Warning: for", |
| 389 | op.type, |
| 390 | "using SAME padding, output dimensions must equal input dimensions multiplied by stride.", |
| 391 | "Placing on CPU", |
| 392 | ) |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 393 | return False |
| 394 | elif op.attrs["padding"] == b"VALID": |
| 395 | kernel_h, kernel_w = weight_tensor.shape[0], weight_tensor.shape[1] |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 396 | if (ofm_h != (ifm_h) * stride_h + max(kernel_h - stride_h, 0)) or ( |
| 397 | ofm_w != (ifm_w) * stride_w + max(kernel_w - stride_w, 0) |
| 398 | ): |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 399 | print( |
| 400 | "Warning: for", |
| 401 | op.type, |
| 402 | "using VALID padding, output dimensions must equal input dimensions multiplied by stride,", |
| 403 | "minus difference between kernel size and stride. Placing on CPU", |
| 404 | ) |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 405 | return False |
| 406 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 407 | return cls.check_convolution_restrictions(op) |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 408 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 409 | @classmethod |
| 410 | def check_pooling_restrictions(cls, op): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 411 | # check stride |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 412 | stride_w, stride_h = op.attrs["stride_w"], op.attrs["stride_h"] |
| 413 | if not is_integer(stride_w) or not is_integer(stride_h): |
| 414 | print("Warning:", op.type, "has non-integer stride, placing on CPU") |
| 415 | return False |
| 416 | if not 1 <= stride_w <= 3 or not 1 <= stride_h <= 3: |
| 417 | print( |
| 418 | "Warning: {} has stride ({}, {}), only strides in range [1, 3] are allowed. Placing on CPU".format( |
| 419 | op.type, stride_w, stride_h |
| 420 | ) |
| 421 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 422 | return False |
| 423 | |
| 424 | # check data type |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 425 | ifm_tensor, ofm_tensor = op.get_ifm_ofm() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 426 | if ifm_tensor.dtype != ofm_tensor.dtype: |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 427 | if op.type != Op.ReduceSum: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 428 | print("Warning: input data type doesn't match output data type, placing on CPU") |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 429 | return False |
| 430 | # TODO: else check ReduceSum restrictions. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 431 | |
| 432 | # check batch size |
| 433 | if ifm_tensor.shape[0] != 1: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 434 | print("Warning: input batch size must be 1, placing on CPU") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 435 | return False |
| 436 | |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 437 | # check kernel size |
| 438 | kernel_w, kernel_h = op.attrs["filter_width"], op.attrs["filter_height"] |
| 439 | if op.type in cls.avg_pooling_ops and op.attrs["padding"] == b"SAME": |
| 440 | if not 1 <= kernel_w <= 8 or not 1 <= kernel_h <= 8: |
| 441 | print( |
| 442 | "Warning:", |
| 443 | op.type, |
| 444 | "has kernel size ({}, {}), only kernel sizes in range [1, 8] are allowed. Placing on CPU".format( |
| 445 | kernel_w, kernel_h |
| 446 | ), |
| 447 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 448 | return False |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 449 | if op.type in cls.avg_pooling_ops and op.attrs["padding"] == b"VALID" or op.type in cls.max_pooling_ops: |
| 450 | if not 1 <= kernel_w * kernel_h <= 256 * 256: |
| 451 | print( |
| 452 | "Warning: product of kernel width and height must be >= 1 and not exceed 256 * 256 ({}),".format( |
| 453 | 256 * 256 |
| 454 | ), |
| 455 | "placing on CPU", |
| 456 | ) |
| 457 | return False |
| 458 | if not 1 <= kernel_h <= 256: |
| 459 | print("Warning:", op.type, "has kernel height outside of range [1, 256], placing on CPU") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 460 | return False |
| 461 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 462 | return True |
| 463 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 464 | @classmethod |
| 465 | def check_resize_restrictions(cls, op): |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 466 | # check unsupported upscaling factor |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 467 | if op.type == Op.ResizeBilinear: |
Charles Xu | 9a03fdf | 2020-07-02 15:12:40 +0200 | [diff] [blame] | 468 | if op.inputs[0].shape[1] == 1 and op.inputs[0].shape[2] == 1: |
| 469 | return True |
Charles Xu | 36ffaf3 | 2020-08-05 15:40:44 +0200 | [diff] [blame] | 470 | if op.inputs[0].shape == op.outputs[0].shape: |
| 471 | return True |
Charles Xu | 87c1350 | 2020-08-06 12:17:26 +0200 | [diff] [blame] | 472 | upscaled_shape = np.array(op.inputs[0].shape[1:3]) |
| 473 | out_shape = np.array(op.outputs[0].shape[1:3]) |
| 474 | while (upscaled_shape < out_shape).all(): |
| 475 | upscaled_shape *= 2 |
| 476 | if op.attrs["align_corners"]: |
| 477 | upscaled_shape -= 1 |
| 478 | if np.array_equal(out_shape, upscaled_shape): |
| 479 | return True |
| 480 | return False |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 481 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 482 | @classmethod |
| 483 | def check_vector_product_restrictions(cls, op): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 484 | # check data type |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 485 | ifm_tensor, _, weight_tensor, bias_tensor, _ = op.get_ifm_ifm2_weights_biases_ofm() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 486 | if weight_tensor.element_size() > 1: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 487 | print("Warning: only 8-bit datatypes supported for {}, placing on CPU".format(op.type)) |
| 488 | return False |
| 489 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 490 | if not cls.check_bias_restrictions(bias_tensor): |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 491 | return False |
| 492 | |
Andreas Nevalainen | d8c032d | 2020-09-11 10:25:09 +0200 | [diff] [blame] | 493 | # check non const weights |
| 494 | if weight_tensor.values is None: |
| 495 | print("Warning:", op.type, "has non-const weights, placing on CPU") |
| 496 | return False |
| 497 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 498 | return True |
| 499 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 500 | @classmethod |
| 501 | def check_element_wise_restrictions(cls, op): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 502 | # check data type |
| 503 | ifm_tensor, ifm2_tensor, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm() |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 504 | # input and output datatype must match for these operators |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 505 | if ( |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 506 | op.type in cls.binary_elem_wise_min_max_ops | cls.unary_elem_wise_main_ops |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 507 | and ifm_tensor.dtype != ofm_tensor.dtype |
| 508 | ): |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 509 | print("Warning:", op.type, "must have same input and output datatype, placing on CPU") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 510 | return False |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 511 | if op.type in cls.binary_elem_wise_add_mul_sub: |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 512 | # both inputs must have same type |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 513 | if ifm_tensor.dtype != ifm2_tensor.dtype: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 514 | print("Warning:", op.type, "must have same datatype on both inputs, placing on CPU") |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 515 | return False |
| 516 | # signed input check |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 517 | if ifm_tensor.dtype.type & BaseType.Signed: |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 518 | # output must be signed |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 519 | if ofm_tensor.dtype.type & BaseType.Unsigned: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 520 | print("Warning: only signed output types supported for {}, placing on CPU".format(op.type)) |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 521 | return False |
| 522 | # and 8, 16 or 32-bit |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 523 | bit_lengths = {8, 16, 32} |
| 524 | if ofm_tensor.element_size() * 8 not in bit_lengths: |
| 525 | print( |
| 526 | "Warning:", op.type, "is only supported for bit lengths {}, placing on CPU".format(bit_lengths) |
| 527 | ) |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 528 | return False |
| 529 | # unsigned input check, output must be same type or int32 |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 530 | if ifm_tensor.dtype.type & BaseType.Unsigned and not ( |
| 531 | ifm_tensor.dtype == ofm_tensor.dtype or ofm_tensor.dtype == DataType.int32 |
| 532 | ): |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 533 | print("Warning:", op.type, "has unsigned input but output is not unsigned or int32, placing on CPU") |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 534 | return False |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 535 | elif op.type in cls.binary_elem_wise_shift_ops: |
Fredrik Svedberg | 597fd3f | 2020-08-13 10:02:53 +0200 | [diff] [blame] | 536 | if ifm_tensor.dtype != DataType.int32 or ifm2_tensor.dtype != DataType.int32: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 537 | print("Warning:", op.type, "input datatypes are not int32, placing on CPU") |
Fredrik Svedberg | 597fd3f | 2020-08-13 10:02:53 +0200 | [diff] [blame] | 538 | return False |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 539 | if op.type in (Op.CLZ, Op.SHL) and ofm_tensor.dtype != DataType.int32: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 540 | print("Warning:", op.type, "output datatype is not int32, placing on CPU") |
Fredrik Svedberg | 597fd3f | 2020-08-13 10:02:53 +0200 | [diff] [blame] | 541 | return False |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 542 | |
| 543 | # check batch size |
Dwight Lidman | f995db7 | 2020-04-27 11:15:12 +0200 | [diff] [blame] | 544 | if len(ifm_tensor.shape) > 2 and ifm_tensor.shape[0] != 1: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 545 | print( |
| 546 | "Warning:", |
| 547 | op.type, |
| 548 | "only supports batch size 1 for tensors with more than 2 dimensions, placing on CPU", |
| 549 | ) |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 550 | return False |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 551 | if op.type in cls.binary_elem_wise_main_ops: # if op type is unary, ifm2_tensor is None |
Dwight Lidman | f995db7 | 2020-04-27 11:15:12 +0200 | [diff] [blame] | 552 | if len(ifm2_tensor.shape) > 2 and ifm2_tensor.shape[0] != 1: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 553 | print( |
| 554 | "Warning:", |
| 555 | op.type, |
| 556 | "only supports batch size 1 for tensors with more than 2 dimensions, placing on CPU", |
| 557 | ) |
Dwight Lidman | f995db7 | 2020-04-27 11:15:12 +0200 | [diff] [blame] | 558 | return False |
Dwight Lidman | 332a704 | 2020-06-11 15:32:42 +0200 | [diff] [blame] | 559 | |
| 560 | # negative alpha values are not supported |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 561 | if op.type == Op.LeakyRelu and op.attrs["alpha"] < 0: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 562 | print("Warning:", op.type, "has negative alpha, placing on CPU") |
Dwight Lidman | 332a704 | 2020-06-11 15:32:42 +0200 | [diff] [blame] | 563 | return False |
| 564 | |
Andreas Nevalainen | d8c032d | 2020-09-11 10:25:09 +0200 | [diff] [blame] | 565 | # check if ifm or ifm2 has ofm shape |
| 566 | if ifm_tensor.shape != ofm_tensor.shape and ifm2_tensor.shape != ofm_tensor.shape: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 567 | print("Warning:", op.type, "input shape(s) differ from output shape, placing on CPU") |
Andreas Nevalainen | d8c032d | 2020-09-11 10:25:09 +0200 | [diff] [blame] | 568 | return False |
| 569 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 570 | if op.type in cls.binary_elem_wise_min_max_ops and not cls.check_quantization_restrictions_binary_elem_wise(op): |
Patrik Gustavsson | 530992a | 2020-09-30 13:26:59 +0200 | [diff] [blame] | 571 | return False |
| 572 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 573 | return True |
| 574 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 575 | @classmethod |
| 576 | def check_memory_only_restrictions(cls, op): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 577 | if op.type == Op.StridedSlice: |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 578 | if len(op.inputs) != 4: |
| 579 | warn_cpu(op, "has {} input tensors, only 4 inputs are supported".format(len(op.inputs))) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 580 | return False |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 581 | input_tens, begin_tens, end_tens, strides_tens = op.inputs |
| 582 | if begin_tens.values is None or end_tens.values is None or strides_tens.values is None: |
| 583 | warn_cpu(op, "has a non-constant begin, end, or stride input tensor, which is not supported") |
| 584 | return False |
| 585 | if not ( |
| 586 | len(input_tens.shape) |
| 587 | == len(op.outputs[0].shape) |
| 588 | == len(begin_tens.values) |
| 589 | == len(end_tens.values) |
| 590 | == len(strides_tens.values) |
| 591 | ): |
| 592 | warn_cpu(op, "has input tensors with shapes that are not supported") |
| 593 | return False |
| 594 | # check stride size |
| 595 | if any(stride != 1 for stride in strides_tens.values): |
| 596 | warn_cpu(op, "has stride values {}, only stride 1 values are supported".format(strides_tens.values)) |
Michael McGeagh | ecd2052 | 2020-07-31 16:59:45 +0100 | [diff] [blame] | 597 | return False |
Patrik Gustavsson | cf72890 | 2020-04-30 08:57:23 +0200 | [diff] [blame] | 598 | # check ellipsis_mask |
| 599 | if op.attrs["ellipsis_mask"] != 0: |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 600 | warn_cpu(op, "ellipsis_mask is {}, only 0 is supported".format(op.attrs["ellipsis_mask"])) |
Patrik Gustavsson | cf72890 | 2020-04-30 08:57:23 +0200 | [diff] [blame] | 601 | return False |
| 602 | # check if both new_axis_mask and shrink_axis_mask have bit set |
| 603 | if op.attrs["new_axis_mask"] != 0 and op.attrs["shrink_axis_mask"] != 0: |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 604 | warn_cpu(op, "new_axis_mask and shrink_axis_mask are both non-zero, which is not supported") |
| 605 | return False |
| 606 | # Calculate offset start/end |
| 607 | offset_start = get_slice_offsets(input_tens.shape, begin_tens, op.attrs["begin_mask"], is_begin=True) |
| 608 | offset_end = get_slice_offsets(input_tens.shape, end_tens, op.attrs["end_mask"], is_begin=False) |
| 609 | # check "end - begin" doesn't result in any zero or negative elements |
| 610 | if any((end - begin) <= 0 for begin, end in zip(offset_start, offset_end)): |
| 611 | warn_cpu( |
| 612 | op, |
| 613 | "has slice begin values {}, some of which are >= end values {}, which is illegal".format( |
| 614 | begin_tens.values, end_tens.values |
| 615 | ), |
| 616 | ) |
Patrik Gustavsson | cf72890 | 2020-04-30 08:57:23 +0200 | [diff] [blame] | 617 | return False |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 618 | if op.type == Op.SplitV: |
Patrik Gustavsson | 271ddc3 | 2020-09-01 09:15:27 +0200 | [diff] [blame] | 619 | # check that maximum one size is set to -1, indicating that size should be inferred |
| 620 | sizes = op.inputs[1].values |
| 621 | num_to_be_inferred = 0 |
| 622 | for size in sizes: |
| 623 | if size == -1: |
| 624 | num_to_be_inferred += 1 |
| 625 | |
| 626 | if num_to_be_inferred > 1: |
| 627 | print("Warning:", op.type, "has more than one size to be inferred, which is illegal, placing on CPU") |
| 628 | return False |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 629 | if op.type in set((Op.Concat, Op.ConcatTFLite,)): |
Fredrik Svedberg | 0f98b36 | 2020-09-29 10:00:39 +0200 | [diff] [blame] | 630 | axis = op.attrs.get("axis", None) |
| 631 | if axis is None: |
| 632 | print("Warning:", op.type, "invalid or missing axis, placing on CPU") |
| 633 | return False |
| 634 | if axis < 0: |
| 635 | axis += len(op.inputs[0].shape) |
Patrik Gustavsson | 36ad73a | 2020-10-06 13:58:24 +0200 | [diff] [blame] | 636 | if not 0 <= axis < len(op.inputs[0].shape): |
Fredrik Svedberg | 0f98b36 | 2020-09-29 10:00:39 +0200 | [diff] [blame] | 637 | print("Warning:", op.type, "invalid axis", axis, ", placing on CPU") |
| 638 | return False |
| 639 | ofm = op.outputs[0] |
| 640 | ofm_dims = len(ofm.shape) |
| 641 | for ifm in op.inputs: |
| 642 | if len(ifm.shape) != ofm_dims: |
| 643 | return False |
| 644 | for i in range(ofm_dims): |
| 645 | if i != axis and ifm.shape[i] != ofm.shape[i]: |
Patrik Gustavsson | 530992a | 2020-09-30 13:26:59 +0200 | [diff] [blame] | 646 | print( |
| 647 | "Warning:", |
| 648 | op.type, |
| 649 | "invalid ifm:", |
| 650 | ifm.name, |
| 651 | ifm.shape, |
| 652 | "mismatch in dimension", |
| 653 | i, |
| 654 | ", placing on CPU", |
| 655 | ) |
Fredrik Svedberg | 0f98b36 | 2020-09-29 10:00:39 +0200 | [diff] [blame] | 656 | return False |
Patrik Gustavsson | 271ddc3 | 2020-09-01 09:15:27 +0200 | [diff] [blame] | 657 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 658 | return True |
Dwight Lidman | ebe26c7 | 2020-06-09 11:40:54 +0200 | [diff] [blame] | 659 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 660 | @classmethod |
| 661 | def check_quantization_restrictions_binary_elem_wise(cls, op): |
Dwight Lidman | ebe26c7 | 2020-06-09 11:40:54 +0200 | [diff] [blame] | 662 | # makes sure IFM1, IFM2 and OFM quantization are equal for binary ops |
Tim Hall | e3786ac | 2020-07-28 17:40:50 +0100 | [diff] [blame] | 663 | assert len(op.inputs) >= 2 and len(op.outputs) == 1 |
| 664 | |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 665 | if ( |
Tim Hall | e3786ac | 2020-07-28 17:40:50 +0100 | [diff] [blame] | 666 | op.inputs[0].quantization is None |
Michael McGeagh | 34ad19b | 2020-09-04 15:44:23 +0100 | [diff] [blame] | 667 | or not op.inputs[0].is_scaling_equal(op.inputs[1]) |
| 668 | or not op.inputs[0].is_scaling_equal(op.outputs[0]) |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 669 | ): |
| 670 | print( |
| 671 | "Warning: Input/output tensors with different quantization is unsupported for the", op.type, "operator" |
| 672 | ) |
Dwight Lidman | ebe26c7 | 2020-06-09 11:40:54 +0200 | [diff] [blame] | 673 | return False |
Tim Hall | e3786ac | 2020-07-28 17:40:50 +0100 | [diff] [blame] | 674 | |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 675 | return True |
| 676 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 677 | @classmethod |
| 678 | def check_activation_ops(cls, op): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 679 | if op.type == Op.Softmax: |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 680 | ifm_tensor = op.inputs[0] |
| 681 | ofm_tensor = op.outputs[0] |
| 682 | |
| 683 | # check data type |
| 684 | if ifm_tensor.dtype != ofm_tensor.dtype: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 685 | print("Warning:", op.type, "input type differs from output type, placing on CPU") |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 686 | return False |
| 687 | |
Fredrik Svedberg | 597fd3f | 2020-08-13 10:02:53 +0200 | [diff] [blame] | 688 | if ifm_tensor.dtype not in (DataType.uint8, DataType.int8, DataType.int16): |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 689 | print( |
| 690 | "Warning: only datatypes supported for {} are uint8, int8 and int16; placing on CPU".format(op.type) |
| 691 | ) |
Fredrik Svedberg | 597fd3f | 2020-08-13 10:02:53 +0200 | [diff] [blame] | 692 | return False |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 693 | |
Fredrik Svedberg | 835d8e1 | 2020-09-04 09:46:17 +0200 | [diff] [blame] | 694 | # check shape |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 695 | if ifm_tensor.shape != ofm_tensor.shape: |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 696 | print("Warning:", op.type, "input shape differs from output shape, placing on CPU") |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 697 | return False |
| 698 | |
| 699 | return True |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 700 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 701 | @classmethod |
| 702 | def check_bias_restrictions(cls, bias_tensor): |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 703 | # check data type |
Jacob Bohlin | 258ebba | 2020-08-31 10:44:35 +0200 | [diff] [blame] | 704 | if bias_tensor is not None and bias_tensor.dtype not in (DataType.int32, DataType.int64): |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 705 | print("Warning: bias tensor datatype must be int32 or int64, placing on CPU") |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 706 | return False |
| 707 | |
| 708 | # check if values fits in 40-bit |
Jacob Bohlin | 258ebba | 2020-08-31 10:44:35 +0200 | [diff] [blame] | 709 | if bias_tensor is not None and bias_tensor.dtype == DataType.int64: |
Tim Hall | 7152517 | 2020-08-29 15:09:57 +0100 | [diff] [blame] | 710 | for quant_value in bias_tensor.quant_values: |
| 711 | if not (-(1 << 39) <= quant_value < (1 << 39)): |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 712 | print("Warning: bias tensor values are larger than 40 bits, placing on CPU") |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 713 | return False |
| 714 | |
| 715 | return True |