arm_svm_sigmoid_predict_f16.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_svm_sigmoid_predict_f16.c
  4. * Description: SVM Sigmoid Classifier
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #include "dsp/svm_functions_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. #include <limits.h>
  31. #include <math.h>
  32. /**
  33. * @addtogroup sigmoidsvm
  34. * @{
  35. */
  36. /**
  37. * @brief SVM sigmoid prediction
  38. * @param[in] S Pointer to an instance of the rbf SVM structure.
  39. * @param[in] in Pointer to input vector
  40. * @param[out] pResult Decision value
  41. *
  42. */
  43. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. #include "arm_helium_utils.h"
  45. #include "arm_vec_math_f16.h"
  46. void arm_svm_sigmoid_predict_f16(
  47. const arm_svm_sigmoid_instance_f16 *S,
  48. const float16_t * in,
  49. int32_t * pResult)
  50. {
  51. /* inlined Matrix x Vector function interleaved with dot prod */
  52. uint32_t numRows = S->nbOfSupportVectors;
  53. uint32_t numCols = S->vectorDimension;
  54. const float16_t *pSupport = S->supportVectors;
  55. const float16_t *pSrcA = pSupport;
  56. const float16_t *pInA0;
  57. const float16_t *pInA1;
  58. uint32_t row;
  59. uint32_t blkCnt; /* loop counters */
  60. const float16_t *pDualCoef = S->dualCoefficients;
  61. _Float16 sum = S->intercept;
  62. f16x8_t vSum = vdupq_n_f16(0.0f);
  63. row = numRows;
  64. /*
  65. * compute 4 rows in parrallel
  66. */
  67. while (row >= 4) {
  68. const float16_t *pInA2, *pInA3;
  69. float16_t const *pSrcA0Vec, *pSrcA1Vec, *pSrcA2Vec, *pSrcA3Vec, *pInVec;
  70. f16x8_t vecIn, acc0, acc1, acc2, acc3;
  71. float16_t const *pSrcVecPtr = in;
  72. /*
  73. * Initialize the pointers to 4 consecutive MatrixA rows
  74. */
  75. pInA0 = pSrcA;
  76. pInA1 = pInA0 + numCols;
  77. pInA2 = pInA1 + numCols;
  78. pInA3 = pInA2 + numCols;
  79. /*
  80. * Initialize the vector pointer
  81. */
  82. pInVec = pSrcVecPtr;
  83. /*
  84. * reset accumulators
  85. */
  86. acc0 = vdupq_n_f16(0.0f);
  87. acc1 = vdupq_n_f16(0.0f);
  88. acc2 = vdupq_n_f16(0.0f);
  89. acc3 = vdupq_n_f16(0.0f);
  90. pSrcA0Vec = pInA0;
  91. pSrcA1Vec = pInA1;
  92. pSrcA2Vec = pInA2;
  93. pSrcA3Vec = pInA3;
  94. blkCnt = numCols >> 3;
  95. while (blkCnt > 0U) {
  96. f16x8_t vecA;
  97. vecIn = vld1q(pInVec);
  98. pInVec += 8;
  99. vecA = vld1q(pSrcA0Vec);
  100. pSrcA0Vec += 8;
  101. acc0 = vfmaq(acc0, vecIn, vecA);
  102. vecA = vld1q(pSrcA1Vec);
  103. pSrcA1Vec += 8;
  104. acc1 = vfmaq(acc1, vecIn, vecA);
  105. vecA = vld1q(pSrcA2Vec);
  106. pSrcA2Vec += 8;
  107. acc2 = vfmaq(acc2, vecIn, vecA);
  108. vecA = vld1q(pSrcA3Vec);
  109. pSrcA3Vec += 8;
  110. acc3 = vfmaq(acc3, vecIn, vecA);
  111. blkCnt--;
  112. }
  113. /*
  114. * tail
  115. * (will be merged thru tail predication)
  116. */
  117. blkCnt = numCols & 7;
  118. if (blkCnt > 0U) {
  119. mve_pred16_t p0 = vctp16q(blkCnt);
  120. f16x8_t vecA;
  121. vecIn = vldrhq_z_f16(pInVec, p0);
  122. vecA = vldrhq_z_f16(pSrcA0Vec, p0);
  123. acc0 = vfmaq(acc0, vecIn, vecA);
  124. vecA = vldrhq_z_f16(pSrcA1Vec, p0);
  125. acc1 = vfmaq(acc1, vecIn, vecA);
  126. vecA = vldrhq_z_f16(pSrcA2Vec, p0);
  127. acc2 = vfmaq(acc2, vecIn, vecA);
  128. vecA = vldrhq_z_f16(pSrcA3Vec, p0);
  129. acc3 = vfmaq(acc3, vecIn, vecA);
  130. }
  131. /*
  132. * Sum the partial parts
  133. */
  134. f16x8_t vtmp = vuninitializedq_f16();
  135. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc0), vtmp, 0);
  136. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc1), vtmp, 1);
  137. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc2), vtmp, 2);
  138. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc3), vtmp, 3);
  139. vSum =
  140. vfmaq_m_f16(vSum, vld1q(pDualCoef),
  141. vtanhq_f16(vaddq_n_f16(vmulq_n_f16(vtmp, S->gamma), S->coef0)),vctp16q(4));
  142. pDualCoef += 4;
  143. pSrcA += numCols * 4;
  144. /*
  145. * Decrement the row loop counter
  146. */
  147. row -= 4;
  148. }
  149. /*
  150. * compute 2 rows in parrallel
  151. */
  152. if (row >= 2) {
  153. float16_t const *pSrcA0Vec, *pSrcA1Vec, *pInVec;
  154. f16x8_t vecIn, acc0, acc1;
  155. float16_t const *pSrcVecPtr = in;
  156. /*
  157. * Initialize the pointers to 2 consecutive MatrixA rows
  158. */
  159. pInA0 = pSrcA;
  160. pInA1 = pInA0 + numCols;
  161. /*
  162. * Initialize the vector pointer
  163. */
  164. pInVec = pSrcVecPtr;
  165. /*
  166. * reset accumulators
  167. */
  168. acc0 = vdupq_n_f16(0.0f);
  169. acc1 = vdupq_n_f16(0.0f);
  170. pSrcA0Vec = pInA0;
  171. pSrcA1Vec = pInA1;
  172. blkCnt = numCols >> 3;
  173. while (blkCnt > 0U) {
  174. f16x8_t vecA;
  175. vecIn = vld1q(pInVec);
  176. pInVec += 8;
  177. vecA = vld1q(pSrcA0Vec);
  178. pSrcA0Vec += 8;
  179. acc0 = vfmaq(acc0, vecIn, vecA);
  180. vecA = vld1q(pSrcA1Vec);
  181. pSrcA1Vec += 8;
  182. acc1 = vfmaq(acc1, vecIn, vecA);
  183. blkCnt--;
  184. }
  185. /*
  186. * tail
  187. * (will be merged thru tail predication)
  188. */
  189. blkCnt = numCols & 7;
  190. if (blkCnt > 0U) {
  191. mve_pred16_t p0 = vctp16q(blkCnt);
  192. f16x8_t vecA;
  193. vecIn = vldrhq_z_f16(pInVec, p0);
  194. vecA = vldrhq_z_f16(pSrcA0Vec, p0);
  195. acc0 = vfmaq(acc0, vecIn, vecA);
  196. vecA = vldrhq_z_f16(pSrcA1Vec, p0);
  197. acc1 = vfmaq(acc1, vecIn, vecA);
  198. }
  199. /*
  200. * Sum the partial parts
  201. */
  202. f16x8_t vtmp = vuninitializedq_f16();
  203. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc0), vtmp, 0);
  204. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc1), vtmp, 1);
  205. vSum =
  206. vfmaq_m_f16(vSum, vld1q(pDualCoef),
  207. vtanhq_f16(vaddq_n_f16(vmulq_n_f16(vtmp, S->gamma), S->coef0)),
  208. vctp16q(2));
  209. pSrcA += numCols * 2;
  210. row -= 2;
  211. }
  212. if (row >= 1) {
  213. f16x8_t vecIn, acc0;
  214. float16_t const *pSrcA0Vec, *pInVec;
  215. float16_t const *pSrcVecPtr = in;
  216. /*
  217. * Initialize the pointers to last MatrixA row
  218. */
  219. pInA0 = pSrcA;
  220. /*
  221. * Initialize the vector pointer
  222. */
  223. pInVec = pSrcVecPtr;
  224. /*
  225. * reset accumulators
  226. */
  227. acc0 = vdupq_n_f16(0.0f);
  228. pSrcA0Vec = pInA0;
  229. blkCnt = numCols >> 3;
  230. while (blkCnt > 0U) {
  231. f16x8_t vecA;
  232. vecIn = vld1q(pInVec);
  233. pInVec += 8;
  234. vecA = vld1q(pSrcA0Vec);
  235. pSrcA0Vec += 8;
  236. acc0 = vfmaq(acc0, vecIn, vecA);
  237. blkCnt--;
  238. }
  239. /*
  240. * tail
  241. * (will be merged thru tail predication)
  242. */
  243. blkCnt = numCols & 7;
  244. if (blkCnt > 0U) {
  245. mve_pred16_t p0 = vctp16q(blkCnt);
  246. f16x8_t vecA;
  247. vecIn = vldrhq_z_f16(pInVec, p0);
  248. vecA = vldrhq_z_f16(pSrcA0Vec, p0);
  249. acc0 = vfmaq(acc0, vecIn, vecA);
  250. }
  251. /*
  252. * Sum the partial parts
  253. */
  254. f16x8_t vtmp = vuninitializedq_f16();
  255. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc0), vtmp, 0);
  256. vSum =
  257. vfmaq_m_f16(vSum, vld1q(pDualCoef),
  258. vtanhq_f16(vaddq_n_f16(vmulq_n_f16(vtmp, S->gamma), S->coef0)),
  259. vctp16q(1));
  260. }
  261. sum += (_Float16)vecAddAcrossF16Mve(vSum);
  262. *pResult = S->classes[STEP(sum)];
  263. }
  264. #else
  265. void arm_svm_sigmoid_predict_f16(
  266. const arm_svm_sigmoid_instance_f16 *S,
  267. const float16_t * in,
  268. int32_t * pResult)
  269. {
  270. _Float16 sum=S->intercept;
  271. _Float16 dot=0.0f16;
  272. uint32_t i,j;
  273. const float16_t *pSupport = S->supportVectors;
  274. for(i=0; i < S->nbOfSupportVectors; i++)
  275. {
  276. dot=0.0f16;
  277. for(j=0; j < S->vectorDimension; j++)
  278. {
  279. dot = (_Float16)dot + (_Float16)in[j] * (_Float16)*pSupport++;
  280. }
  281. sum += (_Float16)S->dualCoefficients[i] * (_Float16)tanhf((float32_t)((_Float16)S->gamma * (_Float16)dot + (_Float16)S->coef0));
  282. }
  283. *pResult=S->classes[STEP(sum)];
  284. }
  285. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  286. /**
  287. * @} end of sigmoidsvm group
  288. */
  289. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */