arm_avgpool_s8.c 13 KB

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