arm_convolve_s8.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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_convolve_s8.c
  21. * Description: s8 version of convolution using symmetric quantization.
  22. *
  23. * $Date: October 27, 2021
  24. * $Revision: V.2.0.7
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. /**
  32. * @ingroup groupNN
  33. */
  34. /**
  35. * @addtogroup NNConv
  36. * @{
  37. */
  38. /*
  39. * Basic s8 convolution function.
  40. *
  41. * Refer header file for details. Optimal use case for the DSP/MVE implementation is when input and output channels
  42. * are multiples of 4 or atleast greater than 4.
  43. *
  44. */
  45. arm_status arm_convolve_s8(const cmsis_nn_context *ctx,
  46. const cmsis_nn_conv_params *conv_params,
  47. const cmsis_nn_per_channel_quant_params *quant_params,
  48. const cmsis_nn_dims *input_dims,
  49. const q7_t *input_data,
  50. const cmsis_nn_dims *filter_dims,
  51. const q7_t *filter_data,
  52. const cmsis_nn_dims *bias_dims,
  53. const int32_t *bias_data,
  54. const cmsis_nn_dims *output_dims,
  55. q7_t *output_data)
  56. {
  57. (void)bias_dims;
  58. if (ctx->buf == NULL && arm_convolve_s8_get_buffer_size(input_dims, filter_dims) > 0)
  59. {
  60. return ARM_MATH_ARGUMENT_ERROR;
  61. }
  62. q15_t *buffer_a = (q15_t *)ctx->buf;
  63. const int32_t input_batches = input_dims->n;
  64. const uint16_t input_x = input_dims->w;
  65. const uint16_t input_y = input_dims->h;
  66. const uint16_t input_ch = input_dims->c;
  67. const uint16_t kernel_x = filter_dims->w;
  68. const uint16_t kernel_y = filter_dims->h;
  69. const uint16_t output_x = output_dims->w;
  70. const uint16_t output_y = output_dims->h;
  71. const uint16_t output_ch = output_dims->c;
  72. const uint16_t pad_x = conv_params->padding.w;
  73. const uint16_t pad_y = conv_params->padding.h;
  74. const uint16_t stride_x = conv_params->stride.w;
  75. const uint16_t stride_y = conv_params->stride.h;
  76. const int32_t input_offset = conv_params->input_offset;
  77. const int32_t out_offset = conv_params->output_offset;
  78. const int32_t out_activation_min = conv_params->activation.min;
  79. const int32_t out_activation_max = conv_params->activation.max;
  80. int32_t *output_mult = quant_params->multiplier;
  81. int32_t *output_shift = quant_params->shift;
  82. int i_batch;
  83. for (i_batch = 0; i_batch < input_batches; i_batch++)
  84. {
  85. #if defined(ARM_MATH_MVEI)
  86. /* Generate upto four columns from the input tensor a GEMM computation */
  87. q7_t *im2col_buf = (q7_t *)buffer_a;
  88. q7_t *out = output_data;
  89. int32_t buffer_fill_cnt = 0;
  90. int32_t padded = 0;
  91. const int32_t num_elem = kernel_x * kernel_y * input_ch;
  92. /* This part implements the im2col function */
  93. for (int i_out_y = 0; i_out_y < output_y; i_out_y++)
  94. {
  95. for (int i_out_x = 0; i_out_x < output_x; i_out_x++)
  96. {
  97. for (int i_ker_y = i_out_y * stride_y - pad_y; i_ker_y < i_out_y * stride_y - pad_y + kernel_y;
  98. i_ker_y++)
  99. {
  100. for (int i_ker_x = i_out_x * stride_x - pad_x; i_ker_x < i_out_x * stride_x - pad_x + kernel_x;
  101. 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. memset(im2col_buf, (int8_t)-input_offset, sizeof(q7_t) * input_ch);
  106. padded = 1;
  107. }
  108. else
  109. {
  110. arm_memcpy_q7(im2col_buf, input_data + (i_ker_y * input_x + i_ker_x) * input_ch, input_ch);
  111. }
  112. im2col_buf += input_ch;
  113. }
  114. }
  115. buffer_fill_cnt++;
  116. /* Computation is filed for every 4 columns */
  117. if (buffer_fill_cnt == 4 && (padded == 0))
  118. {
  119. buffer_fill_cnt = 0;
  120. for (int i_out_ch = 0; i_out_ch < output_ch; i_out_ch++)
  121. {
  122. int32_t sum_row;
  123. int32_t acc[4];
  124. (void)arm_nn_mat_mul_core_4x_s8(
  125. num_elem, num_elem, (q7_t *)buffer_a, filter_data + num_elem * i_out_ch, &sum_row, acc);
  126. int32x4_t s_offset = vdupq_n_s32(sum_row);
  127. int32x4_t res = vldrwq_s32(acc);
  128. s_offset = vmulq_n_s32(s_offset, input_offset);
  129. if (bias_data)
  130. {
  131. res = vaddq_n_s32(res, bias_data[i_out_ch]);
  132. }
  133. res = vaddq_s32(res, s_offset);
  134. res = arm_requantize_mve(res, output_mult[i_out_ch], output_shift[i_out_ch]);
  135. res = vaddq_n_s32(res, out_offset);
  136. res = vmaxq_s32(res, vdupq_n_s32(out_activation_min));
  137. res = vminq_s32(res, vdupq_n_s32(out_activation_max));
  138. const uint32x4_t scatter_offset = {0, output_ch, output_ch * 2, output_ch * 3};
  139. vstrbq_scatter_offset_s32(out, scatter_offset, res);
  140. out++;
  141. }
  142. out += (3 * output_ch);
  143. im2col_buf = (q7_t *)buffer_a;
  144. }
  145. else if (buffer_fill_cnt == 4 && (padded != 0))
  146. {
  147. buffer_fill_cnt = 0;
  148. out = arm_nn_mat_mult_s8(filter_data,
  149. (q7_t *)buffer_a,
  150. output_ch,
  151. 4,
  152. output_shift,
  153. output_mult,
  154. out_offset,
  155. input_offset,
  156. 0,
  157. out_activation_min,
  158. out_activation_max,
  159. num_elem,
  160. bias_data,
  161. out);
  162. im2col_buf = (q7_t *)buffer_a;
  163. padded = 0;
  164. }
  165. }
  166. }
  167. /* Handle left over columns */
  168. if (buffer_fill_cnt != 0)
  169. {
  170. out = arm_nn_mat_mult_s8(filter_data,
  171. (q7_t *)buffer_a,
  172. output_ch,
  173. buffer_fill_cnt,
  174. output_shift,
  175. output_mult,
  176. out_offset,
  177. input_offset,
  178. 0,
  179. out_activation_min,
  180. out_activation_max,
  181. num_elem,
  182. bias_data,
  183. out);
  184. }
  185. #elif defined(ARM_MATH_DSP)
  186. int32_t i_out_y, i_out_x, i_ker_y, i_ker_x;
  187. /* Generate two columns from the input tensor a GEMM computation */
  188. q15_t *two_column_buf = buffer_a;
  189. q7_t *out = output_data;
  190. /* This part implements the im2col function */
  191. for (i_out_y = 0; i_out_y < output_y; i_out_y++)
  192. {
  193. for (i_out_x = 0; i_out_x < output_x; i_out_x++)
  194. {
  195. for (i_ker_y = i_out_y * stride_y - pad_y; i_ker_y < i_out_y * stride_y - pad_y + kernel_y; i_ker_y++)
  196. {
  197. for (i_ker_x = i_out_x * stride_x - pad_x; i_ker_x < i_out_x * stride_x - pad_x + kernel_x;
  198. i_ker_x++)
  199. {
  200. if (i_ker_y < 0 || i_ker_y >= input_y || i_ker_x < 0 || i_ker_x >= input_x)
  201. {
  202. /* Filling 0 for out-of-bound paddings */
  203. memset(two_column_buf, 0, sizeof(q15_t) * input_ch);
  204. }
  205. else
  206. {
  207. /* Copying the pixel data to column */
  208. arm_q7_to_q15_with_offset(input_data + (i_ker_y * input_x + i_ker_x) * input_ch,
  209. two_column_buf,
  210. input_ch,
  211. input_offset);
  212. }
  213. two_column_buf += input_ch;
  214. }
  215. }
  216. /* Computation is filed for every 2 columns */
  217. if (two_column_buf == buffer_a + 2 * input_ch * kernel_y * kernel_x)
  218. {
  219. out = arm_nn_mat_mult_kernel_s8_s16(filter_data,
  220. buffer_a,
  221. output_ch,
  222. output_shift,
  223. output_mult,
  224. out_offset,
  225. out_activation_min,
  226. out_activation_max,
  227. input_ch * kernel_y * kernel_x,
  228. bias_data,
  229. out);
  230. /* counter reset */
  231. two_column_buf = buffer_a;
  232. }
  233. }
  234. }
  235. /* left-over because odd number of output pixels */
  236. if (two_column_buf != buffer_a)
  237. {
  238. const q7_t *ker_a = filter_data;
  239. int i;
  240. for (i = 0; i < output_ch; i++)
  241. {
  242. /* Load the accumulator with bias first */
  243. q31_t sum = 0;
  244. if (bias_data)
  245. {
  246. sum = bias_data[i];
  247. }
  248. /* Point to the beginning of the im2col buffer where the input is available as a rearranged column */
  249. const q15_t *ip_as_col = buffer_a;
  250. /* 4 multiply and accumulates are done in one loop. */
  251. uint16_t col_count = (input_ch * kernel_y * kernel_x) >> 2;
  252. while (col_count)
  253. {
  254. q31_t ker_a1, ker_a2;
  255. q31_t ip_b1, ip_b2;
  256. ker_a = read_and_pad(ker_a, &ker_a1, &ker_a2);
  257. ip_b1 = arm_nn_read_q15x2_ia(&ip_as_col);
  258. sum = __SMLAD(ker_a1, ip_b1, sum);
  259. ip_b2 = arm_nn_read_q15x2_ia(&ip_as_col);
  260. sum = __SMLAD(ker_a2, ip_b2, sum);
  261. col_count--;
  262. }
  263. /* Handle left over mac */
  264. col_count = input_ch * kernel_y * kernel_x & 0x3;
  265. while (col_count)
  266. {
  267. q7_t ker_a1 = *ker_a++;
  268. q15_t ip_b1 = *ip_as_col++;
  269. sum += ker_a1 * ip_b1;
  270. col_count--;
  271. }
  272. sum = arm_nn_requantize(sum, output_mult[i], output_shift[i]);
  273. sum += out_offset;
  274. sum = MAX(sum, out_activation_min);
  275. sum = MIN(sum, out_activation_max);
  276. *out++ = (q7_t)sum;
  277. }
  278. }
  279. #else
  280. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  281. (void)buffer_a;
  282. int32_t i_out_ch, i_out_y, i_out_x, i_input_ch, i_ker_y, i_ker_x;
  283. int32_t conv_out;
  284. for (i_out_ch = 0; i_out_ch < output_ch; i_out_ch++)
  285. {
  286. for (i_out_y = 0; i_out_y < output_y; i_out_y++)
  287. {
  288. for (i_out_x = 0; i_out_x < output_x; i_out_x++)
  289. {
  290. conv_out = 0;
  291. const int32_t base_idx_y = stride_y * i_out_y - pad_y;
  292. const int32_t base_idx_x = stride_x * i_out_x - pad_x;
  293. const int32_t ker_y_start = MAX(0, -base_idx_y);
  294. const int32_t ker_x_start = MAX(0, -base_idx_x);
  295. const int32_t ker_y_end = MIN(kernel_y, input_y - base_idx_y);
  296. const int32_t ker_x_end = MIN(kernel_x, input_x - base_idx_x);
  297. for (i_ker_y = ker_y_start; i_ker_y < ker_y_end; i_ker_y++)
  298. {
  299. for (i_ker_x = ker_x_start; i_ker_x < ker_x_end; i_ker_x++)
  300. {
  301. const int32_t in_row = base_idx_y + i_ker_y;
  302. const int32_t in_col = base_idx_x + i_ker_x;
  303. for (i_input_ch = 0; i_input_ch < input_ch; i_input_ch++)
  304. {
  305. conv_out +=
  306. (input_data[(in_row * input_x + in_col) * input_ch + i_input_ch] + input_offset) *
  307. filter_data[i_out_ch * input_ch * kernel_y * kernel_x +
  308. (i_ker_y * kernel_x + i_ker_x) * input_ch + i_input_ch];
  309. }
  310. }
  311. }
  312. if (bias_data)
  313. {
  314. conv_out += bias_data[i_out_ch];
  315. }
  316. conv_out = arm_nn_requantize(conv_out, output_mult[i_out_ch], output_shift[i_out_ch]);
  317. conv_out += out_offset;
  318. conv_out = MAX(conv_out, out_activation_min);
  319. conv_out = MIN(conv_out, out_activation_max);
  320. output_data[i_out_ch + (i_out_y * output_x + i_out_x) * output_ch] = (int8_t)conv_out;
  321. }
  322. }
  323. }
  324. #endif
  325. /* Advance to the next batch */
  326. input_data += (input_x * input_y * input_ch);
  327. output_data += (output_x * output_y * output_ch);
  328. }
  329. /* Return to application */
  330. return ARM_MATH_SUCCESS;
  331. }
  332. int32_t arm_convolve_s8_get_buffer_size(const cmsis_nn_dims *input_dims, const cmsis_nn_dims *filter_dims)
  333. {
  334. #if defined(ARM_MATH_MVEI)
  335. int32_t col_length = input_dims->c * filter_dims->w * filter_dims->h;
  336. // Get number of complete int16 lanes(multiple of 8) for given col_length. This is dependent on
  337. // implementation of arm_nn_mat_mult_s8
  338. col_length = (col_length + 7) / 8;
  339. // 4 -> number of im2col buffers, 8 -> 8 elements per Q register
  340. return 4 * col_length * 8 * (int32_t)sizeof(int8_t);
  341. #elif defined(ARM_MATH_DSP)
  342. return (2 * input_dims->c * filter_dims->w * filter_dims->h) * (int32_t)sizeof(int16_t);
  343. #else
  344. (void)input_dims;
  345. (void)filter_dims;
  346. return 0;
  347. #endif
  348. }
  349. /**
  350. * @} end of NNConv group
  351. */