arm_pool_q7_HWC.c 13 KB

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