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