1 /*
2 * Copyright (C) 2010-2020 Arm Limited or its affiliates. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 /* ----------------------------------------------------------------------
20 * Project: CMSIS NN Library
21 * Title: arm_depthwise_conv_u8_basic_ver1.c
22 * Description: u8 depthwise convolution function
23 *
24 * $Date: 09. October 2020
25 * $Revision: V.1.1.1
26 *
27 * Target : Cortex-M CPUs
28 *
29 * -------------------------------------------------------------------- */
30
31 #include "arm_nnfunctions.h"
32 #include "arm_nnsupportfunctions.h"
33
34 /**
35 * @ingroup groupNN
36 */
37
38 /**
39 * @addtogroup NNConv
40 * @{
41 */
42
depthwise_conv_u8_mult_4(const uint8_t * input,const int32_t input_x,const int32_t input_y,const int32_t input_ch,const uint8_t * kernel,const int32_t output_ch,const int32_t ch_mult,const int32_t kernel_x,const int32_t kernel_y,const int32_t pad_x,const int32_t pad_y,const int32_t stride_x,const int32_t stride_y,const int32_t * bias,uint8_t * output,const int32_t output_shift,const int32_t output_mult,const int32_t output_x,const int32_t output_y,const int32_t output_offset,const int32_t input_offset,const int32_t filter_offset,const int32_t output_activation_min,const int32_t output_activation_max)43 static void depthwise_conv_u8_mult_4(const uint8_t *input,
44 const int32_t input_x,
45 const int32_t input_y,
46 const int32_t input_ch,
47 const uint8_t *kernel,
48 const int32_t output_ch,
49 const int32_t ch_mult,
50 const int32_t kernel_x,
51 const int32_t kernel_y,
52 const int32_t pad_x,
53 const int32_t pad_y,
54 const int32_t stride_x,
55 const int32_t stride_y,
56 const int32_t *bias,
57 uint8_t *output,
58 const int32_t output_shift,
59 const int32_t output_mult,
60 const int32_t output_x,
61 const int32_t output_y,
62 const int32_t output_offset,
63 const int32_t input_offset,
64 const int32_t filter_offset,
65 const int32_t output_activation_min,
66 const int32_t output_activation_max)
67 {
68 for (int32_t in_h = -pad_y, out_h = 0, out_idx = 0; out_h < output_y; in_h += stride_y, ++out_h)
69 {
70 for (int32_t in_w = -pad_x, out_w = 0, ker_h_start = MAX(0, -in_h); out_w < output_x; in_w += stride_x, ++out_w)
71 {
72 for (int32_t in_ch = 0, out_ch = 0, ker_w_start = MAX(0, -in_w); out_ch < output_ch;
73 ++in_ch, out_ch += ch_mult)
74 {
75 for (int mult_tile = 0; mult_tile < ch_mult; mult_tile += 4)
76 {
77 int32_t out_buff[4];
78
79 out_buff[0] = 0;
80 out_buff[1] = 0;
81 out_buff[2] = 0;
82 out_buff[3] = 0;
83
84 for (int32_t ker_h = ker_h_start; ker_h < MIN(kernel_y, input_y - in_h); ++ker_h)
85 {
86 int32_t ker_idx = ker_h * (output_ch * kernel_x) + ker_w_start * output_ch + out_ch;
87 int32_t in_idx = (in_h + ker_h) * (input_ch * input_x) + in_w * input_ch + in_ch;
88
89 for (int32_t ker_w = ker_w_start; ker_w < MIN(kernel_x, input_x - in_w);
90 ++ker_w, ker_idx += output_ch)
91 {
92 int32_t in_val = input[in_idx + ker_w * input_ch] + input_offset;
93 out_buff[0] += in_val * (kernel[ker_idx + 0 + mult_tile] + filter_offset);
94 out_buff[1] += in_val * (kernel[ker_idx + 1 + mult_tile] + filter_offset);
95 out_buff[2] += in_val * (kernel[ker_idx + 2 + mult_tile] + filter_offset);
96 out_buff[3] += in_val * (kernel[ker_idx + 3 + mult_tile] + filter_offset);
97 }
98 }
99
100 if (bias != NULL)
101 {
102 out_buff[0] += bias[out_ch + 0 + mult_tile];
103 out_buff[1] += bias[out_ch + 1 + mult_tile];
104 out_buff[2] += bias[out_ch + 2 + mult_tile];
105 out_buff[3] += bias[out_ch + 3 + mult_tile];
106 }
107 out_buff[0] = arm_nn_requantize(out_buff[0], output_mult, output_shift);
108 out_buff[1] = arm_nn_requantize(out_buff[1], output_mult, output_shift);
109 out_buff[2] = arm_nn_requantize(out_buff[2], output_mult, output_shift);
110 out_buff[3] = arm_nn_requantize(out_buff[3], output_mult, output_shift);
111
112 out_buff[0] += output_offset;
113 out_buff[1] += output_offset;
114 out_buff[2] += output_offset;
115 out_buff[3] += output_offset;
116
117 out_buff[0] = MIN(MAX(out_buff[0], output_activation_min), output_activation_max);
118 out_buff[1] = MIN(MAX(out_buff[1], output_activation_min), output_activation_max);
119 out_buff[2] = MIN(MAX(out_buff[2], output_activation_min), output_activation_max);
120 out_buff[3] = MIN(MAX(out_buff[3], output_activation_min), output_activation_max);
121
122 output[out_idx++] = (uint8_t)out_buff[0];
123 output[out_idx++] = (uint8_t)out_buff[1];
124 output[out_idx++] = (uint8_t)out_buff[2];
125 output[out_idx++] = (uint8_t)out_buff[3];
126 }
127 }
128 }
129 }
130 }
131
depthwise_conv_u8_generic(const uint8_t * input,const int32_t input_x,const int32_t input_y,const int32_t input_ch,const uint8_t * kernel,const int32_t output_ch,const int32_t ch_mult,const int32_t kernel_x,const int32_t kernel_y,const int32_t pad_x,const int32_t pad_y,const int32_t stride_x,const int32_t stride_y,const int32_t * bias,uint8_t * output,const int32_t output_shift,const int32_t output_mult,const int32_t output_x,const int32_t output_y,const int32_t output_offset,const int32_t input_offset,const int32_t filter_offset,const int32_t output_activation_min,const int32_t output_activation_max)132 static void depthwise_conv_u8_generic(const uint8_t *input,
133 const int32_t input_x,
134 const int32_t input_y,
135 const int32_t input_ch,
136 const uint8_t *kernel,
137 const int32_t output_ch,
138 const int32_t ch_mult,
139 const int32_t kernel_x,
140 const int32_t kernel_y,
141 const int32_t pad_x,
142 const int32_t pad_y,
143 const int32_t stride_x,
144 const int32_t stride_y,
145 const int32_t *bias,
146 uint8_t *output,
147 const int32_t output_shift,
148 const int32_t output_mult,
149 const int32_t output_x,
150 const int32_t output_y,
151 const int32_t output_offset,
152 const int32_t input_offset,
153 const int32_t filter_offset,
154 const int32_t output_activation_min,
155 const int32_t output_activation_max)
156 {
157 (void)output_ch;
158 int i_out = 0;
159 for (int i_out_y = 0; i_out_y < output_y; i_out_y++)
160 {
161 const int16_t base_idx_y = (i_out_y * stride_y) - pad_y;
162 for (int i_out_x = 0; i_out_x < output_x; i_out_x++)
163 {
164 const int16_t base_idx_x = (i_out_x * stride_x) - pad_x;
165 for (int i_input_ch = 0; i_input_ch < input_ch; i_input_ch++)
166 {
167 for (int i_ch_mult = 0; i_ch_mult < ch_mult; i_ch_mult++)
168 {
169 const int idx_out_ch = i_ch_mult + i_input_ch * ch_mult;
170 int32_t acc_0;
171 /* Condition for kernel start dimension: (base_idx_<x,y> + ker_<x,y>_start) >= 0 */
172 const int ker_y_start = MAX(0, -base_idx_y);
173 const int ker_x_start = MAX(0, -base_idx_x);
174 /* Condition for kernel end dimension: (base_idx_<x,y> + ker_<x,y>_end) < input_<x,y> */
175 const int ker_y_end = MIN(kernel_y, input_y - base_idx_y);
176 const int ker_x_end = MIN(kernel_x, input_x - base_idx_x);
177 acc_0 = 0;
178
179 for (int i_ker_y = ker_y_start; i_ker_y < ker_y_end; i_ker_y++)
180 {
181 const int32_t idx_y = base_idx_y + i_ker_y;
182 for (int i_ker_x = ker_x_start; i_ker_x < ker_x_end; i_ker_x++)
183 {
184 const int32_t idx_x = base_idx_x + i_ker_x;
185 int32_t idx_0 = (idx_y * input_x + idx_x) * input_ch + i_input_ch;
186 int32_t ker_idx_0 = (i_ker_y * kernel_x + i_ker_x) * (input_ch * ch_mult) + idx_out_ch;
187
188 acc_0 += (input[idx_0] + input_offset) * (kernel[ker_idx_0] + filter_offset);
189 }
190 }
191 if (bias != NULL)
192 {
193 acc_0 += bias[idx_out_ch];
194 }
195
196 /* Requantize and clamp output to provided range */
197 acc_0 = arm_nn_requantize(acc_0, output_mult, output_shift);
198 acc_0 += output_offset;
199 acc_0 = MAX(acc_0, output_activation_min);
200 acc_0 = MIN(acc_0, output_activation_max);
201
202 output[i_out++] = acc_0;
203 }
204 }
205 }
206 }
207 }
208
209 /**
210 * @brief uint8 depthwise convolution function with asymmetric quantization
211 *
212 * @param[in] input Pointer to input tensor
213 * @param[in] input_x Width of input tensor
214 * @param[in] input_y Height of input tensor
215 * @param[in] input_ch Channels in input tensor
216 * @param[in] kernel Pointer to kernel weights
217 * @param[in] kernel_x Width of kernel
218 * @param[in] kernel_y Height of kernel
219 * @param[in] ch_mult Number of channel multiplier
220 * @param[in] pad_x Padding sizes x
221 * @param[in] pad_y Padding sizes y
222 * @param[in] stride_x Convolution stride along the width
223 * @param[in] stride_y Convolution stride along the height
224 * @param[in] dilation_x Dilation along width. Not used and intended for future enhancement.
225 * @param[in] dilation_y Dilation along height. Not used and intended for future enhancement.
226 * @param[in] bias Pointer to optional bias values. If no bias is
227 * available, NULL is expected
228 * @param[in] input_offset Input tensor zero offset
229 * @param[in] filter_offset Kernel tensor zero offset
230 * @param[in] output_offset Output tensor zero offset
231 * @param[in,out] output Pointer to output tensor
232 * @param[in] output_x Width of output tensor
233 * @param[in] output_y Height of output tensor
234 * @param[in] output_activation_min Minimum value to clamp the output to. Range : {0, 255}
235 * @param[in] output_activation_max Minimum value to clamp the output to. Range : {0, 255}
236 * @param[in] output_shift Amount of right-shift for output
237 * @param[in] output_mult Output multiplier for requantization
238 * @return The function returns one of the following
239 * <code>ARM_MATH_SIZE_MISMATCH</code> - Not supported dimension of tensors
240 * <code>ARM_MATH_SUCCESS</code> - Successful operation
241 * <code>ARM_MATH_ARGUMENT_ERROR</code> - Implementation not available
242 *
243 *
244 */
245
arm_depthwise_conv_u8_basic_ver1(const uint8_t * input,const uint16_t input_x,const uint16_t input_y,const uint16_t input_ch,const uint8_t * kernel,const uint16_t kernel_x,const uint16_t kernel_y,const int16_t ch_mult,const int16_t pad_x,const int16_t pad_y,const int16_t stride_x,const int16_t stride_y,const int16_t dilation_x,const int16_t dilation_y,const int32_t * bias,const int32_t input_offset,const int32_t filter_offset,const int32_t output_offset,uint8_t * output,const uint16_t output_x,const uint16_t output_y,const int32_t output_activation_min,const int32_t output_activation_max,const int32_t output_shift,const int32_t output_mult)246 arm_status arm_depthwise_conv_u8_basic_ver1(const uint8_t *input,
247 const uint16_t input_x,
248 const uint16_t input_y,
249 const uint16_t input_ch,
250 const uint8_t *kernel,
251 const uint16_t kernel_x,
252 const uint16_t kernel_y,
253 const int16_t ch_mult,
254 const int16_t pad_x,
255 const int16_t pad_y,
256 const int16_t stride_x,
257 const int16_t stride_y,
258 const int16_t dilation_x,
259 const int16_t dilation_y,
260 const int32_t *bias,
261 const int32_t input_offset,
262 const int32_t filter_offset,
263 const int32_t output_offset,
264 uint8_t *output,
265 const uint16_t output_x,
266 const uint16_t output_y,
267 const int32_t output_activation_min,
268 const int32_t output_activation_max,
269 const int32_t output_shift,
270 const int32_t output_mult)
271 {
272 (void)dilation_x;
273 (void)dilation_y;
274
275 if (ch_mult % 4 == 0)
276 {
277 depthwise_conv_u8_mult_4(input,
278 input_x,
279 input_y,
280 input_ch,
281 kernel,
282 ch_mult * input_ch,
283 ch_mult,
284 kernel_x,
285 kernel_y,
286 pad_x,
287 pad_y,
288 stride_x,
289 stride_y,
290 bias,
291 output,
292 output_shift,
293 output_mult,
294 output_x,
295 output_y,
296 output_offset,
297 input_offset,
298 filter_offset,
299 output_activation_min,
300 output_activation_max);
301 }
302 else
303 {
304 depthwise_conv_u8_generic(input,
305 input_x,
306 input_y,
307 input_ch,
308 kernel,
309 ch_mult * input_ch,
310 ch_mult,
311 kernel_x,
312 kernel_y,
313 pad_x,
314 pad_y,
315 stride_x,
316 stride_y,
317 bias,
318 output,
319 output_shift,
320 output_mult,
321 output_x,
322 output_y,
323 output_offset,
324 input_offset,
325 filter_offset,
326 output_activation_min,
327 output_activation_max);
328 }
329
330 /* Return to application */
331 return ARM_MATH_SUCCESS;
332 }
333
334 /**
335 * @} end of NNConv group
336 */
337