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