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