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