arm_avgpool_s8.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2010-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_s8.c
  21. * Description: Pooling function implementations
  22. *
  23. * $Date: 27 November 2023
  24. * $Revision: V.3.3.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_q7_and_clamp(const int32_t *buffer,
  33. int8_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] = (int8_t)sum;
  47. }
  48. }
  49. #endif
  50. /**
  51. * @ingroup Public
  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_cmsis_nn_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 int8_t *src,
  68. const cmsis_nn_dims *filter_dims,
  69. const cmsis_nn_dims *output_dims,
  70. int8_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. const int32_t batch_input = input_x * input_y * ch_src;
  87. const int32_t batch_output = output_x * output_y * ch_src;
  88. int32_t batch_cnt = input_dims->n;
  89. if (batch_cnt < 1)
  90. {
  91. return ARM_CMSIS_NN_ARG_ERROR;
  92. }
  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 int8_t *src_base = src;
  104. int8_t *out = &dst[ch_src * (i_x + i_y * output_x)];
  105. int32_t ch_count = (ch_src + 15) / 16;
  106. int32_t channels = ch_src;
  107. while (ch_count > 0)
  108. {
  109. int8x16_t temp;
  110. int16x8_t temp_lo, temp_hi;
  111. int32x4_t temp_lo_lo, temp_lo_hi, temp_hi_lo, temp_hi_hi;
  112. int32_t count = 0;
  113. int32x4_t sum_1 = vdupq_n_s32(0);
  114. int32x4_t sum_2 = vdupq_n_s32(0);
  115. int32x4_t sum_3 = vdupq_n_s32(0);
  116. int32x4_t sum_4 = vdupq_n_s32(0);
  117. // Load store tail predicate
  118. const mve_pred16_t ld_st_p = vctp8q(channels);
  119. channels -= 16;
  120. for (int k_y = k_y_start; k_y < k_y_end; k_y++)
  121. {
  122. for (int k_x = k_x_start; k_x < k_x_end; k_x++)
  123. {
  124. const int8_t *src_inner = src_base + (ch_src * (k_x + k_y * input_x));
  125. temp = vldrbq_z_s8(src_inner, ld_st_p);
  126. temp_lo = vmovlbq_s8(temp);
  127. temp_hi = vmovltq_s8(temp);
  128. temp_lo_lo = vmovlbq_s16(temp_lo);
  129. temp_lo_hi = vmovltq_s16(temp_lo);
  130. temp_hi_lo = vmovlbq_s16(temp_hi);
  131. temp_hi_hi = vmovltq_s16(temp_hi);
  132. sum_1 = vaddq_s32(sum_1, temp_lo_lo);
  133. sum_2 = vaddq_s32(sum_2, temp_lo_hi);
  134. sum_3 = vaddq_s32(sum_3, temp_hi_lo);
  135. sum_4 = vaddq_s32(sum_4, temp_hi_hi);
  136. count++;
  137. }
  138. }
  139. // Prevent static code issue DIVIDE_BY_ZERO.
  140. if (count == 0)
  141. {
  142. return ARM_CMSIS_NN_ARG_ERROR;
  143. }
  144. // Perform the following operation
  145. // sum = sum > 0 ? (sum + count / 2) / count : (sum - count / 2) / count;
  146. const int32_t half_count = count / 2;
  147. // Predicate for 'sum > 0' operation
  148. mve_pred16_t p = vcmpgtq_n_s32(sum_1, 0);
  149. sum_1 = vaddq_m_n_s32(sum_1, sum_1, half_count, p);
  150. sum_1 = vsubq_m_n_s32(sum_1, sum_1, half_count, ~p);
  151. p = vcmpgtq_n_s32(sum_2, 0);
  152. sum_2 = vaddq_m_n_s32(sum_2, sum_2, half_count, p);
  153. sum_2 = vsubq_m_n_s32(sum_2, sum_2, half_count, ~p);
  154. p = vcmpgtq_n_s32(sum_3, 0);
  155. sum_3 = vaddq_m_n_s32(sum_3, sum_3, half_count, p);
  156. sum_3 = vsubq_m_n_s32(sum_3, sum_3, half_count, ~p);
  157. p = vcmpgtq_n_s32(sum_4, 0);
  158. sum_4 = vaddq_m_n_s32(sum_4, sum_4, half_count, p);
  159. sum_4 = vsubq_m_n_s32(sum_4, sum_4, half_count, ~p);
  160. for (int i = 0; i < 4; i++)
  161. {
  162. sum_1[i] = sum_1[i] / count;
  163. sum_2[i] = sum_2[i] / count;
  164. sum_3[i] = sum_3[i] / count;
  165. sum_4[i] = sum_4[i] / count;
  166. }
  167. sum_1 = vmaxq_s32(sum_1, vdupq_n_s32(act_min));
  168. sum_1 = vminq_s32(sum_1, vdupq_n_s32(act_max));
  169. sum_2 = vmaxq_s32(sum_2, vdupq_n_s32(act_min));
  170. sum_2 = vminq_s32(sum_2, vdupq_n_s32(act_max));
  171. sum_3 = vmaxq_s32(sum_3, vdupq_n_s32(act_min));
  172. sum_3 = vminq_s32(sum_3, vdupq_n_s32(act_max));
  173. sum_4 = vmaxq_s32(sum_4, vdupq_n_s32(act_min));
  174. sum_4 = vminq_s32(sum_4, vdupq_n_s32(act_max));
  175. temp_lo = vmovnbq_s32(temp_lo, sum_1);
  176. temp_lo = vmovntq_s32(temp_lo, sum_2);
  177. temp_hi = vmovnbq_s32(temp_hi, sum_3);
  178. temp_hi = vmovntq_s32(temp_hi, sum_4);
  179. temp = vmovnbq_s16(temp, temp_lo);
  180. temp = vmovntq_s16(temp, temp_hi);
  181. vstrbq_p_s8(out, temp, ld_st_p);
  182. out += 16;
  183. ch_count--;
  184. src_base += 16;
  185. }
  186. }
  187. }
  188. src += batch_input;
  189. dst += batch_output;
  190. batch_cnt--;
  191. }
  192. return ARM_CMSIS_NN_SUCCESS;
  193. }
  194. #else
  195. arm_cmsis_nn_status arm_avgpool_s8(const cmsis_nn_context *ctx,
  196. const cmsis_nn_pool_params *pool_params,
  197. const cmsis_nn_dims *input_dims,
  198. const int8_t *src,
  199. const cmsis_nn_dims *filter_dims,
  200. const cmsis_nn_dims *output_dims,
  201. int8_t *dst)
  202. {
  203. const int32_t input_y = input_dims->h;
  204. const int32_t input_x = input_dims->w;
  205. const int32_t output_y = output_dims->h;
  206. const int32_t output_x = output_dims->w;
  207. const int32_t stride_y = pool_params->stride.h;
  208. const int32_t stride_x = pool_params->stride.w;
  209. const int32_t kernel_y = filter_dims->h;
  210. const int32_t kernel_x = filter_dims->w;
  211. const int32_t pad_y = pool_params->padding.h;
  212. const int32_t pad_x = pool_params->padding.w;
  213. const int32_t act_min = pool_params->activation.min;
  214. const int32_t act_max = pool_params->activation.max;
  215. const int32_t ch_src = input_dims->c;
  216. int32_t batch_cnt = input_dims->n;
  217. if (batch_cnt < 1)
  218. {
  219. return ARM_CMSIS_NN_ARG_ERROR;
  220. }
  221. if (ctx->buf == NULL && arm_avgpool_s8_get_buffer_size(output_dims->w, input_dims->c))
  222. {
  223. return ARM_CMSIS_NN_ARG_ERROR;
  224. }
  225. #if defined(ARM_MATH_DSP)
  226. /* Run the following code for CPU's with DSP extension
  227. */
  228. const int32_t batch_size = input_x * input_y * ch_src;
  229. int32_t *buffer = (int32_t *)ctx->buf;
  230. while (batch_cnt)
  231. {
  232. for (int i_y = 0, idx_y = -pad_y; i_y < output_y; idx_y += stride_y, i_y++)
  233. {
  234. for (int i_x = 0, idx_x = -pad_x; i_x < output_x; idx_x += stride_x, i_x++)
  235. {
  236. /* Condition for kernel start dimension:
  237. (base_idx_<x,y> + kernel_<x,y>_start) >= 0 */
  238. const int32_t kernel_y_start = MAX(0, -idx_y);
  239. const int32_t kernel_x_start = MAX(0, -idx_x);
  240. /* Condition for kernel end dimension:
  241. (base_idx_<x,y> + kernel_<x,y>_end) < dim_src_<width,height> */
  242. const int32_t kernel_y_end = MIN(kernel_y, input_y - idx_y);
  243. const int32_t kernel_x_end = MIN(kernel_x, input_x - idx_x);
  244. int count = 0;
  245. for (int k_y = kernel_y_start; k_y < kernel_y_end; k_y++)
  246. {
  247. for (int k_x = kernel_x_start; k_x < kernel_x_end; k_x++)
  248. {
  249. const int8_t *start = src + ch_src * (k_x + idx_x + (k_y + idx_y) * input_x);
  250. if (count == 0)
  251. {
  252. for (int i = 0; i < ch_src; i++)
  253. {
  254. buffer[i] = start[i];
  255. }
  256. }
  257. else
  258. {
  259. for (int i = 0; i < ch_src; i++)
  260. {
  261. buffer[i] = QADD(start[i], buffer[i]);
  262. }
  263. }
  264. count++;
  265. }
  266. }
  267. // Prevent static code issue DIVIDE_BY_ZERO.
  268. if (count == 0)
  269. {
  270. return ARM_CMSIS_NN_ARG_ERROR;
  271. }
  272. scale_q31_to_q7_and_clamp(buffer, dst, ch_src, count, act_min, act_max);
  273. dst += ch_src;
  274. }
  275. }
  276. src += batch_size;
  277. batch_cnt--;
  278. }
  279. #else
  280. /* Reference C code adapted from CMSIS-NN arm_avepool_q7_HWC.
  281. */
  282. const int32_t batch_input = input_x * input_y * ch_src;
  283. const int32_t batch_output = output_x * output_y * ch_src;
  284. while (batch_cnt)
  285. {
  286. for (int i_y = 0; i_y < output_y; i_y++)
  287. {
  288. for (int i_x = 0; i_x < output_x; i_x++)
  289. {
  290. for (int i_ch_in = 0; i_ch_in < ch_src; i_ch_in++)
  291. {
  292. int sum = 0;
  293. int count = 0;
  294. for (int k_y = i_y * stride_y - pad_y; k_y < i_y * stride_y - pad_y + kernel_y; k_y++)
  295. {
  296. for (int k_x = i_x * stride_x - pad_x; k_x < i_x * stride_x - pad_x + kernel_x; k_x++)
  297. {
  298. if (k_y >= 0 && k_x >= 0 && k_y < input_y && k_x < input_x)
  299. {
  300. sum += src[i_ch_in + ch_src * (k_x + k_y * input_x)];
  301. count++;
  302. }
  303. }
  304. }
  305. // Prevent static code issue DIVIDE_BY_ZERO.
  306. if (count == 0)
  307. {
  308. return ARM_CMSIS_NN_ARG_ERROR;
  309. }
  310. sum = sum > 0 ? (sum + count / 2) / count : (sum - count / 2) / count;
  311. sum = MAX(sum, act_min);
  312. sum = MIN(sum, act_max);
  313. dst[i_ch_in + ch_src * (i_x + i_y * output_x)] = sum;
  314. }
  315. }
  316. }
  317. src += batch_input;
  318. dst += batch_output;
  319. batch_cnt--;
  320. }
  321. #endif
  322. return ARM_CMSIS_NN_SUCCESS;
  323. }
  324. #endif /* ARM_MATH_MVEI */
  325. /**
  326. * @} end of Pooling group
  327. */