arm_depthwise_conv_s8_opt.c 17 KB

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