arm_convolve_HWC_q7_RGB.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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_RGB.c
  21. * Description: Q7 version of convolution for RGB image
  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 Q7 convolution function for RGB image
  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 equals 3
  69. *
  70. * This kernel is written exclusively for convolution with ch_im_in
  71. * equals 3. This applies on the first layer of CNNs which has input
  72. * image with RGB format.
  73. */
  74. arm_status
  75. arm_convolve_HWC_q7_RGB(const q7_t * Im_in,
  76. const uint16_t dim_im_in,
  77. const uint16_t ch_im_in,
  78. const q7_t * wt,
  79. const uint16_t ch_im_out,
  80. const uint16_t dim_kernel,
  81. const uint16_t padding,
  82. const uint16_t stride,
  83. const q7_t * bias,
  84. const uint16_t bias_shift,
  85. const uint16_t out_shift,
  86. q7_t * Im_out, const uint16_t dim_im_out, q15_t * bufferA, q7_t * bufferB)
  87. {
  88. #if defined (ARM_MATH_DSP)
  89. /* Run the following code for Cortex-M4 and Cortex-M7 */
  90. int16_t i_out_y, i_out_x, i_ker_y, i_ker_x;
  91. /*
  92. * Here we use bufferA as q15_t internally as computation are done with q15_t level
  93. * im2col are done to output in q15_t format from q7_t input
  94. */
  95. q15_t *pBuffer = bufferA;
  96. q7_t *pOut = Im_out;
  97. // check if number of input channels is 3
  98. if (ch_im_in != 3)
  99. {
  100. return ARM_MATH_SIZE_MISMATCH;
  101. }
  102. // This part implements the im2col function
  103. for (i_out_y = 0; i_out_y < dim_im_out; i_out_y++)
  104. {
  105. for (i_out_x = 0; i_out_x < dim_im_out; i_out_x++)
  106. {
  107. for (i_ker_y = i_out_y * stride - padding; i_ker_y < i_out_y * stride - padding + dim_kernel; i_ker_y++)
  108. {
  109. for (i_ker_x = i_out_x * stride - padding; i_ker_x < i_out_x * stride - padding + dim_kernel; i_ker_x++)
  110. {
  111. if (i_ker_y < 0 || i_ker_y >= dim_im_in || i_ker_x < 0 || i_ker_x >= dim_im_in)
  112. {
  113. /* Equivalent to arm_fill_q15(0, pBuffer, ch_im_in) with assumption: ch_im_in = 3 */
  114. *__SIMD32(pBuffer) = 0x0;
  115. *(pBuffer + 2) = 0;
  116. pBuffer += 3;
  117. } else
  118. {
  119. /*
  120. * Equivalent to:
  121. * arm_q7_to_q15_no_shift( (q7_t*)Im_in+(i_ker_y*dim_im_in+i_ker_x)*3, pBuffer, 3);
  122. */
  123. const q7_t *pPixel = Im_in + (i_ker_y * dim_im_in + i_ker_x) * 3;
  124. q31_t buf = *__SIMD32(pPixel);
  125. union arm_nnword top;
  126. union arm_nnword bottom;
  127. top.word = __SXTB16(buf);
  128. bottom.word = __SXTB16(__ROR(buf, 8));
  129. #ifndef ARM_MATH_BIG_ENDIAN
  130. /*
  131. * little-endian, | omit | 3rd | 2nd | 1st |
  132. * MSB LSB
  133. * top | 3rd | 1st |; bottom | omit | 2nd |
  134. *
  135. * version 1, need to swap 2nd and 3rd weight
  136. * *__SIMD32(pBuffer) = top.word;
  137. * *(pBuffer+2) = bottom.half_words[0];
  138. *
  139. * version 2, no weight shuffling required
  140. */
  141. *pBuffer++ = top.half_words[0];
  142. *__SIMD32(pBuffer) = __PKHBT(bottom.word, top.word, 0);
  143. #else
  144. /*
  145. * big-endian, | 1st | 2nd | 3rd | omit |
  146. * MSB LSB
  147. * top | 2nd | omit |; bottom | 1st | 3rd |
  148. *
  149. * version 1, need to swap 2nd and 3rd weight
  150. * *__SIMD32(pBuffer) = bottom.word;
  151. * *(pBuffer+2) = top.half_words[1];
  152. *
  153. * version 2, no weight shuffling required
  154. */
  155. *pBuffer++ = bottom.half_words[0];
  156. *__SIMD32(pBuffer) = __PKHTB(top.word, bottom.word, 0);
  157. #endif
  158. pBuffer += 2;
  159. }
  160. }
  161. }
  162. if (pBuffer == bufferA + 2 * 3 * dim_kernel * dim_kernel)
  163. {
  164. pOut =
  165. arm_nn_mat_mult_kernel_q7_q15(wt, bufferA,
  166. ch_im_out,
  167. 3 * dim_kernel * dim_kernel, bias_shift, out_shift, bias, pOut);
  168. /* counter reset */
  169. pBuffer = bufferA;
  170. }
  171. }
  172. }
  173. /* left-over because odd number of output pixels */
  174. if (pBuffer != bufferA)
  175. {
  176. const q7_t *pA = wt;
  177. int i;
  178. for (i = 0; i < ch_im_out; i++)
  179. {
  180. q31_t sum = ((q31_t)bias[i] << bias_shift) + NN_ROUND(out_shift);
  181. q15_t *pB = bufferA;
  182. /* basically each time it process 4 entries */
  183. uint16_t colCnt = 3 * dim_kernel * dim_kernel >> 2;
  184. while (colCnt)
  185. {
  186. q31_t inA1, inA2;
  187. q31_t inB1, inB2;
  188. pA = (q7_t *) read_and_pad((void *)pA, &inA1, &inA2);
  189. inB1 = *__SIMD32(pB)++;
  190. sum = __SMLAD(inA1, inB1, sum);
  191. inB2 = *__SIMD32(pB)++;
  192. sum = __SMLAD(inA2, inB2, sum);
  193. colCnt--;
  194. }
  195. colCnt = 3 * dim_kernel * dim_kernel & 0x3;
  196. while (colCnt)
  197. {
  198. q7_t inA1 = *pA++;
  199. q15_t inB1 = *pB++;
  200. sum += inA1 * inB1;
  201. colCnt--;
  202. }
  203. *pOut++ = (q7_t) __SSAT((sum >> out_shift), 8);
  204. }
  205. }
  206. #else
  207. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  208. uint16_t i, j, k, l, m, n;
  209. int conv_out;
  210. signed char in_row, in_col;
  211. // check if number of input channels is 3
  212. if (ch_im_in != 3)
  213. {
  214. return ARM_MATH_SIZE_MISMATCH;
  215. }
  216. for (i = 0; i < ch_im_out; i++)
  217. {
  218. for (j = 0; j < dim_im_out; j++)
  219. {
  220. for (k = 0; k < dim_im_out; k++)
  221. {
  222. conv_out = (bias[i] << bias_shift) + NN_ROUND(out_shift);
  223. for (m = 0; m < dim_kernel; m++)
  224. {
  225. for (n = 0; n < dim_kernel; n++)
  226. {
  227. /* if-for implementation */
  228. in_row = stride * j + m - padding;
  229. in_col = stride * k + n - padding;
  230. if (in_row >= 0 && in_col >= 0 && in_row < dim_im_in && in_col < dim_im_in)
  231. {
  232. for (l = 0; l < ch_im_in; l++)
  233. {
  234. conv_out +=
  235. Im_in[(in_row * dim_im_in + in_col) * ch_im_in +
  236. l] * wt[i * ch_im_in * dim_kernel * dim_kernel + (m * dim_kernel +
  237. n) * ch_im_in + l];
  238. }
  239. }
  240. }
  241. }
  242. Im_out[i + (j * dim_im_out + k) * ch_im_out] = (q7_t) __SSAT((conv_out >> out_shift), 8);
  243. }
  244. }
  245. }
  246. #endif /* ARM_MATH_DSP */
  247. /* Return to application */
  248. return (ARM_MATH_SUCCESS);
  249. }
  250. /**
  251. * @} end of NNConv group
  252. */