arm_pool_q7_HWC.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Copyright (C) 2010-2021 Arm Limited or its affiliates. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /* ----------------------------------------------------------------------
  19. * Project: CMSIS NN Library
  20. * Title: arm_pool_q7_HWC.c
  21. * Description: Pooling function implementations
  22. *
  23. * $Date: 20. July 2021
  24. * $Revision: V.1.1.1
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. #if defined(ARM_MATH_DSP) && !defined(ARM_MATH_MVEI)
  32. /**
  33. * @brief A few utility functions used by pooling functions
  34. *
  35. *
  36. */
  37. static void buffer_scale_back_q15_to_q7(q15_t *buffer, q7_t *target, uint16_t length, uint16_t scale)
  38. {
  39. int i;
  40. for (i = 0; i < length; i++)
  41. {
  42. target[i] = (q7_t)(buffer[i] / scale);
  43. }
  44. }
  45. static void compare_and_replace_if_larger_q7(q7_t *base, // base data
  46. const q7_t *target, // compare target
  47. const uint16_t length // data length
  48. )
  49. {
  50. q7_t *pIn = base;
  51. const q7_t *pCom = target;
  52. union arm_nnword in;
  53. union arm_nnword com;
  54. uint16_t cnt = length >> 2;
  55. while (cnt > 0u)
  56. {
  57. in.word = arm_nn_read_q7x4((const q7_t *)pIn);
  58. com.word = arm_nn_read_q7x4_ia((const q7_t **)&pCom);
  59. // if version
  60. if (com.bytes[0] > in.bytes[0])
  61. in.bytes[0] = com.bytes[0];
  62. if (com.bytes[1] > in.bytes[1])
  63. in.bytes[1] = com.bytes[1];
  64. if (com.bytes[2] > in.bytes[2])
  65. in.bytes[2] = com.bytes[2];
  66. if (com.bytes[3] > in.bytes[3])
  67. in.bytes[3] = com.bytes[3];
  68. arm_nn_write_q7x4_ia(&pIn, in.word);
  69. cnt--;
  70. }
  71. cnt = length & 0x3;
  72. while (cnt > 0u)
  73. {
  74. if (*pCom > *pIn)
  75. {
  76. *pIn = *pCom;
  77. }
  78. pIn++;
  79. pCom++;
  80. cnt--;
  81. }
  82. }
  83. static void accumulate_q7_to_q15(q15_t *base, q7_t *target, const uint16_t length)
  84. {
  85. q15_t *pCnt = base;
  86. q7_t *pV = target;
  87. q31_t v1, v2, vo1, vo2;
  88. uint16_t cnt = length >> 2;
  89. q31_t in;
  90. while (cnt > 0u)
  91. {
  92. q31_t value = arm_nn_read_q7x4_ia((const q7_t **)&pV);
  93. v1 = __SXTB16(__ROR(value, 8));
  94. v2 = __SXTB16(value);
  95. #ifndef ARM_MATH_BIG_ENDIAN
  96. vo2 = __PKHTB(v1, v2, 16);
  97. vo1 = __PKHBT(v2, v1, 16);
  98. #else
  99. vo1 = __PKHTB(v1, v2, 16);
  100. vo2 = __PKHBT(v2, v1, 16);
  101. #endif
  102. in = arm_nn_read_q15x2(pCnt);
  103. arm_nn_write_q15x2_ia(&pCnt, __QADD16(vo1, in));
  104. in = arm_nn_read_q15x2(pCnt);
  105. arm_nn_write_q15x2_ia(&pCnt, __QADD16(vo2, in));
  106. cnt--;
  107. }
  108. cnt = length & 0x3;
  109. while (cnt > 0u)
  110. {
  111. *pCnt++ += *pV++;
  112. cnt--;
  113. }
  114. }
  115. #endif // ARM_MATH_DSP
  116. /**
  117. * @ingroup groupNN
  118. */
  119. /**
  120. * @addtogroup Pooling
  121. * @{
  122. */
  123. /**
  124. * @brief Q7 max pooling function
  125. * @param[in, out] Im_in pointer to input tensor
  126. * @param[in] dim_im_in input tensor dimention
  127. * @param[in] ch_im_in number of input tensor channels
  128. * @param[in] dim_kernel filter kernel size
  129. * @param[in] padding padding sizes
  130. * @param[in] stride convolution stride
  131. * @param[in] dim_im_out output tensor dimension
  132. * @param[in,out] bufferA Not used
  133. * @param[in,out] Im_out pointer to output tensor
  134. *
  135. * @details
  136. *
  137. * The pooling function is implemented as split x-pooling then
  138. * y-pooling.
  139. *
  140. * This pooling function is input-destructive. Input data is undefined
  141. * after calling this function.
  142. *
  143. */
  144. void arm_maxpool_q7_HWC(q7_t *Im_in,
  145. const uint16_t dim_im_in,
  146. const uint16_t ch_im_in,
  147. const uint16_t dim_kernel,
  148. const uint16_t padding,
  149. const uint16_t stride,
  150. const uint16_t dim_im_out,
  151. q7_t *bufferA,
  152. q7_t *Im_out)
  153. {
  154. (void)bufferA;
  155. #if defined(ARM_MATH_DSP) && !defined(ARM_MATH_MVEI)
  156. /* Run the following code for Cortex-M4 and Cortex-M7 */
  157. int16_t i_x, i_y;
  158. /* first does the pooling along x axis */
  159. for (i_y = 0; i_y < dim_im_in; i_y++)
  160. {
  161. for (i_x = 0; i_x < dim_im_out; i_x++)
  162. {
  163. /* for each output pixel */
  164. q7_t *target = Im_in + (i_y * dim_im_in + i_x) * ch_im_in;
  165. q7_t *win_start;
  166. q7_t *win_stop;
  167. if (i_x * stride - padding < 0)
  168. {
  169. win_start = target;
  170. }
  171. else
  172. {
  173. win_start = Im_in + (i_y * dim_im_in + i_x * stride - padding) * ch_im_in;
  174. }
  175. if (i_x * stride - padding + dim_kernel >= dim_im_in)
  176. {
  177. win_stop = Im_in + (i_y * dim_im_in + dim_im_in) * ch_im_in;
  178. }
  179. else
  180. {
  181. win_stop = Im_in + (i_y * dim_im_in + i_x * stride - padding + dim_kernel) * ch_im_in;
  182. }
  183. /* first step is to copy over initial data */
  184. /* arm_copy_q7(win_start, target, ch_im_in); */
  185. memmove(target, win_start, ch_im_in);
  186. /* start the max operation from the second part */
  187. win_start += ch_im_in;
  188. for (; win_start < win_stop; win_start += ch_im_in)
  189. {
  190. compare_and_replace_if_larger_q7(target, win_start, ch_im_in);
  191. }
  192. }
  193. }
  194. /* then does the pooling along y axis */
  195. for (i_y = 0; i_y < dim_im_out; i_y++)
  196. {
  197. /* for each output row */
  198. q7_t *target = Im_out + i_y * dim_im_out * ch_im_in;
  199. q7_t *row_start;
  200. q7_t *row_end;
  201. /* setting the starting row */
  202. if (i_y * stride - padding < 0)
  203. {
  204. row_start = Im_in;
  205. }
  206. else
  207. {
  208. row_start = Im_in + (i_y * stride - padding) * dim_im_in * ch_im_in;
  209. }
  210. /* setting the stopping row */
  211. if (i_y * stride - padding + dim_kernel >= dim_im_in)
  212. {
  213. row_end = Im_in + dim_im_in * dim_im_in * ch_im_in;
  214. }
  215. else
  216. {
  217. row_end = Im_in + (i_y * stride - padding + dim_kernel) * dim_im_in * ch_im_in;
  218. }
  219. /* copy over the first row */
  220. /* arm_copy_q7(row_start, target, dim_im_out * ch_im_in); */
  221. memmove(target, row_start, dim_im_out * ch_im_in);
  222. /* move over to next row */
  223. row_start += ch_im_in * dim_im_in;
  224. for (; row_start < row_end; row_start += dim_im_in * ch_im_in)
  225. {
  226. compare_and_replace_if_larger_q7(target, row_start, dim_im_out * ch_im_in);
  227. }
  228. }
  229. #else
  230. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  231. int16_t i_ch_in, i_x, i_y;
  232. int16_t k_x, k_y;
  233. for (i_ch_in = 0; i_ch_in < ch_im_in; i_ch_in++)
  234. {
  235. for (i_y = 0; i_y < dim_im_out; i_y++)
  236. {
  237. for (i_x = 0; i_x < dim_im_out; i_x++)
  238. {
  239. int max = -129;
  240. for (k_y = i_y * stride - padding; k_y < i_y * stride - padding + dim_kernel; k_y++)
  241. {
  242. for (k_x = i_x * stride - padding; k_x < i_x * stride - padding + dim_kernel; k_x++)
  243. {
  244. if (k_y >= 0 && k_x >= 0 && k_y < dim_im_in && k_x < dim_im_in)
  245. {
  246. if (Im_in[i_ch_in + ch_im_in * (k_x + k_y * dim_im_in)] > max)
  247. {
  248. max = Im_in[i_ch_in + ch_im_in * (k_x + k_y * dim_im_in)];
  249. }
  250. }
  251. }
  252. }
  253. Im_out[i_ch_in + ch_im_in * (i_x + i_y * dim_im_out)] = max;
  254. }
  255. }
  256. }
  257. #endif /* ARM_MATH_DSP */
  258. }
  259. /**
  260. * @brief Q7 average pooling function
  261. * @param[in,out] Im_in pointer to input tensor
  262. * @param[in] dim_im_in input tensor dimention
  263. * @param[in] ch_im_in number of input tensor channels
  264. * @param[in] dim_kernel filter kernel size
  265. * @param[in] padding padding sizes
  266. * @param[in] stride convolution stride
  267. * @param[in] dim_im_out output tensor dimension
  268. * @param[in,out] bufferA pointer to buffer space for input
  269. * @param[in,out] Im_out pointer to output tensor
  270. *
  271. * @details
  272. *
  273. * <b>Buffer size:</b>
  274. *
  275. * bufferA size: 2*dim_im_out*ch_im_in
  276. *
  277. * The pooling function is implemented as split x-pooling then
  278. * y-pooling.
  279. *
  280. * This pooling function is input-destructive. Input data is undefined
  281. * after calling this function.
  282. *
  283. */
  284. void arm_avepool_q7_HWC(q7_t *Im_in,
  285. const uint16_t dim_im_in,
  286. const uint16_t ch_im_in,
  287. const uint16_t dim_kernel,
  288. const uint16_t padding,
  289. const uint16_t stride,
  290. const uint16_t dim_im_out,
  291. q7_t *bufferA,
  292. q7_t *Im_out)
  293. {
  294. #if defined(ARM_MATH_DSP) && !defined(ARM_MATH_MVEI)
  295. /* Run the following code for Cortex-M4 and Cortex-M7 */
  296. q15_t *buffer = (q15_t *)bufferA;
  297. int16_t i_x, i_y;
  298. int16_t count = 0;
  299. /* first does the pooling along x axis */
  300. for (i_y = 0; i_y < dim_im_in; i_y++)
  301. {
  302. for (i_x = 0; i_x < dim_im_out; i_x++)
  303. {
  304. /* for each output pixel */
  305. q7_t *target = Im_in + (i_y * dim_im_in + i_x) * ch_im_in;
  306. q7_t *win_start;
  307. q7_t *win_stop;
  308. if (i_x * stride - padding < 0)
  309. {
  310. win_start = target;
  311. }
  312. else
  313. {
  314. win_start = Im_in + (i_y * dim_im_in + i_x * stride - padding) * ch_im_in;
  315. }
  316. if (i_x * stride - padding + dim_kernel >= dim_im_in)
  317. {
  318. win_stop = Im_in + (i_y * dim_im_in + dim_im_in) * ch_im_in;
  319. }
  320. else
  321. {
  322. win_stop = Im_in + (i_y * dim_im_in + i_x * stride - padding + dim_kernel) * ch_im_in;
  323. }
  324. /* first step is to copy over initial data */
  325. arm_q7_to_q15_no_shift(win_start, buffer, ch_im_in);
  326. count = 1;
  327. /* start the max operation from the second part */
  328. win_start += ch_im_in;
  329. for (; win_start < win_stop; win_start += ch_im_in)
  330. {
  331. accumulate_q7_to_q15(buffer, win_start, ch_im_in);
  332. count++;
  333. }
  334. buffer_scale_back_q15_to_q7(buffer, target, ch_im_in, count);
  335. }
  336. }
  337. /* then does the pooling along y axis */
  338. for (i_y = 0; i_y < dim_im_out; i_y++)
  339. {
  340. /* for each output row */
  341. q7_t *target = Im_out + i_y * dim_im_out * ch_im_in;
  342. q7_t *row_start;
  343. q7_t *row_end;
  344. /* setting the starting row */
  345. if (i_y * stride - padding < 0)
  346. {
  347. row_start = Im_in;
  348. }
  349. else
  350. {
  351. row_start = Im_in + (i_y * stride - padding) * dim_im_in * ch_im_in;
  352. }
  353. /* setting the stopping row */
  354. if (i_y * stride - padding + dim_kernel >= dim_im_in)
  355. {
  356. row_end = Im_in + dim_im_in * dim_im_in * ch_im_in;
  357. }
  358. else
  359. {
  360. row_end = Im_in + (i_y * stride - padding + dim_kernel) * dim_im_in * ch_im_in;
  361. }
  362. /* copy over the first row */
  363. arm_q7_to_q15_no_shift(row_start, buffer, dim_im_out * ch_im_in);
  364. count = 1;
  365. /* move over to next row */
  366. row_start += ch_im_in * dim_im_in;
  367. for (; row_start < row_end; row_start += dim_im_in * ch_im_in)
  368. {
  369. accumulate_q7_to_q15(buffer, row_start, dim_im_out * ch_im_in);
  370. count++;
  371. }
  372. buffer_scale_back_q15_to_q7(buffer, target, dim_im_out * ch_im_in, count);
  373. }
  374. #else
  375. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  376. (void)bufferA;
  377. int16_t i_ch_in, i_x, i_y;
  378. int16_t k_x, k_y;
  379. for (i_ch_in = 0; i_ch_in < ch_im_in; i_ch_in++)
  380. {
  381. for (i_y = 0; i_y < dim_im_out; i_y++)
  382. {
  383. for (i_x = 0; i_x < dim_im_out; i_x++)
  384. {
  385. int sum = 0;
  386. int count = 0;
  387. for (k_y = i_y * stride - padding; k_y < i_y * stride - padding + dim_kernel; k_y++)
  388. {
  389. for (k_x = i_x * stride - padding; k_x < i_x * stride - padding + dim_kernel; k_x++)
  390. {
  391. if (k_y >= 0 && k_x >= 0 && k_y < dim_im_in && k_x < dim_im_in)
  392. {
  393. sum += Im_in[i_ch_in + ch_im_in * (k_x + k_y * dim_im_in)];
  394. count++;
  395. }
  396. }
  397. }
  398. Im_out[i_ch_in + ch_im_in * (i_x + i_y * dim_im_out)] = sum / count;
  399. }
  400. }
  401. }
  402. #endif /* ARM_MATH_DSP */
  403. }
  404. /**
  405. * @} end of Pooling group
  406. */