arm_convolve_HWC_q7_basic.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_convolve_HWC_q7_basic.c
  21. * Description: Q7 version of convolution
  22. *
  23. * $Date: 20. July 2021
  24. * $Revision: V.1.1.1
  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. * @brief Basic 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 <code>ARM_MATH_SUCCESS</code>
  56. *
  57. * @details
  58. *
  59. * <b>Buffer size:</b>
  60. *
  61. * bufferA size: 2*ch_im_in*dim_kernel*dim_kernel
  62. *
  63. * bufferB size: 0
  64. *
  65. * This basic version is designed to work for any input tensor and weight
  66. * dimension.
  67. */
  68. arm_status arm_convolve_HWC_q7_basic(const q7_t *Im_in,
  69. const uint16_t dim_im_in,
  70. const uint16_t ch_im_in,
  71. const q7_t *wt,
  72. const uint16_t ch_im_out,
  73. const uint16_t dim_kernel,
  74. const uint16_t padding,
  75. const uint16_t stride,
  76. const q7_t *bias,
  77. const uint16_t bias_shift,
  78. const uint16_t out_shift,
  79. q7_t *Im_out,
  80. const uint16_t dim_im_out,
  81. q15_t *bufferA,
  82. q7_t *bufferB)
  83. {
  84. (void)bufferB;
  85. #if defined(ARM_MATH_DSP) && !defined(ARM_MATH_MVEI)
  86. /* Run the following code for Cortex-M4 and Cortex-M7 */
  87. int16_t i_out_y, i_out_x, i_ker_y, i_ker_x;
  88. /*
  89. * Here we use bufferA as q15_t internally as computation are done with q15_t level
  90. * im2col are done to output in q15_t format from q7_t input
  91. */
  92. q15_t *pBuffer = bufferA;
  93. q7_t *pOut = Im_out;
  94. /* This part implements the im2col function */
  95. for (i_out_y = 0; i_out_y < dim_im_out; i_out_y++)
  96. {
  97. for (i_out_x = 0; i_out_x < dim_im_out; i_out_x++)
  98. {
  99. for (i_ker_y = i_out_y * stride - padding; i_ker_y < i_out_y * stride - padding + dim_kernel; i_ker_y++)
  100. {
  101. for (i_ker_x = i_out_x * stride - padding; i_ker_x < i_out_x * stride - padding + dim_kernel; i_ker_x++)
  102. {
  103. if (i_ker_y < 0 || i_ker_y >= dim_im_in || i_ker_x < 0 || i_ker_x >= dim_im_in)
  104. {
  105. /* Filling 0 for out-of-bound paddings */
  106. /* arm_fill_q15(0, pBuffer, ch_im_in); */
  107. memset(pBuffer, 0, sizeof(q15_t) * ch_im_in);
  108. }
  109. else
  110. {
  111. /* Copying the pixel data to column */
  112. arm_q7_to_q15_no_shift(
  113. (q7_t *)Im_in + (i_ker_y * dim_im_in + i_ker_x) * ch_im_in, pBuffer, ch_im_in);
  114. }
  115. pBuffer += ch_im_in;
  116. }
  117. }
  118. /* Computation is filed for every 2 columns */
  119. if (pBuffer == bufferA + 2 * ch_im_in * dim_kernel * dim_kernel)
  120. {
  121. pOut = arm_nn_mat_mult_kernel_q7_q15(
  122. wt, bufferA, ch_im_out, ch_im_in * dim_kernel * dim_kernel, bias_shift, out_shift, bias, pOut);
  123. /* counter reset */
  124. pBuffer = bufferA;
  125. }
  126. }
  127. }
  128. /* left-over because odd number of output pixels */
  129. if (pBuffer != bufferA)
  130. {
  131. const q7_t *pA = wt;
  132. int i;
  133. for (i = 0; i < ch_im_out; i++)
  134. {
  135. /* Load the accumulator with bias first */
  136. q31_t sum = ((q31_t)bias[i] << bias_shift) + NN_ROUND(out_shift);
  137. /* Point to the beging of the im2col buffer */
  138. const q15_t *pB = bufferA;
  139. /* Each time it process 4 entries */
  140. uint16_t colCnt = ch_im_in * dim_kernel * dim_kernel >> 2;
  141. while (colCnt)
  142. {
  143. q31_t inA1, inA2;
  144. q31_t inB1, inB2;
  145. pA = read_and_pad(pA, &inA1, &inA2);
  146. inB1 = arm_nn_read_q15x2_ia(&pB);
  147. sum = __SMLAD(inA1, inB1, sum);
  148. inB2 = arm_nn_read_q15x2_ia(&pB);
  149. sum = __SMLAD(inA2, inB2, sum);
  150. colCnt--;
  151. }
  152. colCnt = ch_im_in * dim_kernel * dim_kernel & 0x3;
  153. while (colCnt)
  154. {
  155. q7_t inA1 = *pA++;
  156. q15_t inB1 = *pB++;
  157. sum += inA1 * inB1;
  158. colCnt--;
  159. }
  160. *pOut++ = (q7_t)__SSAT((sum >> out_shift), 8);
  161. }
  162. }
  163. #else
  164. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  165. (void)bufferA;
  166. int i, j, k, l, m, n;
  167. int conv_out;
  168. int in_row, in_col;
  169. for (i = 0; i < ch_im_out; i++)
  170. {
  171. for (j = 0; j < dim_im_out; j++)
  172. {
  173. for (k = 0; k < dim_im_out; k++)
  174. {
  175. conv_out = ((q31_t)bias[i] << bias_shift) + NN_ROUND(out_shift);
  176. for (m = 0; m < dim_kernel; m++)
  177. {
  178. for (n = 0; n < dim_kernel; n++)
  179. {
  180. // if-for implementation
  181. in_row = stride * j + m - padding;
  182. in_col = stride * k + n - padding;
  183. if (in_row >= 0 && in_col >= 0 && in_row < dim_im_in && in_col < dim_im_in)
  184. {
  185. for (l = 0; l < ch_im_in; l++)
  186. {
  187. conv_out += Im_in[(in_row * dim_im_in + in_col) * ch_im_in + l] *
  188. wt[i * ch_im_in * dim_kernel * dim_kernel + (m * dim_kernel + n) * ch_im_in + l];
  189. }
  190. }
  191. }
  192. }
  193. Im_out[i + (j * dim_im_out + k) * ch_im_out] = (q7_t)__SSAT((conv_out >> out_shift), 8);
  194. }
  195. }
  196. }
  197. #endif /* ARM_MATH_DSP */
  198. /* Return to application */
  199. return ARM_MATH_SUCCESS;
  200. }
  201. /**
  202. * @} end of NNConv group
  203. */