arm_svm_polynomial_predict_f16.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_svm_polynomial_predict_f16.c
  4. * Description: SVM Polynomial 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. #if !defined(ARM_MATH_MVE_FLOAT16) || defined(ARM_MATH_AUTOVECTORIZE)
  33. /*
  34. _Float16 is not supported in g++ so we avoid putting _Float16 definitions
  35. in the public headers.
  36. This function should at some point be moved in FastMath.
  37. */
  38. __STATIC_INLINE float16_t arm_exponent_f16(float16_t x, int32_t nb)
  39. {
  40. float16_t r = x;
  41. nb --;
  42. while(nb > 0)
  43. {
  44. r = (_Float16)r * (_Float16)x;
  45. nb--;
  46. }
  47. return(r);
  48. }
  49. #endif
  50. /**
  51. * @addtogroup polysvm
  52. * @{
  53. */
  54. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  55. #include "arm_helium_utils.h"
  56. #include "arm_vec_math_f16.h"
  57. /**
  58. * @brief SVM polynomial prediction
  59. * @param[in] S Pointer to an instance of the polynomial SVM structure.
  60. * @param[in] in Pointer to input vector
  61. * @param[out] pResult Decision value
  62. *
  63. */
  64. void arm_svm_polynomial_predict_f16(
  65. const arm_svm_polynomial_instance_f16 *S,
  66. const float16_t * in,
  67. int32_t * pResult)
  68. {
  69. /* inlined Matrix x Vector function interleaved with dot prod */
  70. uint32_t numRows = S->nbOfSupportVectors;
  71. uint32_t numCols = S->vectorDimension;
  72. const float16_t *pSupport = S->supportVectors;
  73. const float16_t *pSrcA = pSupport;
  74. const float16_t *pInA0;
  75. const float16_t *pInA1;
  76. uint32_t row;
  77. uint32_t blkCnt; /* loop counters */
  78. const float16_t *pDualCoef = S->dualCoefficients;
  79. _Float16 sum = S->intercept;
  80. f16x8_t vSum = vdupq_n_f16(0.0f);
  81. row = numRows;
  82. /*
  83. * compute 4 rows in parrallel
  84. */
  85. while (row >= 4) {
  86. const float16_t *pInA2, *pInA3;
  87. float16_t const *pSrcA0Vec, *pSrcA1Vec, *pSrcA2Vec, *pSrcA3Vec, *pInVec;
  88. f16x8_t vecIn, acc0, acc1, acc2, acc3;
  89. float16_t const *pSrcVecPtr = in;
  90. /*
  91. * Initialize the pointers to 4 consecutive MatrixA rows
  92. */
  93. pInA0 = pSrcA;
  94. pInA1 = pInA0 + numCols;
  95. pInA2 = pInA1 + numCols;
  96. pInA3 = pInA2 + numCols;
  97. /*
  98. * Initialize the vector pointer
  99. */
  100. pInVec = pSrcVecPtr;
  101. /*
  102. * reset accumulators
  103. */
  104. acc0 = vdupq_n_f16(0.0f);
  105. acc1 = vdupq_n_f16(0.0f);
  106. acc2 = vdupq_n_f16(0.0f);
  107. acc3 = vdupq_n_f16(0.0f);
  108. pSrcA0Vec = pInA0;
  109. pSrcA1Vec = pInA1;
  110. pSrcA2Vec = pInA2;
  111. pSrcA3Vec = pInA3;
  112. blkCnt = numCols >> 3;
  113. while (blkCnt > 0U) {
  114. f16x8_t vecA;
  115. vecIn = vld1q(pInVec);
  116. pInVec += 8;
  117. vecA = vld1q(pSrcA0Vec);
  118. pSrcA0Vec += 8;
  119. acc0 = vfmaq(acc0, vecIn, vecA);
  120. vecA = vld1q(pSrcA1Vec);
  121. pSrcA1Vec += 8;
  122. acc1 = vfmaq(acc1, vecIn, vecA);
  123. vecA = vld1q(pSrcA2Vec);
  124. pSrcA2Vec += 8;
  125. acc2 = vfmaq(acc2, vecIn, vecA);
  126. vecA = vld1q(pSrcA3Vec);
  127. pSrcA3Vec += 8;
  128. acc3 = vfmaq(acc3, vecIn, vecA);
  129. blkCnt--;
  130. }
  131. /*
  132. * tail
  133. * (will be merged thru tail predication)
  134. */
  135. blkCnt = numCols & 7;
  136. if (blkCnt > 0U) {
  137. mve_pred16_t p0 = vctp16q(blkCnt);
  138. f16x8_t vecA;
  139. vecIn = vldrhq_z_f16(pInVec, p0);
  140. vecA = vldrhq_z_f16(pSrcA0Vec, p0);
  141. acc0 = vfmaq(acc0, vecIn, vecA);
  142. vecA = vldrhq_z_f16(pSrcA1Vec, p0);
  143. acc1 = vfmaq(acc1, vecIn, vecA);
  144. vecA = vldrhq_z_f16(pSrcA2Vec, p0);
  145. acc2 = vfmaq(acc2, vecIn, vecA);
  146. vecA = vldrhq_z_f16(pSrcA3Vec, p0);
  147. acc3 = vfmaq(acc3, vecIn, vecA);
  148. }
  149. /*
  150. * Sum the partial parts
  151. */
  152. f16x8_t vtmp = vuninitializedq_f16();
  153. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc0), vtmp, 0);
  154. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc1), vtmp, 1);
  155. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc2), vtmp, 2);
  156. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc3), vtmp, 3);
  157. vSum = vfmaq_m_f16(vSum, vld1q(pDualCoef),
  158. arm_vec_exponent_f16
  159. (vaddq_n_f16(vmulq_n_f16(vtmp, S->gamma), S->coef0),
  160. S->degree),vctp16q(4));
  161. pDualCoef += 4;
  162. pSrcA += numCols * 4;
  163. /*
  164. * Decrement the row loop counter
  165. */
  166. row -= 4;
  167. }
  168. /*
  169. * compute 2 rows in parrallel
  170. */
  171. if (row >= 2) {
  172. float16_t const *pSrcA0Vec, *pSrcA1Vec, *pInVec;
  173. f16x8_t vecIn, acc0, acc1;
  174. float16_t const *pSrcVecPtr = in;
  175. /*
  176. * Initialize the pointers to 2 consecutive MatrixA rows
  177. */
  178. pInA0 = pSrcA;
  179. pInA1 = pInA0 + numCols;
  180. /*
  181. * Initialize the vector pointer
  182. */
  183. pInVec = pSrcVecPtr;
  184. /*
  185. * reset accumulators
  186. */
  187. acc0 = vdupq_n_f16(0.0f);
  188. acc1 = vdupq_n_f16(0.0f);
  189. pSrcA0Vec = pInA0;
  190. pSrcA1Vec = pInA1;
  191. blkCnt = numCols >> 3;
  192. while (blkCnt > 0U) {
  193. f16x8_t vecA;
  194. vecIn = vld1q(pInVec);
  195. pInVec += 8;
  196. vecA = vld1q(pSrcA0Vec);
  197. pSrcA0Vec += 8;
  198. acc0 = vfmaq(acc0, vecIn, vecA);
  199. vecA = vld1q(pSrcA1Vec);
  200. pSrcA1Vec += 8;
  201. acc1 = vfmaq(acc1, vecIn, vecA);
  202. blkCnt--;
  203. }
  204. /*
  205. * tail
  206. * (will be merged thru tail predication)
  207. */
  208. blkCnt = numCols & 7;
  209. if (blkCnt > 0U) {
  210. mve_pred16_t p0 = vctp16q(blkCnt);
  211. f16x8_t vecA;
  212. vecIn = vldrhq_z_f16(pInVec, p0);
  213. vecA = vldrhq_z_f16(pSrcA0Vec, p0);
  214. acc0 = vfmaq(acc0, vecIn, vecA);
  215. vecA = vldrhq_z_f16(pSrcA1Vec, p0);
  216. acc1 = vfmaq(acc1, vecIn, vecA);
  217. }
  218. /*
  219. * Sum the partial parts
  220. */
  221. f16x8_t vtmp = vuninitializedq_f16();
  222. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc0), vtmp, 0);
  223. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc1), vtmp, 1);
  224. vSum = vfmaq_m_f16(vSum, vld1q(pDualCoef),
  225. arm_vec_exponent_f16
  226. (vaddq_n_f16(vmulq_n_f16(vtmp, S->gamma), S->coef0), S->degree),
  227. vctp16q(2));
  228. pDualCoef += 2;
  229. pSrcA += numCols * 2;
  230. row -= 2;
  231. }
  232. if (row >= 1) {
  233. f16x8_t vecIn, acc0;
  234. float16_t const *pSrcA0Vec, *pInVec;
  235. float16_t const *pSrcVecPtr = in;
  236. /*
  237. * Initialize the pointers to last MatrixA row
  238. */
  239. pInA0 = pSrcA;
  240. /*
  241. * Initialize the vector pointer
  242. */
  243. pInVec = pSrcVecPtr;
  244. /*
  245. * reset accumulators
  246. */
  247. acc0 = vdupq_n_f16(0.0f);
  248. pSrcA0Vec = pInA0;
  249. blkCnt = numCols >> 3;
  250. while (blkCnt > 0U) {
  251. f16x8_t vecA;
  252. vecIn = vld1q(pInVec);
  253. pInVec += 8;
  254. vecA = vld1q(pSrcA0Vec);
  255. pSrcA0Vec += 8;
  256. acc0 = vfmaq(acc0, vecIn, vecA);
  257. blkCnt--;
  258. }
  259. /*
  260. * tail
  261. * (will be merged thru tail predication)
  262. */
  263. blkCnt = numCols & 7;
  264. if (blkCnt > 0U) {
  265. mve_pred16_t p0 = vctp16q(blkCnt);
  266. f16x8_t vecA;
  267. vecIn = vldrhq_z_f16(pInVec, p0);
  268. vecA = vldrhq_z_f16(pSrcA0Vec, p0);
  269. acc0 = vfmaq(acc0, vecIn, vecA);
  270. }
  271. /*
  272. * Sum the partial parts
  273. */
  274. f16x8_t vtmp = vuninitializedq_f16();
  275. vtmp = vsetq_lane(vecAddAcrossF16Mve(acc0), vtmp, 0);
  276. vSum = vfmaq_m_f16(vSum, vld1q(pDualCoef),
  277. arm_vec_exponent_f16
  278. (vaddq_n_f16(vmulq_n_f16(vtmp, S->gamma), S->coef0), S->degree),
  279. vctp16q(1));
  280. }
  281. sum += (_Float16)vecAddAcrossF16Mve(vSum);
  282. *pResult = S->classes[STEP(sum)];
  283. }
  284. #else
  285. /**
  286. * @brief SVM polynomial prediction
  287. * @param[in] S Pointer to an instance of the polynomial SVM structure.
  288. * @param[in] in Pointer to input vector
  289. * @param[out] pResult Decision value
  290. *
  291. */
  292. void arm_svm_polynomial_predict_f16(
  293. const arm_svm_polynomial_instance_f16 *S,
  294. const float16_t * in,
  295. int32_t * pResult)
  296. {
  297. _Float16 sum=S->intercept;
  298. _Float16 dot=0;
  299. uint32_t i,j;
  300. const float16_t *pSupport = S->supportVectors;
  301. for(i=0; i < S->nbOfSupportVectors; i++)
  302. {
  303. dot=0;
  304. for(j=0; j < S->vectorDimension; j++)
  305. {
  306. dot = (_Float16)dot + (_Float16)in[j]* (_Float16)*pSupport++;
  307. }
  308. sum += (_Float16)S->dualCoefficients[i] * (_Float16)arm_exponent_f16((_Float16)S->gamma * (_Float16)dot + (_Float16)S->coef0, S->degree);
  309. }
  310. *pResult=S->classes[STEP(sum)];
  311. }
  312. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  313. /**
  314. * @} end of polysvm group
  315. */
  316. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */