arm_avgpool_s16.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
  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_s16.c
  21. * Description: Pooling function implementations
  22. *
  23. * $Date: 27 November 2023
  24. * $Revision: V.2.5.0
  25. *
  26. * Target : Arm(R) M-Profile Architecture
  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_q15_and_clamp(const int32_t *buffer,
  33. int16_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] = (int16_t)sum;
  47. }
  48. }
  49. #endif
  50. /**
  51. * @ingroup Public
  52. */
  53. /**
  54. * @addtogroup Pooling
  55. * @{
  56. */
  57. /*
  58. * s16 average pooling function
  59. *
  60. * Refer to header file for details.
  61. *
  62. */
  63. arm_cmsis_nn_status arm_avgpool_s16(const cmsis_nn_context *ctx,
  64. const cmsis_nn_pool_params *pool_params,
  65. const cmsis_nn_dims *input_dims,
  66. const int16_t *src,
  67. const cmsis_nn_dims *filter_dims,
  68. const cmsis_nn_dims *output_dims,
  69. int16_t *dst)
  70. {
  71. const int32_t input_y = input_dims->h;
  72. const int32_t input_x = input_dims->w;
  73. const int32_t output_y = output_dims->h;
  74. const int32_t output_x = output_dims->w;
  75. const int32_t stride_y = pool_params->stride.h;
  76. const int32_t stride_x = pool_params->stride.w;
  77. const int32_t kernel_y = filter_dims->h;
  78. const int32_t kernel_x = filter_dims->w;
  79. const int32_t pad_y = pool_params->padding.h;
  80. const int32_t pad_x = pool_params->padding.w;
  81. const int32_t act_min = pool_params->activation.min;
  82. const int32_t act_max = pool_params->activation.max;
  83. const int32_t ch_src = input_dims->c;
  84. const int32_t batch_input = input_x * input_y * ch_src;
  85. int32_t batch_cnt = input_dims->n;
  86. if (batch_cnt < 1)
  87. {
  88. return ARM_CMSIS_NN_ARG_ERROR;
  89. }
  90. #if defined(ARM_MATH_MVEI)
  91. (void)ctx;
  92. const int32_t batch_output = output_x * output_y * ch_src;
  93. while (batch_cnt)
  94. {
  95. for (int i_y = 0; i_y < output_y; i_y++)
  96. {
  97. for (int i_x = 0; i_x < output_x; i_x++)
  98. {
  99. const int32_t k_y_start = MAX(0, i_y * stride_y - pad_y);
  100. const int32_t k_y_end = MIN(i_y * stride_y - pad_y + kernel_y, input_y);
  101. const int32_t k_x_start = MAX(0, i_x * stride_x - pad_x);
  102. const int32_t k_x_end = MIN(i_x * stride_x - pad_x + kernel_x, input_x);
  103. const int16_t *src_base = src;
  104. int16_t *out = &dst[ch_src * (i_x + i_y * output_x)];
  105. int32_t ch_count = (ch_src + 7) / 8;
  106. int32_t channels = ch_src;
  107. while (ch_count > 0)
  108. {
  109. int32_t count = 0;
  110. int32x4_t sum_1 = vdupq_n_s32(0);
  111. int32x4_t sum_2 = vdupq_n_s32(0);
  112. // Load store tail predicate
  113. const mve_pred16_t ld_st_p = vctp16q(channels);
  114. channels -= 8;
  115. for (int k_y = k_y_start; k_y < k_y_end; k_y++)
  116. {
  117. for (int k_x = k_x_start; k_x < k_x_end; k_x++)
  118. {
  119. const int16_t *src_inner = src_base + (ch_src * (k_x + k_y * input_x));
  120. const int16x8_t temp = vldrhq_z_s16(src_inner, ld_st_p);
  121. const int32x4_t temp_lo = vmovlbq_s16(temp);
  122. const int32x4_t temp_hi = vmovltq_s16(temp);
  123. sum_1 = vaddq_s32(sum_1, temp_lo);
  124. sum_2 = vaddq_s32(sum_2, temp_hi);
  125. count++;
  126. }
  127. }
  128. // Prevent static code issue DIVIDE_BY_ZERO.
  129. if (count == 0)
  130. {
  131. return ARM_CMSIS_NN_ARG_ERROR;
  132. }
  133. // Perform the following operation
  134. // sum = sum > 0 ? (sum + count / 2) / count : (sum - count / 2) / count;
  135. const int32_t half_count = count / 2;
  136. // Predicate for 'sum > 0' operation
  137. mve_pred16_t p = vcmpgtq_n_s32(sum_1, 0);
  138. sum_1 = vaddq_m_n_s32(sum_1, sum_1, half_count, p);
  139. sum_1 = vsubq_m_n_s32(sum_1, sum_1, half_count, ~p);
  140. p = vcmpgtq_n_s32(sum_2, 0);
  141. sum_2 = vaddq_m_n_s32(sum_2, sum_2, half_count, p);
  142. sum_2 = vsubq_m_n_s32(sum_2, sum_2, half_count, ~p);
  143. for (int i = 0; i < 4; i++)
  144. {
  145. sum_1[i] = sum_1[i] / count;
  146. sum_2[i] = sum_2[i] / count;
  147. }
  148. sum_1 = vmaxq_s32(sum_1, vdupq_n_s32(act_min));
  149. sum_1 = vminq_s32(sum_1, vdupq_n_s32(act_max));
  150. sum_2 = vmaxq_s32(sum_2, vdupq_n_s32(act_min));
  151. sum_2 = vminq_s32(sum_2, vdupq_n_s32(act_max));
  152. int16x8_t temp = vdupq_n_s16(0);
  153. temp = vmovnbq_s32(temp, sum_1);
  154. temp = vmovntq_s32(temp, sum_2);
  155. vstrhq_p_s16(out, temp, ld_st_p);
  156. out += 8;
  157. ch_count--;
  158. src_base += 8;
  159. }
  160. }
  161. }
  162. src += batch_input;
  163. dst += batch_output;
  164. batch_cnt--;
  165. }
  166. #elif defined(ARM_MATH_DSP)
  167. /* Run the following code for CPU's with DSP extension
  168. */
  169. int32_t *buffer = (int32_t *)ctx->buf;
  170. if (buffer == NULL)
  171. {
  172. return ARM_CMSIS_NN_ARG_ERROR;
  173. }
  174. while (batch_cnt)
  175. {
  176. for (int i_y = 0, idx_y = -pad_y; i_y < output_y; idx_y += stride_y, i_y++)
  177. {
  178. for (int i_x = 0, idx_x = -pad_x; i_x < output_x; idx_x += stride_x, i_x++)
  179. {
  180. /* Condition for kernel start dimension:
  181. (base_idx_<x,y> + kernel_<x,y>_start) >= 0 */
  182. const int32_t kernel_y_start = MAX(0, -idx_y);
  183. const int32_t kernel_x_start = MAX(0, -idx_x);
  184. /* Condition for kernel end dimension:
  185. (base_idx_<x,y> + kernel_<x,y>_end) < dim_src_<width,height> */
  186. const int32_t kernel_y_end = MIN(kernel_y, input_y - idx_y);
  187. const int32_t kernel_x_end = MIN(kernel_x, input_x - idx_x);
  188. int count = 0;
  189. for (int k_y = kernel_y_start; k_y < kernel_y_end; k_y++)
  190. {
  191. for (int k_x = kernel_x_start; k_x < kernel_x_end; k_x++)
  192. {
  193. const int16_t *start = src + ch_src * (k_x + idx_x + (k_y + idx_y) * input_x);
  194. if (count == 0)
  195. {
  196. for (int i = 0; i < ch_src; i++)
  197. {
  198. buffer[i] = start[i];
  199. }
  200. }
  201. else
  202. {
  203. for (int i = 0; i < ch_src; i++)
  204. {
  205. buffer[i] = QADD(start[i], buffer[i]);
  206. }
  207. }
  208. count++;
  209. }
  210. }
  211. // Prevent static code issue DIVIDE_BY_ZERO.
  212. if (count == 0)
  213. {
  214. return ARM_CMSIS_NN_ARG_ERROR;
  215. }
  216. scale_q31_to_q15_and_clamp(buffer, dst, ch_src, count, act_min, act_max);
  217. dst += ch_src;
  218. }
  219. }
  220. src += batch_input;
  221. batch_cnt--;
  222. }
  223. #else
  224. /* Reference C code adapted from CMSIS-NN arm_avgpool_s8.c.
  225. */
  226. const int32_t batch_output = output_x * output_y * ch_src;
  227. (void)ctx;
  228. while (batch_cnt)
  229. {
  230. for (int i_y = 0, base_idx_y = -pad_y; i_y < output_y; base_idx_y += stride_y, i_y++)
  231. {
  232. for (int i_x = 0, base_idx_x = -pad_x; i_x < output_x; base_idx_x += stride_x, i_x++)
  233. {
  234. /* Condition for kernel start dimension: (base_idx_<x,y> + kernel_<x,y>_start) >= 0 */
  235. const int32_t ker_y_start = MAX(0, -base_idx_y);
  236. const int32_t ker_x_start = MAX(0, -base_idx_x);
  237. /* Condition for kernel end dimension: (base_idx_<x,y> + kernel_<x,y>_end) < dim_src_<width,height> */
  238. const int32_t kernel_y_end = MIN(kernel_y, input_y - base_idx_y);
  239. const int32_t kernel_x_end = MIN(kernel_x, input_x - base_idx_x);
  240. for (int i_ch_in = 0; i_ch_in < ch_src; i_ch_in++)
  241. {
  242. int sum = 0;
  243. int count = 0;
  244. for (int k_y = ker_y_start; k_y < kernel_y_end; k_y++)
  245. {
  246. for (int k_x = ker_x_start; k_x < kernel_x_end; k_x++)
  247. {
  248. sum += src[i_ch_in + ch_src * (k_x + base_idx_x + (k_y + base_idx_y) * input_x)];
  249. count++;
  250. }
  251. }
  252. // Prevent static code issue DIVIDE_BY_ZERO.
  253. if (count == 0)
  254. {
  255. return ARM_CMSIS_NN_ARG_ERROR;
  256. }
  257. sum = sum > 0 ? (sum + count / 2) / count : (sum - count / 2) / count;
  258. sum = MAX(sum, act_min);
  259. sum = MIN(sum, act_max);
  260. dst[i_ch_in + ch_src * (i_x + i_y * output_x)] = sum;
  261. }
  262. }
  263. }
  264. src += batch_input;
  265. dst += batch_output;
  266. batch_cnt--;
  267. }
  268. #endif
  269. return ARM_CMSIS_NN_SUCCESS;
  270. }
  271. /**
  272. * @} end of Pooling group
  273. */