arm_svm_sigmoid_predict_f32.c 13 KB

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