blob: c4176d96113d7da894690c3245858245d651acf6 [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
125def create_clz(
126 name: str,
127 ifm: Tensor,
128 quantization: QuantizationParameters,
129 activation: Optional[ActivationFunction] = None,
130 dtype: Optional[DataType] = None,
131 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100132 ifm_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100133) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100134 return create_unary_elementwise(Op.CLZ, name, ifm, quantization, activation, dtype, attrs, ifm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100135
136
137def create_mul(
138 name: str,
139 ifm: Tensor,
140 ifm2: 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,
146 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100147) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100148 return create_binary_elementwise(
149 Op.Mul, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
150 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100151
152
153def create_shl(
154 name: str,
155 ifm: Tensor,
156 ifm2: Tensor,
157 quantization: QuantizationParameters,
158 activation: Optional[ActivationFunction] = None,
159 dtype: Optional[DataType] = None,
160 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100161 ifm_shape: Optional[Shape4D] = None,
162 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100163) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100164 return create_binary_elementwise(
165 Op.SHL, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
166 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100167
168
169def create_shr(
170 name: str,
171 ifm: Tensor,
172 ifm2: Tensor,
173 quantization: QuantizationParameters,
174 activation: Optional[ActivationFunction] = None,
175 dtype: Optional[DataType] = None,
176 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100177 ifm_shape: Optional[Shape4D] = None,
178 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100179) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100180 return create_binary_elementwise(
181 Op.SHR, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
182 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100183
184
185def create_sub(
186 name: str,
187 ifm: Tensor,
188 ifm2: Tensor,
189 quantization: QuantizationParameters,
190 activation: Optional[ActivationFunction] = None,
191 dtype: Optional[DataType] = None,
192 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100193 ifm_shape: Optional[Shape4D] = None,
194 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100195) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100196 return create_binary_elementwise(
197 Op.Sub, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
198 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100199
200
201def create_unary_elementwise(
202 op_type: Op,
203 name: str,
204 ifm: Tensor,
205 quantization: QuantizationParameters,
206 activation: Optional[ActivationFunction] = None,
207 dtype: Optional[DataType] = None,
208 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100209 ifm_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100210) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100211 return create_binary_elementwise(op_type, name, ifm, None, quantization, activation, dtype, attrs, ifm_shape, None)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100212
213
214def create_binary_elementwise(
215 op_type: Op,
216 name: str,
217 ifm: Tensor,
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100218 ifm2: Optional[Tensor],
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100219 quantization: QuantizationParameters,
220 activation: Optional[ActivationFunction] = None,
221 dtype: Optional[DataType] = None,
222 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100223 ifm_shape: Optional[Shape4D] = None,
224 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100225) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100226 if ifm_shape is None:
227 ifm_shape = Shape4D(ifm.shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100228 op = Operation(op_type, name)
229 op.add_input_tensor(ifm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100230 op.ifm_shapes.append(ifm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100231 if ifm2:
232 op.add_input_tensor(ifm2)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100233 if ifm2_shape is None:
234 ifm2_shape = Shape4D(ifm2.shape)
235 op.ifm_shapes.append(ifm2_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100236 op.activation = activation
237 if not dtype:
238 dtype = ifm.dtype
239 if attrs:
240 op.attrs.update(attrs)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100241
242 if ifm2 is None:
243 ofm_shape = ifm_shape
244 else:
Johan Alfvén56a71b02022-10-19 11:20:12 +0200245 in_shape = None if ifm.shape == [] else ifm_shape
246 in2_shape = None if ifm2.shape == [] else ifm2_shape
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100247 ofm_shape = ifm_shape if ifm_ifm2_correct_order(in_shape, in2_shape) else ifm2_shape
248
249 ofm = Tensor(ofm_shape.as_list(), dtype, f"{op.name}_tens0")
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100250 ofm.quantization = quantization
251 op.set_output_tensor(ofm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100252 op.ofm_shapes.append(ofm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100253 return op
Louis Verhaardc822d622021-03-11 14:59:06 +0100254
255
256def get_pad_values_from_input(padding) -> Tuple:
257 """Returns top, left, bottom, right padding from input values in a Pad input tensor"""
258 return (padding[-3][0], padding[-2][0], padding[-3][1], padding[-2][1])