arm_fully_connected_q7.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2010-2021 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_fully_connected_q7.c
  21. * Description: Q7 basic fully-connected layer function
  22. *
  23. * $Date: July 20, 2021
  24. * $Revision: V.1.1.2
  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 FC
  36. * @{
  37. */
  38. /**
  39. * @brief Q7 basic fully-connected layer function
  40. * @param[in] pV pointer to input vector
  41. * @param[in] pM pointer to matrix weights
  42. * @param[in] dim_vec length of the vector
  43. * @param[in] num_of_rows number of rows in weight matrix
  44. * @param[in] bias_shift amount of left-shift for bias
  45. * @param[in] out_shift amount of right-shift for output
  46. * @param[in] bias pointer to bias
  47. * @param[in,out] pOut pointer to output vector
  48. * @param[in,out] vec_buffer pointer to buffer space for input
  49. * @return The function returns <code>ARM_MATH_SUCCESS</code>
  50. *
  51. * @details
  52. *
  53. * <b>Buffer size:</b>
  54. *
  55. * vec_buffer size: dim_vec
  56. *
  57. * This basic function is designed to work with regular weight
  58. * matrix without interleaving.
  59. *
  60. */
  61. arm_status arm_fully_connected_q7(const q7_t *pV,
  62. const q7_t *pM,
  63. const uint16_t dim_vec,
  64. const uint16_t num_of_rows,
  65. const uint16_t bias_shift,
  66. const uint16_t out_shift,
  67. const q7_t *bias,
  68. q7_t *pOut,
  69. q15_t *vec_buffer)
  70. {
  71. #if defined(ARM_MATH_DSP) && !defined(ARM_MATH_MVEI)
  72. /* Run the following code for Cortex-M4 and Cortex-M7 */
  73. const q7_t *pB = pM;
  74. const q7_t *pB2;
  75. q7_t *pO = pOut;
  76. const q7_t *pBias = bias;
  77. const q15_t *pA;
  78. uint16_t rowCnt = num_of_rows >> 1;
  79. /* expand the vector into the buffer */
  80. arm_q7_to_q15_reordered_no_shift(pV, vec_buffer, dim_vec);
  81. while (rowCnt)
  82. {
  83. q31_t sum = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  84. q31_t sum2 = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  85. uint16_t colCnt = dim_vec >> 2;
  86. pA = vec_buffer;
  87. pB2 = pB + dim_vec;
  88. while (colCnt)
  89. {
  90. q31_t inV, inM11, inM12, inM21, inM22;
  91. pB = read_and_pad_reordered(pB, &inM11, &inM12);
  92. pB2 = read_and_pad_reordered(pB2, &inM21, &inM22);
  93. inV = arm_nn_read_q15x2_ia(&pA);
  94. sum = __SMLAD(inV, inM11, sum);
  95. sum2 = __SMLAD(inV, inM21, sum2);
  96. inV = arm_nn_read_q15x2_ia(&pA);
  97. sum = __SMLAD(inV, inM12, sum);
  98. sum2 = __SMLAD(inV, inM22, sum2);
  99. colCnt--;
  100. }
  101. colCnt = dim_vec & 0x3;
  102. while (colCnt)
  103. {
  104. q7_t inV = *pA++;
  105. q15_t inM = *pB++;
  106. q15_t inM2 = *pB2++;
  107. sum += inV * inM;
  108. sum2 += inV * inM2;
  109. colCnt--;
  110. } /* while over colCnt */
  111. *pO++ = (q7_t)(__SSAT((sum >> out_shift), 8));
  112. *pO++ = (q7_t)(__SSAT((sum2 >> out_shift), 8));
  113. /* adjust the pointers and counters */
  114. pB += dim_vec;
  115. rowCnt--;
  116. }
  117. /* left-over part of the rows */
  118. rowCnt = num_of_rows & 0x1;
  119. while (rowCnt)
  120. {
  121. uint16_t colCnt = dim_vec >> 2;
  122. q31_t sum = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  123. pA = vec_buffer;
  124. while (colCnt)
  125. {
  126. q31_t inV1, inV2, inM11, inM12;
  127. pB = read_and_pad_reordered(pB, &inM11, &inM12);
  128. inV1 = arm_nn_read_q15x2_ia(&pA);
  129. sum = __SMLAD(inV1, inM11, sum);
  130. inV2 = arm_nn_read_q15x2_ia(&pA);
  131. sum = __SMLAD(inV2, inM12, sum);
  132. colCnt--;
  133. }
  134. /* left-over of the vector */
  135. colCnt = dim_vec & 0x3;
  136. while (colCnt)
  137. {
  138. q7_t inV = *pA++;
  139. q15_t inM = *pB++;
  140. sum += inV * inM;
  141. colCnt--;
  142. }
  143. *pO++ = (q7_t)(__SSAT((sum >> out_shift), 8));
  144. rowCnt--;
  145. }
  146. #else
  147. (void)vec_buffer;
  148. int i, j;
  149. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  150. for (i = 0; i < num_of_rows; i++)
  151. {
  152. int ip_out = ((q31_t)(bias[i]) << bias_shift) + NN_ROUND(out_shift);
  153. for (j = 0; j < dim_vec; j++)
  154. {
  155. ip_out += pV[j] * pM[i * dim_vec + j];
  156. }
  157. pOut[i] = (q7_t)__SSAT((ip_out >> out_shift), 8);
  158. }
  159. #endif /* ARM_MATH_DSP */
  160. /* Return to ARM_MATH_SUCCESS */
  161. return (ARM_MATH_SUCCESS);
  162. }
  163. /**
  164. * @} end of FC group
  165. */