arm_fully_connected_q7.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_fully_connected_q7.c
  21. * Description: Q7 basic fully-connected layer function
  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 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
  62. arm_fully_connected_q7(const q7_t * pV,
  63. const q7_t * pM,
  64. const uint16_t dim_vec,
  65. const uint16_t num_of_rows,
  66. const uint16_t bias_shift,
  67. const uint16_t out_shift, const q7_t * bias, q7_t * pOut, q15_t * vec_buffer)
  68. {
  69. #if defined (ARM_MATH_DSP)
  70. /* Run the following code for Cortex-M4 and Cortex-M7 */
  71. const q7_t *pB = pM;
  72. const q7_t *pB2;
  73. q7_t *pO = pOut;
  74. const q7_t *pBias = bias;
  75. q15_t *pA;
  76. uint16_t rowCnt = num_of_rows >> 1;
  77. /* expand the vector into the buffer */
  78. arm_q7_to_q15_reordered_no_shift(pV, vec_buffer, dim_vec);
  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 = vec_buffer;
  85. pB2 = pB + dim_vec;
  86. while (colCnt)
  87. {
  88. q31_t inV, inM11, inM12, inM21, inM22;
  89. pB = (q7_t *) read_and_pad_reordered((void *)pB, &inM11, &inM12);
  90. pB2 = (q7_t *) read_and_pad_reordered((void *)pB2, &inM21, &inM22);
  91. inV = *__SIMD32(pA)++;
  92. sum = __SMLAD(inV, inM11, sum);
  93. sum2 = __SMLAD(inV, inM21, sum2);
  94. inV = *__SIMD32(pA)++;
  95. sum = __SMLAD(inV, inM12, sum);
  96. sum2 = __SMLAD(inV, inM22, sum2);
  97. colCnt--;
  98. }
  99. colCnt = dim_vec & 0x3;
  100. while (colCnt)
  101. {
  102. q7_t inV = *pA++;
  103. q15_t inM = *pB++;
  104. q15_t inM2 = *pB2++;
  105. sum += inV * inM;
  106. sum2 += inV * inM2;
  107. colCnt--;
  108. } /* while over colCnt */
  109. *pO++ = (q7_t) (__SSAT((sum >> out_shift), 8));
  110. *pO++ = (q7_t) (__SSAT((sum2 >> out_shift), 8));
  111. /* adjust the pointers and counters */
  112. pB += dim_vec;
  113. rowCnt--;
  114. }
  115. /* left-over part of the rows */
  116. rowCnt = num_of_rows & 0x1;
  117. while (rowCnt)
  118. {
  119. uint16_t colCnt = dim_vec >> 2;
  120. q31_t sum = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  121. pA = vec_buffer;
  122. while (colCnt)
  123. {
  124. q31_t inV1, inV2, inM11, inM12;
  125. pB = (q7_t *) read_and_pad_reordered((void *)pB, &inM11, &inM12);
  126. inV1 = *__SIMD32(pA)++;
  127. sum = __SMLAD(inV1, inM11, sum);
  128. inV2 = *__SIMD32(pA)++;
  129. sum = __SMLAD(inV2, inM12, sum);
  130. colCnt--;
  131. }
  132. /* left-over of the vector */
  133. colCnt = dim_vec & 0x3;
  134. while (colCnt)
  135. {
  136. q7_t inV = *pA++;
  137. q15_t inM = *pB++;
  138. sum += inV * inM;
  139. colCnt--;
  140. }
  141. *pO++ = (q7_t) (__SSAT((sum >> out_shift), 8));
  142. rowCnt--;
  143. }
  144. #else
  145. int i, j;
  146. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  147. for (i = 0; i < num_of_rows; i++)
  148. {
  149. int ip_out = ((q31_t)(bias[i]) << bias_shift) + NN_ROUND(out_shift);
  150. for (j = 0; j < dim_vec; j++)
  151. {
  152. ip_out += pV[j] * pM[i * dim_vec + j];
  153. }
  154. pOut[i] = (q7_t) __SSAT((ip_out >> out_shift), 8);
  155. }
  156. #endif /* ARM_MATH_DSP */
  157. /* Return to ARM_MATH_SUCCESS */
  158. return (ARM_MATH_SUCCESS);
  159. }
  160. /**
  161. * @} end of FC group
  162. */