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