blob: 0fbed46b335c3d0e3770ce2fea204b6b69eb3f36 [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
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010053def create_depthwise_maxpool(
Patrik Gustavsson3a269202021-01-21 08:28:55 +010054 name: str,
55 ifm: Tensor,
56 inp_shape: Shape4D,
57 quantization: QuantizationParameters,
58 activation: Optional[ActivationFunction] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010059) -> Operation:
60 op = Operation(Op.MaxPool, name)
Patrik Gustavsson3a269202021-01-21 08:28:55 +010061 height = inp_shape.height * inp_shape.width
62 width = inp_shape.depth
63 ifm_shape = Shape4D([1, height, width, 1])
64
Michael McGeagh16895482020-12-14 15:51:20 +000065 op.attrs["padding"] = Padding.VALID
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010066 op.attrs["stride_w"] = 1
67 op.attrs["stride_h"] = 1
68 op.attrs["filter_width"] = width
69 op.attrs["filter_height"] = 1
70 op.attrs["strides"] = [1, op.attrs["stride_h"], op.attrs["stride_w"], 1]
71 op.attrs["ksize"] = [1, op.attrs["filter_height"], op.attrs["filter_width"], 1]
72 op.activation = activation
Patrik Gustavsson3a269202021-01-21 08:28:55 +010073 op.inputs = [ifm]
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010074 ofm = Tensor([1, height, 1, 1], ifm.dtype, op.name + "_tens0")
75 ofm.quantization = quantization
76 op.set_output_tensor(ofm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +010077 op.ifm_shapes.append(ifm_shape)
78 op.ofm_shapes.append(Shape4D(ofm.shape))
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010079 return op
80
81
82def create_reduce_sum(
83 name: str, ifm: Tensor, quantization: QuantizationParameters, activation: Optional[ActivationFunction] = None
84) -> Operation:
85 op = Operation(Op.ReduceSum, name)
Michael McGeagh16895482020-12-14 15:51:20 +000086 op.attrs["padding"] = Padding.VALID
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010087 op.attrs["stride_w"] = 1
88 op.attrs["stride_h"] = 1
89 op.attrs["filter_width"] = 1
90 op.attrs["filter_height"] = 1
91 op.attrs["strides"] = [1, op.attrs["stride_h"], op.attrs["stride_w"], 1]
92 op.attrs["ksize"] = [1, op.attrs["filter_height"], op.attrs["filter_width"], 1]
93 op.add_input_tensor(ifm)
94 op.activation = activation
95 ofm_shape = [1, ifm.shape[1], ifm.shape[2], 1]
96 sum_of_exp = Tensor(ofm_shape, DataType.int32, op.name + "_tens0")
97 sum_of_exp.quantization = quantization
98 op.set_output_tensor(sum_of_exp)
Patrik Gustavsson2349d422020-12-01 16:02:29 +010099 op.set_ifm_ofm_shapes()
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100100 return op
101
102
103def create_add(
104 name: str,
105 ifm: Tensor,
106 ifm2: Tensor,
107 quantization: QuantizationParameters,
108 activation: Optional[ActivationFunction] = None,
109 dtype: Optional[DataType] = None,
110 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100111 ifm_shape: Optional[Shape4D] = None,
112 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100113) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100114 return create_binary_elementwise(
115 Op.Add, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
116 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100117
118
Fredrik Svedberge82be7c2021-01-18 15:21:03 +0100119def create_rescale_add(
120 name: str,
121 ifm: Tensor,
122 ifm2: Tensor,
123 rescale: Tuple[int, int],
124 quantization: QuantizationParameters,
125 activation: Optional[ActivationFunction] = None,
126 dtype: Optional[DataType] = None,
127 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100128 ifm_shape: Optional[Shape4D] = None,
129 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedberge82be7c2021-01-18 15:21:03 +0100130) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100131 op = create_binary_elementwise(
132 Op.RescaleAdd, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
133 )
Fredrik Svedberge82be7c2021-01-18 15:21:03 +0100134 op.rescale = rescale
135 return op
136
137
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100138def create_clz(
139 name: str,
140 ifm: Tensor,
141 quantization: QuantizationParameters,
142 activation: Optional[ActivationFunction] = None,
143 dtype: Optional[DataType] = None,
144 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100145 ifm_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100146) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100147 return create_unary_elementwise(Op.CLZ, name, ifm, quantization, activation, dtype, attrs, ifm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100148
149
150def create_mul(
151 name: str,
152 ifm: Tensor,
153 ifm2: Tensor,
154 quantization: QuantizationParameters,
155 activation: Optional[ActivationFunction] = None,
156 dtype: Optional[DataType] = None,
157 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100158 ifm_shape: Optional[Shape4D] = None,
159 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100160) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100161 return create_binary_elementwise(
162 Op.Mul, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
163 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100164
165
166def create_shl(
167 name: str,
168 ifm: Tensor,
169 ifm2: Tensor,
170 quantization: QuantizationParameters,
171 activation: Optional[ActivationFunction] = None,
172 dtype: Optional[DataType] = None,
173 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100174 ifm_shape: Optional[Shape4D] = None,
175 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100176) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100177 return create_binary_elementwise(
178 Op.SHL, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
179 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100180
181
182def create_shr(
183 name: str,
184 ifm: Tensor,
185 ifm2: Tensor,
186 quantization: QuantizationParameters,
187 activation: Optional[ActivationFunction] = None,
188 dtype: Optional[DataType] = None,
189 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100190 ifm_shape: Optional[Shape4D] = None,
191 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100192) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100193 return create_binary_elementwise(
194 Op.SHR, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
195 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100196
197
198def create_sub(
199 name: str,
200 ifm: Tensor,
201 ifm2: Tensor,
202 quantization: QuantizationParameters,
203 activation: Optional[ActivationFunction] = None,
204 dtype: Optional[DataType] = None,
205 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100206 ifm_shape: Optional[Shape4D] = None,
207 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100208) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100209 return create_binary_elementwise(
210 Op.Sub, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
211 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100212
213
214def create_unary_elementwise(
215 op_type: Op,
216 name: str,
217 ifm: Tensor,
218 quantization: QuantizationParameters,
219 activation: Optional[ActivationFunction] = None,
220 dtype: Optional[DataType] = None,
221 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100222 ifm_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100223) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100224 return create_binary_elementwise(op_type, name, ifm, None, quantization, activation, dtype, attrs, ifm_shape, None)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100225
226
227def create_binary_elementwise(
228 op_type: Op,
229 name: str,
230 ifm: Tensor,
231 ifm2: Tensor,
232 quantization: QuantizationParameters,
233 activation: Optional[ActivationFunction] = None,
234 dtype: Optional[DataType] = None,
235 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100236 ifm_shape: Optional[Shape4D] = None,
237 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100238) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100239 if ifm_shape is None:
240 ifm_shape = Shape4D(ifm.shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100241 op = Operation(op_type, name)
242 op.add_input_tensor(ifm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100243 op.ifm_shapes.append(ifm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100244 if ifm2:
245 op.add_input_tensor(ifm2)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100246 if ifm2_shape is None:
247 ifm2_shape = Shape4D(ifm2.shape)
248 op.ifm_shapes.append(ifm2_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100249 op.activation = activation
250 if not dtype:
251 dtype = ifm.dtype
252 if attrs:
253 op.attrs.update(attrs)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100254
255 if ifm2 is None:
256 ofm_shape = ifm_shape
257 else:
258 in_shape = [] if ifm.shape == [] else ifm_shape.as_list()
259 in2_shape = [] if ifm2.shape == [] else ifm2_shape.as_list()
260 ofm_shape = ifm_shape if ifm_ifm2_correct_order(in_shape, in2_shape) else ifm2_shape
261
262 ofm = Tensor(ofm_shape.as_list(), dtype, f"{op.name}_tens0")
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100263 ofm.quantization = quantization
264 op.set_output_tensor(ofm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100265 op.ofm_shapes.append(ofm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100266 return op
Louis Verhaardc822d622021-03-11 14:59:06 +0100267
268
269def get_pad_values_from_input(padding) -> Tuple:
270 """Returns top, left, bottom, right padding from input values in a Pad input tensor"""
271 return (padding[-3][0], padding[-2][0], padding[-3][1], padding[-2][1])