arm_avgpool_s8.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (C) 2010-2021 Arm Limited or its affiliates.
  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_avgpool_s8.c
  21. * Description: Pooling function implementations
  22. *
  23. * $Date: 01. March 2021
  24. * $Revision: V.2.0.4
  25. *
  26. * Target Processor: Cortex-M CPUs
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. #if defined(ARM_MATH_DSP) && !defined(ARM_MATH_MVEI)
  32. static void scale_q31_to_q7_and_clamp(const q31_t *buffer,
  33. q7_t *target,
  34. int32_t length,
  35. const int32_t count,
  36. const int act_min,
  37. const int act_max)
  38. {
  39. const int half_count = count / 2;
  40. // Prevent static code issue DIVIDE_BY_ZERO.
  41. if (count == 0)
  42. {
  43. return;
  44. }
  45. for (int i = 0; i < length; i++)
  46. {
  47. int32_t sum = buffer[i] > 0 ? (buffer[i] + half_count) : (buffer[i] - half_count);
  48. sum = sum / count;
  49. sum = MAX(sum, act_min);
  50. sum = MIN(sum, act_max);
  51. target[i] = (q7_t)sum;
  52. }
  53. }
  54. #endif
  55. /**
  56. * @ingroup groupNN
  57. */
  58. /**
  59. * @addtogroup Pooling
  60. * @{
  61. */
  62. /*
  63. * s8 average pooling function
  64. *
  65. * Refer to header file for details.
  66. *
  67. */
  68. #if defined(ARM_MATH_MVEI)
  69. arm_status arm_avgpool_s8(const cmsis_nn_context *ctx,
  70. const cmsis_nn_pool_params *pool_params,
  71. const cmsis_nn_dims *input_dims,
  72. const q7_t *src,
  73. const cmsis_nn_dims *filter_dims,
  74. const cmsis_nn_dims *output_dims,
  75. q7_t *dst)
  76. {
  77. (void)ctx;
  78. const int32_t input_y = input_dims->h;
  79. const int32_t input_x = input_dims->w;
  80. const int32_t output_y = output_dims->h;
  81. const int32_t output_x = output_dims->w;
  82. const int32_t stride_y = pool_params->stride.h;
  83. const int32_t stride_x = pool_params->stride.w;
  84. const int32_t kernel_y = filter_dims->h;
  85. const int32_t kernel_x = filter_dims->w;
  86. const int32_t pad_y = pool_params->padding.h;
  87. const int32_t pad_x = pool_params->padding.w;
  88. const int32_t act_min = pool_params->activation.min;
  89. const int32_t act_max = pool_params->activation.max;
  90. const int32_t ch_src = input_dims->c;
  91. int32_t i_x, i_y;
  92. int32_t k_x, k_y;
  93. for (i_y = 0; i_y < output_y; i_y++)
  94. {
  95. for (i_x = 0; i_x < output_x; i_x++)
  96. {
  97. int32_t k_y_start, k_y_end;
  98. int32_t k_x_start, k_x_end;
  99. int32_t chCnt;
  100. const int8_t *pTmp, *pTmpInner;
  101. int8_t *pDst;
  102. k_y_start = MAX(0, i_y * stride_y - pad_y);
  103. k_y_end = MIN(i_y * stride_y - pad_y + kernel_y, input_y);
  104. k_x_start = MAX(0, i_x * stride_x - pad_x);
  105. k_x_end = MIN(i_x * stride_x - pad_x + kernel_x, input_x);
  106. pTmp = src;
  107. pDst = &dst[ch_src * (i_x + i_y * output_x)];
  108. chCnt = ch_src >> 4;
  109. while (chCnt > 0)
  110. {
  111. int32x4_t sumV1, sumV2, sumV3, sumV4;
  112. int8x16_t tempV;
  113. int16x8_t tempVLO, tempVHI;
  114. int32x4_t tempVLOLO, tempVLOHI, tempVHILO, tempVHIHI;
  115. int32_t count = 0;
  116. sumV1 = vdupq_n_s32(0);
  117. sumV2 = vdupq_n_s32(0);
  118. sumV3 = vdupq_n_s32(0);
  119. sumV4 = vdupq_n_s32(0);
  120. for (k_y = k_y_start; k_y < k_y_end; k_y++)
  121. {
  122. for (k_x = k_x_start; k_x < k_x_end; k_x++)
  123. {
  124. pTmpInner = pTmp + (ch_src * (k_x + k_y * input_x));
  125. tempV = vldrbq_s8(pTmpInner);
  126. tempVLO = vmovlbq_s8(tempV);
  127. tempVHI = vmovltq_s8(tempV);
  128. tempVLOLO = vmovlbq_s16(tempVLO);
  129. tempVLOHI = vmovltq_s16(tempVLO);
  130. tempVHILO = vmovlbq_s16(tempVHI);
  131. tempVHIHI = vmovltq_s16(tempVHI);
  132. sumV1 = vaddq_s32(sumV1, tempVLOLO);
  133. sumV2 = vaddq_s32(sumV2, tempVLOHI);
  134. sumV3 = vaddq_s32(sumV3, tempVHILO);
  135. sumV4 = vaddq_s32(sumV4, tempVHIHI);
  136. count++;
  137. }
  138. }
  139. // Prevent static code issue DIVIDE_BY_ZERO.
  140. if (count == 0)
  141. {
  142. return ARM_MATH_ARGUMENT_ERROR;
  143. }
  144. sumV1[0] = sumV1[0] > 0 ? (sumV1[0] + count / 2) / count : (sumV1[0] - count / 2) / count;
  145. sumV1[1] = sumV1[1] > 0 ? (sumV1[1] + count / 2) / count : (sumV1[1] - count / 2) / count;
  146. sumV1[2] = sumV1[2] > 0 ? (sumV1[2] + count / 2) / count : (sumV1[2] - count / 2) / count;
  147. sumV1[3] = sumV1[3] > 0 ? (sumV1[3] + count / 2) / count : (sumV1[3] - count / 2) / count;
  148. sumV2[0] = sumV2[0] > 0 ? (sumV2[0] + count / 2) / count : (sumV2[0] - count / 2) / count;
  149. sumV2[1] = sumV2[1] > 0 ? (sumV2[1] + count / 2) / count : (sumV2[1] - count / 2) / count;
  150. sumV2[2] = sumV2[2] > 0 ? (sumV2[2] + count / 2) / count : (sumV2[2] - count / 2) / count;
  151. sumV2[3] = sumV2[3] > 0 ? (sumV2[3] + count / 2) / count : (sumV2[3] - count / 2) / count;
  152. sumV3[0] = sumV3[0] > 0 ? (sumV3[0] + count / 2) / count : (sumV3[0] - count / 2) / count;
  153. sumV3[1] = sumV3[1] > 0 ? (sumV3[1] + count / 2) / count : (sumV3[1] - count / 2) / count;
  154. sumV3[2] = sumV3[2] > 0 ? (sumV3[2] + count / 2) / count : (sumV3[2] - count / 2) / count;
  155. sumV3[3] = sumV3[3] > 0 ? (sumV3[3] + count / 2) / count : (sumV3[3] - count / 2) / count;
  156. sumV4[0] = sumV4[0] > 0 ? (sumV4[0] + count / 2) / count : (sumV4[0] - count / 2) / count;
  157. sumV4[1] = sumV4[1] > 0 ? (sumV4[1] + count / 2) / count : (sumV4[1] - count / 2) / count;
  158. sumV4[2] = sumV4[2] > 0 ? (sumV4[2] + count / 2) / count : (sumV4[2] - count / 2) / count;
  159. sumV4[3] = sumV4[3] > 0 ? (sumV4[3] + count / 2) / count : (sumV4[3] - count / 2) / count;
  160. sumV1 = vmaxq_s32(sumV1, vdupq_n_s32(act_min));
  161. sumV1 = vminq_s32(sumV1, vdupq_n_s32(act_max));
  162. sumV2 = vmaxq_s32(sumV2, vdupq_n_s32(act_min));
  163. sumV2 = vminq_s32(sumV2, vdupq_n_s32(act_max));
  164. sumV3 = vmaxq_s32(sumV3, vdupq_n_s32(act_min));
  165. sumV3 = vminq_s32(sumV3, vdupq_n_s32(act_max));
  166. sumV4 = vmaxq_s32(sumV4, vdupq_n_s32(act_min));
  167. sumV4 = vminq_s32(sumV4, vdupq_n_s32(act_max));
  168. tempVLO = vmovnbq_s32(tempVLO, sumV1);
  169. tempVLO = vmovntq_s32(tempVLO, sumV2);
  170. tempVHI = vmovnbq_s32(tempVHI, sumV3);
  171. tempVHI = vmovntq_s32(tempVHI, sumV4);
  172. tempV = vmovnbq_s16(tempV, tempVLO);
  173. tempV = vmovntq_s16(tempV, tempVHI);
  174. vstrbq_s8(pDst, tempV);
  175. pDst += 16;
  176. chCnt--;
  177. pTmp += 16;
  178. }
  179. chCnt = ch_src & 0xF;
  180. while (chCnt > 0)
  181. {
  182. int32_t sum = 0;
  183. int32_t count = 0;
  184. for (k_y = k_y_start; k_y < k_y_end; k_y++)
  185. {
  186. for (k_x = k_x_start; k_x < k_x_end; k_x++)
  187. {
  188. sum += pTmp[ch_src * (k_x + k_y * input_x)];
  189. count++;
  190. }
  191. }
  192. // Prevent static code issue DIVIDE_BY_ZERO.
  193. if (count == 0)
  194. {
  195. return ARM_MATH_ARGUMENT_ERROR;
  196. }
  197. sum = sum > 0 ? (sum + count / 2) / count : (sum - count / 2) / count;
  198. sum = MAX(sum, act_min);
  199. sum = MIN(sum, act_max);
  200. *pDst++ = sum;
  201. chCnt--;
  202. pTmp++;
  203. }
  204. }
  205. }
  206. return ARM_MATH_SUCCESS;
  207. }
  208. #else
  209. arm_status arm_avgpool_s8(const cmsis_nn_context *ctx,
  210. const cmsis_nn_pool_params *pool_params,
  211. const cmsis_nn_dims *input_dims,
  212. const q7_t *src,
  213. const cmsis_nn_dims *filter_dims,
  214. const cmsis_nn_dims *output_dims,
  215. q7_t *dst)
  216. {
  217. const int32_t input_y = input_dims->h;
  218. const int32_t input_x = input_dims->w;
  219. const int32_t output_y = output_dims->h;
  220. const int32_t output_x = output_dims->w;
  221. const int32_t stride_y = pool_params->stride.h;
  222. const int32_t stride_x = pool_params->stride.w;
  223. const int32_t kernel_y = filter_dims->h;
  224. const int32_t kernel_x = filter_dims->w;
  225. const int32_t pad_y = pool_params->padding.h;
  226. const int32_t pad_x = pool_params->padding.w;
  227. const int32_t act_min = pool_params->activation.min;
  228. const int32_t act_max = pool_params->activation.max;
  229. const int32_t ch_src = input_dims->c;
  230. if (ctx->buf == NULL && arm_avgpool_s8_get_buffer_size(output_dims->w, input_dims->c))
  231. {
  232. return ARM_MATH_ARGUMENT_ERROR;
  233. }
  234. q31_t *buffer = (q31_t *)ctx->buf;
  235. #if defined(ARM_MATH_DSP)
  236. /* Run the following code for CPU's with DSP extension
  237. */
  238. for (int i_y = 0, idx_y = -pad_y; i_y < output_y; idx_y += stride_y, i_y++)
  239. {
  240. for (int i_x = 0, idx_x = -pad_x; i_x < output_x; idx_x += stride_x, i_x++)
  241. {
  242. /* Condition for kernel start dimension:
  243. (base_idx_<x,y> + kernel_<x,y>_start) >= 0 */
  244. const int32_t kernel_y_start = MAX(0, -idx_y);
  245. const int32_t kernel_x_start = MAX(0, -idx_x);
  246. /* Condition for kernel end dimension:
  247. (base_idx_<x,y> + kernel_<x,y>_end) < dim_src_<width,height> */
  248. const int32_t kernel_y_end = MIN(kernel_y, input_y - idx_y);
  249. const int32_t kernel_x_end = MIN(kernel_x, input_x - idx_x);
  250. int count = 0;
  251. for (int k_y = kernel_y_start; k_y < kernel_y_end; k_y++)
  252. {
  253. for (int k_x = kernel_x_start; k_x < kernel_x_end; k_x++)
  254. {
  255. const q7_t *start = src + ch_src * (k_x + idx_x + (k_y + idx_y) * input_x);
  256. if (count == 0)
  257. {
  258. for (int i = 0; i < ch_src; i++)
  259. {
  260. buffer[i] = start[i];
  261. }
  262. }
  263. else
  264. {
  265. for (int i = 0; i < ch_src; i++)
  266. {
  267. buffer[i] = __QADD(start[i], buffer[i]);
  268. }
  269. }
  270. count++;
  271. }
  272. }
  273. // Prevent static code issue DIVIDE_BY_ZERO.
  274. if (count == 0)
  275. {
  276. return ARM_MATH_ARGUMENT_ERROR;
  277. }
  278. scale_q31_to_q7_and_clamp(buffer, dst, ch_src, count, act_min, act_max);
  279. dst += ch_src;
  280. }
  281. }
  282. #else
  283. /* Reference C code adapted from CMSIS-NN arm_avepool_q7_HWC.
  284. */
  285. (void)buffer;
  286. int16_t i_ch_in, i_x, i_y;
  287. int16_t k_x, k_y;
  288. for (i_y = 0; i_y < output_y; i_y++)
  289. {
  290. for (i_x = 0; i_x < output_x; i_x++)
  291. {
  292. for (i_ch_in = 0; i_ch_in < ch_src; i_ch_in++)
  293. {
  294. int sum = 0;
  295. int count = 0;
  296. for (k_y = i_y * stride_y - pad_y; k_y < i_y * stride_y - pad_y + kernel_y; k_y++)
  297. {
  298. for (k_x = i_x * stride_x - pad_x; k_x < i_x * stride_x - pad_x + kernel_x; k_x++)
  299. {
  300. if (k_y >= 0 && k_x >= 0 && k_y < input_y && k_x < input_x)
  301. {
  302. sum += src[i_ch_in + ch_src * (k_x + k_y * input_x)];
  303. count++;
  304. }
  305. }
  306. }
  307. // Prevent static code issue DIVIDE_BY_ZERO.
  308. if (count == 0)
  309. {
  310. return ARM_MATH_ARGUMENT_ERROR;
  311. }
  312. sum = sum > 0 ? (sum + count / 2) / count : (sum - count / 2) / count;
  313. sum = MAX(sum, act_min);
  314. sum = MIN(sum, act_max);
  315. dst[i_ch_in + ch_src * (i_x + i_y * output_x)] = sum;
  316. }
  317. }
  318. }
  319. #endif
  320. return ARM_MATH_SUCCESS;
  321. }
  322. #endif /* ARM_MATH_MVEI */
  323. int32_t arm_avgpool_s8_get_buffer_size(const int output_x, const int ch_src)
  324. {
  325. (void)output_x;
  326. #if defined(ARM_MATH_DSP) && !defined(ARM_MATH_MVEI)
  327. return (ch_src * sizeof(int32_t));
  328. #else
  329. (void)ch_src;
  330. return 0;
  331. #endif
  332. }
  333. /**
  334. * @} end of Pooling group
  335. */