arm_depthwise_conv_s8_opt.c 17 KB

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