arm_depthwise_conv_s8_opt.c 17 KB

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