blob: 7b66dff371425b136113547dc7d61307fa0c3932 [file] [log] [blame]
Rickard Bolinbc6ee582022-11-04 08:24:29 +00001# SPDX-FileCopyrightText: Copyright 2020-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
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.
Rickard Bolinbc6ee582022-11-04 08:24:29 +000016#
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010017# Description:
18# Utility functions for creating Network Operations.
19from typing import Optional
Fredrik Svedberge82be7c2021-01-18 15:21:03 +010020from typing import Tuple
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010021
22from .data_type import DataType
23from .high_level_command_to_npu_op import ifm_ifm2_correct_order
24from .operation import ActivationFunction
25from .operation import Op
26from .operation import Operation
Michael McGeagh16895482020-12-14 15:51:20 +000027from .operation import Padding
Patrik Gustavsson3a269202021-01-21 08:28:55 +010028from .shape4d import Shape4D
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010029from .tensor import QuantizationParameters
30from .tensor import Tensor
31
32
33def create_avgpool_nop(name: str) -> Operation:
34 op = Operation(Op.AvgPool, name)
Michael McGeagh16895482020-12-14 15:51:20 +000035 op.attrs["padding"] = Padding.VALID
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010036 op.attrs["stride_w"] = 1
37 op.attrs["stride_h"] = 1
38 op.attrs["filter_width"] = 1
39 op.attrs["filter_height"] = 1
40 op.attrs["strides"] = [1, 1, 1, 1]
41 op.attrs["ksize"] = [1, 1, 1, 1]
42 op.attrs["skirt"] = [0, 0, 0, 0]
Patrik Gustavssonc74682c2021-08-17 14:26:38 +020043 op.attrs["explicit_padding"] = [0, 0, 0, 0] # [top, left, bottom, right]
Louis Verhaardc822d622021-03-11 14:59:06 +010044 op.run_on_npu = True
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010045 return op
46
47
Patrik Gustavssonf1580f02021-09-01 12:43:02 +020048def create_add_nop(name: str) -> Operation:
49 op = Operation(Op.Add, name)
50 op.run_on_npu = True
51 return op
52
53
Patrik Gustavssonb4936ad2021-10-05 13:53:34 +020054def create_pad_nop(name: str) -> Operation:
55 op = Operation(Op.Pad, name)
56 op.run_on_npu = True
57 return op
58
59
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010060def create_depthwise_maxpool(
Patrik Gustavsson3a269202021-01-21 08:28:55 +010061 name: str,
62 ifm: Tensor,
63 inp_shape: Shape4D,
64 quantization: QuantizationParameters,
65 activation: Optional[ActivationFunction] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010066) -> Operation:
67 op = Operation(Op.MaxPool, name)
Patrik Gustavsson3a269202021-01-21 08:28:55 +010068 height = inp_shape.height * inp_shape.width
69 width = inp_shape.depth
70 ifm_shape = Shape4D([1, height, width, 1])
71
Michael McGeagh16895482020-12-14 15:51:20 +000072 op.attrs["padding"] = Padding.VALID
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010073 op.attrs["stride_w"] = 1
74 op.attrs["stride_h"] = 1
75 op.attrs["filter_width"] = width
76 op.attrs["filter_height"] = 1
77 op.attrs["strides"] = [1, op.attrs["stride_h"], op.attrs["stride_w"], 1]
78 op.attrs["ksize"] = [1, op.attrs["filter_height"], op.attrs["filter_width"], 1]
79 op.activation = activation
Patrik Gustavsson3a269202021-01-21 08:28:55 +010080 op.inputs = [ifm]
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010081 ofm = Tensor([1, height, 1, 1], ifm.dtype, op.name + "_tens0")
82 ofm.quantization = quantization
83 op.set_output_tensor(ofm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +010084 op.ifm_shapes.append(ifm_shape)
85 op.ofm_shapes.append(Shape4D(ofm.shape))
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010086 return op
87
88
89def create_reduce_sum(
90 name: str, ifm: Tensor, quantization: QuantizationParameters, activation: Optional[ActivationFunction] = None
91) -> Operation:
92 op = Operation(Op.ReduceSum, name)
Michael McGeagh16895482020-12-14 15:51:20 +000093 op.attrs["padding"] = Padding.VALID
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010094 op.attrs["stride_w"] = 1
95 op.attrs["stride_h"] = 1
96 op.attrs["filter_width"] = 1
97 op.attrs["filter_height"] = 1
98 op.attrs["strides"] = [1, op.attrs["stride_h"], op.attrs["stride_w"], 1]
99 op.attrs["ksize"] = [1, op.attrs["filter_height"], op.attrs["filter_width"], 1]
100 op.add_input_tensor(ifm)
101 op.activation = activation
102 ofm_shape = [1, ifm.shape[1], ifm.shape[2], 1]
103 sum_of_exp = Tensor(ofm_shape, DataType.int32, op.name + "_tens0")
104 sum_of_exp.quantization = quantization
105 op.set_output_tensor(sum_of_exp)
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100106 op.set_ifm_ofm_shapes()
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100107 return op
108
109
110def create_add(
111 name: str,
112 ifm: Tensor,
113 ifm2: Tensor,
114 quantization: QuantizationParameters,
115 activation: Optional[ActivationFunction] = None,
116 dtype: Optional[DataType] = None,
117 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100118 ifm_shape: Optional[Shape4D] = None,
119 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100120) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100121 return create_binary_elementwise(
122 Op.Add, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
123 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100124
125
126def create_clz(
127 name: str,
128 ifm: Tensor,
129 quantization: QuantizationParameters,
130 activation: Optional[ActivationFunction] = None,
131 dtype: Optional[DataType] = None,
132 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100133 ifm_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100134) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100135 return create_unary_elementwise(Op.CLZ, name, ifm, quantization, activation, dtype, attrs, ifm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100136
137
138def create_mul(
139 name: str,
140 ifm: Tensor,
141 ifm2: Tensor,
142 quantization: QuantizationParameters,
143 activation: Optional[ActivationFunction] = None,
144 dtype: Optional[DataType] = None,
145 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100146 ifm_shape: Optional[Shape4D] = None,
147 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100148) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100149 return create_binary_elementwise(
150 Op.Mul, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
151 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100152
153
154def create_shl(
155 name: str,
156 ifm: Tensor,
157 ifm2: Tensor,
158 quantization: QuantizationParameters,
159 activation: Optional[ActivationFunction] = None,
160 dtype: Optional[DataType] = None,
161 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100162 ifm_shape: Optional[Shape4D] = None,
163 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100164) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100165 return create_binary_elementwise(
166 Op.SHL, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
167 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100168
169
170def create_shr(
171 name: str,
172 ifm: Tensor,
173 ifm2: Tensor,
174 quantization: QuantizationParameters,
175 activation: Optional[ActivationFunction] = None,
176 dtype: Optional[DataType] = None,
177 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100178 ifm_shape: Optional[Shape4D] = None,
179 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100180) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100181 return create_binary_elementwise(
182 Op.SHR, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
183 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100184
185
186def create_sub(
187 name: str,
188 ifm: Tensor,
189 ifm2: Tensor,
190 quantization: QuantizationParameters,
191 activation: Optional[ActivationFunction] = None,
192 dtype: Optional[DataType] = None,
193 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100194 ifm_shape: Optional[Shape4D] = None,
195 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100196) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100197 return create_binary_elementwise(
198 Op.Sub, name, ifm, ifm2, quantization, activation, dtype, attrs, ifm_shape, ifm2_shape
199 )
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100200
201
202def create_unary_elementwise(
203 op_type: Op,
204 name: str,
205 ifm: Tensor,
206 quantization: QuantizationParameters,
207 activation: Optional[ActivationFunction] = None,
208 dtype: Optional[DataType] = None,
209 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100210 ifm_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100211) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100212 return create_binary_elementwise(op_type, name, ifm, None, quantization, activation, dtype, attrs, ifm_shape, None)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100213
214
215def create_binary_elementwise(
216 op_type: Op,
217 name: str,
218 ifm: Tensor,
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100219 ifm2: Optional[Tensor],
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100220 quantization: QuantizationParameters,
221 activation: Optional[ActivationFunction] = None,
222 dtype: Optional[DataType] = None,
223 attrs: Optional[dict] = None,
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100224 ifm_shape: Optional[Shape4D] = None,
225 ifm2_shape: Optional[Shape4D] = None,
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100226) -> Operation:
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100227 if ifm_shape is None:
228 ifm_shape = Shape4D(ifm.shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100229 op = Operation(op_type, name)
230 op.add_input_tensor(ifm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100231 op.ifm_shapes.append(ifm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100232 if ifm2:
233 op.add_input_tensor(ifm2)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100234 if ifm2_shape is None:
235 ifm2_shape = Shape4D(ifm2.shape)
236 op.ifm_shapes.append(ifm2_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100237 op.activation = activation
238 if not dtype:
239 dtype = ifm.dtype
240 if attrs:
241 op.attrs.update(attrs)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100242
243 if ifm2 is None:
244 ofm_shape = ifm_shape
245 else:
Johan Alfvén56a71b02022-10-19 11:20:12 +0200246 in_shape = None if ifm.shape == [] else ifm_shape
247 in2_shape = None if ifm2.shape == [] else ifm2_shape
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100248 ofm_shape = ifm_shape if ifm_ifm2_correct_order(in_shape, in2_shape) else ifm2_shape
249
250 ofm = Tensor(ofm_shape.as_list(), dtype, f"{op.name}_tens0")
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100251 ofm.quantization = quantization
252 op.set_output_tensor(ofm)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100253 op.ofm_shapes.append(ofm_shape)
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +0100254 return op
Louis Verhaardc822d622021-03-11 14:59:06 +0100255
256
257def get_pad_values_from_input(padding) -> Tuple:
258 """Returns top, left, bottom, right padding from input values in a Pad input tensor"""
259 return (padding[-3][0], padding[-2][0], padding[-3][1], padding[-2][1])