arm_convolve_s8.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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: December 14, 2021
  24. * $Revision: V.2.1.0
  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. const int32_t dilation_x = conv_params->dilation.w;
  93. const int32_t dilation_y = conv_params->dilation.h;
  94. /* This part implements the im2col function */
  95. for (int i_out_y = 0; i_out_y < output_y; i_out_y++)
  96. {
  97. for (int i_out_x = 0; i_out_x < output_x; i_out_x++)
  98. {
  99. const int32_t base_idx_x = stride_x * i_out_x - pad_x;
  100. const int32_t base_idx_y = stride_y * i_out_y - pad_y;
  101. for (int32_t i_ker_y = 0; i_ker_y < kernel_y; i_ker_y++)
  102. {
  103. for (int32_t i_ker_x = 0; i_ker_x < kernel_x; i_ker_x++)
  104. {
  105. const int32_t k_y = base_idx_y + dilation_y * i_ker_y;
  106. const int32_t k_x = base_idx_x + dilation_x * i_ker_x;
  107. if (k_y < 0 || k_y >= input_y || k_x < 0 || k_x >= input_x)
  108. {
  109. memset(im2col_buf, (int8_t)-input_offset, sizeof(q7_t) * input_ch);
  110. padded = 1;
  111. }
  112. else
  113. {
  114. arm_memcpy_q7(im2col_buf, input_data + (k_y * input_x + k_x) * input_ch, input_ch);
  115. }
  116. im2col_buf += input_ch;
  117. }
  118. }
  119. buffer_fill_cnt++;
  120. /* Computation is filed for every 4 columns */
  121. if (buffer_fill_cnt == 4 && (padded == 0))
  122. {
  123. buffer_fill_cnt = 0;
  124. out = arm_nn_mat_mul_core_4x_s8(num_elem,
  125. num_elem,
  126. (q7_t *)buffer_a,
  127. filter_data,
  128. output_ch,
  129. conv_params,
  130. quant_params,
  131. bias_data,
  132. out);
  133. im2col_buf = (q7_t *)buffer_a;
  134. }
  135. else if (buffer_fill_cnt == 4 && (padded != 0))
  136. {
  137. buffer_fill_cnt = 0;
  138. out = arm_nn_mat_mult_s8(filter_data,
  139. (q7_t *)buffer_a,
  140. output_ch,
  141. 4,
  142. output_shift,
  143. output_mult,
  144. out_offset,
  145. input_offset,
  146. 0,
  147. out_activation_min,
  148. out_activation_max,
  149. num_elem,
  150. bias_data,
  151. out);
  152. im2col_buf = (q7_t *)buffer_a;
  153. padded = 0;
  154. }
  155. }
  156. }
  157. /* Handle left over columns */
  158. if (buffer_fill_cnt != 0)
  159. {
  160. out = arm_nn_mat_mult_s8(filter_data,
  161. (q7_t *)buffer_a,
  162. output_ch,
  163. buffer_fill_cnt,
  164. output_shift,
  165. output_mult,
  166. out_offset,
  167. input_offset,
  168. 0,
  169. out_activation_min,
  170. out_activation_max,
  171. num_elem,
  172. bias_data,
  173. out);
  174. }
  175. #else // #if defined(ARM_MATH_MVEI)
  176. const uint16_t dilation_x = conv_params->dilation.w;
  177. const uint16_t dilation_y = conv_params->dilation.h;
  178. int32_t i_out_y, i_out_x, i_ker_y, i_ker_x;
  179. /* Generate two columns from the input tensor a GEMM computation */
  180. q15_t *two_column_buf = buffer_a;
  181. q7_t *out = output_data;
  182. /* This part implements the im2col function */
  183. for (i_out_y = 0; i_out_y < output_y; i_out_y++)
  184. {
  185. for (i_out_x = 0; i_out_x < output_x; i_out_x++)
  186. {
  187. const int32_t base_idx_y = stride_y * i_out_y - pad_y;
  188. const int32_t base_idx_x = stride_x * i_out_x - pad_x;
  189. for (i_ker_y = 0; i_ker_y < kernel_y; i_ker_y++)
  190. {
  191. for (i_ker_x = 0; i_ker_x < kernel_x; i_ker_x++)
  192. {
  193. const int32_t k_y = base_idx_y + dilation_y * i_ker_y;
  194. const int32_t k_x = base_idx_x + dilation_x * i_ker_x;
  195. if (k_y < 0 || k_y >= input_y || k_x < 0 || k_x >= input_x)
  196. {
  197. /* Filling 0 for out-of-bound paddings */
  198. memset(two_column_buf, 0, sizeof(q15_t) * input_ch);
  199. }
  200. else
  201. {
  202. /* Copying the pixel data to column */
  203. arm_q7_to_q15_with_offset(
  204. input_data + (k_y * input_x + k_x) * input_ch, two_column_buf, input_ch, input_offset);
  205. }
  206. two_column_buf += input_ch;
  207. }
  208. }
  209. /* Computation is filed for every 2 columns */
  210. if (two_column_buf == buffer_a + 2 * input_ch * kernel_y * kernel_x)
  211. {
  212. out = arm_nn_mat_mult_kernel_s8_s16(filter_data,
  213. buffer_a,
  214. output_ch,
  215. output_shift,
  216. output_mult,
  217. out_offset,
  218. out_activation_min,
  219. out_activation_max,
  220. input_ch * kernel_y * kernel_x,
  221. bias_data,
  222. out);
  223. /* counter reset */
  224. two_column_buf = buffer_a;
  225. }
  226. }
  227. }
  228. /* left-over because odd number of output pixels */
  229. if (two_column_buf != buffer_a)
  230. {
  231. const q7_t *ker_a = filter_data;
  232. int i;
  233. for (i = 0; i < output_ch; i++)
  234. {
  235. /* Load the accumulator with bias first */
  236. q31_t sum = 0;
  237. if (bias_data)
  238. {
  239. sum = bias_data[i];
  240. }
  241. /* Point to the beginning of the im2col buffer where the input is available as a rearranged column */
  242. const q15_t *ip_as_col = buffer_a;
  243. /* 4 multiply and accumulates are done in one loop. */
  244. #if defined(ARM_MATH_DSP)
  245. uint16_t col_count = (input_ch * kernel_y * kernel_x) >> 2;
  246. while (col_count)
  247. {
  248. q31_t ker_a1, ker_a2;
  249. q31_t ip_b1, ip_b2;
  250. ker_a = read_and_pad(ker_a, &ker_a1, &ker_a2);
  251. ip_b1 = arm_nn_read_q15x2_ia(&ip_as_col);
  252. sum = __SMLAD(ker_a1, ip_b1, sum);
  253. ip_b2 = arm_nn_read_q15x2_ia(&ip_as_col);
  254. sum = __SMLAD(ker_a2, ip_b2, sum);
  255. col_count--;
  256. }
  257. /* Handle left over mac */
  258. col_count = input_ch * kernel_y * kernel_x & 0x3;
  259. #else
  260. uint16_t col_count = input_ch * kernel_y * kernel_x;
  261. #endif
  262. while (col_count)
  263. {
  264. q7_t ker_a1 = *ker_a++;
  265. q15_t ip_b1 = *ip_as_col++;
  266. sum += ker_a1 * ip_b1;
  267. col_count--;
  268. }
  269. sum = arm_nn_requantize(sum, output_mult[i], output_shift[i]);
  270. sum += out_offset;
  271. sum = MAX(sum, out_activation_min);
  272. sum = MIN(sum, out_activation_max);
  273. *out++ = (q7_t)sum;
  274. }
  275. }
  276. #endif // #if defined(ARM_MATH_MVEI)
  277. /* Advance to the next batch */
  278. input_data += (input_x * input_y * input_ch);
  279. output_data += (output_x * output_y * output_ch);
  280. }
  281. /* Return to application */
  282. return ARM_MATH_SUCCESS;
  283. }
  284. int32_t arm_convolve_s8_get_buffer_size(const cmsis_nn_dims *input_dims, const cmsis_nn_dims *filter_dims)
  285. {
  286. #if defined(ARM_MATH_MVEI)
  287. int32_t col_length = input_dims->c * filter_dims->w * filter_dims->h;
  288. // Get number of complete int16 lanes(multiple of 8) for given col_length. This is dependent on
  289. // implementation of arm_nn_mat_mult_s8
  290. col_length = (col_length + 7) / 8;
  291. // 4 -> number of im2col buffers, 8 -> 8 elements per Q register
  292. return 4 * col_length * 8 * (int32_t)sizeof(int8_t);
  293. #else
  294. return (2 * input_dims->c * filter_dims->w * filter_dims->h) * (int32_t)sizeof(int16_t);
  295. #endif
  296. }
  297. /**
  298. * @} end of NNConv group
  299. */