arm_svm_polynomial_predict_f16.c 9.5 KB

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