arm_max_pool_s16.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_max_pool_s16.c
  21. * Description: Pooling function implementations
  22. *
  23. * $Date: 27 November 2023
  24. * $Revision: V.2.2.0
  25. *
  26. * Target Processor: Cortex-M CPUs
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. static void compare_and_replace_if_larger(int16_t *base, const int16_t *target, int32_t length)
  32. {
  33. #if defined(ARM_MATH_MVEI)
  34. int32_t loop_count = (length + 7) / 8;
  35. for (int i = 0; i < loop_count; i++)
  36. {
  37. mve_pred16_t p = vctp16q((uint32_t)length);
  38. const int16x8_t op_1 = vldrhq_z_s16(base, p);
  39. const int16x8_t op_2 = vldrhq_z_s16(target, p);
  40. const int16x8_t max = vmaxq_s16(op_1, op_2);
  41. vstrhq_p_s16(base, max, p);
  42. base += 8;
  43. target += 8;
  44. length -= 8;
  45. }
  46. #else
  47. int16_t *dst = base;
  48. const int16_t *src = target;
  49. union arm_nnword ref_max;
  50. union arm_nnword comp_max;
  51. int32_t cnt = length >> 1;
  52. while (cnt > 0l)
  53. {
  54. ref_max.word = arm_nn_read_s16x2(dst);
  55. comp_max.word = arm_nn_read_q15x2_ia(&src);
  56. if (comp_max.half_words[0] > ref_max.half_words[0])
  57. {
  58. ref_max.half_words[0] = comp_max.half_words[0];
  59. }
  60. if (comp_max.half_words[1] > ref_max.half_words[1])
  61. {
  62. ref_max.half_words[1] = comp_max.half_words[1];
  63. }
  64. arm_nn_write_q15x2_ia(&dst, ref_max.word);
  65. cnt--;
  66. }
  67. if (length & 0x1)
  68. {
  69. if (*src > *dst)
  70. {
  71. *dst = *src;
  72. }
  73. }
  74. #endif
  75. }
  76. static void clamp_output(int16_t *source, int32_t length, const int16_t act_min, const int16_t act_max)
  77. {
  78. #if defined(ARM_MATH_MVEI)
  79. const int16x8_t min = vdupq_n_s16((int16_t)act_min);
  80. const int16x8_t max = vdupq_n_s16((int16_t)act_max);
  81. int32_t loop_count = (length + 7) / 8;
  82. for (int i = 0; i < loop_count; i++)
  83. {
  84. mve_pred16_t p = vctp16q((uint32_t)length);
  85. length -= 8;
  86. const int16x8_t src = vldrhq_z_s16(source, p);
  87. int16x8_t res = vmaxq_x_s16(src, min, p);
  88. res = vminq_x_s16(res, max, p);
  89. vstrhq_p_s16(source, res, p);
  90. source += 8;
  91. }
  92. #else
  93. union arm_nnword in;
  94. int32_t cnt = length >> 1;
  95. while (cnt > 0l)
  96. {
  97. in.word = arm_nn_read_s16x2(source);
  98. in.half_words[0] = MAX(in.half_words[0], act_min);
  99. in.half_words[0] = MIN(in.half_words[0], act_max);
  100. in.half_words[1] = MAX(in.half_words[1], act_min);
  101. in.half_words[1] = MIN(in.half_words[1], act_max);
  102. arm_nn_write_q15x2_ia(&source, in.word);
  103. cnt--;
  104. }
  105. if (length & 0x1)
  106. {
  107. int16_t comp = *source;
  108. comp = MAX(comp, act_min);
  109. comp = MIN(comp, act_max);
  110. *source = comp;
  111. }
  112. #endif
  113. }
  114. /**
  115. * @ingroup Public
  116. */
  117. /**
  118. * @addtogroup Pooling
  119. * @{
  120. */
  121. /*
  122. * Optimized s16 max pooling function
  123. *
  124. * Refer to header file for details.
  125. *
  126. */
  127. arm_cmsis_nn_status arm_max_pool_s16(const cmsis_nn_context *ctx,
  128. const cmsis_nn_pool_params *pool_params,
  129. const cmsis_nn_dims *input_dims,
  130. const int16_t *src,
  131. const cmsis_nn_dims *filter_dims,
  132. const cmsis_nn_dims *output_dims,
  133. int16_t *dst)
  134. {
  135. (void)ctx;
  136. const int32_t input_y = input_dims->h;
  137. const int32_t input_x = input_dims->w;
  138. const int32_t output_y = output_dims->h;
  139. const int32_t output_x = output_dims->w;
  140. const int32_t stride_y = pool_params->stride.h;
  141. const int32_t stride_x = pool_params->stride.w;
  142. const int32_t kernel_y = filter_dims->h;
  143. const int32_t kernel_x = filter_dims->w;
  144. const int32_t pad_y = pool_params->padding.h;
  145. const int32_t pad_x = pool_params->padding.w;
  146. const int16_t act_min = pool_params->activation.min;
  147. const int16_t act_max = pool_params->activation.max;
  148. const int32_t channel_in = input_dims->c;
  149. const int32_t batch_size = input_x * input_y * channel_in;
  150. int32_t batch_cnt = input_dims->n;
  151. if (batch_cnt < 1)
  152. {
  153. return ARM_CMSIS_NN_ARG_ERROR;
  154. }
  155. while (batch_cnt)
  156. {
  157. int16_t *dst_base = dst;
  158. for (int i_y = 0, base_idx_y = -pad_y; i_y < output_y; base_idx_y += stride_y, i_y++)
  159. {
  160. for (int i_x = 0, base_idx_x = -pad_x; i_x < output_x; base_idx_x += stride_x, i_x++)
  161. {
  162. /* Condition for kernel start dimension: (base_idx_<x,y> + kernel_<x,y>_start) >= 0 */
  163. const int32_t ker_y_start = MAX(0, -base_idx_y);
  164. const int32_t ker_x_start = MAX(0, -base_idx_x);
  165. /* Condition for kernel end dimension: (base_idx_<x,y> + kernel_<x,y>_end) < dim_src_<width,height> */
  166. const int32_t kernel_y_end = MIN(kernel_y, input_y - base_idx_y);
  167. const int32_t kernel_x_end = MIN(kernel_x, input_x - base_idx_x);
  168. int count = 0;
  169. for (int k_y = ker_y_start; k_y < kernel_y_end; k_y++)
  170. {
  171. for (int k_x = ker_x_start; k_x < kernel_x_end; k_x++)
  172. {
  173. const int16_t *start = src + channel_in * (k_x + base_idx_x + (k_y + base_idx_y) * input_x);
  174. if (count == 0)
  175. {
  176. memcpy(dst, start, channel_in * sizeof(int16_t));
  177. count++;
  178. }
  179. else
  180. {
  181. compare_and_replace_if_larger(dst, start, channel_in);
  182. }
  183. }
  184. }
  185. /* 'count' is expected to be non-zero here. */
  186. dst += channel_in;
  187. }
  188. }
  189. clamp_output(dst_base, output_x * output_y * channel_in, act_min, act_max);
  190. src += batch_size;
  191. batch_cnt--;
  192. }
  193. return ARM_CMSIS_NN_SUCCESS;
  194. }
  195. /**
  196. * @} end of Pooling group
  197. */