arm_depthwise_conv_s8_opt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2010-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_s8_opt.c
  21. * Description: Optimized s8 depthwise separable convolution function for
  22. * channel multiplier of 1.
  23. *
  24. * $Date: 22 March 2023
  25. * $Revision: V.3.5.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 s8 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_s8_opt(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 int8_t *input,
  50. const cmsis_nn_dims *filter_dims,
  51. const int8_t *kernel,
  52. const cmsis_nn_dims *bias_dims,
  53. const int32_t *bias,
  54. const cmsis_nn_dims *output_dims,
  55. int8_t *output)
  56. {
  57. const int32_t input_ch = input_dims->c;
  58. const int32_t output_ch = output_dims->c;
  59. /* Check depth multiplier is 1 */
  60. if (input_ch != output_ch)
  61. {
  62. return ARM_CMSIS_NN_ARG_ERROR;
  63. }
  64. if (ctx->buf == NULL && arm_depthwise_conv_s8_opt_get_buffer_size(input_dims, filter_dims) > 0)
  65. {
  66. return ARM_CMSIS_NN_ARG_ERROR;
  67. }
  68. #ifdef ARM_MATH_DSP
  69. (void)bias_dims;
  70. const int32_t input_x = input_dims->w;
  71. const int32_t input_y = input_dims->h;
  72. const int32_t kernel_x = filter_dims->w;
  73. const int32_t kernel_y = filter_dims->h;
  74. const int32_t pad_x = dw_conv_params->padding.w;
  75. const int32_t pad_y = dw_conv_params->padding.h;
  76. const int32_t stride_x = dw_conv_params->stride.w;
  77. const int32_t stride_y = dw_conv_params->stride.h;
  78. const int32_t *output_shift = quant_params->shift;
  79. const int32_t *output_mult = quant_params->multiplier;
  80. const int32_t output_x = output_dims->w;
  81. const int32_t output_y = output_dims->h;
  82. const int32_t output_offset = dw_conv_params->output_offset;
  83. const int32_t input_offset = dw_conv_params->input_offset;
  84. const int32_t output_activation_min = dw_conv_params->activation.min;
  85. const int32_t output_activation_max = dw_conv_params->activation.max;
  86. int16_t *buffer_a = (int16_t *)ctx->buf;
  87. #ifdef ARM_MATH_MVEI
  88. /* Generate two columns from the input tensor */
  89. int8_t *lhs_buffer = (int8_t *)buffer_a;
  90. int8_t *out = output;
  91. int buffer_count = 0;
  92. const int32_t kernel_size = kernel_x * kernel_y;
  93. const int32_t ch_loop = (input_ch + (CH_IN_BLOCK_MVE - 1)) / CH_IN_BLOCK_MVE;
  94. int32_t remaining_ch = output_ch;
  95. int32_t active_ch = MIN(CH_IN_BLOCK_MVE, remaining_ch);
  96. remaining_ch -= CH_IN_BLOCK_MVE;
  97. for (int i_ch = 0; i_ch < ch_loop; i_ch++)
  98. {
  99. out = output + i_ch * CH_IN_BLOCK_MVE;
  100. const int8_t *input_slice = input + (i_ch * CH_IN_BLOCK_MVE);
  101. for (int i_out_y = 0, base_idx_y = -pad_y; i_out_y < output_y; base_idx_y += stride_y, i_out_y++)
  102. {
  103. for (int i_out_x = 0, base_idx_x = -pad_x; i_out_x < output_x; base_idx_x += stride_x, i_out_x++)
  104. {
  105. for (int i_ker_y = base_idx_y; i_ker_y < base_idx_y + kernel_y; i_ker_y++)
  106. {
  107. for (int i_ker_x = base_idx_x; i_ker_x < base_idx_x + kernel_x; i_ker_x++)
  108. {
  109. if (i_ker_y < 0 || i_ker_y >= input_y || i_ker_x < 0 || i_ker_x >= input_x)
  110. {
  111. arm_memset_s8(lhs_buffer, (int8_t)-input_offset, (uint32_t)active_ch);
  112. }
  113. else
  114. {
  115. arm_memcpy_s8(lhs_buffer,
  116. input_slice + (i_ker_y * input_x + i_ker_x) * input_ch,
  117. (uint32_t)active_ch);
  118. }
  119. lhs_buffer += CH_IN_BLOCK_MVE;
  120. }
  121. }
  122. buffer_count++;
  123. if (buffer_count == 4)
  124. {
  125. const int32_t block_offset = i_ch * CH_IN_BLOCK_MVE;
  126. lhs_buffer = (int8_t *)buffer_a;
  127. arm_nn_depthwise_conv_nt_t_s8(lhs_buffer,
  128. kernel + block_offset,
  129. input_offset,
  130. active_ch,
  131. input_ch,
  132. output_shift + block_offset,
  133. output_mult + block_offset,
  134. output_offset,
  135. output_activation_min,
  136. output_activation_max,
  137. kernel_size,
  138. bias + block_offset,
  139. out);
  140. out += (4 * input_ch);
  141. buffer_count = 0;
  142. }
  143. }
  144. }
  145. /* Handle left over buffers */
  146. lhs_buffer = (int8_t *)buffer_a;
  147. int8_t *out_base = out;
  148. for (int i_buf = 0; i_buf < buffer_count; i_buf++)
  149. {
  150. int32_t loop_count = (active_ch + 3) / 4;
  151. int32_t num_ch_to_process = active_ch;
  152. out = out_base + (i_buf * input_ch);
  153. for (int i_loop_cnt = 0, offset = i_ch * CH_IN_BLOCK_MVE; i_loop_cnt < loop_count;
  154. num_ch_to_process -= 4, offset += 4, i_loop_cnt++)
  155. {
  156. const int8_t *col_0 = lhs_buffer + (kernel_size * CH_IN_BLOCK_MVE * i_buf) + (i_loop_cnt * 4);
  157. const int8_t *row_0 = kernel + offset;
  158. int32x4_t out_0 = vdupq_n_s32(0);
  159. if (bias)
  160. {
  161. out_0 = vldrwq_s32(&bias[offset]);
  162. }
  163. for (int i_ker = 0; i_ker < kernel_size; i_ker++)
  164. {
  165. const int32x4_t ker_0 = vldrbq_s32(row_0);
  166. int32x4_t ip_0 = vldrbq_s32(col_0);
  167. ip_0 = vaddq_n_s32(ip_0, input_offset);
  168. out_0 += vmulq_s32(ip_0, ker_0);
  169. col_0 += CH_IN_BLOCK_MVE;
  170. row_0 += input_ch;
  171. }
  172. const int32x4_t mult = vldrwq_s32(&output_mult[offset]);
  173. const int32x4_t shift = vldrwq_s32(&output_shift[offset]);
  174. out_0 = arm_requantize_mve_32x4(out_0, mult, shift);
  175. out_0 = vaddq_n_s32(out_0, output_offset);
  176. out_0 = vmaxq_s32(out_0, vdupq_n_s32(output_activation_min));
  177. out_0 = vminq_s32(out_0, vdupq_n_s32(output_activation_max));
  178. mve_pred16_t p = vctp32q((uint32_t)num_ch_to_process);
  179. vstrbq_p_s32(out, out_0, p);
  180. out += 4;
  181. }
  182. }
  183. buffer_count = 0;
  184. active_ch = MIN(CH_IN_BLOCK_MVE, remaining_ch);
  185. remaining_ch -= CH_IN_BLOCK_MVE;
  186. }
  187. #else // ARM_MATH_DSP
  188. /* Run the following code in cores using DSP extension */
  189. int16_t *const col_buffer_start = buffer_a;
  190. int16_t *col_buffer = col_buffer_start;
  191. const int32_t *const bias_start_pos = bias;
  192. const int32_t *const out_mult_start_pos = output_mult;
  193. const int32_t *const out_shift_start_pos = output_shift;
  194. uint16_t row_count;
  195. uint16_t row_shift;
  196. for (int i_out_y = 0; i_out_y < output_y; i_out_y++)
  197. {
  198. const int16_t base_idx_y = (i_out_y * stride_y) - pad_y;
  199. for (int i_out_x = 0; i_out_x < output_x; i_out_x++)
  200. {
  201. const int16_t base_idx_x = (i_out_x * stride_x) - pad_x;
  202. /* Out of bounds is only considered for the y axis as it provides a contiguous zero'ing opportunity than
  203. along the x axis */
  204. const int ker_y_start = MAX(0, -base_idx_y);
  205. /* Condition for kernel end dimension: (base_idx_y + ker_y_end) < input_y */
  206. const int ker_y_end = MIN(kernel_y, input_y - base_idx_y);
  207. int32_t index = 0;
  208. if (ker_y_start != 0)
  209. {
  210. memset(&col_buffer[index], 0, (kernel_x * input_ch) * ker_y_start * sizeof(int16_t));
  211. index += (kernel_x * input_ch) * ker_y_start;
  212. }
  213. for (int i_ker_y = ker_y_start; i_ker_y < ker_y_end; i_ker_y++)
  214. {
  215. const int32_t idx_y = base_idx_y + i_ker_y;
  216. for (int i_ker_x = 0; i_ker_x < kernel_x; i_ker_x++)
  217. {
  218. const int32_t idx_x = base_idx_x + i_ker_x;
  219. if (idx_x < 0 || idx_x >= input_x)
  220. {
  221. memset(&col_buffer[index], 0, input_ch * sizeof(int16_t));
  222. }
  223. else
  224. {
  225. arm_q7_to_q15_with_offset((int8_t *)input + (idx_y * input_x + idx_x) * input_ch,
  226. &col_buffer[index],
  227. input_ch,
  228. (int16_t)input_offset);
  229. }
  230. index += input_ch;
  231. }
  232. }
  233. const int diff = kernel_y - ker_y_end;
  234. if (diff != 0)
  235. {
  236. memset(&col_buffer[index], 0, (kernel_x * input_ch) * diff * sizeof(int16_t));
  237. }
  238. row_count = output_ch / 4;
  239. row_shift = 0;
  240. bias = bias_start_pos;
  241. output_mult = out_mult_start_pos;
  242. output_shift = out_shift_start_pos;
  243. while (row_count)
  244. {
  245. int32_t sum = 0;
  246. int32_t sum_2 = 0;
  247. int32_t sum_3 = 0;
  248. int32_t sum_4 = 0;
  249. if (bias)
  250. {
  251. sum = *bias++;
  252. sum_2 = *bias++;
  253. sum_3 = *bias++;
  254. sum_4 = *bias++;
  255. }
  256. uint16_t col_count = (kernel_x * kernel_y) / 2;
  257. int16_t *col_pos = col_buffer_start + row_shift;
  258. const int8_t *row_pos = kernel + row_shift;
  259. row_shift += 4;
  260. while (col_count)
  261. {
  262. /* General idea is to read 4 + 4 (input, kernel) pair and re-arrange them in the right order to
  263. use in a SMLAD instruction . One run of this loop produces 4 partial outputs with 8 MACs. */
  264. /* Note: variable names can be improved here to align with rows and columns. */
  265. int32_t ip_a1, ip_a2, ip_b1, ip_b2, op_a, op_b, op_c;
  266. /* Read 4 weights */
  267. ip_b1 = arm_nn_read_s8x4(row_pos);
  268. ip_a1 = arm_nn_read_s8x4(row_pos + input_ch);
  269. op_a = arm_nn_read_s16x2(col_pos);
  270. op_b = arm_nn_read_s16x2(col_pos + input_ch);
  271. ip_a2 = SXTB16(ip_b1);
  272. ip_b1 = SXTB16(ROR(ip_b1, 8));
  273. ip_b2 = SXTB16(ip_a1);
  274. ip_a1 = SXTB16(ROR(ip_a1, 8));
  275. op_c = PKHBT(op_b, op_a, 16);
  276. op_a = PKHTB(op_b, op_a, 16);
  277. op_b = PKHBT(ip_b2, ip_a2, 16);
  278. sum = SMLAD(op_c, op_b, sum);
  279. op_b = PKHBT(ip_b1, ip_a1, 16);
  280. sum_2 = SMLAD(op_a, op_b, sum_2);
  281. op_a = arm_nn_read_s16x2(col_pos + 2);
  282. op_b = arm_nn_read_s16x2(col_pos + input_ch + 2);
  283. op_c = PKHBT(op_b, op_a, 16);
  284. op_a = PKHTB(op_b, op_a, 16);
  285. op_b = PKHTB(ip_a2, ip_b2, 16);
  286. sum_3 = SMLAD(op_c, op_b, sum_3);
  287. op_b = PKHTB(ip_a1, ip_b1, 16);
  288. sum_4 = SMLAD(op_a, op_b, sum_4);
  289. row_pos += input_ch << 1;
  290. col_pos += input_ch << 1;
  291. col_count--;
  292. }
  293. col_count = (kernel_x * kernel_y) & 0x1;
  294. while (col_count)
  295. {
  296. sum += row_pos[0] * col_pos[0];
  297. sum_2 += row_pos[1] * col_pos[1];
  298. sum_3 += row_pos[2] * col_pos[2];
  299. sum_4 += row_pos[3] * col_pos[3];
  300. row_pos += input_ch;
  301. col_pos += input_ch;
  302. col_count--;
  303. }
  304. sum = arm_nn_requantize(sum, *output_mult++, *output_shift++);
  305. sum += output_offset;
  306. sum = MAX(sum, output_activation_min);
  307. sum = MIN(sum, output_activation_max);
  308. *output++ = (int8_t)sum;
  309. sum_2 = arm_nn_requantize(sum_2, *output_mult++, *output_shift++);
  310. sum_2 += output_offset;
  311. sum_2 = MAX(sum_2, output_activation_min);
  312. sum_2 = MIN(sum_2, output_activation_max);
  313. *output++ = (int8_t)sum_2;
  314. sum_3 = arm_nn_requantize(sum_3, *output_mult++, *output_shift++);
  315. sum_3 += output_offset;
  316. sum_3 = MAX(sum_3, output_activation_min);
  317. sum_3 = MIN(sum_3, output_activation_max);
  318. *output++ = (int8_t)sum_3;
  319. sum_4 = arm_nn_requantize(sum_4, *output_mult++, *output_shift++);
  320. sum_4 += output_offset;
  321. sum_4 = MAX(sum_4, output_activation_min);
  322. sum_4 = MIN(sum_4, output_activation_max);
  323. *output++ = (int8_t)sum_4;
  324. row_count--;
  325. }
  326. row_count = output_ch & 0x3;
  327. while (row_count)
  328. {
  329. int16_t *col_pos = col_buffer_start + row_shift;
  330. const int8_t *row_pos = kernel + row_shift;
  331. int32_t sum = 0;
  332. if (bias)
  333. {
  334. sum = *bias++;
  335. }
  336. const uint16_t col_count = (kernel_x * kernel_y);
  337. row_shift += 1;
  338. for (int i = 0; i < col_count; i++)
  339. {
  340. sum += row_pos[i * input_ch] * col_pos[i * input_ch];
  341. }
  342. sum = arm_nn_requantize(sum, *output_mult++, *output_shift++);
  343. sum += output_offset;
  344. sum = MAX(sum, output_activation_min);
  345. sum = MIN(sum, output_activation_max);
  346. *output++ = (int8_t)sum;
  347. row_count--;
  348. }
  349. // clear counter and pointers
  350. col_buffer = col_buffer_start;
  351. }
  352. }
  353. #endif
  354. #else
  355. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  356. return arm_depthwise_conv_s8(ctx,
  357. dw_conv_params,
  358. quant_params,
  359. input_dims,
  360. input,
  361. filter_dims,
  362. kernel,
  363. bias_dims,
  364. bias,
  365. output_dims,
  366. output);
  367. #endif /* ARM_MATH_MVEI | ARM_MATH_DSP */
  368. /* Return to application */
  369. return ARM_CMSIS_NN_SUCCESS;
  370. }
  371. /**
  372. * @} end of NNConv group
  373. */