arm_fully_connected_q15.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_fully_connected_q15.c
  21. * Description: Q15 basic fully-connected layer function
  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 FC
  36. * @{
  37. */
  38. /**
  39. * @brief Q15 opt 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. *
  52. * @details
  53. *
  54. * <b>Buffer size:</b>
  55. *
  56. * vec_buffer size: 0
  57. *
  58. */
  59. arm_status arm_fully_connected_q15(const q15_t *pV,
  60. const q15_t *pM,
  61. const uint16_t dim_vec,
  62. const uint16_t num_of_rows,
  63. const uint16_t bias_shift,
  64. const uint16_t out_shift,
  65. const q15_t *bias,
  66. q15_t *pOut,
  67. q15_t *vec_buffer)
  68. {
  69. (void)vec_buffer;
  70. #if defined(ARM_MATH_DSP) && !defined(ARM_MATH_MVEI)
  71. /* Run the following code for Cortex-M4 and Cortex-M7 */
  72. const q15_t *pB = pM;
  73. const q15_t *pB2 = pB + dim_vec;
  74. q15_t *pO = pOut;
  75. const q15_t *pA;
  76. const q15_t *pBias = bias;
  77. uint16_t rowCnt = num_of_rows >> 1;
  78. /* this loop loops over different output */
  79. while (rowCnt)
  80. {
  81. q31_t sum = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  82. q31_t sum2 = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  83. uint16_t colCnt = dim_vec >> 2;
  84. pA = pV;
  85. pB2 = pB + dim_vec;
  86. while (colCnt)
  87. {
  88. q31_t inV1, inM1, inM2;
  89. inV1 = arm_nn_read_q15x2_ia(&pA);
  90. inM1 = arm_nn_read_q15x2_ia(&pB);
  91. sum = __SMLAD(inV1, inM1, sum);
  92. inM2 = arm_nn_read_q15x2_ia(&pB2);
  93. sum2 = __SMLAD(inV1, inM2, sum2);
  94. inV1 = arm_nn_read_q15x2_ia(&pA);
  95. inM1 = arm_nn_read_q15x2_ia(&pB);
  96. sum = __SMLAD(inV1, inM1, sum);
  97. inM2 = arm_nn_read_q15x2_ia(&pB2);
  98. sum2 = __SMLAD(inV1, inM2, sum2);
  99. colCnt--;
  100. }
  101. colCnt = dim_vec & 0x3;
  102. while (colCnt)
  103. {
  104. q15_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++ = (q15_t)(__SSAT((sum >> out_shift), 16));
  112. *pO++ = (q15_t)(__SSAT((sum2 >> out_shift), 16));
  113. /* adjust the pointers and counters */
  114. pB = pB + dim_vec;
  115. rowCnt--;
  116. }
  117. rowCnt = num_of_rows & 0x1;
  118. while (rowCnt)
  119. {
  120. q31_t sum = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  121. uint16_t colCnt = dim_vec >> 2;
  122. pA = pV;
  123. while (colCnt)
  124. {
  125. q31_t inV1, inM1;
  126. inV1 = arm_nn_read_q15x2_ia(&pA);
  127. inM1 = arm_nn_read_q15x2_ia(&pB);
  128. sum = __SMLAD(inV1, inM1, sum);
  129. inV1 = arm_nn_read_q15x2_ia(&pA);
  130. inM1 = arm_nn_read_q15x2_ia(&pB);
  131. sum = __SMLAD(inV1, inM1, sum);
  132. colCnt--;
  133. }
  134. /* left-over of the vector */
  135. colCnt = dim_vec & 0x3;
  136. while (colCnt)
  137. {
  138. q15_t inV = *pA++;
  139. q15_t inM = *pB++;
  140. sum += inV * inM;
  141. colCnt--;
  142. }
  143. *pO++ = (q15_t)(__SSAT((sum >> out_shift), 16));
  144. rowCnt--;
  145. }
  146. #else
  147. int i, j;
  148. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  149. for (i = 0; i < num_of_rows; i++)
  150. {
  151. int ip_out = ((q31_t)(bias[i]) << bias_shift) + NN_ROUND(out_shift);
  152. for (j = 0; j < dim_vec; j++)
  153. {
  154. ip_out += pV[j] * pM[i * dim_vec + j];
  155. }
  156. pOut[i] = (q15_t)__SSAT((ip_out >> out_shift), 16);
  157. }
  158. #endif /* ARM_MATH_DSP */
  159. /* Return to application */
  160. return (ARM_MATH_SUCCESS);
  161. }
  162. /**
  163. * @} end of FC group
  164. */