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. |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 18 | from collections import defaultdict |
| 19 | |
Charles Xu | 87c1350 | 2020-08-06 12:17:26 +0200 | [diff] [blame] | 20 | import numpy as np |
| 21 | |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 22 | from .data_type import BaseType |
| 23 | from .data_type import DataType |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 24 | from .numeric_util import is_integer |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 25 | from .operation import get_slice_offsets |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 26 | from .operation import Op |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 27 | from .tensor import check_quantized_tens_scaling_equal |
Michael McGeagh | 219ec07 | 2020-11-09 11:11:26 +0000 | [diff] [blame] | 28 | from .tflite_mapping import optype_to_builtintype |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 29 | |
| 30 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 31 | # Custom decorator function to allow formatting docstrings containing "{}" |
| 32 | def docstring_format_args(args): |
| 33 | def docstring(func): |
| 34 | func.__doc__ = func.__doc__.format(*args) |
| 35 | return func |
| 36 | |
| 37 | return docstring |
| 38 | |
| 39 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 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,)) |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 46 | convolution_like_ops = convolution_ops | depthwise_convolution_ops | transpose_convolution_ops |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 47 | max_pooling_ops = Op.op_set(Op.is_maxpool_op) |
| 48 | avg_pooling_ops = Op.op_set(Op.is_avgpool_op) |
| 49 | pooling_ops = set((Op.ReduceSum,)) | max_pooling_ops | avg_pooling_ops |
| 50 | resizing_ops = set((Op.ResizeBilinear,)) |
| 51 | fc_vector_products = set((Op.QuantizedMatMul, Op.MatMul, Op.FullyConnected,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 52 | mac_main_ops = ( |
| 53 | # RNN/LSTM/GRU |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 54 | set((Op.BlockLSTM,)) |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 55 | # conv/depthwiseconv/transposeconv |
| 56 | | convolution_like_ops |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 57 | # pooling |
| 58 | | pooling_ops |
| 59 | # resizing/upscaling |
| 60 | | resizing_ops |
| 61 | # FC layers |
| 62 | | fc_vector_products |
| 63 | ) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 64 | unary_elem_wise_main_ops = Op.op_set(Op.is_unary_elementwise_op) |
| 65 | binary_elem_wise_min_max_ops = set((Op.Minimum, Op.Maximum,)) |
| 66 | binary_elem_wise_shift_ops = set((Op.SHL, Op.SHR,)) |
| 67 | 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] | 68 | binary_elem_wise_main_ops = binary_elem_wise_min_max_ops | binary_elem_wise_add_mul_sub | binary_elem_wise_shift_ops |
| 69 | 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] | 70 | supported_int32_tensor_ops = ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 71 | 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] | 72 | ) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 73 | relu_ops = Op.op_set(Op.is_relu_op) |
| 74 | activation_ops = relu_ops | set((Op.Tanh, Op.Sigmoid, Op.Softmax,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 75 | npu_post_ops = ( |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 76 | # activation functions |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 77 | activation_ops |
| 78 | # concatenation write direction |
| 79 | | set((Op.ConcatSliceWrite,)) |
| 80 | # Quantization |
| 81 | | set((Op.Quantize,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 82 | ) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 83 | split_ops = set((Op.Split, Op.SplitV, Op.StridedSlice, Op.Slice, Op.UnpackReshaped, Op.Unpack,)) |
| 84 | concat_ops = set((Op.Concat, Op.ConcatTFLite, Op.PackReshaped, Op.Pack,)) |
| 85 | memory_only_ops = set((Op.Squeeze, Op.Reshape, Op.QuantizedReshape, Op.ExpandDims,)) | concat_ops | split_ops |
| 86 | shapeless_input_ops = binary_elem_wise_main_ops | set((Op.Split, Op.SplitV,)) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 87 | supported_fused_activations = relu_ops | set((Op.Tanh, Op.Sigmoid, Op.LUT,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 88 | supported_operators = npu_pre_ops | mac_main_ops | elem_wise_main_ops | npu_post_ops | memory_only_ops |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 89 | # Supported data types |
| 90 | supported_op_dtypes = set((DataType.uint8, DataType.int8, DataType.int16, DataType.int32)) |
| 91 | supported_bias_dtypes = set((DataType.int32, DataType.int64)) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 92 | # Defined ranges for allowed values: |
| 93 | tens_dim_range = (1, 65535) |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 94 | stride_range = (1, 3) |
| 95 | dilation_range = (1, 2) |
| 96 | dilated_height_range = (1, 64) |
| 97 | dilated_product_range = (1, 64 * 64) |
| 98 | weights_limit = 127 * 65536 |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 99 | filter_range = (1, 8) |
| 100 | filter_height_range = (1, 256) |
| 101 | filter_product_range = (1, 256 * 256) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 102 | |
Fredrik Svedberg | 880e735 | 2020-08-25 11:31:47 +0200 | [diff] [blame] | 103 | def __init__(self): |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 104 | # Setup the generic constraints. Note: the order matters |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 105 | self.generic_constraints = [] |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 106 | self.generic_constraints.append(SupportedOperators.constraint_tens_no_dynamic) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 107 | self.generic_constraints.append(SupportedOperators.constraint_tens_defined_shape) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 108 | self.generic_constraints.append(SupportedOperators.constraint_tens_output_scalar) |
| 109 | self.generic_constraints.append(SupportedOperators.constraint_tens_input_scalar) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 110 | self.generic_constraints.append(SupportedOperators.constraint_tens_shape_size) |
| 111 | self.generic_constraints.append(SupportedOperators.constraint_tens_dtype) |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 112 | self.generic_constraints.append(SupportedOperators.constraint_tens_int32_ops) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 113 | self.generic_constraints.append(SupportedOperators.constraint_tens_dimension) |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 114 | self.generic_constraints.append(SupportedOperators.constraint_tens_quant_none_check) |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 115 | self.generic_constraints.append(SupportedOperators.constraint_tens_quant_scale) |
| 116 | self.generic_constraints.append(SupportedOperators.constraint_faf) |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 117 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 118 | # Setup specific constraints. Note: the order matters |
| 119 | self.specific_constraints = defaultdict(list) |
| 120 | |
| 121 | # Conv-like checks: |
| 122 | for op_type in SupportedOperators.convolution_like_ops: |
| 123 | self.specific_constraints[op_type].append(SupportedOperators.constraint_stride_type) |
| 124 | self.specific_constraints[op_type].append(SupportedOperators.constraint_stride_range) |
| 125 | self.specific_constraints[op_type].append(SupportedOperators.constraint_dilation_type) |
| 126 | self.specific_constraints[op_type].append(SupportedOperators.constraint_dilation_range) |
| 127 | self.specific_constraints[op_type].append(SupportedOperators.constraint_dilated_height_range) |
| 128 | self.specific_constraints[op_type].append(SupportedOperators.constraint_dilated_product_range) |
| 129 | self.specific_constraints[op_type].append(SupportedOperators.constraint_weights_type) |
| 130 | self.specific_constraints[op_type].append(SupportedOperators.constraint_weights_const) |
| 131 | self.specific_constraints[op_type].append(SupportedOperators.constraint_weights_limit) |
| 132 | self.specific_constraints[op_type].append(SupportedOperators.constraint_bias_type) |
| 133 | self.specific_constraints[op_type].append(SupportedOperators.constraint_bias_40bit) |
| 134 | self.specific_constraints[op_type].append(SupportedOperators.constraint_batch_size) |
| 135 | # Depthwise Conv specific checks: |
| 136 | for op_type in SupportedOperators.depthwise_convolution_ops: |
| 137 | self.specific_constraints[op_type].append(SupportedOperators.constraint_depth_multiplier) |
| 138 | # Transpose Conv specific checks: |
| 139 | for op_type in SupportedOperators.transpose_convolution_ops: |
| 140 | self.specific_constraints[op_type].append(SupportedOperators.constraint_tconv_stride) |
| 141 | self.specific_constraints[op_type].append(SupportedOperators.constraint_tconv_same) |
| 142 | self.specific_constraints[op_type].append(SupportedOperators.constraint_tconv_valid) |
| 143 | |
| 144 | # Pooling checks: |
| 145 | for op_type in SupportedOperators.pooling_ops: |
| 146 | self.specific_constraints[op_type].append(SupportedOperators.constraint_batch_size) |
| 147 | self.specific_constraints[op_type].append(SupportedOperators.constraint_stride_type) |
| 148 | self.specific_constraints[op_type].append(SupportedOperators.constraint_stride_range) |
| 149 | # AVG pooling specific checks: |
| 150 | for op_type in SupportedOperators.avg_pooling_ops: |
| 151 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_in_out_types) |
| 152 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_type) |
| 153 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_range) |
| 154 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_height_range_valid_pad) |
| 155 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_product_range_valid_pad) |
| 156 | # MAX pooling specific checks: |
| 157 | for op_type in SupportedOperators.max_pooling_ops: |
| 158 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_in_out_types) |
| 159 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_type) |
| 160 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_height_range) |
| 161 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_product_range) |
| 162 | # TODO: Check ReduceSum restrictions |
| 163 | |
| 164 | # Relu specific checks: |
| 165 | for op_type in SupportedOperators.relu_ops: |
| 166 | self.specific_constraints[op_type].append(SupportedOperators.constraint_quant_scale_inf) |
| 167 | |
| 168 | # Resizing specific checks: |
| 169 | for op_type in SupportedOperators.resizing_ops: |
| 170 | self.specific_constraints[op_type].append(SupportedOperators.constraint_resize) |
| 171 | |
| 172 | # Vector Product specific checks: |
| 173 | for op_type in SupportedOperators.fc_vector_products: |
| 174 | self.specific_constraints[op_type].append(SupportedOperators.constraint_weights_type) |
| 175 | self.specific_constraints[op_type].append(SupportedOperators.constraint_weights_const) |
| 176 | self.specific_constraints[op_type].append(SupportedOperators.constraint_bias_type) |
| 177 | self.specific_constraints[op_type].append(SupportedOperators.constraint_bias_40bit) |
| 178 | |
| 179 | # Concat specific checks: |
| 180 | for op_type in (Op.Concat, Op.ConcatTFLite): |
| 181 | self.specific_constraints[op_type].append(SupportedOperators.constraint_axis_exists) |
| 182 | self.specific_constraints[op_type].append(SupportedOperators.constraint_axis_valid) |
| 183 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_dimensionality) |
| 184 | self.specific_constraints[op_type].append(SupportedOperators.constraint_valid_dimensions) |
| 185 | |
| 186 | # Element-wise checks: |
| 187 | for op_type in SupportedOperators.elem_wise_main_ops: |
| 188 | self.specific_constraints[op_type].append(SupportedOperators.constraint_elemwise_batch_size) |
| 189 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_either_shapes) |
| 190 | # Unary specific checks: |
| 191 | for op_type in SupportedOperators.unary_elem_wise_main_ops: |
| 192 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_in_out_types) |
| 193 | # Binary Min/Max specific checks: |
| 194 | for op_type in SupportedOperators.binary_elem_wise_min_max_ops: |
| 195 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_in_out_types) |
| 196 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_quantization_parameters) |
| 197 | # Binary Add/Mul/Sub specific checks: |
| 198 | for op_type in SupportedOperators.binary_elem_wise_add_mul_sub: |
| 199 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_inputs_types) |
| 200 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_signed) |
| 201 | self.specific_constraints[op_type].append(SupportedOperators.constraint_unsigned_valid) |
| 202 | # Binary Shift specific checks: |
| 203 | for op_type in SupportedOperators.binary_elem_wise_shift_ops: |
| 204 | self.specific_constraints[op_type].append(SupportedOperators.constraint_inputs_int32) |
| 205 | |
| 206 | # SHL specific checks: |
| 207 | self.specific_constraints[Op.SHL].append(SupportedOperators.constraint_output_int32) |
| 208 | |
| 209 | # CLZ specific checks: |
| 210 | self.specific_constraints[Op.CLZ].append(SupportedOperators.constraint_output_int32) |
| 211 | |
| 212 | # Softmax specific checks: |
| 213 | self.specific_constraints[Op.Softmax].append(SupportedOperators.constraint_matching_shapes) |
| 214 | self.specific_constraints[Op.Softmax].append(SupportedOperators.constraint_matching_in_out_types) |
| 215 | |
| 216 | # SplitV specific checks: |
| 217 | self.specific_constraints[Op.SplitV].append(SupportedOperators.constraint_splitv_inferred) |
| 218 | |
| 219 | # StridedSlice specific checks: |
| 220 | self.specific_constraints[Op.StridedSlice].append(SupportedOperators.constraint_stridedslice_input_count) |
| 221 | self.specific_constraints[Op.StridedSlice].append(SupportedOperators.constraint_stridedslice_inputs_const) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 222 | self.specific_constraints[Op.StridedSlice].append(SupportedOperators.constraint_stridedslice_stride_values) |
| 223 | self.specific_constraints[Op.StridedSlice].append(SupportedOperators.constraint_ellipsis_mask) |
| 224 | self.specific_constraints[Op.StridedSlice].append(SupportedOperators.constraint_axis_masks) |
| 225 | self.specific_constraints[Op.StridedSlice].append(SupportedOperators.constraint_slice_ranges) |
| 226 | |
| 227 | # LeakyRelu specific checks: |
| 228 | self.specific_constraints[Op.LeakyRelu].append(SupportedOperators.constraint_alpha_valid) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 229 | |
| 230 | def is_operator_supported(self, op): |
Michael McGeagh | 219ec07 | 2020-11-09 11:11:26 +0000 | [diff] [blame] | 231 | ext_type = optype_to_builtintype(op.type) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 232 | if op.type not in SupportedOperators.supported_operators: |
Louis Verhaard | 5f2ea2f | 2020-10-15 08:39:44 +0200 | [diff] [blame] | 233 | if op.type not in (Op.Placeholder, Op.SubgraphInput, Op.Const): |
Michael McGeagh | 219ec07 | 2020-11-09 11:11:26 +0000 | [diff] [blame] | 234 | print(f"Info: {ext_type} '{op.name}' is a CPU only op") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 235 | return False |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 236 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 237 | for constraint in self.generic_constraints + self.specific_constraints[op.type]: |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 238 | valid, extra = constraint(op) |
| 239 | if not valid: |
Michael McGeagh | 219ec07 | 2020-11-09 11:11:26 +0000 | [diff] [blame] | 240 | print(f"Warning: {ext_type} '{op.name}' is not supported on the NPU. Placing on CPU instead") |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 241 | print(f" - {constraint.__doc__}") |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 242 | if extra: |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 243 | print(f" {extra}") |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 244 | return False |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 245 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 246 | return True |
| 247 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 248 | @staticmethod |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 249 | def constraint_tens_no_dynamic(op): |
| 250 | "Input(s) and Output tensors must not be dynamic" |
| 251 | valid = True |
| 252 | extra = [] |
| 253 | tensors = [tens for tens in op.inputs + op.outputs if tens] |
| 254 | for tens in tensors: |
| 255 | if (tens.shape == []) and (tens.values is None): |
| 256 | valid = False |
| 257 | extra.append(tens.name) |
| 258 | extra = ", ".join(extra) |
| 259 | return valid, f"Op has dynamic tensor(s): {extra}" |
| 260 | |
| 261 | @staticmethod |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 262 | def constraint_tens_defined_shape(op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 263 | "Input(s) and Output tensors must have a defined shape" |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 264 | valid = True |
| 265 | extra = [] |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 266 | tensors = [tens for tens in op.inputs + op.outputs if tens] |
| 267 | for tens in tensors: |
| 268 | if not tens.has_fully_defined_shape(): |
| 269 | valid = False |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 270 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 271 | return valid, ", ".join(extra) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 272 | |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 273 | @staticmethod |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 274 | def constraint_tens_output_scalar(op): |
| 275 | "Output tensors cannot be scalar" |
| 276 | ofm = op.ofm |
| 277 | valid = ofm.shape != [] |
| 278 | return valid, f"Output Tensor '{ofm.name}' is scalar" |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 279 | |
| 280 | @classmethod |
| 281 | @docstring_format_args([shapeless_input_ops]) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 282 | def constraint_tens_input_scalar(cls, op): |
| 283 | "Scalar Input tensors are only valid for op type: {}" |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 284 | valid = True |
| 285 | extra = [] |
| 286 | tensors = [tens for tens in op.inputs if tens] |
| 287 | for tens in tensors: |
| 288 | if (tens.shape == []) and (op.type not in cls.shapeless_input_ops): |
| 289 | valid = False |
| 290 | extra.append(tens.name) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 291 | extra = ", ".join(extra) |
| 292 | return valid, f"Op has scalar input tensor(s): {extra}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 293 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 294 | @staticmethod |
| 295 | def constraint_tens_shape_size(op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 296 | "Input(s) and Output tensors must not be greater than 4D" |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 297 | valid = True |
| 298 | extra = [] |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 299 | tensors = [tens for tens in op.inputs + op.outputs if tens] |
| 300 | for tens in tensors: |
| 301 | if len(tens.shape) > 4: |
| 302 | valid = False |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 303 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 304 | return valid, ", ".join(extra) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 305 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 306 | @classmethod |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 307 | @docstring_format_args([supported_op_dtypes]) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 308 | def constraint_tens_dtype(cls, op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 309 | "Tensors must be of type: {}" |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 310 | valid = True |
| 311 | extra = [] |
| 312 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 313 | if not tensors: |
| 314 | tensors = [tens for tens in op.inputs if tens] |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 315 | for tens in tensors: |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 316 | if tens.dtype not in cls.supported_op_dtypes: |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 317 | valid = False |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 318 | extra.append(f"Tensor '{tens.name}' has data type: {tens.dtype}") |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 319 | return valid, ", ".join(extra) |
| 320 | |
| 321 | @classmethod |
| 322 | @docstring_format_args([supported_int32_tensor_ops]) |
| 323 | def constraint_tens_int32_ops(cls, op): |
| 324 | "Tensors which are int32 are only valid when op type is: {}" |
| 325 | valid = True |
| 326 | extra = [] |
| 327 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 328 | if not tensors: |
| 329 | tensors = [tens for tens in op.inputs if tens] |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 330 | for tens in tensors: |
| 331 | if (tens.dtype == DataType.int32) and (op.type not in cls.supported_int32_tensor_ops): |
| 332 | valid = False |
| 333 | extra.append(tens.name) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 334 | extra = ", ".join(extra) |
| 335 | return valid, f"Op has int32 tensor(s): {extra}" |
Andreas Nevalainen | eadb166 | 2020-09-01 15:36:26 +0200 | [diff] [blame] | 336 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 337 | @classmethod |
| 338 | @docstring_format_args(tens_dim_range) |
| 339 | def constraint_tens_dimension(cls, op): |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 340 | "Tensor dimensions must be in the range [{}, {}]" |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 341 | tens_min, tens_max = cls.tens_dim_range |
| 342 | valid = True |
| 343 | extra = [] |
| 344 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 345 | if not tensors: |
| 346 | tensors = [tens for tens in op.inputs if tens] |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 347 | for tens in tensors: |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 348 | if not all(tens_min <= dim <= tens_max for dim in tens.shape): |
| 349 | valid = False |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 350 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 351 | return valid, ", ".join(extra) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 352 | |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 353 | @staticmethod |
| 354 | def constraint_tens_quant_none_check(op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 355 | "Input(s), Output and Weight tensors must have quantization parameters" |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 356 | valid = True |
| 357 | extra = [] |
| 358 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 359 | for tens in tensors: |
| 360 | if tens.quantization is None: |
| 361 | valid = False |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 362 | extra.append(tens.name) |
| 363 | extra = ", ".join(extra) |
| 364 | return valid, f"Op has tensors with missing quantization parameters: {extra}" |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 365 | |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 366 | @staticmethod |
| 367 | def constraint_tens_quant_scale(op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 368 | "Input(s), Output and Weight tensors with quantization scales must be finite" |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 369 | valid = True |
| 370 | extra = [] |
| 371 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 372 | for tens in tensors: |
| 373 | if (tens.quantization.scale_f32 is not None) and np.isinf(tens.quantization.scale_f32).any(): |
| 374 | valid = False |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 375 | extra.append(f"Tensor '{tens.name}' has quantization scale: {tens.quantization.scale_f32}") |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 376 | return valid, ", ".join(extra) |
| 377 | |
| 378 | @classmethod |
| 379 | @docstring_format_args([supported_fused_activations]) |
| 380 | def constraint_faf(cls, op): |
| 381 | "The fused activation function (if present) must be one of type: {}" |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame^] | 382 | if op.activation is None: |
| 383 | res = True, "Op has no fused activation function" |
| 384 | else: |
| 385 | faf = op.activation.op_type |
| 386 | valid = faf in cls.supported_fused_activations |
| 387 | res = valid, f"Op has its fused activation function as: {faf}" |
| 388 | return res |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 389 | |
| 390 | @staticmethod |
| 391 | def constraint_stride_type(op): |
| 392 | "Stride values for both width and height must be integer types" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 393 | w, h = op.get_kernel_stride() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 394 | valid = is_integer(w) and is_integer(h) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 395 | return valid, f"Op has stride WxH as: {repr(w)}x{repr(h)}" |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 396 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 397 | @classmethod |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 398 | @docstring_format_args(stride_range) |
| 399 | def constraint_stride_range(cls, op): |
| 400 | "Stride values for both width and height must be in the range [{}, {}]" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 401 | w, h = op.get_kernel_stride() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 402 | stride_min, stride_max = cls.stride_range |
| 403 | valid = (stride_min <= w <= stride_max) and (stride_min <= h <= stride_max) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 404 | return valid, f"Op has stride WxH as: {w}x{h}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 405 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 406 | @staticmethod |
| 407 | def constraint_dilation_type(op): |
| 408 | "Dilation factor values for both width and height must be integer types" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 409 | w, h = op.get_kernel_dilation() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 410 | valid = is_integer(w) and is_integer(h) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 411 | return valid, f"Op has dilation factor WxH as: {repr(w)}x{repr(h)}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 412 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 413 | @classmethod |
| 414 | @docstring_format_args(dilation_range) |
| 415 | def constraint_dilation_range(cls, op): |
| 416 | "Dilation factor values for both width and height must be in the range [{}, {}]" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 417 | w, h = op.get_kernel_dilation() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 418 | dilation_min, dilation_max = cls.dilation_range |
| 419 | valid = (dilation_min <= w <= dilation_max) and (dilation_min <= h <= dilation_max) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 420 | return valid, f"Op has dilation factor WxH as: {w}x{h}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 421 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 422 | @classmethod |
| 423 | @docstring_format_args(dilated_height_range) |
| 424 | def constraint_dilated_height_range(cls, op): |
| 425 | "Dilated kernel height must be in the range [{}, {}]" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 426 | h = op.kernel.area_height() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 427 | dilated_height_min, dilated_height_max = cls.dilated_height_range |
| 428 | valid = dilated_height_min <= h <= dilated_height_max |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 429 | return valid, f"Op has dilated kernel height as: {h}" |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 430 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 431 | @classmethod |
| 432 | @docstring_format_args(dilated_product_range) |
| 433 | def constraint_dilated_product_range(cls, op): |
| 434 | "Product of dilated kernel width and height must be in the range [{}, {}]" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 435 | product = op.kernel.area_width() * op.kernel.area_height() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 436 | dilated_product_min, dilated_product_max = cls.dilated_product_range |
| 437 | valid = dilated_product_min <= product <= dilated_product_max |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 438 | return valid, f"Op has product of dilated kernel width and height as: {product}" |
Andreas Nevalainen | f0c59bf | 2020-08-26 10:56:23 +0200 | [diff] [blame] | 439 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 440 | @staticmethod |
| 441 | def constraint_weights_type(op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 442 | "Weight tensor must be 8-bit" |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 443 | weights = op.weights |
| 444 | valid = weights.element_size() == 1 |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 445 | return valid, f"Tensor '{weights.name}' is {int(weights.element_size() * 8)}-bit" |
Andreas Nevalainen | f0c59bf | 2020-08-26 10:56:23 +0200 | [diff] [blame] | 446 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 447 | @staticmethod |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 448 | def constraint_weights_const(op): |
| 449 | "Weight tensor must be constant" |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 450 | weights = op.weights |
| 451 | valid = weights.values is not None |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 452 | return valid, f"Tensor '{weights.name}' has non-constant values" |
Andreas Nevalainen | 8854dc9 | 2020-09-24 13:43:00 +0200 | [diff] [blame] | 453 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 454 | @classmethod |
| 455 | @docstring_format_args([weights_limit]) |
| 456 | def constraint_weights_limit(cls, op): |
| 457 | "The sum of the weights cannot exceed {}" |
| 458 | weights = op.weights |
| 459 | values = weights.quant_values.astype(np.int64) - weights.quantization.zero_point |
| 460 | limit = np.amax(np.sum(np.absolute(values), axis=(0, 1, 2))) |
| 461 | valid = limit <= cls.weights_limit |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 462 | return valid, f"Tensor '{weights.name}' has the sum of weights: {limit}" |
Andreas Nevalainen | f0c59bf | 2020-08-26 10:56:23 +0200 | [diff] [blame] | 463 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 464 | @classmethod |
| 465 | @docstring_format_args([supported_bias_dtypes]) |
| 466 | def constraint_bias_type(cls, op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 467 | "Optional Bias tensor must be of type: {}" |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 468 | bias = op.bias |
| 469 | if bias: |
| 470 | valid = bias.dtype in cls.supported_bias_dtypes |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 471 | return valid, f"Tensor '{bias.name}' has data type: {bias.dtype}" |
| 472 | return True, "Op has no bias tensor" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 473 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 474 | @staticmethod |
| 475 | def constraint_bias_40bit(op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 476 | "Optional Bias tensor values must fit within 40-bits" |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 477 | bias = op.bias |
| 478 | if bias and bias.dtype == DataType.int64: |
| 479 | valid = all(len(bin(quant_value)[2:]) <= 40 for quant_value in bias.quant_values) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 480 | return valid, f"Tensor '{bias.name}' has values larger than 40-bits" |
| 481 | return True, "Op has no bias tensor, or it fits in 40-bit" |
Andreas Nevalainen | d8c032d | 2020-09-11 10:25:09 +0200 | [diff] [blame] | 482 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 483 | @staticmethod |
| 484 | def constraint_batch_size(op): |
| 485 | "IFM Tensor batch size must be 1" |
| 486 | ifm = op.ifm |
| 487 | valid = ifm.shape[0] == 1 |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 488 | return valid, f"Tensor '{ifm.name}' has batch size: {ifm.shape[0]}" |
| 489 | |
| 490 | @staticmethod |
| 491 | def constraint_quant_scale_inf(op): |
| 492 | "The IFM quantization scale divided by the OFM quantization scale must not be infinite" |
| 493 | ifm_scale = op.ifm.quantization.scale_f32 |
| 494 | ofm_scale = op.ofm.quantization.scale_f32 |
| 495 | valid = not np.isinf(ifm_scale / ofm_scale) |
| 496 | return valid, f"Op has infinite quantization scale. ifm_scale={ifm_scale} ofm_scale={ofm_scale}" |
| 497 | |
| 498 | @staticmethod |
| 499 | def constraint_depth_multiplier(op): |
| 500 | "For depth multipliers > 1, IFM channels must be 1 and OFM channels must be equal to the depth multiplier" |
| 501 | depth_multiplier = op.attrs.get("depth_multiplier", 1) |
| 502 | if depth_multiplier > 1: |
| 503 | ifm_channels = op.ifm.shape[3] |
| 504 | ofm_channels = op.ofm.shape[3] |
| 505 | valid = (ifm_channels == 1) and (ofm_channels == depth_multiplier) |
| 506 | extra = ( |
| 507 | f"Op has ifm_channels={ifm_channels}, ofm_channels={ofm_channels}" |
| 508 | f" and depth_multiplier={depth_multiplier}" |
| 509 | ) |
| 510 | return valid, extra |
| 511 | return True, "Op has depth_multiplier=1" |
| 512 | |
| 513 | @staticmethod |
| 514 | def constraint_tconv_stride(op): |
| 515 | "Stride values for both width and height must be 2" |
| 516 | w = op.kernel.stride.x |
| 517 | h = op.kernel.stride.y |
| 518 | valid = (w == 2) and (h == 2) |
| 519 | return valid, f"Op has stride WxH as: {w}x{h}" |
| 520 | |
| 521 | @staticmethod |
| 522 | def constraint_tconv_same(op): |
| 523 | "SAME padding: OFM dimensions must equal IFM dimensions multiplied by stride" |
| 524 | if op.attrs["padding"] == b"SAME": |
| 525 | w = op.kernel.stride.x |
| 526 | h = op.kernel.stride.y |
| 527 | ifm_shape = op.ifm.shape |
| 528 | ofm_shape = op.ofm.shape |
| 529 | valid = (ofm_shape[1] == (ifm_shape[1] * h)) and (ofm_shape[2] == (ifm_shape[2] * w)) |
| 530 | return valid, f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape} and stride WxH as {w}x{h}" |
| 531 | return True, "Op has padding=VALID" |
| 532 | |
| 533 | @staticmethod |
| 534 | def constraint_tconv_valid(op): |
| 535 | """VALID padding: OFM dimensions must equal IFM dimensions multiplied by stride, |
| 536 | minus difference between kernel size and stride""" |
| 537 | if op.attrs["padding"] == b"VALID": |
| 538 | s_w = op.kernel.stride.x |
| 539 | s_h = op.kernel.stride.y |
| 540 | k_w = op.kernel.width |
| 541 | k_h = op.kernel.height |
| 542 | ifm_shape = op.ifm.shape |
| 543 | ofm_shape = op.ofm.shape |
| 544 | height_check = ofm_shape[1] == (ifm_shape[1] * s_h + max(k_h - s_h, 0)) |
| 545 | width_check = ofm_shape[2] == (ifm_shape[2] * s_w + max(k_w - s_w, 0)) |
| 546 | valid = height_check and width_check |
| 547 | extra = ( |
| 548 | f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape}," |
| 549 | f" stride WxH as {s_w}x{s_h} and kernel WxH as {k_w}x{k_h}" |
| 550 | ) |
| 551 | return valid, extra |
| 552 | return True, "Op has padding=SAME" |
| 553 | |
| 554 | @staticmethod |
| 555 | def constraint_matching_in_out_types(op): |
| 556 | "IFM and OFM data types must match" |
| 557 | ifm_dtype = op.ifm.dtype |
| 558 | ofm_dtype = op.ofm.dtype |
| 559 | valid = ifm_dtype == ofm_dtype |
| 560 | return valid, f"Op has ifm_dtype={ifm_dtype} and ofm_dtype={ofm_dtype}" |
| 561 | |
| 562 | @staticmethod |
| 563 | def constraint_filter_type(op): |
| 564 | "Kernel filter values for both width and height must be integer types" |
| 565 | w = op.kernel.width |
| 566 | h = op.kernel.height |
| 567 | valid = is_integer(w) and is_integer(h) |
| 568 | return valid, f"Op has kernel filter WxH as: {repr(w)}x{repr(h)}" |
| 569 | |
| 570 | @classmethod |
| 571 | @docstring_format_args(filter_range) |
| 572 | def constraint_filter_range(cls, op): |
| 573 | "Kernel filter values for both width and height must be in the range [{}, {}]" |
| 574 | if op.attrs["padding"] == b"SAME": |
| 575 | w = op.kernel.width |
| 576 | h = op.kernel.height |
| 577 | filter_min, filter_max = cls.filter_range |
| 578 | valid = (filter_min <= w <= filter_max) and (filter_min <= h <= filter_max) |
| 579 | return valid, f"Op has kernel filter WxH as: {w}x{h}" |
| 580 | return True, "Op has padding=VALID" |
| 581 | |
| 582 | @classmethod |
| 583 | @docstring_format_args(filter_height_range) |
| 584 | def constraint_filter_height_range(cls, op): |
| 585 | "Kernel filter height must be in the range [{}, {}]" |
| 586 | h = op.kernel.height |
| 587 | filter_height_min, filter_height_max = cls.filter_height_range |
| 588 | valid = filter_height_min <= h <= filter_height_max |
| 589 | return valid, f"Op has kernel filter height as: {h}" |
| 590 | |
| 591 | @classmethod |
| 592 | @docstring_format_args(filter_product_range) |
| 593 | def constraint_filter_product_range(cls, op): |
| 594 | "Product of kernel filter width and height must be in the range [{}, {}]" |
| 595 | product = op.kernel.elements_wh() |
| 596 | filter_product_min, filter_product_max = cls.filter_product_range |
| 597 | valid = filter_product_min <= product <= filter_product_max |
| 598 | return valid, f"Op has product of kernel filter width and height as: {product}" |
| 599 | |
| 600 | @staticmethod |
| 601 | @docstring_format_args(filter_height_range) |
| 602 | def constraint_filter_height_range_valid_pad(op): |
| 603 | "VALID padding: Kernel filter height must be in the range [{}, {}]" |
| 604 | if op.attrs["padding"] == b"VALID": |
| 605 | return SupportedOperators.constraint_filter_height_range(op) |
| 606 | return True, "Op has padding=SAME" |
| 607 | |
| 608 | @staticmethod |
| 609 | @docstring_format_args(filter_product_range) |
| 610 | def constraint_filter_product_range_valid_pad(op): |
| 611 | "VALID padding: Product of kernel filter width and height must be in the range [{}, {}]" |
| 612 | if op.attrs["padding"] == b"VALID": |
| 613 | return SupportedOperators.constraint_filter_product_range(op) |
| 614 | return True, "Op has padding=SAME" |
| 615 | |
| 616 | @staticmethod |
| 617 | def constraint_resize(op): |
| 618 | """The width and height of the IFM and OFM must match one of the following criteria: |
| 619 | IFM W and H must both be 1 |
| 620 | IFM must match OFM |
| 621 | OFM W and H must be 2x IFM -1, if align_corners is True |
| 622 | OFM W and H must be 2x IFM, if align_corners is False""" |
| 623 | # Easier to start with False condition as very few cases result in a supported resize |
| 624 | valid = False |
| 625 | ifm_shape = op.ifm.shape |
| 626 | ofm_shape = op.ofm.shape |
| 627 | align_corners = op.attrs.get("align_corners", False) |
| 628 | if len(ifm_shape) == 4: |
| 629 | # Valid if IFM W and H are both 1, or IFM and OFM shape are the same |
| 630 | if ((ifm_shape[1] == 1) and (ifm_shape[2] == 1)) or (ifm_shape == ofm_shape): |
| 631 | valid = True |
| 632 | else: |
| 633 | upscaled_shape = np.array(ifm_shape[1:3]) |
| 634 | out_shape = np.array(ofm_shape[1:3]) |
| 635 | while (upscaled_shape < out_shape).all(): |
| 636 | upscaled_shape *= 2 |
| 637 | if align_corners: |
| 638 | upscaled_shape -= 1 |
| 639 | # Valid if OFM is 2x IFM (-1 for align corners) |
| 640 | if np.array_equal(out_shape, upscaled_shape): |
| 641 | valid = True |
| 642 | break |
| 643 | return valid, f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape} and align_corners={align_corners}" |
| 644 | |
| 645 | @staticmethod |
| 646 | def constraint_matching_shapes(op): |
| 647 | "IFM and OFM shapes must match" |
| 648 | ifm_shape = op.ifm.shape |
| 649 | ofm_shape = op.ofm.shape |
| 650 | valid = ifm_shape == ofm_shape |
| 651 | return valid, f"Op has ifm_shape={ifm_shape} and ofm_shape={ofm_shape}" |
| 652 | |
| 653 | @staticmethod |
| 654 | def constraint_splitv_inferred(op): |
| 655 | "Only one size is allowed to be inferred" |
| 656 | sizes = op.ifm2.values |
| 657 | valid = np.count_nonzero(sizes == -1) <= 1 |
| 658 | return valid, f"Op has multiple inferred sizes (-1): {sizes}" |
| 659 | |
| 660 | @staticmethod |
| 661 | def constraint_axis_exists(op): |
| 662 | "Axis attribute must exist" |
| 663 | axis = op.attrs.get("axis") |
| 664 | valid = axis is not None |
| 665 | return valid, f"Op has axis={axis}" |
| 666 | |
| 667 | @staticmethod |
| 668 | def constraint_axis_valid(op): |
| 669 | "Axis attribute must be in the range [0, <ofm_dimensions>)" |
| 670 | dims = len(op.ofm.shape) |
| 671 | axis = op.attrs["axis"] |
| 672 | axis += dims if axis < 0 else 0 |
| 673 | valid = 0 <= axis < dims |
| 674 | return valid, f"Op has ofm_dimensions={dims} and axis attribute is: {axis}" |
| 675 | |
| 676 | @staticmethod |
| 677 | def constraint_matching_dimensionality(op): |
| 678 | "All Input dimensionalities must match OFM dimensionality" |
| 679 | valid = True |
| 680 | extra = [] |
| 681 | ofm_dim = len(op.ofm.shape) |
| 682 | tensors = [tens for tens in op.inputs if tens] |
| 683 | for tens in tensors: |
| 684 | dim = len(tens.shape) |
| 685 | if dim != ofm_dim: |
| 686 | valid = False |
| 687 | extra.append(f"Tensor '{tens.name}' has dimension: {dim}") |
| 688 | extra = ", ".join(extra) |
| 689 | return valid, f"Op has ofm_dimension={ofm_dim} and the list of mismatching inputs are: {extra}" |
| 690 | |
| 691 | @staticmethod |
| 692 | def constraint_valid_dimensions(op): |
| 693 | "All Input dimensions must match OFM dimension in all axes except the one defined by the axis attribute" |
| 694 | valid = True |
| 695 | extra = [] |
| 696 | ofm_shape = op.ofm.shape |
| 697 | ofm_dim = len(ofm_shape) |
| 698 | axis = op.attrs["axis"] |
| 699 | axis += ofm_dim if axis < 0 else 0 |
| 700 | tensors = [tens for tens in op.inputs if tens] |
| 701 | for tens in tensors: |
| 702 | if any(tens.shape[dim] != ofm_shape[dim] for dim in range(ofm_dim) if dim != axis): |
| 703 | valid = False |
| 704 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
| 705 | extra = ", ".join(extra) |
| 706 | return valid, f"Op has axis={axis}, ofm_shape={ofm_shape} and the list of mismatching inputs are: {extra}" |
| 707 | |
| 708 | @staticmethod |
| 709 | def constraint_stridedslice_input_count(op): |
| 710 | "Exactly 4 Input tensors are required" |
| 711 | inputs = len(op.inputs) |
| 712 | valid = inputs == 4 |
| 713 | return valid, f"Op has {inputs} inputs" |
| 714 | |
| 715 | @staticmethod |
| 716 | def constraint_stridedslice_inputs_const(op): |
| 717 | "Begin, End and Stride Input tensors must be constant" |
| 718 | valid = True |
| 719 | extra = [] |
| 720 | _, begin, end, strides = op.inputs |
| 721 | if begin.values is None: |
| 722 | valid = False |
| 723 | extra.append(f"Begin tensor '{begin.name}'") |
| 724 | if end.values is None: |
| 725 | valid = False |
| 726 | extra.append(f"End tensor '{end.name}'") |
| 727 | if strides.values is None: |
| 728 | valid = False |
| 729 | extra.append(f"Stride tensor '{strides.name}'") |
| 730 | extra = ", ".join(extra) |
| 731 | return valid, f"Op has non-constant tensors: {extra}" |
| 732 | |
| 733 | @staticmethod |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 734 | def constraint_stridedslice_stride_values(op): |
| 735 | "All Strides values must be 1" |
| 736 | strides = op.inputs[3] |
| 737 | valid = all(stride == 1 for stride in strides.values) |
| 738 | return valid, f"Op has strides values {strides.values}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 739 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 740 | @staticmethod |
| 741 | def constraint_ellipsis_mask(op): |
| 742 | "ellipsis_mask must be 0" |
| 743 | ellipsis = op.attrs["ellipsis_mask"] |
| 744 | valid = ellipsis == 0 |
| 745 | return valid, f"Op has ellipsis mask as: {ellipsis}" |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 746 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 747 | @staticmethod |
| 748 | def constraint_axis_masks(op): |
| 749 | "new_axis_mask and shrink_axis_mask cannot both be set" |
| 750 | new_axis = op.attrs["new_axis_mask"] |
| 751 | shrink_axis = op.attrs["shrink_axis_mask"] |
| 752 | valid = (new_axis == 0) or (shrink_axis == 0) |
| 753 | return valid, f"Op has new_axis_mask={new_axis} and shrink_axis_mask={shrink_axis}" |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 754 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 755 | @staticmethod |
| 756 | def constraint_slice_ranges(op): |
| 757 | "Slice 'end' values must be greater than 'begin' values" |
| 758 | ifm, begin, end, _ = op.inputs |
| 759 | # Calculate offset begin/end |
| 760 | offset_begin = get_slice_offsets(ifm.shape, begin, op.attrs["begin_mask"], is_begin=True) |
| 761 | offset_end = get_slice_offsets(ifm.shape, end, op.attrs["end_mask"], is_begin=False) |
| 762 | # Check "end - begin" doesn't result in any zero or negative elements |
| 763 | valid = all((e - b) > 0 for b, e in zip(offset_begin, offset_end)) |
| 764 | return valid, f"Op has begin_values={begin.values} and end_values={end.values}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 765 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 766 | @staticmethod |
| 767 | def constraint_matching_inputs_types(op): |
| 768 | "Both Input data types must match" |
| 769 | ifm_dtype = op.ifm.dtype |
| 770 | ifm2_dtype = op.ifm2.dtype |
| 771 | valid = ifm_dtype == ifm2_dtype |
| 772 | return valid, f"Op has ifm_dtype={ifm_dtype} and ifm2_dtype={ifm2_dtype}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 773 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 774 | @staticmethod |
| 775 | def constraint_matching_signed(op): |
| 776 | "For IFM that are signed, OFM must also be signed" |
| 777 | valid = True |
| 778 | ifm_dtype = op.ifm.dtype |
| 779 | ofm_dtype = op.ofm.dtype |
| 780 | if ifm_dtype.type & BaseType.Signed: |
| 781 | valid = bool(ofm_dtype.type & BaseType.Signed) |
| 782 | return valid, f"Op has ifm_dtype={ifm_dtype} and ofm_dtype={ofm_dtype}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 783 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 784 | @staticmethod |
| 785 | def constraint_unsigned_valid(op): |
| 786 | "For IFM that are unsigned, OFM must either be the same type or int32" |
| 787 | valid = True |
| 788 | ifm_dtype = op.ifm.dtype |
| 789 | ofm_dtype = op.ofm.dtype |
| 790 | if ifm_dtype.type & BaseType.Unsigned: |
| 791 | valid = (ifm_dtype == ofm_dtype) or (ofm_dtype == DataType.int32) |
| 792 | return valid, f"Op has ifm_dtype={ifm_dtype} and ofm_dtype={ofm_dtype}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 793 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 794 | @staticmethod |
| 795 | def constraint_inputs_int32(op): |
| 796 | "Both Input data types must be int32" |
| 797 | ifm_dtype = op.ifm.dtype |
| 798 | ifm2_dtype = op.ifm2.dtype |
| 799 | valid = (ifm_dtype == DataType.int32) and (ifm2_dtype == DataType.int32) |
| 800 | return valid, f"Op has ifm_dtype={ifm_dtype} and ifm2_dtype={ifm2_dtype}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 801 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 802 | @staticmethod |
| 803 | def constraint_output_int32(op): |
| 804 | "OFM must be int32" |
| 805 | ofm_dtype = op.ofm.dtype |
| 806 | valid = ofm_dtype == DataType.int32 |
| 807 | return valid, f"Op has ofm_dtype={ofm_dtype}" |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 808 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 809 | @staticmethod |
| 810 | def constraint_matching_quantization_parameters(op): |
| 811 | "Both Input quantization parameters must match OFM quantization parameters" |
| 812 | valid = True |
| 813 | extra = [] |
| 814 | if not check_quantized_tens_scaling_equal(op.ofm, op.ifm): |
| 815 | valid = False |
| 816 | extra.append(op.ifm.name) |
| 817 | if not check_quantized_tens_scaling_equal(op.ofm, op.ifm2): |
| 818 | valid = False |
| 819 | extra.append(op.ifm2.name) |
| 820 | extra = ", ".join(extra) |
| 821 | return valid, f"Op has tensors with different quantization parameters to the OFM '{op.ofm.name}': {extra}" |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 822 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 823 | @staticmethod |
| 824 | def constraint_elemwise_batch_size(op): |
| 825 | "Batch size must be 1 for Input tensors with more than 2 dimensions" |
| 826 | valid = True |
| 827 | extra = [] |
| 828 | for tens in (op.ifm, op.ifm2): |
| 829 | # Unary ops have ifm2 as None |
| 830 | if tens is not None: |
| 831 | if (len(tens.shape) > 2) and (tens.shape[0] != 1): |
| 832 | valid = False |
| 833 | extra.append(tens.name) |
| 834 | extra = ", ".join(extra) |
| 835 | return valid, f"Op has invalid input tensors: {extra}" |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 836 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 837 | @staticmethod |
| 838 | def constraint_matching_either_shapes(op): |
| 839 | "At least one Input's shape must match the OFM's shape" |
| 840 | ifm_shape = op.ifm.shape |
| 841 | ifm2_shape = op.ifm2.shape if op.ifm2 else None |
| 842 | ofm_shape = op.ofm.shape |
| 843 | valid = (ifm_shape == ofm_shape) or (ifm2_shape == ofm_shape) |
| 844 | return valid, f"Op has ifm_shape={ifm_shape}, ifm2_shape={ifm2_shape} and ofm_shape={ofm_shape}" |
Andreas Nevalainen | d8c032d | 2020-09-11 10:25:09 +0200 | [diff] [blame] | 845 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 846 | @staticmethod |
| 847 | def constraint_alpha_valid(op): |
| 848 | "Alpha must not be negative" |
| 849 | alpha = op.attrs["alpha"] |
| 850 | valid = alpha >= 0 |
| 851 | return valid, f"Op has alpha={alpha}" |