arm_svm_sigmoid_predict_f32.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_svm_sigmoid_predict_f32.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.h"
  29. #include <limits.h>
  30. #include <math.h>
  31. /**
  32. * @addtogroup sigmoidsvm
  33. * @{
  34. */
  35. /**
  36. * @brief SVM sigmoid prediction
  37. * @param[in] S Pointer to an instance of the rbf SVM structure.
  38. * @param[in] in Pointer to input vector
  39. * @param[out] pResult Decision value
  40. * @return none.
  41. *
  42. */
  43. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. #include "arm_helium_utils.h"
  45. #include "arm_vec_math.h"
  46. void arm_svm_sigmoid_predict_f32(
  47. const arm_svm_sigmoid_instance_f32 *S,
  48. const float32_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 float32_t *pSupport = S->supportVectors;
  55. const float32_t *pSrcA = pSupport;
  56. const float32_t *pInA0;
  57. const float32_t *pInA1;
  58. uint32_t row;
  59. uint32_t blkCnt; /* loop counters */
  60. const float32_t *pDualCoef = S->dualCoefficients;
  61. float32_t sum = S->intercept;
  62. f32x4_t vSum = vdupq_n_f32(0.0f);
  63. row = numRows;
  64. /*
  65. * compute 4 rows in parrallel
  66. */
  67. while (row >= 4) {
  68. const float32_t *pInA2, *pInA3;
  69. float32_t const *pSrcA0Vec, *pSrcA1Vec, *pSrcA2Vec, *pSrcA3Vec, *pInVec;
  70. f32x4_t vecIn, acc0, acc1, acc2, acc3;
  71. float32_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_f32(0.0f);
  87. acc1 = vdupq_n_f32(0.0f);
  88. acc2 = vdupq_n_f32(0.0f);
  89. acc3 = vdupq_n_f32(0.0f);
  90. pSrcA0Vec = pInA0;
  91. pSrcA1Vec = pInA1;
  92. pSrcA2Vec = pInA2;
  93. pSrcA3Vec = pInA3;
  94. blkCnt = numCols >> 2;
  95. while (blkCnt > 0U) {
  96. f32x4_t vecA;
  97. vecIn = vld1q(pInVec);
  98. pInVec += 4;
  99. vecA = vld1q(pSrcA0Vec);
  100. pSrcA0Vec += 4;
  101. acc0 = vfmaq(acc0, vecIn, vecA);
  102. vecA = vld1q(pSrcA1Vec);
  103. pSrcA1Vec += 4;
  104. acc1 = vfmaq(acc1, vecIn, vecA);
  105. vecA = vld1q(pSrcA2Vec);
  106. pSrcA2Vec += 4;
  107. acc2 = vfmaq(acc2, vecIn, vecA);
  108. vecA = vld1q(pSrcA3Vec);
  109. pSrcA3Vec += 4;
  110. acc3 = vfmaq(acc3, vecIn, vecA);
  111. blkCnt--;
  112. }
  113. /*
  114. * tail
  115. * (will be merged thru tail predication)
  116. */
  117. blkCnt = numCols & 3;
  118. if (blkCnt > 0U) {
  119. mve_pred16_t p0 = vctp32q(blkCnt);
  120. f32x4_t vecA;
  121. vecIn = vldrwq_z_f32(pInVec, p0);
  122. vecA = vldrwq_z_f32(pSrcA0Vec, p0);
  123. acc0 = vfmaq(acc0, vecIn, vecA);
  124. vecA = vldrwq_z_f32(pSrcA1Vec, p0);
  125. acc1 = vfmaq(acc1, vecIn, vecA);
  126. vecA = vldrwq_z_f32(pSrcA2Vec, p0);
  127. acc2 = vfmaq(acc2, vecIn, vecA);
  128. vecA = vldrwq_z_f32(pSrcA3Vec, p0);
  129. acc3 = vfmaq(acc3, vecIn, vecA);
  130. }
  131. /*
  132. * Sum the partial parts
  133. */
  134. f32x4_t vtmp = vuninitializedq_f32();
  135. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc0), vtmp, 0);
  136. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc1), vtmp, 1);
  137. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc2), vtmp, 2);
  138. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc3), vtmp, 3);
  139. vSum =
  140. vfmaq_f32(vSum, vld1q(pDualCoef),
  141. vtanhq_f32(vaddq_n_f32(vmulq_n_f32(vtmp, S->gamma), S->coef0)));
  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. float32_t const *pSrcA0Vec, *pSrcA1Vec, *pInVec;
  154. f32x4_t vecIn, acc0, acc1;
  155. float32_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_f32(0.0f);
  169. acc1 = vdupq_n_f32(0.0f);
  170. pSrcA0Vec = pInA0;
  171. pSrcA1Vec = pInA1;
  172. blkCnt = numCols >> 2;
  173. while (blkCnt > 0U) {
  174. f32x4_t vecA;
  175. vecIn = vld1q(pInVec);
  176. pInVec += 4;
  177. vecA = vld1q(pSrcA0Vec);
  178. pSrcA0Vec += 4;
  179. acc0 = vfmaq(acc0, vecIn, vecA);
  180. vecA = vld1q(pSrcA1Vec);
  181. pSrcA1Vec += 4;
  182. acc1 = vfmaq(acc1, vecIn, vecA);
  183. blkCnt--;
  184. }
  185. /*
  186. * tail
  187. * (will be merged thru tail predication)
  188. */
  189. blkCnt = numCols & 3;
  190. if (blkCnt > 0U) {
  191. mve_pred16_t p0 = vctp32q(blkCnt);
  192. f32x4_t vecA;
  193. vecIn = vldrwq_z_f32(pInVec, p0);
  194. vecA = vldrwq_z_f32(pSrcA0Vec, p0);
  195. acc0 = vfmaq(acc0, vecIn, vecA);
  196. vecA = vldrwq_z_f32(pSrcA1Vec, p0);
  197. acc1 = vfmaq(acc1, vecIn, vecA);
  198. }
  199. /*
  200. * Sum the partial parts
  201. */
  202. f32x4_t vtmp = vuninitializedq_f32();
  203. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc0), vtmp, 0);
  204. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc1), vtmp, 1);
  205. vSum =
  206. vfmaq_m_f32(vSum, vld1q(pDualCoef),
  207. vtanhq_f32(vaddq_n_f32(vmulq_n_f32(vtmp, S->gamma), S->coef0)),
  208. vctp32q(2));
  209. pSrcA += numCols * 2;
  210. row -= 2;
  211. }
  212. if (row >= 1) {
  213. f32x4_t vecIn, acc0;
  214. float32_t const *pSrcA0Vec, *pInVec;
  215. float32_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_f32(0.0f);
  228. pSrcA0Vec = pInA0;
  229. blkCnt = numCols >> 2;
  230. while (blkCnt > 0U) {
  231. f32x4_t vecA;
  232. vecIn = vld1q(pInVec);
  233. pInVec += 4;
  234. vecA = vld1q(pSrcA0Vec);
  235. pSrcA0Vec += 4;
  236. acc0 = vfmaq(acc0, vecIn, vecA);
  237. blkCnt--;
  238. }
  239. /*
  240. * tail
  241. * (will be merged thru tail predication)
  242. */
  243. blkCnt = numCols & 3;
  244. if (blkCnt > 0U) {
  245. mve_pred16_t p0 = vctp32q(blkCnt);
  246. f32x4_t vecA;
  247. vecIn = vldrwq_z_f32(pInVec, p0);
  248. vecA = vldrwq_z_f32(pSrcA0Vec, p0);
  249. acc0 = vfmaq(acc0, vecIn, vecA);
  250. }
  251. /*
  252. * Sum the partial parts
  253. */
  254. f32x4_t vtmp = vuninitializedq_f32();
  255. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc0), vtmp, 0);
  256. vSum =
  257. vfmaq_m_f32(vSum, vld1q(pDualCoef),
  258. vtanhq_f32(vaddq_n_f32(vmulq_n_f32(vtmp, S->gamma), S->coef0)),
  259. vctp32q(1));
  260. }
  261. sum += vecAddAcrossF32Mve(vSum);
  262. *pResult = S->classes[STEP(sum)];
  263. }
  264. #else
  265. #if defined(ARM_MATH_NEON)
  266. #include "NEMath.h"
  267. void arm_svm_sigmoid_predict_f32(
  268. const arm_svm_sigmoid_instance_f32 *S,
  269. const float32_t * in,
  270. int32_t * pResult)
  271. {
  272. float32_t sum = S->intercept;
  273. float32_t dot;
  274. float32x4_t dotV;
  275. float32x4_t accuma,accumb,accumc,accumd,accum;
  276. float32x2_t accum2;
  277. float32x4_t vec1;
  278. float32x4_t coef0 = vdupq_n_f32(S->coef0);
  279. float32x4_t vec2,vec2a,vec2b,vec2c,vec2d;
  280. uint32_t blkCnt;
  281. uint32_t vectorBlkCnt;
  282. const float32_t *pIn = in;
  283. const float32_t *pSupport = S->supportVectors;
  284. const float32_t *pSupporta = S->supportVectors;
  285. const float32_t *pSupportb;
  286. const float32_t *pSupportc;
  287. const float32_t *pSupportd;
  288. pSupportb = pSupporta + S->vectorDimension;
  289. pSupportc = pSupportb + S->vectorDimension;
  290. pSupportd = pSupportc + S->vectorDimension;
  291. const float32_t *pDualCoefs = S->dualCoefficients;
  292. vectorBlkCnt = S->nbOfSupportVectors >> 2;
  293. while (vectorBlkCnt > 0U)
  294. {
  295. accuma = vdupq_n_f32(0);
  296. accumb = vdupq_n_f32(0);
  297. accumc = vdupq_n_f32(0);
  298. accumd = vdupq_n_f32(0);
  299. pIn = in;
  300. blkCnt = S->vectorDimension >> 2;
  301. while (blkCnt > 0U)
  302. {
  303. vec1 = vld1q_f32(pIn);
  304. vec2a = vld1q_f32(pSupporta);
  305. vec2b = vld1q_f32(pSupportb);
  306. vec2c = vld1q_f32(pSupportc);
  307. vec2d = vld1q_f32(pSupportd);
  308. pIn += 4;
  309. pSupporta += 4;
  310. pSupportb += 4;
  311. pSupportc += 4;
  312. pSupportd += 4;
  313. accuma = vmlaq_f32(accuma, vec1,vec2a);
  314. accumb = vmlaq_f32(accumb, vec1,vec2b);
  315. accumc = vmlaq_f32(accumc, vec1,vec2c);
  316. accumd = vmlaq_f32(accumd, vec1,vec2d);
  317. blkCnt -- ;
  318. }
  319. accum2 = vpadd_f32(vget_low_f32(accuma),vget_high_f32(accuma));
  320. dotV = vsetq_lane_f32(vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1),dotV,0);
  321. accum2 = vpadd_f32(vget_low_f32(accumb),vget_high_f32(accumb));
  322. dotV = vsetq_lane_f32(vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1),dotV,1);
  323. accum2 = vpadd_f32(vget_low_f32(accumc),vget_high_f32(accumc));
  324. dotV = vsetq_lane_f32(vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1),dotV,2);
  325. accum2 = vpadd_f32(vget_low_f32(accumd),vget_high_f32(accumd));
  326. dotV = vsetq_lane_f32(vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1),dotV,3);
  327. blkCnt = S->vectorDimension & 3;
  328. while (blkCnt > 0U)
  329. {
  330. dotV = vsetq_lane_f32(vgetq_lane_f32(dotV,0) + *pIn * *pSupporta++, dotV,0);
  331. dotV = vsetq_lane_f32(vgetq_lane_f32(dotV,1) + *pIn * *pSupportb++, dotV,1);
  332. dotV = vsetq_lane_f32(vgetq_lane_f32(dotV,2) + *pIn * *pSupportc++, dotV,2);
  333. dotV = vsetq_lane_f32(vgetq_lane_f32(dotV,3) + *pIn * *pSupportd++, dotV,3);
  334. pIn++;
  335. blkCnt -- ;
  336. }
  337. vec1 = vld1q_f32(pDualCoefs);
  338. pDualCoefs += 4;
  339. // To vectorize later
  340. dotV = vmulq_n_f32(dotV, S->gamma);
  341. dotV = vaddq_f32(dotV, coef0);
  342. dotV = vtanhq_f32(dotV);
  343. accum = vmulq_f32(vec1,dotV);
  344. accum2 = vpadd_f32(vget_low_f32(accum),vget_high_f32(accum));
  345. sum += vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1);
  346. pSupporta += 3*S->vectorDimension;
  347. pSupportb += 3*S->vectorDimension;
  348. pSupportc += 3*S->vectorDimension;
  349. pSupportd += 3*S->vectorDimension;
  350. vectorBlkCnt -- ;
  351. }
  352. pSupport = pSupporta;
  353. vectorBlkCnt = S->nbOfSupportVectors & 3;
  354. while (vectorBlkCnt > 0U)
  355. {
  356. accum = vdupq_n_f32(0);
  357. dot = 0.0f;
  358. pIn = in;
  359. blkCnt = S->vectorDimension >> 2;
  360. while (blkCnt > 0U)
  361. {
  362. vec1 = vld1q_f32(pIn);
  363. vec2 = vld1q_f32(pSupport);
  364. pIn += 4;
  365. pSupport += 4;
  366. accum = vmlaq_f32(accum, vec1,vec2);
  367. blkCnt -- ;
  368. }
  369. accum2 = vpadd_f32(vget_low_f32(accum),vget_high_f32(accum));
  370. dot = vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1);
  371. blkCnt = S->vectorDimension & 3;
  372. while (blkCnt > 0U)
  373. {
  374. dot = dot + *pIn++ * *pSupport++;
  375. blkCnt -- ;
  376. }
  377. sum += *pDualCoefs++ * tanhf(S->gamma * dot + S->coef0);
  378. vectorBlkCnt -- ;
  379. }
  380. *pResult=S->classes[STEP(sum)];
  381. }
  382. #else
  383. void arm_svm_sigmoid_predict_f32(
  384. const arm_svm_sigmoid_instance_f32 *S,
  385. const float32_t * in,
  386. int32_t * pResult)
  387. {
  388. float32_t sum=S->intercept;
  389. float32_t dot=0;
  390. uint32_t i,j;
  391. const float32_t *pSupport = S->supportVectors;
  392. for(i=0; i < S->nbOfSupportVectors; i++)
  393. {
  394. dot=0;
  395. for(j=0; j < S->vectorDimension; j++)
  396. {
  397. dot = dot + in[j]* *pSupport++;
  398. }
  399. sum += S->dualCoefficients[i] * tanhf(S->gamma * dot + S->coef0);
  400. }
  401. *pResult=S->classes[STEP(sum)];
  402. }
  403. #endif
  404. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  405. /**
  406. * @} end of sigmoidsvm group
  407. */