Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 1 | # Copyright (C) 2021 Arm Limited or its affiliates. All rights reserved. |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the License); you may |
| 6 | # not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # Description: |
| 17 | # The TosaSupportedOperators class which is a collection of all supported operators and parameter checks. |
| 18 | from collections import defaultdict |
| 19 | |
| 20 | from .data_type import DataType |
| 21 | from .operation import Op |
| 22 | from .supported_operators_util import docstring_format_args |
| 23 | from .supported_operators_util import list_formatter |
| 24 | from .tosa_mapping import optype_to_tosa_op_type |
| 25 | |
| 26 | |
| 27 | class TosaSupportedOperators: |
| 28 | # TODO currently sparsely populated |
| 29 | # Categorised lists of supported operators |
| 30 | convolution_ops = set((Op.Conv2DBias,)) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame^] | 31 | depthwise_convolution_ops = set((Op.DepthwiseConv2DBias,)) |
| 32 | convolution_like_ops = convolution_ops | depthwise_convolution_ops |
| 33 | |
| 34 | # TODO depending on what will be committed |
Patrik Gustavsson | c74682c | 2021-08-17 14:26:38 +0200 | [diff] [blame] | 35 | max_pooling_ops = Op.op_set(Op.is_maxpool_op) |
| 36 | avg_pooling_ops = Op.op_set(Op.is_avgpool_op) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame^] | 37 | pooling_ops = max_pooling_ops | avg_pooling_ops |
| 38 | fc_vector_products = set((Op.FullyConnected,)) |
Patrik Gustavsson | c74682c | 2021-08-17 14:26:38 +0200 | [diff] [blame] | 39 | |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame^] | 40 | mac_main_ops = convolution_like_ops | pooling_ops | fc_vector_products |
| 41 | memory_only_ops = set((Op.Reshape, Op.Transpose,)) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 42 | |
| 43 | type_conversion_ops = set((Op.Rescale,)) |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 44 | relu_ops = set((Op.Clamp, Op.ReluN,)) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 45 | activation_ops = relu_ops |
| 46 | |
| 47 | npu_post_ops = activation_ops |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame^] | 48 | supported_operators = mac_main_ops | type_conversion_ops | npu_post_ops | memory_only_ops |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 49 | |
| 50 | # Supported data types |
| 51 | # TODO will differ compared to TensorFlow Lite, currently set to the same |
| 52 | supported_op_dtypes = set((DataType.uint8, DataType.int8, DataType.int16, DataType.int32)) |
| 53 | |
| 54 | def __init__(self): |
| 55 | # Setup the generic constraints. Note: the order matters |
| 56 | self.generic_constraints = [] |
| 57 | self.generic_constraints.append(TosaSupportedOperators.constraint_tens_dtype) |
| 58 | |
| 59 | # Setup specific constraints. Note: the order matters |
| 60 | self.specific_constraints = defaultdict(list) |
| 61 | |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame^] | 62 | self.specific_constraints[Op.Transpose].append(TosaSupportedOperators.constraint_ifm_producer) |
| 63 | |
| 64 | # Depthwise Conv specific checks: |
| 65 | for op_type in TosaSupportedOperators.depthwise_convolution_ops: |
| 66 | self.specific_constraints[op_type].append(TosaSupportedOperators.constraint_depth_multiplier) |
| 67 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 68 | def is_operator_supported(self, op): |
| 69 | ext_type = optype_to_tosa_op_type(op.type) |
| 70 | if op.type not in TosaSupportedOperators.supported_operators: |
| 71 | if op.type not in (Op.Placeholder, Op.SubgraphInput, Op.Const): |
| 72 | print(f"Info: {ext_type} '{op.name}' is not a NPU op") |
| 73 | return False |
| 74 | |
| 75 | for constraint in self.generic_constraints + self.specific_constraints[op.type]: |
| 76 | valid, extra = constraint(op) |
| 77 | if not valid: |
| 78 | print(f"Warning: {ext_type} '{op.name}' is not supported on the NPU") |
| 79 | print(f" - {constraint.__doc__}") |
| 80 | if extra: |
| 81 | print(f" {extra}") |
| 82 | return False |
| 83 | |
| 84 | return True |
| 85 | |
| 86 | # TODO this function is the same for TensorFlow Lite, but input might differ |
| 87 | @classmethod |
| 88 | @docstring_format_args([list_formatter(supported_op_dtypes)]) |
| 89 | def constraint_tens_dtype(cls, op): |
| 90 | "Tensors must be of type: {}" |
| 91 | valid = True |
| 92 | extra = [] |
| 93 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 94 | if not tensors: |
| 95 | tensors = [tens for tens in op.inputs if tens] |
| 96 | for tens in tensors: |
| 97 | if tens.dtype not in cls.supported_op_dtypes: |
| 98 | valid = False |
| 99 | extra.append(f"Tensor '{tens.name}' has data type: {tens.dtype}") |
| 100 | return valid, ", ".join(extra) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame^] | 101 | |
| 102 | @staticmethod |
| 103 | def constraint_ifm_producer(cls, op): |
| 104 | "Input must be constant data" |
| 105 | valid = op.ifm.ops and op.ifm.ops[0].type == Op.Const |
| 106 | return valid, "Op has ifm with non-constant data" |
| 107 | |
| 108 | # TODO duplicates tflite_supported operators, but support for depth multiplier should be added at a later stage |
| 109 | @staticmethod |
| 110 | def constraint_depth_multiplier(op): |
| 111 | "For depth multipliers > 1, IFM channels must be 1 and OFM channels must be equal to the depth multiplier" |
| 112 | depth_multiplier = op.attrs.get("depth_multiplier", 1) |
| 113 | if depth_multiplier > 1: |
| 114 | ifm_channels = op.ifm.shape[3] |
| 115 | ofm_channels = op.ofm.shape[3] |
| 116 | valid = (ifm_channels == 1) and (ofm_channels == depth_multiplier) |
| 117 | extra = ( |
| 118 | f"Op has ifm_channels={ifm_channels}, ofm_channels={ofm_channels}" |
| 119 | f" and depth_multiplier={depth_multiplier}" |
| 120 | ) |
| 121 | return valid, extra |
| 122 | return True, "Op has depth_multiplier=1" |