blob: 29caf6d036df23d5f521d501365e9f6b7493e17d [file] [log] [blame]
erik.andersson@arm.com460c6892021-02-24 14:38:09 +01001# Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved.
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +01002#
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.
18from typing import Optional
Fredrik Svedberge82be7c2021-01-18 15:21:03 +010019from typing import Tuple
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010020
21from .data_type import DataType
22from .high_level_command_to_npu_op import ifm_ifm2_correct_order
23from .operation import ActivationFunction
24from .operation import Op
25from .operation import Operation
Michael McGeagh16895482020-12-14 15:51:20 +000026from .operation import Padding
Patrik Gustavsson3a269202021-01-21 08:28:55 +010027from .shape4d import Shape4D
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010028from .tensor import QuantizationParameters
29from .tensor import Tensor
30
31
32def create_avgpool_nop(name: str) -> Operation:
33 op = Operation(Op.AvgPool, name)
Michael McGeagh16895482020-12-14 15:51:20 +000034 op.attrs["padding"] = Padding.VALID
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010035 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 Gustavssonc74682c2021-08-17 14:26:38 +020042 op.attrs["explicit_padding"] = [0, 0, 0, 0] # [top, left, bottom, right]
Louis Verhaardc822d622021-03-11 14:59:06 +010043 op.run_on_npu = True
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010044 return op
45
46
Patrik Gustavssonf1580f02021-09-01 12:43:02 +020047def create_add_nop(name: str) -> Operation:
48 op = Operation(Op.Add, name)
49 op.run_on_npu = True
50 return op
51
52
Patrik Gustavssonb4936ad2021-10-05 13:53:34 +020053def create_pad_nop(name: str) -> Operation:
54 op = Operation(Op.Pad, name)
55 op.run_on_npu = True
56 return op
57
58
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010059def create_depthwise_maxpool(
Patrik Gustavsson3a269202021-01-21 08:28:55 +010060 name: str,
61 ifm: Tensor,
62 inp_shape: Shape4D,
63 quantization: QuantizationParameters,
64 activation: Optional[ActivationFunction] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010065) -> Operation:
66 op = Operation(Op.MaxPool, name)
Patrik Gustavsson3a269202021-01-21 08:28:55 +010067 height = inp_shape.height * inp_shape.width
68 width = inp_shape.depth
69 ifm_shape = Shape4D([1, height, width, 1])
70
Michael McGeagh16895482020-12-14 15:51:20 +000071 op.attrs["padding"] = Padding.VALID
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010072 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 Gustavsson3a269202021-01-21 08:28:55 +010079 op.inputs = [ifm]
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010080 ofm = Tensor([1, height, 1, 1], ifm.dtype, op.name + "_tens0")
81 ofm.quantization = quantization
82 op.set_output_tensor(ofm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +010083 op.ifm_shapes.append(ifm_shape)
84 op.ofm_shapes.append(Shape4D(ofm.shape))
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010085 return op
86
87
88def create_reduce_sum(
89 name: str, ifm: Tensor, quantization: QuantizationParameters, activation: Optional[ActivationFunction] = None
90) -> Operation:
91 op = Operation(Op.ReduceSum, name)
Michael McGeagh16895482020-12-14 15:51:20 +000092 op.attrs["padding"] = Padding.VALID
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010093 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 Gustavsson2349d422020-12-01 16:02:29 +0100105 op.set_ifm_ofm_shapes()
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100106 return op
107
108
109def 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 Gustavsson3a269202021-01-21 08:28:55 +0100117 ifm_shape: Optional[Shape4D] = None,
118 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100119) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100120 return create_binary_elementwise(
121 Op.Add, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
122 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100123
124
Fredrik Svedberge82be7c2021-01-18 15:21:03 +0100125def 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 Gustavsson3a269202021-01-21 08:28:55 +0100134 ifm_shape: Optional[Shape4D] = None,
135 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedberge82be7c2021-01-18 15:21:03 +0100136) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100137 op = create_binary_elementwise(
138 Op.RescaleAdd, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
139 )
Fredrik Svedberge82be7c2021-01-18 15:21:03 +0100140 op.rescale = rescale
141 return op
142
143
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100144def 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 Gustavsson3a269202021-01-21 08:28:55 +0100151 ifm_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100152) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100153 return create_unary_elementwise(Op.CLZ, name, ifm, quantization, activation, dtype, attrs, ifm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100154
155
156def 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 Gustavsson3a269202021-01-21 08:28:55 +0100164 ifm_shape: Optional[Shape4D] = None,
165 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100166) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100167 return create_binary_elementwise(
168 Op.Mul, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
169 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100170
171
172def 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 Gustavsson3a269202021-01-21 08:28:55 +0100180 ifm_shape: Optional[Shape4D] = None,
181 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100182) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100183 return create_binary_elementwise(
184 Op.SHL, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
185 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100186
187
188def 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 Gustavsson3a269202021-01-21 08:28:55 +0100196 ifm_shape: Optional[Shape4D] = None,
197 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100198) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100199 return create_binary_elementwise(
200 Op.SHR, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
201 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100202
203
204def 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 Gustavsson3a269202021-01-21 08:28:55 +0100212 ifm_shape: Optional[Shape4D] = None,
213 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100214) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100215 return create_binary_elementwise(
216 Op.Sub, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
217 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100218
219
220def 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 Gustavsson3a269202021-01-21 08:28:55 +0100228 ifm_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100229) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100230 return create_binary_elementwise(op_type, name, ifm, None, quantization, activation, dtype, attrs, ifm_shape, None)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100231
232
233def 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 Gustavsson3a269202021-01-21 08:28:55 +0100242 ifm_shape: Optional[Shape4D] = None,
243 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100244) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100245 if ifm_shape is None:
246 ifm_shape = Shape4D(ifm.shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100247 op = Operation(op_type, name)
248 op.add_input_tensor(ifm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100249 op.ifm_shapes.append(ifm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100250 if ifm2:
251 op.add_input_tensor(ifm2)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100252 if ifm2_shape is None:
253 ifm2_shape = Shape4D(ifm2.shape)
254 op.ifm_shapes.append(ifm2_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100255 op.activation = activation
256 if not dtype:
257 dtype = ifm.dtype
258 if attrs:
259 op.attrs.update(attrs)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100260
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 Svedbergd9c2c422020-12-01 16:33:45 +0100269 ofm.quantization = quantization
270 op.set_output_tensor(ofm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100271 op.ofm_shapes.append(ofm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100272 return op
Louis Verhaardc822d622021-03-11 14:59:06 +0100273
274
275def 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])