blob: c51a6b58b722c22d0a22fd27b81e9995a00dc6b9 [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]
42 op.attrs["explicit_padding"] = [0, 0, 0, 0]
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
47def create_depthwise_maxpool(
Patrik Gustavsson3a269202021-01-21 08:28:55 +010048 name: str,
49 ifm: Tensor,
50 inp_shape: Shape4D,
51 quantization: QuantizationParameters,
52 activation: Optional[ActivationFunction] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010053) -> Operation:
54 op = Operation(Op.MaxPool, name)
Patrik Gustavsson3a269202021-01-21 08:28:55 +010055 height = inp_shape.height * inp_shape.width
56 width = inp_shape.depth
57 ifm_shape = Shape4D([1, height, width, 1])
58
Michael McGeagh16895482020-12-14 15:51:20 +000059 op.attrs["padding"] = Padding.VALID
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010060 op.attrs["stride_w"] = 1
61 op.attrs["stride_h"] = 1
62 op.attrs["filter_width"] = width
63 op.attrs["filter_height"] = 1
64 op.attrs["strides"] = [1, op.attrs["stride_h"], op.attrs["stride_w"], 1]
65 op.attrs["ksize"] = [1, op.attrs["filter_height"], op.attrs["filter_width"], 1]
66 op.activation = activation
Patrik Gustavsson3a269202021-01-21 08:28:55 +010067 op.inputs = [ifm]
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010068 ofm = Tensor([1, height, 1, 1], ifm.dtype, op.name + "_tens0")
69 ofm.quantization = quantization
70 op.set_output_tensor(ofm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +010071 op.ifm_shapes.append(ifm_shape)
72 op.ofm_shapes.append(Shape4D(ofm.shape))
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010073 return op
74
75
76def create_reduce_sum(
77 name: str, ifm: Tensor, quantization: QuantizationParameters, activation: Optional[ActivationFunction] = None
78) -> Operation:
79 op = Operation(Op.ReduceSum, name)
Michael McGeagh16895482020-12-14 15:51:20 +000080 op.attrs["padding"] = Padding.VALID
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010081 op.attrs["stride_w"] = 1
82 op.attrs["stride_h"] = 1
83 op.attrs["filter_width"] = 1
84 op.attrs["filter_height"] = 1
85 op.attrs["strides"] = [1, op.attrs["stride_h"], op.attrs["stride_w"], 1]
86 op.attrs["ksize"] = [1, op.attrs["filter_height"], op.attrs["filter_width"], 1]
87 op.add_input_tensor(ifm)
88 op.activation = activation
89 ofm_shape = [1, ifm.shape[1], ifm.shape[2], 1]
90 sum_of_exp = Tensor(ofm_shape, DataType.int32, op.name + "_tens0")
91 sum_of_exp.quantization = quantization
92 op.set_output_tensor(sum_of_exp)
Patrik Gustavsson2349d422020-12-01 16:02:29 +010093 op.set_ifm_ofm_shapes()
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010094 return op
95
96
97def create_add(
98 name: str,
99 ifm: Tensor,
100 ifm2: Tensor,
101 quantization: QuantizationParameters,
102 activation: Optional[ActivationFunction] = None,
103 dtype: Optional[DataType] = None,
104 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100105 ifm_shape: Optional[Shape4D] = None,
106 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100107) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100108 return create_binary_elementwise(
109 Op.Add, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
110 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100111
112
Fredrik Svedberge82be7c2021-01-18 15:21:03 +0100113def create_rescale_add(
114 name: str,
115 ifm: Tensor,
116 ifm2: Tensor,
117 rescale: Tuple[int, int],
118 quantization: QuantizationParameters,
119 activation: Optional[ActivationFunction] = None,
120 dtype: Optional[DataType] = None,
121 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100122 ifm_shape: Optional[Shape4D] = None,
123 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedberge82be7c2021-01-18 15:21:03 +0100124) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100125 op = create_binary_elementwise(
126 Op.RescaleAdd, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
127 )
Fredrik Svedberge82be7c2021-01-18 15:21:03 +0100128 op.rescale = rescale
129 return op
130
131
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100132def create_clz(
133 name: str,
134 ifm: Tensor,
135 quantization: QuantizationParameters,
136 activation: Optional[ActivationFunction] = None,
137 dtype: Optional[DataType] = None,
138 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100139 ifm_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100140) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100141 return create_unary_elementwise(Op.CLZ, name, ifm, quantization, activation, dtype, attrs, ifm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100142
143
144def create_mul(
145 name: str,
146 ifm: Tensor,
147 ifm2: Tensor,
148 quantization: QuantizationParameters,
149 activation: Optional[ActivationFunction] = None,
150 dtype: Optional[DataType] = None,
151 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100152 ifm_shape: Optional[Shape4D] = None,
153 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100154) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100155 return create_binary_elementwise(
156 Op.Mul, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
157 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100158
159
160def create_shl(
161 name: str,
162 ifm: Tensor,
163 ifm2: Tensor,
164 quantization: QuantizationParameters,
165 activation: Optional[ActivationFunction] = None,
166 dtype: Optional[DataType] = None,
167 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100168 ifm_shape: Optional[Shape4D] = None,
169 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100170) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100171 return create_binary_elementwise(
172 Op.SHL, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
173 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100174
175
176def create_shr(
177 name: str,
178 ifm: Tensor,
179 ifm2: Tensor,
180 quantization: QuantizationParameters,
181 activation: Optional[ActivationFunction] = None,
182 dtype: Optional[DataType] = None,
183 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100184 ifm_shape: Optional[Shape4D] = None,
185 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100186) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100187 return create_binary_elementwise(
188 Op.SHR, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
189 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100190
191
192def create_sub(
193 name: str,
194 ifm: Tensor,
195 ifm2: Tensor,
196 quantization: QuantizationParameters,
197 activation: Optional[ActivationFunction] = None,
198 dtype: Optional[DataType] = None,
199 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100200 ifm_shape: Optional[Shape4D] = None,
201 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100202) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100203 return create_binary_elementwise(
204 Op.Sub, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
205 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100206
207
208def create_unary_elementwise(
209 op_type: Op,
210 name: str,
211 ifm: Tensor,
212 quantization: QuantizationParameters,
213 activation: Optional[ActivationFunction] = None,
214 dtype: Optional[DataType] = None,
215 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100216 ifm_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100217) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100218 return create_binary_elementwise(op_type, name, ifm, None, quantization, activation, dtype, attrs, ifm_shape, None)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100219
220
221def create_binary_elementwise(
222 op_type: Op,
223 name: str,
224 ifm: Tensor,
225 ifm2: Tensor,
226 quantization: QuantizationParameters,
227 activation: Optional[ActivationFunction] = None,
228 dtype: Optional[DataType] = None,
229 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100230 ifm_shape: Optional[Shape4D] = None,
231 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100232) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100233 if ifm_shape is None:
234 ifm_shape = Shape4D(ifm.shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100235 op = Operation(op_type, name)
236 op.add_input_tensor(ifm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100237 op.ifm_shapes.append(ifm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100238 if ifm2:
239 op.add_input_tensor(ifm2)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100240 if ifm2_shape is None:
241 ifm2_shape = Shape4D(ifm2.shape)
242 op.ifm_shapes.append(ifm2_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100243 op.activation = activation
244 if not dtype:
245 dtype = ifm.dtype
246 if attrs:
247 op.attrs.update(attrs)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100248
249 if ifm2 is None:
250 ofm_shape = ifm_shape
251 else:
252 in_shape = [] if ifm.shape == [] else ifm_shape.as_list()
253 in2_shape = [] if ifm2.shape == [] else ifm2_shape.as_list()
254 ofm_shape = ifm_shape if ifm_ifm2_correct_order(in_shape, in2_shape) else ifm2_shape
255
256 ofm = Tensor(ofm_shape.as_list(), dtype, f"{op.name}_tens0")
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100257 ofm.quantization = quantization
258 op.set_output_tensor(ofm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100259 op.ofm_shapes.append(ofm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100260 return op
Louis Verhaardc822d622021-03-11 14:59:06 +0100261
262
263def get_pad_values_from_input(padding) -> Tuple:
264 """Returns top, left, bottom, right padding from input values in a Pad input tensor"""
265 return (padding[-3][0], padding[-2][0], padding[-3][1], padding[-2][1])