arm_pool_q7_HWC.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright (C) 2010-2018 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: 17. January 2018
  24. * $Revision: V.1.0.0
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_math.h"
  30. #include "arm_nnfunctions.h"
  31. #if defined (ARM_MATH_DSP)
  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 = *__SIMD32(pIn);
  58. com.word = *__SIMD32(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. *__SIMD32(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 = *__SIMD32(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 = *__SIMD32(pCnt);
  103. *__SIMD32(pCnt)++ = __QADD16(vo1, in);
  104. in = *__SIMD32(pCnt);
  105. *__SIMD32(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 pointer to buffer space for input
  133. * @param[in,out] Im_out pointer to output tensor
  134. * @return none.
  135. *
  136. * @details
  137. *
  138. * <b>Buffer size:</b>
  139. *
  140. * bufferA size: 0
  141. *
  142. * The pooling function is implemented as split x-pooling then
  143. * y-pooling.
  144. *
  145. * This pooling function is input-destructive. Input data is undefined
  146. * after calling this function.
  147. *
  148. */
  149. void
  150. arm_maxpool_q7_HWC(q7_t * Im_in,
  151. const uint16_t dim_im_in,
  152. const uint16_t ch_im_in,
  153. const uint16_t dim_kernel,
  154. const uint16_t padding,
  155. const uint16_t stride, const uint16_t dim_im_out, q7_t * bufferA, q7_t * Im_out)
  156. {
  157. #if defined (ARM_MATH_DSP)
  158. /* Run the following code for Cortex-M4 and Cortex-M7 */
  159. int16_t i_x, i_y;
  160. /* first does the pooling along x axis */
  161. for (i_y = 0; i_y < dim_im_in; i_y++)
  162. {
  163. for (i_x = 0; i_x < dim_im_out; i_x++)
  164. {
  165. /* for each output pixel */
  166. q7_t *target = Im_in + (i_y * dim_im_in + i_x) * ch_im_in;
  167. q7_t *win_start;
  168. q7_t *win_stop;
  169. if (i_x * stride - padding < 0)
  170. {
  171. win_start = target;
  172. } else
  173. {
  174. win_start = Im_in + (i_y * dim_im_in + i_x * stride - padding) * ch_im_in;
  175. }
  176. if (i_x * stride - padding + dim_kernel >= dim_im_in)
  177. {
  178. win_stop = Im_in + (i_y * dim_im_in + dim_im_in) * ch_im_in;
  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. } else
  206. {
  207. row_start = Im_in + (i_y * stride - padding) * dim_im_in * ch_im_in;
  208. }
  209. /* setting the stopping row */
  210. if (i_y * stride - padding + dim_kernel >= dim_im_in)
  211. {
  212. row_end = Im_in + dim_im_in * dim_im_in * ch_im_in;
  213. } else
  214. {
  215. row_end = Im_in + (i_y * stride - padding + dim_kernel) * dim_im_in * ch_im_in;
  216. }
  217. /* copy over the first row */
  218. /* arm_copy_q7(row_start, target, dim_im_out * ch_im_in); */
  219. memmove(target, row_start, dim_im_out * ch_im_in);
  220. /* move over to next row */
  221. row_start += ch_im_in * dim_im_in;
  222. for (; row_start < row_end; row_start += dim_im_in * ch_im_in)
  223. {
  224. compare_and_replace_if_larger_q7(target, row_start, dim_im_out * ch_im_in);
  225. }
  226. }
  227. #else
  228. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  229. int16_t i_ch_in, i_x, i_y;
  230. int16_t k_x, k_y;
  231. for (i_ch_in = 0; i_ch_in < ch_im_in; i_ch_in++)
  232. {
  233. for (i_y = 0; i_y < dim_im_out; i_y++)
  234. {
  235. for (i_x = 0; i_x < dim_im_out; i_x++)
  236. {
  237. int max = -129;
  238. for (k_y = i_y * stride - padding; k_y < i_y * stride - padding + dim_kernel; k_y++)
  239. {
  240. for (k_x = i_x * stride - padding; k_x < i_x * stride - padding + dim_kernel; k_x++)
  241. {
  242. if (k_y >= 0 && k_x >= 0 && k_y < dim_im_in && k_x < dim_im_in)
  243. {
  244. if (Im_in[i_ch_in + ch_im_in * (k_x + k_y * dim_im_in)] > max)
  245. {
  246. max = Im_in[i_ch_in + ch_im_in * (k_x + k_y * dim_im_in)];
  247. }
  248. }
  249. }
  250. }
  251. Im_out[i_ch_in + ch_im_in * (i_x + i_y * dim_im_out)] = max;
  252. }
  253. }
  254. }
  255. #endif /* ARM_MATH_DSP */
  256. }
  257. /**
  258. * @brief Q7 average pooling function
  259. * @param[in,out] Im_in pointer to input tensor
  260. * @param[in] dim_im_in input tensor dimention
  261. * @param[in] ch_im_in number of input tensor channels
  262. * @param[in] dim_kernel filter kernel size
  263. * @param[in] padding padding sizes
  264. * @param[in] stride convolution stride
  265. * @param[in] dim_im_out output tensor dimension
  266. * @param[in,out] bufferA pointer to buffer space for input
  267. * @param[in,out] Im_out pointer to output tensor
  268. * @return none.
  269. *
  270. * @details
  271. *
  272. * <b>Buffer size:</b>
  273. *
  274. * bufferA size: 2*dim_im_out*ch_im_in
  275. *
  276. * The pooling function is implemented as split x-pooling then
  277. * y-pooling.
  278. *
  279. * This pooling function is input-destructive. Input data is undefined
  280. * after calling this function.
  281. *
  282. */
  283. void
  284. 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, const uint16_t dim_im_out, q7_t * bufferA, q7_t * Im_out)
  290. {
  291. #if defined (ARM_MATH_DSP)
  292. /* Run the following code for Cortex-M4 and Cortex-M7 */
  293. q15_t *buffer = (q15_t *) bufferA;
  294. int16_t i_x, i_y;
  295. int16_t count = 0;
  296. /* first does the pooling along x axis */
  297. for (i_y = 0; i_y < dim_im_in; i_y++)
  298. {
  299. for (i_x = 0; i_x < dim_im_out; i_x++)
  300. {
  301. /* for each output pixel */
  302. q7_t *target = Im_in + (i_y * dim_im_in + i_x) * ch_im_in;
  303. q7_t *win_start;
  304. q7_t *win_stop;
  305. if (i_x * stride - padding < 0)
  306. {
  307. win_start = target;
  308. } else
  309. {
  310. win_start = Im_in + (i_y * dim_im_in + i_x * stride - padding) * ch_im_in;
  311. }
  312. if (i_x * stride - padding + dim_kernel >= dim_im_in)
  313. {
  314. win_stop = Im_in + (i_y * dim_im_in + dim_im_in) * ch_im_in;
  315. } else
  316. {
  317. win_stop = Im_in + (i_y * dim_im_in + i_x * stride - padding + dim_kernel) * ch_im_in;
  318. }
  319. /* first step is to copy over initial data */
  320. arm_q7_to_q15_no_shift(win_start, buffer, ch_im_in);
  321. count = 1;
  322. /* start the max operation from the second part */
  323. win_start += ch_im_in;
  324. for (; win_start < win_stop; win_start += ch_im_in)
  325. {
  326. accumulate_q7_to_q15(buffer, win_start, ch_im_in);
  327. count++;
  328. }
  329. buffer_scale_back_q15_to_q7(buffer, target, ch_im_in, count);
  330. }
  331. }
  332. /* then does the pooling along y axis */
  333. for (i_y = 0; i_y < dim_im_out; i_y++)
  334. {
  335. /* for each output row */
  336. q7_t *target = Im_out + i_y * dim_im_out * ch_im_in;
  337. q7_t *row_start;
  338. q7_t *row_end;
  339. /* setting the starting row */
  340. if (i_y * stride - padding < 0)
  341. {
  342. row_start = Im_in;
  343. } else
  344. {
  345. row_start = Im_in + (i_y * stride - padding) * dim_im_in * ch_im_in;
  346. }
  347. /* setting the stopping row */
  348. if (i_y * stride - padding + dim_kernel >= dim_im_in)
  349. {
  350. row_end = Im_in + dim_im_in * dim_im_in * ch_im_in;
  351. } else
  352. {
  353. row_end = Im_in + (i_y * stride - padding + dim_kernel) * dim_im_in * ch_im_in;
  354. }
  355. /* copy over the first row */
  356. arm_q7_to_q15_no_shift(row_start, buffer, dim_im_out * ch_im_in);
  357. count = 1;
  358. /* move over to next row */
  359. row_start += ch_im_in * dim_im_in;
  360. for (; row_start < row_end; row_start += dim_im_in * ch_im_in)
  361. {
  362. accumulate_q7_to_q15(buffer, row_start, dim_im_out * ch_im_in);
  363. count++;
  364. }
  365. buffer_scale_back_q15_to_q7(buffer, target, dim_im_out * ch_im_in, count);
  366. }
  367. #else
  368. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  369. int16_t i_ch_in, i_x, i_y;
  370. int16_t k_x, k_y;
  371. for (i_ch_in = 0; i_ch_in < ch_im_in; i_ch_in++)
  372. {
  373. for (i_y = 0; i_y < dim_im_out; i_y++)
  374. {
  375. for (i_x = 0; i_x < dim_im_out; i_x++)
  376. {
  377. int sum = 0;
  378. int count = 0;
  379. for (k_y = i_y * stride - padding; k_y < i_y * stride - padding + dim_kernel; k_y++)
  380. {
  381. for (k_x = i_x * stride - padding; k_x < i_x * stride - padding + dim_kernel; k_x++)
  382. {
  383. if (k_y >= 0 && k_x >= 0 && k_y < dim_im_in && k_x < dim_im_in)
  384. {
  385. sum += Im_in[i_ch_in + ch_im_in * (k_x + k_y * dim_im_in)];
  386. count++;
  387. }
  388. }
  389. }
  390. Im_out[i_ch_in + ch_im_in * (i_x + i_y * dim_im_out)] = sum / count;
  391. }
  392. }
  393. }
  394. #endif /* ARM_MATH_DSP */
  395. }
  396. /**
  397. * @} end of Pooling group
  398. */