erik.andersson@arm.com | 460c689 | 2021-02-24 14:38:09 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved. |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 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 | # Utility functions for creating Network Operations. |
| 18 | from typing import Optional |
Fredrik Svedberg | e82be7c | 2021-01-18 15:21:03 +0100 | [diff] [blame] | 19 | from typing import Tuple |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 20 | |
| 21 | from .data_type import DataType |
| 22 | from .high_level_command_to_npu_op import ifm_ifm2_correct_order |
| 23 | from .operation import ActivationFunction |
| 24 | from .operation import Op |
| 25 | from .operation import Operation |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 26 | from .operation import Padding |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 27 | from .shape4d import Shape4D |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 28 | from .tensor import QuantizationParameters |
| 29 | from .tensor import Tensor |
| 30 | |
| 31 | |
| 32 | def create_avgpool_nop(name: str) -> Operation: |
| 33 | op = Operation(Op.AvgPool, name) |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 34 | op.attrs["padding"] = Padding.VALID |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 35 | op.attrs["stride_w"] = 1 |
| 36 | op.attrs["stride_h"] = 1 |
| 37 | op.attrs["filter_width"] = 1 |
| 38 | op.attrs["filter_height"] = 1 |
| 39 | op.attrs["strides"] = [1, 1, 1, 1] |
| 40 | op.attrs["ksize"] = [1, 1, 1, 1] |
| 41 | op.attrs["skirt"] = [0, 0, 0, 0] |
Patrik Gustavsson | c74682c | 2021-08-17 14:26:38 +0200 | [diff] [blame] | 42 | op.attrs["explicit_padding"] = [0, 0, 0, 0] # [top, left, bottom, right] |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 43 | op.run_on_npu = True |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 44 | return op |
| 45 | |
| 46 | |
Patrik Gustavsson | f1580f0 | 2021-09-01 12:43:02 +0200 | [diff] [blame] | 47 | def create_add_nop(name: str) -> Operation: |
| 48 | op = Operation(Op.Add, name) |
| 49 | op.run_on_npu = True |
| 50 | return op |
| 51 | |
| 52 | |
Patrik Gustavsson | b4936ad | 2021-10-05 13:53:34 +0200 | [diff] [blame] | 53 | def create_pad_nop(name: str) -> Operation: |
| 54 | op = Operation(Op.Pad, name) |
| 55 | op.run_on_npu = True |
| 56 | return op |
| 57 | |
| 58 | |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 59 | def create_depthwise_maxpool( |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 60 | name: str, |
| 61 | ifm: Tensor, |
| 62 | inp_shape: Shape4D, |
| 63 | quantization: QuantizationParameters, |
| 64 | activation: Optional[ActivationFunction] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 65 | ) -> Operation: |
| 66 | op = Operation(Op.MaxPool, name) |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 67 | height = inp_shape.height * inp_shape.width |
| 68 | width = inp_shape.depth |
| 69 | ifm_shape = Shape4D([1, height, width, 1]) |
| 70 | |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 71 | op.attrs["padding"] = Padding.VALID |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 72 | op.attrs["stride_w"] = 1 |
| 73 | op.attrs["stride_h"] = 1 |
| 74 | op.attrs["filter_width"] = width |
| 75 | op.attrs["filter_height"] = 1 |
| 76 | op.attrs["strides"] = [1, op.attrs["stride_h"], op.attrs["stride_w"], 1] |
| 77 | op.attrs["ksize"] = [1, op.attrs["filter_height"], op.attrs["filter_width"], 1] |
| 78 | op.activation = activation |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 79 | op.inputs = [ifm] |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 80 | ofm = Tensor([1, height, 1, 1], ifm.dtype, op.name + "_tens0") |
| 81 | ofm.quantization = quantization |
| 82 | op.set_output_tensor(ofm) |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 83 | op.ifm_shapes.append(ifm_shape) |
| 84 | op.ofm_shapes.append(Shape4D(ofm.shape)) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 85 | return op |
| 86 | |
| 87 | |
| 88 | def create_reduce_sum( |
| 89 | name: str, ifm: Tensor, quantization: QuantizationParameters, activation: Optional[ActivationFunction] = None |
| 90 | ) -> Operation: |
| 91 | op = Operation(Op.ReduceSum, name) |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 92 | op.attrs["padding"] = Padding.VALID |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 93 | op.attrs["stride_w"] = 1 |
| 94 | op.attrs["stride_h"] = 1 |
| 95 | op.attrs["filter_width"] = 1 |
| 96 | op.attrs["filter_height"] = 1 |
| 97 | op.attrs["strides"] = [1, op.attrs["stride_h"], op.attrs["stride_w"], 1] |
| 98 | op.attrs["ksize"] = [1, op.attrs["filter_height"], op.attrs["filter_width"], 1] |
| 99 | op.add_input_tensor(ifm) |
| 100 | op.activation = activation |
| 101 | ofm_shape = [1, ifm.shape[1], ifm.shape[2], 1] |
| 102 | sum_of_exp = Tensor(ofm_shape, DataType.int32, op.name + "_tens0") |
| 103 | sum_of_exp.quantization = quantization |
| 104 | op.set_output_tensor(sum_of_exp) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 105 | op.set_ifm_ofm_shapes() |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 106 | return op |
| 107 | |
| 108 | |
| 109 | def create_add( |
| 110 | name: str, |
| 111 | ifm: Tensor, |
| 112 | ifm2: Tensor, |
| 113 | quantization: QuantizationParameters, |
| 114 | activation: Optional[ActivationFunction] = None, |
| 115 | dtype: Optional[DataType] = None, |
| 116 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 117 | ifm_shape: Optional[Shape4D] = None, |
| 118 | ifm2_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 119 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 120 | return create_binary_elementwise( |
| 121 | Op.Add, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape |
| 122 | ) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 123 | |
| 124 | |
| 125 | def create_clz( |
| 126 | name: str, |
| 127 | ifm: Tensor, |
| 128 | quantization: QuantizationParameters, |
| 129 | activation: Optional[ActivationFunction] = None, |
| 130 | dtype: Optional[DataType] = None, |
| 131 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 132 | ifm_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 133 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 134 | return create_unary_elementwise(Op.CLZ, name, ifm, quantization, activation, dtype, attrs, ifm_shape) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 135 | |
| 136 | |
| 137 | def create_mul( |
| 138 | name: str, |
| 139 | ifm: Tensor, |
| 140 | ifm2: Tensor, |
| 141 | quantization: QuantizationParameters, |
| 142 | activation: Optional[ActivationFunction] = None, |
| 143 | dtype: Optional[DataType] = None, |
| 144 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 145 | ifm_shape: Optional[Shape4D] = None, |
| 146 | ifm2_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 147 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 148 | return create_binary_elementwise( |
| 149 | Op.Mul, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape |
| 150 | ) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 151 | |
| 152 | |
| 153 | def create_shl( |
| 154 | name: str, |
| 155 | ifm: Tensor, |
| 156 | ifm2: Tensor, |
| 157 | quantization: QuantizationParameters, |
| 158 | activation: Optional[ActivationFunction] = None, |
| 159 | dtype: Optional[DataType] = None, |
| 160 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 161 | ifm_shape: Optional[Shape4D] = None, |
| 162 | ifm2_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 163 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 164 | return create_binary_elementwise( |
| 165 | Op.SHL, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape |
| 166 | ) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 167 | |
| 168 | |
| 169 | def create_shr( |
| 170 | name: str, |
| 171 | ifm: Tensor, |
| 172 | ifm2: Tensor, |
| 173 | quantization: QuantizationParameters, |
| 174 | activation: Optional[ActivationFunction] = None, |
| 175 | dtype: Optional[DataType] = None, |
| 176 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 177 | ifm_shape: Optional[Shape4D] = None, |
| 178 | ifm2_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 179 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 180 | return create_binary_elementwise( |
| 181 | Op.SHR, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape |
| 182 | ) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 183 | |
| 184 | |
| 185 | def create_sub( |
| 186 | name: str, |
| 187 | ifm: Tensor, |
| 188 | ifm2: Tensor, |
| 189 | quantization: QuantizationParameters, |
| 190 | activation: Optional[ActivationFunction] = None, |
| 191 | dtype: Optional[DataType] = None, |
| 192 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 193 | ifm_shape: Optional[Shape4D] = None, |
| 194 | ifm2_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 195 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 196 | return create_binary_elementwise( |
| 197 | Op.Sub, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape |
| 198 | ) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 199 | |
| 200 | |
| 201 | def create_unary_elementwise( |
| 202 | op_type: Op, |
| 203 | name: str, |
| 204 | ifm: Tensor, |
| 205 | quantization: QuantizationParameters, |
| 206 | activation: Optional[ActivationFunction] = None, |
| 207 | dtype: Optional[DataType] = None, |
| 208 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 209 | ifm_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 210 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 211 | return create_binary_elementwise(op_type, name, ifm, None, quantization, activation, dtype, attrs, ifm_shape, None) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 212 | |
| 213 | |
| 214 | def create_binary_elementwise( |
| 215 | op_type: Op, |
| 216 | name: str, |
| 217 | ifm: Tensor, |
Jonas Ohlsson | 845e232 | 2022-03-01 12:39:55 +0100 | [diff] [blame] | 218 | ifm2: Optional[Tensor], |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 219 | quantization: QuantizationParameters, |
| 220 | activation: Optional[ActivationFunction] = None, |
| 221 | dtype: Optional[DataType] = None, |
| 222 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 223 | ifm_shape: Optional[Shape4D] = None, |
| 224 | ifm2_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 225 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 226 | if ifm_shape is None: |
| 227 | ifm_shape = Shape4D(ifm.shape) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 228 | op = Operation(op_type, name) |
| 229 | op.add_input_tensor(ifm) |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 230 | op.ifm_shapes.append(ifm_shape) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 231 | if ifm2: |
| 232 | op.add_input_tensor(ifm2) |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 233 | if ifm2_shape is None: |
| 234 | ifm2_shape = Shape4D(ifm2.shape) |
| 235 | op.ifm_shapes.append(ifm2_shape) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 236 | op.activation = activation |
| 237 | if not dtype: |
| 238 | dtype = ifm.dtype |
| 239 | if attrs: |
| 240 | op.attrs.update(attrs) |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 241 | |
| 242 | if ifm2 is None: |
| 243 | ofm_shape = ifm_shape |
| 244 | else: |
Johan Alfvén | 56a71b0 | 2022-10-19 11:20:12 +0200 | [diff] [blame^] | 245 | in_shape = None if ifm.shape == [] else ifm_shape |
| 246 | in2_shape = None if ifm2.shape == [] else ifm2_shape |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 247 | ofm_shape = ifm_shape if ifm_ifm2_correct_order(in_shape, in2_shape) else ifm2_shape |
| 248 | |
| 249 | ofm = Tensor(ofm_shape.as_list(), dtype, f"{op.name}_tens0") |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 250 | ofm.quantization = quantization |
| 251 | op.set_output_tensor(ofm) |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 252 | op.ofm_shapes.append(ofm_shape) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 253 | return op |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 254 | |
| 255 | |
| 256 | def get_pad_values_from_input(padding) -> Tuple: |
| 257 | """Returns top, left, bottom, right padding from input values in a Pad input tensor""" |
| 258 | return (padding[-3][0], padding[-2][0], padding[-3][1], padding[-2][1]) |