arm_convolve_HWC_q7_fast.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (C) 2010-2018 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_convolve_HWC_q7_fast.c
  21. * Description: Fast Q7 version of convolution
  22. *
  23. * $Date: 17. January 2018
  24. * $Revision: V.1.0.0
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_math.h"
  30. #include "arm_nnfunctions.h"
  31. /**
  32. * @ingroup groupNN
  33. */
  34. /**
  35. * @addtogroup NNConv
  36. * @{
  37. */
  38. /**
  39. * @brief Fast Q7 convolution function
  40. * @param[in] Im_in pointer to input tensor
  41. * @param[in] dim_im_in input tensor dimention
  42. * @param[in] ch_im_in number of input tensor channels
  43. * @param[in] wt pointer to kernel weights
  44. * @param[in] ch_im_out number of filters, i.e., output tensor channels
  45. * @param[in] dim_kernel filter kernel size
  46. * @param[in] padding padding sizes
  47. * @param[in] stride convolution stride
  48. * @param[in] bias pointer to bias
  49. * @param[in] bias_shift amount of left-shift for bias
  50. * @param[in] out_shift amount of right-shift for output
  51. * @param[in,out] Im_out pointer to output tensor
  52. * @param[in] dim_im_out output tensor dimension
  53. * @param[in,out] bufferA pointer to buffer space for input
  54. * @param[in,out] bufferB pointer to buffer space for output
  55. * @return The function returns either
  56. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  57. *
  58. * @details
  59. *
  60. * <b>Buffer size:</b>
  61. *
  62. * bufferA size: 2*ch_im_in*dim_kernel*dim_kernel
  63. *
  64. * bufferB size: 0
  65. *
  66. * <b>Input dimension constraints:</b>
  67. *
  68. * ch_im_in is multiple of 4 ( because of the SIMD32 read and swap )
  69. *
  70. * ch_im_out is multipe of 2 ( bacause 2x2 mat_mult kernel )
  71. *
  72. * The im2col converts the Q7 tensor input into Q15 column, which is stored in
  73. * bufferA. There is reordering happenning during this im2col process with
  74. * arm_q7_to_q15_reordered_no_shift. For every four elements, the second and
  75. * third elements are swapped.
  76. *
  77. * The computation kernel arm_nn_mat_mult_kernel_q7_q15_reordered does the
  78. * GEMM computation with the reordered columns.
  79. *
  80. * To speed-up the determination of the padding condition, we split the
  81. * computation into 3x3 parts, i.e., {top, mid, bottom} X {left, mid, right}.
  82. * This reduces the total number of boundary condition checks and improves
  83. * the data copying performance.
  84. */
  85. arm_status
  86. arm_convolve_HWC_q7_fast(const q7_t * Im_in,
  87. const uint16_t dim_im_in,
  88. const uint16_t ch_im_in,
  89. const q7_t * wt,
  90. const uint16_t ch_im_out,
  91. const uint16_t dim_kernel,
  92. const uint16_t padding,
  93. const uint16_t stride,
  94. const q7_t * bias,
  95. const uint16_t bias_shift,
  96. const uint16_t out_shift,
  97. q7_t * Im_out,
  98. const uint16_t dim_im_out,
  99. q15_t * bufferA,
  100. q7_t * bufferB)
  101. {
  102. (void)bufferB;
  103. #if defined (ARM_MATH_DSP)
  104. /* Run the following code for Cortex-M4 and Cortex-M7 */
  105. int16_t i_out_y, i_out_x, i_ker_y, i_ker_x;
  106. /*
  107. * Here we use bufferA as q15_t internally as computation are done with q15_t level
  108. * im2col are done to output in q15_t format from q7_t input
  109. */
  110. q15_t *pBuffer = bufferA;
  111. q7_t *pOut = Im_out;
  112. if (ch_im_in % 4 != 0 || ch_im_out % 2 != 0)
  113. {
  114. /* check if the input dimension meets the constraints */
  115. return ARM_MATH_SIZE_MISMATCH;
  116. }
  117. /*
  118. * Here we split the entire matrix into three regions depending on the padding situation
  119. * Top: i_out_y from 0 to padding - 1
  120. * Middle: i_out_y from padding to dim_im_out-padding-1
  121. * Bottom: i_out_y from dim_im_out-padding to dim_im_out-1
  122. */
  123. /* top part */
  124. for (i_out_y = 0; i_out_y < padding; i_out_y++)
  125. {
  126. for (i_out_x = 0; i_out_x < dim_im_out; i_out_x++)
  127. {
  128. /* This part implements the im2col function */
  129. for (i_ker_y = i_out_y * stride - padding; i_ker_y < i_out_y * stride - padding + dim_kernel; i_ker_y++)
  130. {
  131. for (i_ker_x = i_out_x * stride - padding; i_ker_x < i_out_x * stride - padding + dim_kernel; i_ker_x++)
  132. {
  133. if (i_ker_y < 0 || i_ker_y >= dim_im_in || i_ker_x < 0 || i_ker_x >= dim_im_in)
  134. {
  135. /* arm_fill_q15(0, pBuffer, ch_im_in); */
  136. memset(pBuffer, 0, sizeof(q15_t)*ch_im_in);
  137. } else
  138. {
  139. arm_q7_to_q15_reordered_no_shift
  140. ((q7_t *) Im_in + (i_ker_y * dim_im_in + i_ker_x) * ch_im_in, pBuffer, ch_im_in);
  141. }
  142. pBuffer += ch_im_in;
  143. }
  144. }
  145. if (pBuffer == bufferA + 2 * ch_im_in * dim_kernel * dim_kernel)
  146. {
  147. pOut =
  148. arm_nn_mat_mult_kernel_q7_q15_reordered(wt,
  149. bufferA,
  150. ch_im_out,
  151. ch_im_in
  152. *
  153. dim_kernel * dim_kernel, bias_shift, out_shift, bias, pOut);
  154. /* counter reset */
  155. pBuffer = bufferA;
  156. }
  157. }
  158. }
  159. /* middle part, here we also divide the x into left, mid and right */
  160. for (; i_out_y < dim_im_out - padding; i_out_y++)
  161. {
  162. /* left part */
  163. for (i_out_x = 0; i_out_x < padding; i_out_x++)
  164. {
  165. /* This part implements the im2col function */
  166. for (i_ker_y = i_out_y * stride - padding; i_ker_y < i_out_y * stride - padding + dim_kernel; i_ker_y++)
  167. {
  168. for (i_ker_x = i_out_x * stride - padding; i_ker_x < i_out_x * stride - padding + dim_kernel; i_ker_x++)
  169. {
  170. if (i_ker_x < 0 || i_ker_x >= dim_im_in)
  171. {
  172. /* arm_fill_q15(0, pBuffer, ch_im_in); */
  173. memset(pBuffer, 0, sizeof(q15_t)*ch_im_in);
  174. } else
  175. {
  176. arm_q7_to_q15_reordered_no_shift
  177. ((q7_t *) Im_in + (i_ker_y * dim_im_in + i_ker_x) * ch_im_in, pBuffer, ch_im_in);
  178. }
  179. pBuffer += ch_im_in;
  180. }
  181. }
  182. if (pBuffer == bufferA + 2 * ch_im_in * dim_kernel * dim_kernel)
  183. {
  184. pOut =
  185. arm_nn_mat_mult_kernel_q7_q15_reordered(wt,
  186. bufferA,
  187. ch_im_out,
  188. ch_im_in
  189. *
  190. dim_kernel * dim_kernel, bias_shift, out_shift, bias, pOut);
  191. /* counter reset */
  192. pBuffer = bufferA;
  193. }
  194. }
  195. /* mid part */
  196. for (; i_out_x < dim_im_out - padding; i_out_x++)
  197. {
  198. /* This part implements the im2col function */
  199. for (i_ker_y = i_out_y * stride - padding; i_ker_y < i_out_y * stride - padding + dim_kernel; i_ker_y++)
  200. {
  201. arm_q7_to_q15_reordered_no_shift((q7_t *) Im_in
  202. +
  203. (i_ker_y *
  204. dim_im_in +
  205. i_out_x *
  206. stride - padding) * ch_im_in, pBuffer, ch_im_in * dim_kernel);
  207. pBuffer += ch_im_in * dim_kernel;
  208. }
  209. if (pBuffer == bufferA + 2 * ch_im_in * dim_kernel * dim_kernel)
  210. {
  211. pOut =
  212. arm_nn_mat_mult_kernel_q7_q15_reordered(wt,
  213. bufferA,
  214. ch_im_out,
  215. ch_im_in
  216. *
  217. dim_kernel * dim_kernel, bias_shift, out_shift, bias, pOut);
  218. /* counter reset */
  219. pBuffer = bufferA;
  220. }
  221. }
  222. /* right part */
  223. for (; i_out_x < dim_im_out; i_out_x++)
  224. {
  225. /* This part implements the im2col function */
  226. for (i_ker_y = i_out_y * stride - padding; i_ker_y < i_out_y * stride - padding + dim_kernel; i_ker_y++)
  227. {
  228. for (i_ker_x = i_out_x * stride - padding; i_ker_x < i_out_x * stride - padding + dim_kernel; i_ker_x++)
  229. {
  230. if (i_ker_x < 0 || i_ker_x >= dim_im_in)
  231. {
  232. /* arm_fill_q15(0, pBuffer, ch_im_in); */
  233. memset(pBuffer, 0, sizeof(q15_t)*ch_im_in);
  234. } else
  235. {
  236. arm_q7_to_q15_reordered_no_shift
  237. ((q7_t *) Im_in + (i_ker_y * dim_im_in + i_ker_x) * ch_im_in, pBuffer, ch_im_in);
  238. }
  239. pBuffer += ch_im_in;
  240. }
  241. }
  242. if (pBuffer == bufferA + 2 * ch_im_in * dim_kernel * dim_kernel)
  243. {
  244. pOut =
  245. arm_nn_mat_mult_kernel_q7_q15_reordered(wt,
  246. bufferA,
  247. ch_im_out,
  248. ch_im_in
  249. *
  250. dim_kernel * dim_kernel, bias_shift, out_shift, bias, pOut);
  251. /* counter reset */
  252. pBuffer = bufferA;
  253. }
  254. }
  255. }
  256. for (; i_out_y < dim_im_out; i_out_y++)
  257. {
  258. for (i_out_x = 0; i_out_x < dim_im_out; i_out_x++)
  259. {
  260. /* This part implements the im2col function */
  261. for (i_ker_y = i_out_y * stride - padding; i_ker_y < i_out_y * stride - padding + dim_kernel; i_ker_y++)
  262. {
  263. for (i_ker_x = i_out_x * stride - padding; i_ker_x < i_out_x * stride - padding + dim_kernel; i_ker_x++)
  264. {
  265. if (i_ker_y < 0 || i_ker_y >= dim_im_in || i_ker_x < 0 || i_ker_x >= dim_im_in)
  266. {
  267. /* arm_fill_q15(0, pBuffer, ch_im_in); */
  268. memset(pBuffer, 0, sizeof(q15_t)*ch_im_in);
  269. } else
  270. {
  271. arm_q7_to_q15_reordered_no_shift
  272. ((q7_t *) Im_in + (i_ker_y * dim_im_in + i_ker_x) * ch_im_in, pBuffer, ch_im_in);
  273. }
  274. pBuffer += ch_im_in;
  275. }
  276. }
  277. if (pBuffer == bufferA + 2 * ch_im_in * dim_kernel * dim_kernel)
  278. {
  279. pOut =
  280. arm_nn_mat_mult_kernel_q7_q15_reordered(wt,
  281. bufferA,
  282. ch_im_out,
  283. ch_im_in
  284. *
  285. dim_kernel * dim_kernel, bias_shift, out_shift, bias, pOut);
  286. /* counter reset */
  287. pBuffer = bufferA;
  288. }
  289. }
  290. }
  291. /* check if there is left-over for compute */
  292. if (pBuffer != bufferA)
  293. {
  294. const q7_t *pA = wt;
  295. int i;
  296. for (i = 0; i < ch_im_out; i++)
  297. {
  298. q31_t sum = ((q31_t)bias[i] << bias_shift) + NN_ROUND(out_shift);
  299. const q15_t *pB = bufferA;
  300. /* each time it process 4 entries */
  301. uint16_t colCnt = ch_im_in * dim_kernel * dim_kernel >> 2;
  302. while (colCnt)
  303. {
  304. q31_t inA1, inA2;
  305. q31_t inB1, inB2;
  306. pA = read_and_pad_reordered(pA, &inA1, &inA2);
  307. inB1 = arm_nn_read_q15x2_ia(&pB);
  308. sum = __SMLAD(inA1, inB1, sum);
  309. inB2 = arm_nn_read_q15x2_ia(&pB);
  310. sum = __SMLAD(inA2, inB2, sum);
  311. colCnt--;
  312. }
  313. colCnt = ch_im_in * dim_kernel * dim_kernel & 0x3;
  314. while (colCnt)
  315. {
  316. q7_t inA1 = *pA++;
  317. q15_t inB1 = *pB++;
  318. sum += inA1 * inB1;
  319. colCnt--;
  320. }
  321. *pOut = (q7_t) __SSAT((sum >> out_shift), 8);
  322. pOut++;
  323. }
  324. }
  325. #else
  326. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  327. uint16_t i, j, k, l, m, n;
  328. int conv_out;
  329. signed char in_row, in_col;
  330. if (ch_im_in % 4 != 0 || ch_im_out % 2 != 0)
  331. {
  332. /* check if the input dimension meets the constraints */
  333. return ARM_MATH_SIZE_MISMATCH;
  334. }
  335. for (i = 0; i < ch_im_out; i++)
  336. {
  337. for (j = 0; j < dim_im_out; j++)
  338. {
  339. for (k = 0; k < dim_im_out; k++)
  340. {
  341. conv_out = (bias[i] << bias_shift) + NN_ROUND(out_shift);
  342. for (m = 0; m < dim_kernel; m++)
  343. {
  344. for (n = 0; n < dim_kernel; n++)
  345. {
  346. // if-for implementation
  347. in_row = stride * j + m - padding;
  348. in_col = stride * k + n - padding;
  349. if (in_row >= 0 && in_col >= 0 && in_row < dim_im_in && in_col < dim_im_in)
  350. {
  351. for (l = 0; l < ch_im_in; l++)
  352. {
  353. conv_out +=
  354. Im_in[(in_row * dim_im_in + in_col) * ch_im_in +
  355. l] * wt[i * ch_im_in * dim_kernel * dim_kernel + (m * dim_kernel +
  356. n) * ch_im_in + l];
  357. }
  358. }
  359. }
  360. }
  361. Im_out[i + (j * dim_im_out + k) * ch_im_out] = (q7_t) __SSAT((conv_out >> out_shift), 8);
  362. }
  363. }
  364. }
  365. #endif /* ARM_MATH_DSP */
  366. /* Return to application */
  367. return ARM_MATH_SUCCESS;
  368. }
  369. /**
  370. * @} end of NNConv group
  371. */