arm_depthwise_conv_fast_s16.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
  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. * Project: CMSIS NN Library
  20. * Title: arm_depthwise_conv_fast_s16.c
  21. * Description: Optimized s16 depthwise separable convolution function for
  22. * channel multiplier of 1.
  23. *
  24. * $Date: 30 January 2023
  25. * $Revision: V.1.3.0
  26. *
  27. * Target : Arm(R) M-Profile Architecture
  28. *
  29. * -------------------------------------------------------------------- */
  30. #include "arm_nnfunctions.h"
  31. #include "arm_nnsupportfunctions.h"
  32. /**
  33. * @ingroup Public
  34. */
  35. /**
  36. * @addtogroup NNConv
  37. * @{
  38. */
  39. /*
  40. * Optimized s16 depthwise convolution function with constraint that in_channel equals out_channel
  41. *
  42. * Refer prototype header file for details.
  43. *
  44. */
  45. arm_cmsis_nn_status arm_depthwise_conv_fast_s16(const cmsis_nn_context *ctx,
  46. const cmsis_nn_dw_conv_params *dw_conv_params,
  47. const cmsis_nn_per_channel_quant_params *quant_params,
  48. const cmsis_nn_dims *input_dims,
  49. const int16_t *input,
  50. const cmsis_nn_dims *filter_dims,
  51. const int8_t *kernel,
  52. const cmsis_nn_dims *bias_dims,
  53. const int64_t *bias,
  54. const cmsis_nn_dims *output_dims,
  55. int16_t *output)
  56. {
  57. const int32_t input_ch = input_dims->c;
  58. const int32_t output_ch = output_dims->c;
  59. /* Check input constraints input_ch == output_ch */
  60. if (input_ch != output_ch)
  61. {
  62. return ARM_CMSIS_NN_ARG_ERROR;
  63. }
  64. if (filter_dims->w * filter_dims->h >= 512)
  65. {
  66. return ARM_CMSIS_NN_ARG_ERROR;
  67. }
  68. if (ctx->buf == NULL && arm_depthwise_conv_fast_s16_get_buffer_size(input_dims, filter_dims) > 0)
  69. {
  70. return ARM_CMSIS_NN_ARG_ERROR;
  71. }
  72. #if defined(ARM_MATH_DSP)
  73. (void)bias_dims;
  74. const int32_t input_x = input_dims->w;
  75. const int32_t input_y = input_dims->h;
  76. const int32_t input_batches = input_dims->n;
  77. const int32_t kernel_x = filter_dims->w;
  78. const int32_t kernel_y = filter_dims->h;
  79. const int32_t pad_x = dw_conv_params->padding.w;
  80. const int32_t pad_y = dw_conv_params->padding.h;
  81. const int32_t stride_x = dw_conv_params->stride.w;
  82. const int32_t stride_y = dw_conv_params->stride.h;
  83. const int32_t *output_shift = quant_params->shift;
  84. const int32_t *output_mult = quant_params->multiplier;
  85. const int32_t output_x = output_dims->w;
  86. const int32_t output_y = output_dims->h;
  87. const int32_t output_activation_min = dw_conv_params->activation.min;
  88. const int32_t output_activation_max = dw_conv_params->activation.max;
  89. int16_t *buffer_a = (int16_t *)ctx->buf;
  90. #if defined(ARM_MATH_MVEI)
  91. int16_t *lhs_buffer = buffer_a;
  92. int16_t *out = output;
  93. int buffer_count = 0;
  94. const int32_t kernel_size = kernel_x * kernel_y;
  95. for (int i_batch = 0; i_batch < input_batches; i_batch++)
  96. {
  97. /* This part implements the im2col function */
  98. for (int i_out_y = 0, base_idx_y = -pad_y; i_out_y < output_y; base_idx_y += stride_y, i_out_y++)
  99. {
  100. for (int i_out_x = 0, base_idx_x = -pad_x; i_out_x < output_x; base_idx_x += stride_x, i_out_x++)
  101. {
  102. for (int i_ker_y = base_idx_y; i_ker_y < base_idx_y + kernel_y; i_ker_y++)
  103. {
  104. for (int i_ker_x = base_idx_x; i_ker_x < base_idx_x + kernel_x; i_ker_x++)
  105. {
  106. if (i_ker_y < 0 || i_ker_y >= input_y || i_ker_x < 0 || i_ker_x >= input_x)
  107. {
  108. memset(lhs_buffer, (int16_t)0, (uint32_t)(input_ch * sizeof(int16_t)));
  109. }
  110. else
  111. {
  112. arm_memcpy_q15(lhs_buffer,
  113. (int16_t *)(input + (i_ker_y * input_x + i_ker_x) * input_ch),
  114. (uint32_t)(input_ch * sizeof(int16_t)));
  115. }
  116. lhs_buffer += input_ch;
  117. }
  118. }
  119. buffer_count++;
  120. if (buffer_count == 4)
  121. {
  122. lhs_buffer = buffer_a;
  123. out = arm_nn_depthwise_conv_nt_t_s16(lhs_buffer,
  124. kernel,
  125. input_ch,
  126. output_shift,
  127. output_mult,
  128. output_activation_min,
  129. output_activation_max,
  130. kernel_size,
  131. bias,
  132. out);
  133. buffer_count = 0;
  134. }
  135. }
  136. }
  137. input += input_x * input_y * input_ch;
  138. }
  139. /* Handle left over buffers */
  140. lhs_buffer = buffer_a;
  141. for (int i_buf = 0; i_buf < buffer_count; i_buf++)
  142. {
  143. int32_t loop_count = (input_ch + 3) / 4;
  144. int32_t num_ch_to_process = input_ch;
  145. for (int i_loop_cnt = 0, offset = 0; i_loop_cnt < loop_count; num_ch_to_process -= 4, offset += 4, i_loop_cnt++)
  146. {
  147. const int8_t *row_0 = kernel + offset;
  148. const int16_t *col_0 = lhs_buffer + (kernel_size * input_ch * i_buf) + offset;
  149. int32x4_t out_0 = vdupq_n_s32(0);
  150. for (int i_ker = 0; i_ker < kernel_size; i_ker++)
  151. {
  152. const int32x4_t ker_0 = vldrbq_s32(row_0);
  153. int32x4_t ip_0 = vldrhq_s32(col_0);
  154. out_0 += vmulq_s32(ip_0, ker_0);
  155. col_0 += input_ch;
  156. row_0 += input_ch;
  157. }
  158. int64_t in_requantize_0 = (int64_t)out_0[0];
  159. int64_t in_requantize_1 = (int64_t)out_0[1];
  160. int64_t in_requantize_2 = (int64_t)out_0[2];
  161. int64_t in_requantize_3 = (int64_t)out_0[3];
  162. if (bias)
  163. {
  164. in_requantize_0 += bias[offset];
  165. in_requantize_1 += bias[offset + 1];
  166. in_requantize_2 += bias[offset + 2];
  167. in_requantize_3 += bias[offset + 3];
  168. }
  169. int32_t reduced_multiplier_0 = REDUCE_MULTIPLIER(output_mult[offset]);
  170. int32_t reduced_multiplier_1 = REDUCE_MULTIPLIER(output_mult[offset + 1]);
  171. int32_t reduced_multiplier_2 = REDUCE_MULTIPLIER(output_mult[offset + 2]);
  172. int32_t reduced_multiplier_3 = REDUCE_MULTIPLIER(output_mult[offset + 3]);
  173. out_0[0] = arm_nn_requantize_s64(in_requantize_0, reduced_multiplier_0, output_shift[offset]);
  174. out_0[1] = arm_nn_requantize_s64(in_requantize_1, reduced_multiplier_1, output_shift[offset + 1]);
  175. out_0[2] = arm_nn_requantize_s64(in_requantize_2, reduced_multiplier_2, output_shift[offset + 2]);
  176. out_0[3] = arm_nn_requantize_s64(in_requantize_3, reduced_multiplier_3, output_shift[offset + 3]);
  177. out_0 = vmaxq_s32(out_0, vdupq_n_s32(output_activation_min));
  178. out_0 = vminq_s32(out_0, vdupq_n_s32(output_activation_max));
  179. mve_pred16_t p = vctp32q((uint32_t)num_ch_to_process);
  180. vstrhq_p_s32(out, out_0, p);
  181. out += 4;
  182. }
  183. const int tail_ch = input_ch & 0x3;
  184. if (tail_ch != 0)
  185. {
  186. out -= (4 - tail_ch);
  187. }
  188. }
  189. #else // ARM_MATH_DSP
  190. /* Run the following code in cores using DSP extension */
  191. int16_t *const col_buffer_start = buffer_a;
  192. int16_t *col_buffer = col_buffer_start;
  193. const int64_t *const bias_start_pos = bias;
  194. const int32_t *const out_mult_start_pos = output_mult;
  195. const int32_t *const out_shift_start_pos = output_shift;
  196. uint16_t row_count;
  197. uint16_t row_shift;
  198. int32_t result;
  199. for (int i_batch = 0; i_batch < input_batches; i_batch++)
  200. {
  201. for (int i_out_y = 0; i_out_y < output_y; i_out_y++)
  202. {
  203. const int16_t base_idx_y = (i_out_y * stride_y) - pad_y;
  204. for (int i_out_x = 0; i_out_x < output_x; i_out_x++)
  205. {
  206. const int16_t base_idx_x = (i_out_x * stride_x) - pad_x;
  207. /* Out of bounds is only considered for the y axis as it provides a contiguous zero'ing opportunity than
  208. along the x axis */
  209. const int ker_y_start = MAX(0, -base_idx_y);
  210. /* Condition for kernel end dimension: (base_idx_y + ker_y_end) < input_y */
  211. const int ker_y_end = MIN(kernel_y, input_y - base_idx_y);
  212. int32_t index = 0;
  213. if (ker_y_start != 0)
  214. {
  215. memset(&col_buffer[index], 0, (kernel_x * input_ch) * ker_y_start * sizeof(int16_t));
  216. index += (kernel_x * input_ch) * ker_y_start;
  217. }
  218. for (int i_ker_y = ker_y_start; i_ker_y < ker_y_end; i_ker_y++)
  219. {
  220. const int32_t idx_y = base_idx_y + i_ker_y;
  221. for (int i_ker_x = 0; i_ker_x < kernel_x; i_ker_x++)
  222. {
  223. const int32_t idx_x = base_idx_x + i_ker_x;
  224. if (idx_x < 0 || idx_x >= input_x)
  225. {
  226. memset(&col_buffer[index], 0, input_ch * sizeof(int16_t));
  227. }
  228. else
  229. {
  230. arm_memcpy_q15(&col_buffer[index],
  231. input + (idx_y * input_x + idx_x) * input_ch,
  232. input_ch * sizeof(int16_t));
  233. }
  234. index += input_ch;
  235. }
  236. }
  237. const int diff = kernel_y - ker_y_end;
  238. if (diff != 0)
  239. {
  240. memset(&col_buffer[index], 0, (kernel_x * input_ch) * diff * sizeof(int16_t));
  241. }
  242. row_count = output_ch / 4;
  243. row_shift = 0;
  244. bias = bias_start_pos;
  245. output_mult = out_mult_start_pos;
  246. output_shift = out_shift_start_pos;
  247. while (row_count)
  248. {
  249. int32_t sum_1 = 0;
  250. int32_t sum_2 = 0;
  251. int32_t sum_3 = 0;
  252. int32_t sum_4 = 0;
  253. int32_t output_mult_1 = REDUCE_MULTIPLIER(output_mult[0]);
  254. int32_t output_mult_2 = REDUCE_MULTIPLIER(output_mult[1]);
  255. int32_t output_mult_3 = REDUCE_MULTIPLIER(output_mult[2]);
  256. int32_t output_mult_4 = REDUCE_MULTIPLIER(output_mult[3]);
  257. output_mult += 4;
  258. uint16_t col_count = (kernel_x * kernel_y) / 2;
  259. int16_t *col_pos = col_buffer_start + row_shift;
  260. const int8_t *row_pos = kernel + row_shift;
  261. row_shift += 4;
  262. while (col_count)
  263. {
  264. /* General idea is to read 4 + 4 (input, kernel) pair and re-arrange them in the right order to
  265. use in a SMLAD instruction . One run of this loop produces 4 partial outputs with 8 MACs. */
  266. int32_t row_a1, row_a2, row_b1, row_b2, col_a, row_c, col_b, col_c;
  267. /* Read 4 weights */
  268. row_b1 = arm_nn_read_s8x4(row_pos);
  269. row_a1 = arm_nn_read_s8x4(row_pos + input_ch);
  270. col_a = arm_nn_read_s16x2(col_pos);
  271. col_b = arm_nn_read_s16x2(col_pos + input_ch);
  272. row_a2 = SXTB16(row_b1);
  273. row_b1 = SXTB16(ROR(row_b1, 8));
  274. row_b2 = SXTB16(row_a1);
  275. row_a1 = SXTB16(ROR(row_a1, 8));
  276. col_c = PKHBT(col_b, col_a, 16);
  277. col_a = PKHTB(col_b, col_a, 16);
  278. row_c = PKHBT(row_b2, row_a2, 16);
  279. sum_1 = SMLAD(col_c, row_c, sum_1);
  280. row_c = PKHBT(row_b1, row_a1, 16);
  281. sum_2 = SMLAD(col_a, row_c, sum_2);
  282. col_a = arm_nn_read_s16x2(col_pos + 2);
  283. col_b = arm_nn_read_s16x2(col_pos + input_ch + 2);
  284. col_c = PKHBT(col_b, col_a, 16);
  285. col_a = PKHTB(col_b, col_a, 16);
  286. row_c = PKHTB(row_a2, row_b2, 16);
  287. sum_3 = SMLAD(col_c, row_c, sum_3);
  288. row_c = PKHTB(row_a1, row_b1, 16);
  289. sum_4 = SMLAD(col_a, row_c, sum_4);
  290. row_pos += input_ch << 1;
  291. col_pos += input_ch << 1;
  292. col_count--;
  293. }
  294. col_count = (kernel_x * kernel_y) & 0x1;
  295. while (col_count)
  296. {
  297. sum_1 += row_pos[0] * col_pos[0];
  298. sum_2 += row_pos[1] * col_pos[1];
  299. sum_3 += row_pos[2] * col_pos[2];
  300. sum_4 += row_pos[3] * col_pos[3];
  301. row_pos += input_ch;
  302. col_pos += input_ch;
  303. col_count--;
  304. }
  305. int64_t acc_1 = sum_1;
  306. int64_t acc_2 = sum_2;
  307. int64_t acc_3 = sum_3;
  308. int64_t acc_4 = sum_4;
  309. if (bias)
  310. {
  311. acc_1 += *bias++;
  312. acc_2 += *bias++;
  313. acc_3 += *bias++;
  314. acc_4 += *bias++;
  315. }
  316. result = arm_nn_requantize_s64(acc_1, output_mult_1, *output_shift++);
  317. result = MAX(result, output_activation_min);
  318. result = MIN(result, output_activation_max);
  319. *output++ = (int16_t)result;
  320. result = arm_nn_requantize_s64(acc_2, output_mult_2, *output_shift++);
  321. result = MAX(result, output_activation_min);
  322. result = MIN(result, output_activation_max);
  323. *output++ = (int16_t)result;
  324. result = arm_nn_requantize_s64(acc_3, output_mult_3, *output_shift++);
  325. result = MAX(result, output_activation_min);
  326. result = MIN(result, output_activation_max);
  327. *output++ = (int16_t)result;
  328. result = arm_nn_requantize_s64(acc_4, output_mult_4, *output_shift++);
  329. result = MAX(result, output_activation_min);
  330. result = MIN(result, output_activation_max);
  331. *output++ = (int16_t)result;
  332. row_count--;
  333. }
  334. row_count = output_ch & 0x3;
  335. while (row_count)
  336. {
  337. int16_t *col_pos = col_buffer_start + row_shift;
  338. const int8_t *row_pos = kernel + row_shift;
  339. int32_t sum = 0;
  340. const uint16_t col_count = (kernel_x * kernel_y);
  341. row_shift += 1;
  342. for (int i = 0; i < col_count; i++)
  343. {
  344. sum += row_pos[i * input_ch] * col_pos[i * input_ch];
  345. }
  346. int64_t acc = sum;
  347. if (bias)
  348. {
  349. acc += *bias++;
  350. }
  351. result = arm_nn_requantize_s64(acc, REDUCE_MULTIPLIER(*output_mult), *output_shift++);
  352. output_mult++;
  353. result = MAX(result, output_activation_min);
  354. result = MIN(result, output_activation_max);
  355. *output++ = (int16_t)result;
  356. row_count--;
  357. }
  358. // clear counter and pointers
  359. col_buffer = col_buffer_start;
  360. }
  361. }
  362. /* Advance to the next batch */
  363. input += (input_x * input_y * input_ch);
  364. }
  365. #endif
  366. #else
  367. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  368. return arm_depthwise_conv_s16(ctx,
  369. dw_conv_params,
  370. quant_params,
  371. input_dims,
  372. input,
  373. filter_dims,
  374. kernel,
  375. bias_dims,
  376. bias,
  377. output_dims,
  378. output);
  379. #endif /* ARM_MATH_MVEI | ARM_MATH_DSP */
  380. /* Return to application */
  381. return ARM_CMSIS_NN_SUCCESS;
  382. }
  383. /**
  384. * @} end of NNConv group
  385. */