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 | |
Fredrik Svedberg | e82be7c | 2021-01-18 15:21:03 +0100 | [diff] [blame] | 125 | def create_rescale_add( |
| 126 | name: str, |
| 127 | ifm: Tensor, |
| 128 | ifm2: Tensor, |
| 129 | rescale: Tuple[int, int], |
| 130 | quantization: QuantizationParameters, |
| 131 | activation: Optional[ActivationFunction] = None, |
| 132 | dtype: Optional[DataType] = None, |
| 133 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 134 | ifm_shape: Optional[Shape4D] = None, |
| 135 | ifm2_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | e82be7c | 2021-01-18 15:21:03 +0100 | [diff] [blame] | 136 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 137 | op = create_binary_elementwise( |
| 138 | Op.RescaleAdd, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape |
| 139 | ) |
Fredrik Svedberg | e82be7c | 2021-01-18 15:21:03 +0100 | [diff] [blame] | 140 | op.rescale = rescale |
| 141 | return op |
| 142 | |
| 143 | |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 144 | def create_clz( |
| 145 | name: str, |
| 146 | ifm: Tensor, |
| 147 | quantization: QuantizationParameters, |
| 148 | activation: Optional[ActivationFunction] = None, |
| 149 | dtype: Optional[DataType] = None, |
| 150 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 151 | ifm_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 152 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 153 | 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] | 154 | |
| 155 | |
| 156 | def create_mul( |
| 157 | name: str, |
| 158 | ifm: Tensor, |
| 159 | ifm2: Tensor, |
| 160 | quantization: QuantizationParameters, |
| 161 | activation: Optional[ActivationFunction] = None, |
| 162 | dtype: Optional[DataType] = None, |
| 163 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 164 | ifm_shape: Optional[Shape4D] = None, |
| 165 | ifm2_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 166 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 167 | return create_binary_elementwise( |
| 168 | Op.Mul, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape |
| 169 | ) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 170 | |
| 171 | |
| 172 | def create_shl( |
| 173 | name: str, |
| 174 | ifm: Tensor, |
| 175 | ifm2: Tensor, |
| 176 | quantization: QuantizationParameters, |
| 177 | activation: Optional[ActivationFunction] = None, |
| 178 | dtype: Optional[DataType] = None, |
| 179 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 180 | ifm_shape: Optional[Shape4D] = None, |
| 181 | ifm2_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 182 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 183 | return create_binary_elementwise( |
| 184 | Op.SHL, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape |
| 185 | ) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 186 | |
| 187 | |
| 188 | def create_shr( |
| 189 | name: str, |
| 190 | ifm: Tensor, |
| 191 | ifm2: Tensor, |
| 192 | quantization: QuantizationParameters, |
| 193 | activation: Optional[ActivationFunction] = None, |
| 194 | dtype: Optional[DataType] = None, |
| 195 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 196 | ifm_shape: Optional[Shape4D] = None, |
| 197 | ifm2_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 198 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 199 | return create_binary_elementwise( |
| 200 | Op.SHR, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape |
| 201 | ) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 202 | |
| 203 | |
| 204 | def create_sub( |
| 205 | name: str, |
| 206 | ifm: Tensor, |
| 207 | ifm2: Tensor, |
| 208 | quantization: QuantizationParameters, |
| 209 | activation: Optional[ActivationFunction] = None, |
| 210 | dtype: Optional[DataType] = None, |
| 211 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 212 | ifm_shape: Optional[Shape4D] = None, |
| 213 | ifm2_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 214 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 215 | return create_binary_elementwise( |
| 216 | Op.Sub, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape |
| 217 | ) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 218 | |
| 219 | |
| 220 | def create_unary_elementwise( |
| 221 | op_type: Op, |
| 222 | name: str, |
| 223 | ifm: Tensor, |
| 224 | quantization: QuantizationParameters, |
| 225 | activation: Optional[ActivationFunction] = None, |
| 226 | dtype: Optional[DataType] = None, |
| 227 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 228 | ifm_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 229 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 230 | 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] | 231 | |
| 232 | |
| 233 | def create_binary_elementwise( |
| 234 | op_type: Op, |
| 235 | name: str, |
| 236 | ifm: Tensor, |
| 237 | ifm2: Tensor, |
| 238 | quantization: QuantizationParameters, |
| 239 | activation: Optional[ActivationFunction] = None, |
| 240 | dtype: Optional[DataType] = None, |
| 241 | attrs: Optional[dict] = None, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 242 | ifm_shape: Optional[Shape4D] = None, |
| 243 | ifm2_shape: Optional[Shape4D] = None, |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 244 | ) -> Operation: |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 245 | if ifm_shape is None: |
| 246 | ifm_shape = Shape4D(ifm.shape) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 247 | op = Operation(op_type, name) |
| 248 | op.add_input_tensor(ifm) |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 249 | op.ifm_shapes.append(ifm_shape) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 250 | if ifm2: |
| 251 | op.add_input_tensor(ifm2) |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 252 | if ifm2_shape is None: |
| 253 | ifm2_shape = Shape4D(ifm2.shape) |
| 254 | op.ifm_shapes.append(ifm2_shape) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 255 | op.activation = activation |
| 256 | if not dtype: |
| 257 | dtype = ifm.dtype |
| 258 | if attrs: |
| 259 | op.attrs.update(attrs) |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 260 | |
| 261 | if ifm2 is None: |
| 262 | ofm_shape = ifm_shape |
| 263 | else: |
| 264 | in_shape = [] if ifm.shape == [] else ifm_shape.as_list() |
| 265 | in2_shape = [] if ifm2.shape == [] else ifm2_shape.as_list() |
| 266 | ofm_shape = ifm_shape if ifm_ifm2_correct_order(in_shape, in2_shape) else ifm2_shape |
| 267 | |
| 268 | ofm = Tensor(ofm_shape.as_list(), dtype, f"{op.name}_tens0") |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 269 | ofm.quantization = quantization |
| 270 | op.set_output_tensor(ofm) |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 271 | op.ofm_shapes.append(ofm_shape) |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 272 | return op |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 273 | |
| 274 | |
| 275 | def get_pad_values_from_input(padding) -> Tuple: |
| 276 | """Returns top, left, bottom, right padding from input values in a Pad input tensor""" |
| 277 | return (padding[-3][0], padding[-2][0], padding[-3][1], padding[-2][1]) |