blob: 95c7de33d712212154e7681723927241fcb92b64 [file] [log] [blame]
Raul Farkas090f18a2023-01-24 16:29:06 +00001# SPDX-FileCopyrightText: Copyright 2020-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Jonas Ohlsson45e653d2021-07-26 16:13:12 +02002#
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.
Rickard Bolinbc6ee582022-11-04 08:24:29 +000016#
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020017# Description:
18# The TFLiteSupportedOperators class which is a collection of all TFLite supported operators and parameter checks.
19from collections import defaultdict
20
21import numpy as np
22
23from .data_type import DataType
Fredrik Svedberg88d5b122022-09-16 16:24:55 +020024from .numeric_util import full_shape
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020025from .operation import Op
26from .operation import Padding
27from .supported_operators_util import docstring_format_args
28from .supported_operators_util import list_formatter
29from .tensor import check_quantized_tens_scaling_equal
30from .tflite_mapping import BUILTIN_OPERATOR_UNKNOWN
31from .tflite_mapping import optype_to_builtintype
32
33
34def _optype_formatter(op_list):
35 # Convert internal op types to external names
36 output = map(optype_to_builtintype, op_list)
37 # Remove UNKNOWNs
38 output = (x for x in output if x is not BUILTIN_OPERATOR_UNKNOWN)
39 return list_formatter(output)
40
41
42class TFLiteSupportedOperators:
43 # Categorised lists of supported operators
Fredrik Svedberg11563172022-07-06 14:54:12 +020044 npu_pre_ops = set(
45 (
46 Op.SplitSliceRead,
47 Op.Shape,
48 )
49 )
Jonas Ohlssond8575072022-03-30 10:30:25 +020050 convolution_ops = set(
51 (
52 Op.Conv2DBias,
53 Op.Conv2D,
54 Op.QuantizedConv2D,
55 )
56 )
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020057 depthwise_convolution_ops = set((Op.DepthwiseConv2DBias,))
58 transpose_convolution_ops = set((Op.Conv2DBackpropInput,))
59 convolution_like_ops = convolution_ops | depthwise_convolution_ops | transpose_convolution_ops
60 max_pooling_ops = Op.op_set(Op.is_maxpool_op)
61 avg_pooling_ops = Op.op_set(Op.is_avgpool_op)
62 pooling_ops = set((Op.ReduceSum,)) | max_pooling_ops | avg_pooling_ops
Tim Hall885033b2022-07-21 11:46:03 +010063 resizing_ops = Op.op_set(Op.is_resize_op)
Jonas Ohlssond8575072022-03-30 10:30:25 +020064 fc_vector_products = set(
65 (
66 Op.QuantizedMatMul,
67 Op.MatMul,
68 Op.FullyConnected,
69 )
70 )
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020071 mac_main_ops = (
Fredrik Svedberg0ac08042023-04-11 22:35:04 +020072 # LSTM
73 set((Op.UnidirectionalSequenceLstm,))
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020074 # conv/depthwiseconv/transposeconv
75 | convolution_like_ops
76 # pooling
77 | pooling_ops
78 # resizing/upscaling
79 | resizing_ops
80 # FC layers
81 | fc_vector_products
82 # Mean (converts to depthwise conv)
83 | set((Op.Mean,))
Rickard Bolin6986a072022-12-19 12:33:40 +000084 # ArgMax (converts to depthwise conv and maxpool)
85 | set((Op.ArgMax,))
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020086 )
87 unary_elem_wise_main_ops = Op.op_set(Op.is_unary_elementwise_op)
Jonas Ohlssond8575072022-03-30 10:30:25 +020088 binary_elem_wise_min_max_ops = set(
89 (
90 Op.Minimum,
91 Op.Maximum,
92 )
93 )
94 binary_elem_wise_shift_ops = set(
95 (
96 Op.SHL,
97 Op.SHR,
98 )
99 )
100 binary_elem_wise_add_mul_sub = set(
101 (
102 Op.Add,
103 Op.Mul,
104 Op.Sub,
105 )
106 )
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200107 binary_elem_wise_main_ops = binary_elem_wise_min_max_ops | binary_elem_wise_add_mul_sub | binary_elem_wise_shift_ops
108 elem_wise_main_ops = binary_elem_wise_main_ops | unary_elem_wise_main_ops
109 pad_ops = set((Op.Pad,))
110 supported_int32_tensor_ops = (
Rickard Bolin6986a072022-12-19 12:33:40 +0000111 set((Op.ReduceSum, Op.CLZ, Op.Shape, Op.ArgMax)) | binary_elem_wise_add_mul_sub | binary_elem_wise_shift_ops
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200112 )
113
Jonas Ohlssond8575072022-03-30 10:30:25 +0200114 relu_ops = set(
115 (
116 Op.Relu,
117 Op.Relu6,
118 Op.ReluN1To1,
119 Op.Clip,
120 )
121 )
Fredrik Svedberg8ddd4892022-08-19 16:06:04 +0200122 activation_ops = relu_ops | set(
123 (
124 Op.Tanh,
125 Op.Sigmoid,
126 Op.Softmax,
127 Op.HardSwish,
Fredrik Svedberg1cd39492022-09-23 15:38:03 +0200128 Op.LeakyRelu,
Fredrik Svedberg8ddd4892022-08-19 16:06:04 +0200129 Op.Prelu,
130 )
131 )
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200132 npu_post_ops = (
133 # activation functions
134 activation_ops
135 # concatenation write direction
136 | set((Op.ConcatSliceWrite,))
137 # Quantization
138 | set((Op.Quantize,))
139 )
Jonas Ohlssond8575072022-03-30 10:30:25 +0200140 split_ops = set(
141 (
142 Op.Split,
143 Op.SplitV,
144 Op.StridedSlice,
145 Op.Slice,
146 Op.UnpackReshaped,
147 Op.Unpack,
148 )
149 )
150 concat_ops = set(
151 (
152 Op.Concat,
153 Op.ConcatTFLite,
154 Op.PackReshaped,
155 Op.Pack,
156 )
157 )
158 memory_only_ops = (
159 set(
160 (
161 Op.Reshape,
162 Op.QuantizedReshape,
163 Op.Squeeze,
164 Op.ExpandDims,
165 )
166 )
167 | concat_ops
168 | split_ops
169 )
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200170 per_axis_quant_ops = convolution_like_ops # per-axis/channel quantization only currently supported for conv ops
Jonas Ohlssond8575072022-03-30 10:30:25 +0200171 supported_fused_activations = relu_ops | set(
172 (
173 Op.Tanh,
174 Op.Sigmoid,
175 Op.LUT,
176 )
177 )
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200178 supported_operators = npu_pre_ops | mac_main_ops | elem_wise_main_ops | pad_ops | npu_post_ops | memory_only_ops
179 # Supported data types
180 supported_op_dtypes = set((DataType.uint8, DataType.int8, DataType.int16, DataType.int32))
181 supported_faf_dtypes = set((DataType.uint8, DataType.int8, DataType.int16))
182 supported_bias_dtypes = set((DataType.int32, DataType.int64))
183 supported_pad_dtypes = set((DataType.int32, DataType.int64))
184 # Defined ranges for allowed values:
185 tens_dim_range = (1, 65535)
186 stride_range = (1, 3)
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200187 dilated_height_range = (1, 64)
188 dilated_product_range = (1, 64 * 64)
189 weights_limit = 127 * 65536
190 filter_range = (1, 8)
191 filter_height_range = (1, 256)
192 filter_product_range = (1, 256 * 256)
193 mean_kernel_product = 64 * 64
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200194
195 def __init__(self):
196 # Setup the generic constraints. Note: the order matters
197 self.generic_constraints = []
198 self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_dtype)
199 self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_int32_ops)
200 self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_dimension)
201 self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_quant_per_axis)
Fredrik Svedberg88d5b122022-09-16 16:24:55 +0200202 self.generic_constraints.append(TFLiteSupportedOperators.constraint_batch_size)
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200203 self.generic_constraints.append(TFLiteSupportedOperators.constraint_faf)
204 self.generic_constraints.append(TFLiteSupportedOperators.constraint_faf_type)
205
Fredrik Svedberg88d5b122022-09-16 16:24:55 +0200206 # Setup generic constraint exceptions
207 self.generic_constraints_exceptions = defaultdict(list)
Johan Alfvenc1ad80b2023-03-31 10:19:23 +0200208 self.generic_constraints_exceptions[Op.ArgMax].append(TFLiteSupportedOperators.constraint_tens_dtype)
Fredrik Svedberg88d5b122022-09-16 16:24:55 +0200209 self.generic_constraints_exceptions[Op.FullyConnected].append(TFLiteSupportedOperators.constraint_batch_size)
210 self.generic_constraints_exceptions[Op.Softmax].append(TFLiteSupportedOperators.constraint_batch_size)
211 self.generic_constraints_exceptions[Op.Reshape].append(TFLiteSupportedOperators.constraint_batch_size)
212 self.generic_constraints_exceptions[Op.Shape].append(TFLiteSupportedOperators.constraint_batch_size)
213 self.generic_constraints_exceptions[Op.Squeeze].append(TFLiteSupportedOperators.constraint_batch_size)
214 for op_type in TFLiteSupportedOperators.split_ops - set((Op.UnpackReshaped,)):
215 self.generic_constraints_exceptions[op_type].append(TFLiteSupportedOperators.constraint_batch_size)
216
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200217 # Setup specific constraints. Note: the order matters
218 self.specific_constraints = defaultdict(list)
219
Raul Farkas090f18a2023-01-24 16:29:06 +0000220 # Conv specific ops:
221 for op_type in TFLiteSupportedOperators.convolution_ops:
222 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_conv_stride)
223
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200224 # Conv-like checks:
225 for op_type in TFLiteSupportedOperators.convolution_like_ops:
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200226 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_dilated_height_range)
227 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_dilated_product_range)
228 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_type)
229 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_const)
230 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_limit)
Johan Alfvénfaa4b782022-12-07 13:56:17 +0100231 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_shape)
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200232 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_type)
233 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_40bit)
Raul Farkas090f18a2023-01-24 16:29:06 +0000234
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200235 # Transpose Conv specific checks:
236 for op_type in TFLiteSupportedOperators.transpose_convolution_ops:
237 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_tconv_stride)
238 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_tconv_same)
239 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_tconv_valid)
Tim Halld3d81b32022-10-18 19:14:04 +0100240 # Depthwise Conv specific checks:
241 for op_type in TFLiteSupportedOperators.depthwise_convolution_ops:
Raul Farkas090f18a2023-01-24 16:29:06 +0000242 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_depthwise_conv_stride)
Tim Halld3d81b32022-10-18 19:14:04 +0100243 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_depth_multiplier)
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200244
245 # Pooling checks:
246 for op_type in TFLiteSupportedOperators.pooling_ops:
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200247 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_stride_range)
248 # AVG pooling specific checks:
249 for op_type in TFLiteSupportedOperators.avg_pooling_ops:
250 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_range)
251 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_height_range_valid_pad)
252 self.specific_constraints[op_type].append(
253 TFLiteSupportedOperators.constraint_filter_product_range_valid_pad
254 )
255 # MAX pooling specific checks:
256 for op_type in TFLiteSupportedOperators.max_pooling_ops:
257 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_height_range)
258 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_product_range)
259
260 # Resizing specific checks:
261 for op_type in TFLiteSupportedOperators.resizing_ops:
Tim Hall885033b2022-07-21 11:46:03 +0100262 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_resize)
263 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_resize_size)
264 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_resize_attrs)
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200265
Rickard Bolinfea15162022-07-04 16:19:16 +0000266 # Resize Bilinear specific checks:
267 self.specific_constraints[Op.ResizeBilinear].append(
268 TFLiteSupportedOperators.constraint_resizebi_half_pixel_centers_dims
269 )
270
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200271 # Vector Product specific checks:
272 for op_type in TFLiteSupportedOperators.fc_vector_products:
273 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_type)
274 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_const)
Johan Alfvénfaa4b782022-12-07 13:56:17 +0100275 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_shape)
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200276 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_type)
277 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_40bit)
278
Fredrik Svedberg88d5b122022-09-16 16:24:55 +0200279 # Element-wise checks
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200280 # Binary Min/Max specific checks:
281 for op_type in TFLiteSupportedOperators.binary_elem_wise_min_max_ops:
282 self.specific_constraints[op_type].append(
283 TFLiteSupportedOperators.constraint_matching_quantization_parameters
284 )
285 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_broadcast_shapes)
286 # Binary Add/Mul/Sub specific checks:
287 for op_type in TFLiteSupportedOperators.binary_elem_wise_add_mul_sub:
288 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_broadcast_shapes)
289 # Binary Shift specific checks:
290 for op_type in TFLiteSupportedOperators.binary_elem_wise_shift_ops:
291 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_inputs_int32)
292 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_broadcast_shapes)
293
294 # SHL specific checks:
295 self.specific_constraints[Op.SHL].append(TFLiteSupportedOperators.constraint_output_int32)
296
297 # CLZ specific checks:
298 self.specific_constraints[Op.CLZ].append(TFLiteSupportedOperators.constraint_output_int32)
299
300 # StridedSlice specific checks:
301 self.specific_constraints[Op.StridedSlice].append(
302 TFLiteSupportedOperators.constraint_stridedslice_stride_values
303 )
304
305 # Pad specific checks:
306 self.specific_constraints[Op.Pad].append(TFLiteSupportedOperators.constraint_pad_shape)
307 self.specific_constraints[Op.Pad].append(TFLiteSupportedOperators.constraint_padding_dimensions)
308 self.specific_constraints[Op.Pad].append(TFLiteSupportedOperators.constraint_pad_type)
309
310 # Mean specific checks:
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200311 self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_mean_height_width_product)
James Peet0bb7ad12022-02-15 15:07:54 +0000312 self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_mean_height_single_axis)
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200313
Tim Hall3584a9c2021-11-18 22:05:17 +0000314 # Reshape specific checks:
315 self.specific_constraints[Op.Reshape].append(TFLiteSupportedOperators.constraint_reshape_shape_constant)
316
Rickard Bolin6986a072022-12-19 12:33:40 +0000317 # ArgMax specific checks:
Rickard Bolin6986a072022-12-19 12:33:40 +0000318 self.specific_constraints[Op.ArgMax].append(TFLiteSupportedOperators.constraint_argmax_axis)
319 self.specific_constraints[Op.ArgMax].append(TFLiteSupportedOperators.constraint_argmax_depth)
320
Fredrik Svedberg0ac08042023-04-11 22:35:04 +0200321 # UnidirectionalSequenceLstm specific checks:
322 op_type = Op.UnidirectionalSequenceLstm
323 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_lstm_no_cifg)
324 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_lstm_no_peep_hole)
325 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_lstm_no_projection)
326 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_lstm_no_normalisation)
327 self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_lstm_weights)
328
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200329 def is_operator_supported(self, op):
330 ext_type = optype_to_builtintype(op.type)
331 if op.type not in TFLiteSupportedOperators.supported_operators:
332 if op.type not in (Op.Placeholder, Op.SubgraphInput, Op.Const):
333 print(f"Info: {ext_type} '{op.name}' is a CPU only op")
334 return False
335
Fredrik Svedberg88d5b122022-09-16 16:24:55 +0200336 op_exceptions = self.generic_constraints_exceptions[op.type]
337 generic_constraints = [constraint for constraint in self.generic_constraints if constraint not in op_exceptions]
338
339 for constraint in generic_constraints + self.specific_constraints[op.type]:
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200340 valid, extra = constraint(op)
341 if not valid:
342 print(f"Warning: {ext_type} '{op.name}' is not supported on the NPU. Placing on CPU instead")
343 print(f" - {constraint.__doc__}")
344 if extra:
345 print(f" {extra}")
346 return False
347
348 return True
349
350 @classmethod
351 @docstring_format_args([list_formatter(supported_op_dtypes)])
352 def constraint_tens_dtype(cls, op):
353 "Tensors must be of type: {}"
354 valid = True
355 extra = []
356 tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens]
357 if not tensors:
358 tensors = [tens for tens in op.inputs if tens]
359 for tens in tensors:
360 if tens.dtype not in cls.supported_op_dtypes:
361 valid = False
362 extra.append(f"Tensor '{tens.name}' has data type: {tens.dtype}")
363 return valid, ", ".join(extra)
364
365 @classmethod
366 @docstring_format_args([_optype_formatter(supported_int32_tensor_ops)])
367 def constraint_tens_int32_ops(cls, op):
368 "Tensors which are int32 are only valid when op type is: {}"
369 valid = True
370 extra = []
371 tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens]
372 if not tensors:
373 tensors = [tens for tens in op.inputs if tens]
374 for tens in tensors:
375 if (tens.dtype == DataType.int32) and (op.type not in cls.supported_int32_tensor_ops):
376 valid = False
377 extra.append(tens.name)
378 extra = ", ".join(extra)
379 return valid, f"Op has int32 tensor(s): {extra}"
380
381 @classmethod
382 @docstring_format_args(tens_dim_range)
383 def constraint_tens_dimension(cls, op):
384 "Tensor dimensions must be in the range [{}, {}]"
385 tens_min, tens_max = cls.tens_dim_range
386 valid = True
387 extra = []
388 tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens]
389 if not tensors:
390 tensors = [tens for tens in op.inputs if tens]
391 for tens in tensors:
392 if not all(tens_min <= dim <= tens_max for dim in tens.shape):
393 valid = False
394 extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}")
395 return valid, ", ".join(extra)
396
397 @classmethod
398 @docstring_format_args([_optype_formatter(per_axis_quant_ops)])
399 def constraint_tens_quant_per_axis(cls, op):
400 "Per-axis quantization is only supported for the following op types: {}"
401 valid = True
402 extra = []
403 if op.type not in cls.per_axis_quant_ops:
404 tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens]
405 for tens in tensors:
Fredrik Svedberg11563172022-07-06 14:54:12 +0200406 if tens.quantization and tens.quantization.is_per_axis():
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200407 valid = False
408 extra.append(tens.name)
409 return valid, "The following tensor(s) have per-axis quantization parameters: " + ", ".join(extra)
410
411 @classmethod
412 @docstring_format_args([_optype_formatter(supported_fused_activations)])
413 def constraint_faf(cls, op):
414 "The fused activation function (if present) must be one of type: {}"
415 if op.activation is None:
416 res = True, "Op has no fused activation function"
417 else:
418 faf = op.activation.op_type
419 valid = faf in cls.supported_fused_activations
420 res = valid, f"Op has its fused activation function as: {faf}"
421 return res
422
423 @classmethod
424 @docstring_format_args([list_formatter(supported_faf_dtypes)])
425 def constraint_faf_type(cls, op):
426 "If a fused activation function is present, the Output tensor must be one of type: {}"
427 if op.activation is None:
428 res = True, "Op has no fused activation function"
429 else:
430 valid = op.ofm.dtype in cls.supported_faf_dtypes
431 ext_type = optype_to_builtintype(op.activation.op_type)
432 res = valid, f"Op has fused activation function {ext_type}, and Output tensor data type: {op.ofm.dtype}"
433 return res
434
435 @classmethod
436 @docstring_format_args(stride_range)
437 def constraint_stride_range(cls, op):
438 "Stride values for both width and height must be in the range [{}, {}]"
439 w, h = op.get_kernel_stride()
440 stride_min, stride_max = cls.stride_range
441 valid = (stride_min <= w <= stride_max) and (stride_min <= h <= stride_max)
442 return valid, f"Op has stride WxH as: {w}x{h}"
443
444 @classmethod
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200445 @docstring_format_args(dilated_height_range)
446 def constraint_dilated_height_range(cls, op):
447 "Dilated kernel height must be in the range [{}, {}]"
448 h = op.kernel.area_height()
449 dilated_height_min, dilated_height_max = cls.dilated_height_range
450 valid = dilated_height_min <= h <= dilated_height_max
451 return valid, f"Op has dilated kernel height as: {h}"
452
453 @classmethod
454 @docstring_format_args(dilated_product_range)
455 def constraint_dilated_product_range(cls, op):
456 "Product of dilated kernel width and height must be in the range [{}, {}]"
457 product = op.kernel.area_width() * op.kernel.area_height()
458 dilated_product_min, dilated_product_max = cls.dilated_product_range
459 valid = dilated_product_min <= product <= dilated_product_max
460 return valid, f"Op has product of dilated kernel width and height as: {product}"
461
462 @staticmethod
463 def constraint_weights_type(op):
464 "Weight tensor must be 8-bit"
465 weights = op.weights
466 valid = weights.element_size() == 1
467 return valid, f"Tensor '{weights.name}' is {int(weights.element_size() * 8)}-bit"
468
469 @staticmethod
470 def constraint_weights_const(op):
471 "Weight tensor must be constant"
472 weights = op.weights
473 valid = weights.values is not None
474 return valid, f"Tensor '{weights.name}' has non-constant values"
475
476 @classmethod
477 @docstring_format_args([weights_limit])
478 def constraint_weights_limit(cls, op):
479 "The sum of the weights cannot exceed {}"
480 weights = op.weights
481 values = weights.values.astype(np.int64) - weights.quantization.zero_point
482 limit = np.amax(np.sum(np.absolute(values), axis=(0, 1, 2)))
483 valid = limit <= cls.weights_limit
484 return valid, f"Tensor '{weights.name}' has the sum of weights: {limit}"
485
Johan Alfvénfaa4b782022-12-07 13:56:17 +0100486 @staticmethod
487 def constraint_bias_shape(op):
488 "Optional Bias tensor must be of shape: 1D"
489 bias = op.bias
490 if bias:
491 valid = len(bias.shape) == 1
492 return valid, f"Tensor '{bias.name}' has shape: {bias.shape}"
493 return True, "Op has no bias tensor"
494
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200495 @classmethod
496 @docstring_format_args([list_formatter(supported_bias_dtypes)])
497 def constraint_bias_type(cls, op):
498 "Optional Bias tensor must be of type: {}"
499 bias = op.bias
500 if bias:
501 valid = bias.dtype in cls.supported_bias_dtypes
502 return valid, f"Tensor '{bias.name}' has data type: {bias.dtype}"
503 return True, "Op has no bias tensor"
504
505 @staticmethod
506 def constraint_bias_40bit(op):
507 "Optional Bias tensor values must fit within 40-bits"
508 bias = op.bias
509 if bias and bias.dtype == DataType.int64 and bias.values is not None:
Tim Hall8ae29292021-07-28 16:52:03 +0100510 valid = all(len(bin(value)[2:]) <= 40 for value in bias.values)
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200511 return valid, f"Tensor '{bias.name}' has values larger than 40-bits"
512 return True, "Op has no bias tensor, or it fits in 40-bit"
513
514 @staticmethod
515 def constraint_batch_size(op):
516 "IFM Tensor batch size must be 1"
Fredrik Svedberg88d5b122022-09-16 16:24:55 +0200517 valid = True
518 extra = []
519 for tens in (op.ifm, op.ifm2):
520 if tens is not None:
521 batch_size = full_shape(4, tens.shape, 1)[0]
522 if batch_size != 1:
523 valid = False
524 extra.append(f"Tensor '{tens.name}' has batch size: {batch_size}")
525 extra = "\n ".join(extra)
526 return valid, extra
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200527
528 @staticmethod
529 def constraint_depth_multiplier(op):
530 "For depth multipliers > 1, IFM channels must be 1 and OFM channels must be equal to the depth multiplier"
531 depth_multiplier = op.attrs.get("depth_multiplier", 1)
532 if depth_multiplier > 1:
533 ifm_channels = op.ifm.shape[3]
534 ofm_channels = op.ofm.shape[3]
535 valid = (ifm_channels == 1) and (ofm_channels == depth_multiplier)
536 extra = (
537 f"Op has ifm_channels={ifm_channels}, ofm_channels={ofm_channels}"
538 f" and depth_multiplier={depth_multiplier}"
539 )
540 return valid, extra
541 return True, "Op has depth_multiplier=1"
542
543 @staticmethod
Raul Farkas090f18a2023-01-24 16:29:06 +0000544 def constraint_conv_stride(op):
Raul Farkas59b9ab92023-02-09 10:03:27 +0000545 "Stride values for height must be between 1 and 3 and for width between 1 and 4"
Raul Farkas090f18a2023-01-24 16:29:06 +0000546 w, h = op.get_kernel_stride()
Raul Farkas59b9ab92023-02-09 10:03:27 +0000547 stride_min_w_h = 1
548 stride_max_w = 4
549 stride_max_h = 3
550 valid = (stride_min_w_h <= w <= stride_max_w) and (stride_min_w_h <= h <= stride_max_h)
Raul Farkas090f18a2023-01-24 16:29:06 +0000551 return valid, f"Op has stride WxH as: {w}x{h}"
552
553 @staticmethod
554 def constraint_depthwise_conv_stride(op):
555 "Stride values for both width and height must be between 1 and 3"
556 w, h = op.get_kernel_stride()
557 stride_min, stride_max = 1, 3
558 valid = (stride_min <= w <= stride_max) and (stride_min <= h <= stride_max)
559 return valid, f"Op has stride WxH as: {w}x{h}"
560
561 @staticmethod
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200562 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"] == Padding.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,
Jonas Ohlssond8575072022-03-30 10:30:25 +0200584 minus difference between kernel size and stride"""
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200585 if op.attrs["padding"] == Padding.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 @classmethod
603 @docstring_format_args(filter_range)
604 def constraint_filter_range(cls, op):
605 "Kernel filter values for both width and height must be in the range [{}, {}]"
606 if op.attrs["padding"] == Padding.SAME:
607 w = op.kernel.width
608 h = op.kernel.height
609 filter_min, filter_max = cls.filter_range
610 valid = (filter_min <= w <= filter_max) and (filter_min <= h <= filter_max)
611 return valid, f"Op has kernel filter WxH as: {w}x{h}"
612 return True, "Op has padding=VALID"
613
614 @classmethod
615 @docstring_format_args(filter_height_range)
616 def constraint_filter_height_range(cls, op):
617 "Kernel filter height must be in the range [{}, {}]"
618 h = op.kernel.height
619 filter_height_min, filter_height_max = cls.filter_height_range
620 valid = filter_height_min <= h <= filter_height_max
621 return valid, f"Op has kernel filter height as: {h}"
622
623 @classmethod
624 @docstring_format_args(filter_product_range)
625 def constraint_filter_product_range(cls, op):
626 "Product of kernel filter width and height must be in the range [{}, {}]"
627 product = op.kernel.elements_wh()
628 filter_product_min, filter_product_max = cls.filter_product_range
629 valid = filter_product_min <= product <= filter_product_max
630 return valid, f"Op has product of kernel filter width and height as: {product}"
631
632 @staticmethod
633 @docstring_format_args(filter_height_range)
634 def constraint_filter_height_range_valid_pad(op):
635 "VALID padding: Kernel filter height must be in the range [{}, {}]"
636 if op.attrs["padding"] == Padding.VALID:
637 return TFLiteSupportedOperators.constraint_filter_height_range(op)
638 return True, "Op has padding=SAME"
639
640 @staticmethod
641 @docstring_format_args(filter_product_range)
642 def constraint_filter_product_range_valid_pad(op):
643 "VALID padding: Product of kernel filter width and height must be in the range [{}, {}]"
644 if op.attrs["padding"] == Padding.VALID:
645 return TFLiteSupportedOperators.constraint_filter_product_range(op)
646 return True, "Op has padding=SAME"
647
648 @staticmethod
Tim Hall885033b2022-07-21 11:46:03 +0100649 def constraint_resize(op):
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200650 """The width and height of the IFM and OFM must match one of the following criteria:
651 IFM W and H must both be 1
652 IFM must match OFM
Rickard Bolinfea15162022-07-04 16:19:16 +0000653 W and H scaling must be equal and OFM W-1 and H-1 must be 2x/4x/8x IFM W-1 and H-1, if align_corners is True
654 W and H scaling must be equal and OFM W and H must be 2x/4x/8x IFM W and H, if align_corners is False"""
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200655 # Easier to start with False condition as very few cases result in a supported resize
656 valid = False
657 ifm_shape = op.ifm.shape
Tim Hall47c76362022-07-18 21:26:47 +0100658 ifm_shape_h = ifm_shape[1]
659 ifm_shape_w = ifm_shape[2]
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200660 ofm_shape = op.ofm.shape
Tim Hall47c76362022-07-18 21:26:47 +0100661 ofm_shape_h = ofm_shape[1]
662 ofm_shape_w = ofm_shape[2]
663
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200664 align_corners = op.attrs.get("align_corners", False)
665 if len(ifm_shape) == 4:
666 # Valid if IFM W and H are both 1, or IFM and OFM shape are the same
Tim Hall47c76362022-07-18 21:26:47 +0100667 if ((ifm_shape_h == 1) and (ifm_shape_w == 1)) or (ifm_shape == ofm_shape):
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200668 valid = True
669 else:
Rickard Boline546def2022-01-25 15:45:00 +0000670 # Valid if OFM is 2/4/8x IFM (-1 for align corners)
Tim Hall47c76362022-07-18 21:26:47 +0100671 if align_corners:
672 h_upscale_factor = (ofm_shape_h - 1) / (ifm_shape_h - 1)
673 w_upscale_factor = (ofm_shape_w - 1) / (ifm_shape_w - 1)
674 else:
675 h_upscale_factor = ofm_shape_h / ifm_shape_h
676 w_upscale_factor = ofm_shape_w / ifm_shape_w
Rickard Boline546def2022-01-25 15:45:00 +0000677
Tim Hall47c76362022-07-18 21:26:47 +0100678 # could use either height or width. save as int because it is more usable later in graph optimiser
679 op.attrs["upscale_factor"] = int(h_upscale_factor)
680 valid = h_upscale_factor == w_upscale_factor and h_upscale_factor in (2.0, 4.0, 8.0)
Rickard Boline546def2022-01-25 15:45:00 +0000681
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200682 return valid, f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape} and align_corners={align_corners}"
683
684 @staticmethod
Tim Hall885033b2022-07-21 11:46:03 +0100685 def constraint_resize_size(op):
Tim Hall47c76362022-07-18 21:26:47 +0100686 "The size tensor must match the output tensor shape"
687 valid = False
688 ofm_shape = op.ofm.shape
689 size_h, size_w = None, None
690 # check that the size tensor (the second input) exists, is not none, and has the correct values
691 if len(op.inputs) == 2 and op.inputs[1] is not None and len(op.inputs[1].values) == 2:
692 size_h, size_w = op.inputs[1].values
693 # check size and output size match
694 if size_h == ofm_shape[1] and size_w == ofm_shape[2]:
695 valid = True
696
697 return valid, f"Op has size={size_h}x{size_w} and ofm_shape={ofm_shape}."
698
699 @staticmethod
Tim Hall885033b2022-07-21 11:46:03 +0100700 def constraint_resize_attrs(op):
Tim Hall47c76362022-07-18 21:26:47 +0100701 "Both align_corners and half_pixel_centers can't be True"
702 valid = True
703 align_corners = op.attrs.get("align_corners", False)
704 half_pixel_centers = op.attrs.get("half_pixel_centers", False)
705
706 if align_corners and half_pixel_centers:
707 valid = False
708 return valid, "Op has both align_corners and half_pixel_centers set to True."
709
710 @staticmethod
Rickard Bolinfea15162022-07-04 16:19:16 +0000711 def constraint_resizebi_half_pixel_centers_dims(op):
712 """Half_pixel_centers for resize bilinear requires that OFM W and H is 2x IFM W and H"""
713 half_pixel_centers = op.attrs.get("half_pixel_centers", False)
714 if not half_pixel_centers:
715 valid = True
716 elif len(op.ifm.shape) >= 3:
717 ifm_h, ifm_w = op.ifm.shape[-3:-1]
718 ofm_h, ofm_w = op.ofm.shape[-3:-1]
719 valid = ofm_h / ifm_h == 2 and ofm_w / ifm_w == 2
720 else:
721 # Unexpected IFM shape
722 valid = False
723 return (
724 valid,
725 f"Op has ifm_shape={op.ifm.shape}, ofm_shape={op.ofm.shape} and half_pixel_centers={half_pixel_centers}",
726 )
erik.andersson@arm.comba2555e2021-10-28 14:08:52 +0200727
728 @staticmethod
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200729 def constraint_pad_shape(op):
730 "The padding tensor must have the shape [3,2] or [4,2]"
731 valid = op.inputs[1].shape in ([3, 2], [4, 2])
732 return valid, f"The pad tensor has the shape: {op.inputs[1].shape}"
733
734 @classmethod
735 @docstring_format_args([list_formatter(supported_pad_dtypes)])
736 def constraint_pad_type(cls, op):
737 "Pad tensor must be of type: {}"
738 pad_tensor = op.inputs[1]
739 valid = pad_tensor.dtype in cls.supported_pad_dtypes
740 return valid, f"Tensor '{pad_tensor.name}' has data type: {pad_tensor.dtype}"
741
742 @staticmethod
743 def constraint_padding_dimensions(op):
744 "The pad tensor can only pad width and height"
745 pad_tensor = op.inputs[1].values
746
747 valid = sum(pad_tensor[-1, :]) == 0
748 if valid and len(pad_tensor) > 3:
749 valid = sum(pad_tensor[0, :]) == 0
750 return valid, f"First dimension padding: {pad_tensor[0,:]}, last dimension padding: {pad_tensor[-1,:]}"
751
752 @staticmethod
753 def constraint_stridedslice_stride_values(op):
754 "All Strides values must be 1"
755 strides = op.inputs[3]
756 valid = all(stride == 1 for stride in strides.values)
757 return valid, f"Op has strides values {strides.values}"
758
759 @staticmethod
760 def constraint_inputs_int32(op):
761 "Both Input data types must be int32"
762 ifm_dtype = op.ifm.dtype
763 ifm2_dtype = op.ifm2.dtype
764 valid = (ifm_dtype == DataType.int32) and (ifm2_dtype == DataType.int32)
765 return valid, f"Op has ifm_dtype={ifm_dtype} and ifm2_dtype={ifm2_dtype}"
766
767 @staticmethod
768 def constraint_output_int32(op):
769 "OFM must be int32"
770 ofm_dtype = op.ofm.dtype
771 valid = ofm_dtype == DataType.int32
772 return valid, f"Op has ofm_dtype={ofm_dtype}"
773
774 @staticmethod
775 def constraint_matching_quantization_parameters(op):
776 "Both Input quantization parameters must match OFM quantization parameters"
777 valid = True
778 extra = []
779 if not check_quantized_tens_scaling_equal(op.ofm, op.ifm):
780 valid = False
781 extra.append(op.ifm.name)
782 if op.ifm2 is not None and not check_quantized_tens_scaling_equal(op.ofm, op.ifm2):
783 valid = False
784 extra.append(op.ifm2.name)
785 extra = ", ".join(extra)
786 return valid, f"Op has tensors with different quantization parameters to the OFM '{op.ofm.name}': {extra}"
787
788 @staticmethod
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200789 def constraint_broadcast_shapes(op):
790 "Broadcasting is only allowed for rank indices with dimension 1, from either IFM1 or IFM2"
791 ifm_shape = op.ifm.shape
792 ifm2_shape = op.ifm2.shape if op.ifm2 else None
793 ofm_shape = op.ofm.shape
794 valid = True
795 if ifm_shape is not None and ifm2_shape is not None:
796 # align trailing dimensions
797 size = min(len(ifm_shape), len(ifm2_shape))
798 for i, i2, o in zip(ifm_shape[-size:], ifm2_shape[-size:], ofm_shape[-size:]):
799 mi = max(i, i2)
800 # Input dimensions should match or one should be of dimension 1
801 # Output dimension should match the largest input dimension, together
802 # with constraint_match_either_shapes ensures broadcast from only one input
803 if not (i == i2 or i == 1 or i2 == 1) or o != mi:
804 valid = False
805 break
806
807 return valid, f"Op has ifm_shape={ifm_shape} and ifm2_shape={ifm2_shape}"
808
809 @classmethod
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200810 @docstring_format_args([mean_kernel_product])
811 def constraint_mean_height_width_product(cls, op):
Johan Alfven7b3008a2023-04-13 18:54:47 +0200812 """Product of height and width must be no greater than {}"""
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200813 shape = op.inputs[0].shape
814 hi = 0 if len(shape) < 4 else 1
815 h, w = shape[hi : hi + 2]
816 max_prod = cls.mean_kernel_product
817 return h * w <= max_prod, f"Product of height and width is {h * w}"
818
Johan Alfvén05916632022-09-06 20:33:22 +0200819 @classmethod
Johan Alfven7b3008a2023-04-13 18:54:47 +0200820 @docstring_format_args([dilated_height_range[1]])
James Peet0bb7ad12022-02-15 15:07:54 +0000821 def constraint_mean_height_single_axis(cls, op):
822 """For single axis averages across the height dimension:
Johan Alfven7b3008a2023-04-13 18:54:47 +0200823 IFM height must be no greater than {}"""
Rickard Bolin7d7cb672021-12-07 09:09:14 +0000824 inp, axis = op.inputs
825 if axis.shape == [] or axis.shape[0] == 1: # single axis
826 axis = int(axis.values) if len(axis.shape) == 0 else int(axis.values[0])
827 else:
James Peet0bb7ad12022-02-15 15:07:54 +0000828 # Multiple axes
Rickard Bolin7d7cb672021-12-07 09:09:14 +0000829 return True, ""
830
Rickard Bolin7d7cb672021-12-07 09:09:14 +0000831 shape = inp.shape
James Peet0bb7ad12022-02-15 15:07:54 +0000832 if len(shape) < 3:
833 # No height dimension present in IFM
834 return True, ""
835 if axis != len(shape) - 3:
836 # Not averaging across the height dimension
837 return True, ""
Rickard Bolin7d7cb672021-12-07 09:09:14 +0000838
James Peet0bb7ad12022-02-15 15:07:54 +0000839 h = shape[axis]
Rickard Bolin7d7cb672021-12-07 09:09:14 +0000840 ifm, ofm = op.get_ifm_ofm()
James Peet0bb7ad12022-02-15 15:07:54 +0000841
Johan Alfven7b3008a2023-04-13 18:54:47 +0200842 return h <= cls.dilated_height_range[1], f"Height is {h}"
Rickard Bolin7d7cb672021-12-07 09:09:14 +0000843
Tim Hall3584a9c2021-11-18 22:05:17 +0000844 @staticmethod
845 def constraint_reshape_shape_constant(op):
846 "Shape must be constant"
847 valid = True
848 extra = []
849
Tim Hall2180a172023-03-10 18:11:34 +0000850 # if a reshape tensor is specified then it must be constant
851 if len(op.inputs) > 1:
852 reshape_tens = op.inputs[1]
853 if reshape_tens is not None:
854 # constant inputs have either no driving operator or a const one
855 # create a list of non-constant inputs
856 if not (len(reshape_tens.ops) == 0 or reshape_tens.ops[0].type == Op.Const):
857 valid = False
858 extra.append(reshape_tens.name)
Tim Hall3584a9c2021-11-18 22:05:17 +0000859 extra = ", ".join(extra)
860
861 return valid, f"Op has non-const input(s): {extra}"
Rickard Bolin6986a072022-12-19 12:33:40 +0000862
863 @staticmethod
864 def constraint_argmax_axis(op):
865 "Operation must be performed along the depth axis"
866 inp_dims = len(op.inputs[0].shape)
867 axis = op.inputs[1].values
868 return (
Johan Alfven56811e62023-03-27 11:33:50 +0200869 axis in (inp_dims - 1, -1),
Rickard Bolin6986a072022-12-19 12:33:40 +0000870 f"Axis is {axis} and number of input dimensions is {inp_dims}",
871 )
872
873 @staticmethod
Rickard Bolin6986a072022-12-19 12:33:40 +0000874 def constraint_argmax_depth(op):
875 "IFM depth must be no greater than 127"
876 ifm_depth = op.inputs[0].shape[-1]
877 return ifm_depth <= 127, f"IFM depth is {ifm_depth}"
Fredrik Svedberg0ac08042023-04-11 22:35:04 +0200878
879 @staticmethod
880 def constraint_lstm_no_cifg(op):
881 "Must not use CIFG"
882 cifg = None not in op.inputs[2:5] + op.inputs[6:9]
883 cifg = cifg and op.inputs[1] is None
884 cifg = cifg and op.inputs[5] is None
885 return not cifg, "Op uses CIFG"
886
887 @staticmethod
888 def constraint_lstm_no_peep_hole(op):
889 "Must not use Peephole"
890 valid = all([tens is None for tens in op.inputs[9:12]])
891 return valid, "Op uses peephole"
892
893 @staticmethod
894 def constraint_lstm_no_projection(op):
895 "Must not use Projection"
896 valid = all([tens is None for tens in op.inputs[16:18]])
897 return valid, "Op uses projection"
898
899 @staticmethod
900 def constraint_lstm_no_normalisation(op):
901 "Must not use Normalisation"
902 valid = all([tens is None for tens in op.inputs[20:24]])
903 return valid, "Op uses normalisation"
904
905 @staticmethod
906 def constraint_lstm_weights(op):
907 "All input and recurrent weights must be available"
908 valid = None not in op.inputs[1:9]
909 return valid, "Op has missing weights"