arm_max_pool_s8.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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_max_pool_s8.c
  21. * Description: Pooling function implementations
  22. *
  23. * $Date: 27 November 2023
  24. * $Revision: V.3.1.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_q7(int8_t *base, const int8_t *target, int32_t length)
  32. {
  33. #if defined(ARM_MATH_MVEI)
  34. int32_t loop_count = (length + 15) / 16;
  35. for (int i = 0; i < loop_count; i++)
  36. {
  37. mve_pred16_t p = vctp8q((uint32_t)length);
  38. const int8x16_t op_1 = vldrbq_z_s8(base, p);
  39. const int8x16_t op_2 = vldrbq_z_s8(target, p);
  40. const int8x16_t max = vmaxq_x_s8(op_1, op_2, p);
  41. vstrbq_p_s8(base, max, p);
  42. base += 16;
  43. target += 16;
  44. length -= 16;
  45. }
  46. #else
  47. int8_t *dst = base;
  48. const int8_t *src = target;
  49. union arm_nnword ref_max;
  50. union arm_nnword comp_max;
  51. int32_t cnt = length >> 2;
  52. while (cnt > 0l)
  53. {
  54. ref_max.word = arm_nn_read_s8x4(dst);
  55. comp_max.word = arm_nn_read_s8x4_ia(&src);
  56. if (comp_max.bytes[0] > ref_max.bytes[0])
  57. {
  58. ref_max.bytes[0] = comp_max.bytes[0];
  59. }
  60. if (comp_max.bytes[1] > ref_max.bytes[1])
  61. {
  62. ref_max.bytes[1] = comp_max.bytes[1];
  63. }
  64. if (comp_max.bytes[2] > ref_max.bytes[2])
  65. {
  66. ref_max.bytes[2] = comp_max.bytes[2];
  67. }
  68. if (comp_max.bytes[3] > ref_max.bytes[3])
  69. {
  70. ref_max.bytes[3] = comp_max.bytes[3];
  71. }
  72. arm_nn_write_s8x4_ia(&dst, ref_max.word);
  73. cnt--;
  74. }
  75. cnt = length & 0x3;
  76. while (cnt > 0l)
  77. {
  78. if (*src > *dst)
  79. {
  80. *dst = *src;
  81. }
  82. dst++;
  83. src++;
  84. cnt--;
  85. }
  86. #endif
  87. }
  88. static void clamp_output(int8_t *source, int32_t length, const int32_t act_min, const int32_t act_max)
  89. {
  90. #if defined(ARM_MATH_MVEI)
  91. int32_t loop_count = (length + 15) / 16;
  92. const int8x16_t vmin = vdupq_n_s8((int8_t)act_min);
  93. const int8x16_t vmax = vdupq_n_s8((int8_t)act_max);
  94. for (int i = 0; i < loop_count; i++)
  95. {
  96. mve_pred16_t p = vctp8q((uint32_t)length);
  97. length -= 16;
  98. const int8x16_t src = vldrbq_z_s8(source, p);
  99. int8x16_t res = vmaxq_x_s8(src, vmin, p);
  100. res = vminq_x_s8(res, vmax, p);
  101. vstrbq_p_s8(source, res, p);
  102. source += 16;
  103. }
  104. #else
  105. union arm_nnword in;
  106. int32_t cnt = length >> 2;
  107. while (cnt > 0l)
  108. {
  109. in.word = arm_nn_read_s8x4(source);
  110. in.bytes[0] = MAX(in.bytes[0], act_min);
  111. in.bytes[0] = MIN(in.bytes[0], act_max);
  112. in.bytes[1] = MAX(in.bytes[1], act_min);
  113. in.bytes[1] = MIN(in.bytes[1], act_max);
  114. in.bytes[2] = MAX(in.bytes[2], act_min);
  115. in.bytes[2] = MIN(in.bytes[2], act_max);
  116. in.bytes[3] = MAX(in.bytes[3], act_min);
  117. in.bytes[3] = MIN(in.bytes[3], act_max);
  118. arm_nn_write_s8x4_ia(&source, in.word);
  119. cnt--;
  120. }
  121. cnt = length & 0x3;
  122. while (cnt > 0l)
  123. {
  124. int32_t comp = *source;
  125. comp = MAX(comp, act_min);
  126. comp = MIN(comp, act_max);
  127. *source++ = (int8_t)comp;
  128. cnt--;
  129. }
  130. #endif
  131. }
  132. /**
  133. * @ingroup Public
  134. */
  135. /**
  136. * @addtogroup Pooling
  137. * @{
  138. */
  139. /*
  140. * Optimized s8 max pooling function
  141. *
  142. * Refer to header file for details.
  143. *
  144. */
  145. arm_cmsis_nn_status arm_max_pool_s8(const cmsis_nn_context *ctx,
  146. const cmsis_nn_pool_params *pool_params,
  147. const cmsis_nn_dims *input_dims,
  148. const int8_t *src,
  149. const cmsis_nn_dims *filter_dims,
  150. const cmsis_nn_dims *output_dims,
  151. int8_t *dst)
  152. {
  153. (void)ctx;
  154. const int32_t input_y = input_dims->h;
  155. const int32_t input_x = input_dims->w;
  156. const int32_t output_y = output_dims->h;
  157. const int32_t output_x = output_dims->w;
  158. const int32_t stride_y = pool_params->stride.h;
  159. const int32_t stride_x = pool_params->stride.w;
  160. const int32_t kernel_y = filter_dims->h;
  161. const int32_t kernel_x = filter_dims->w;
  162. const int32_t pad_y = pool_params->padding.h;
  163. const int32_t pad_x = pool_params->padding.w;
  164. const int32_t act_min = pool_params->activation.min;
  165. const int32_t act_max = pool_params->activation.max;
  166. const int32_t channel_in = input_dims->c;
  167. const int32_t batch_size = input_x * input_y * channel_in;
  168. int32_t batch_cnt = input_dims->n;
  169. if (batch_cnt < 1)
  170. {
  171. return ARM_CMSIS_NN_ARG_ERROR;
  172. }
  173. while (batch_cnt)
  174. {
  175. int8_t *dst_base = dst;
  176. for (int i_y = 0, base_idx_y = -pad_y; i_y < output_y; base_idx_y += stride_y, i_y++)
  177. {
  178. for (int i_x = 0, base_idx_x = -pad_x; i_x < output_x; base_idx_x += stride_x, i_x++)
  179. {
  180. /* Condition for kernel start dimension: (base_idx_<x,y> + kernel_<x,y>_start) >= 0 */
  181. const int32_t ker_y_start = MAX(0, -base_idx_y);
  182. const int32_t ker_x_start = MAX(0, -base_idx_x);
  183. /* Condition for kernel end dimension: (base_idx_<x,y> + kernel_<x,y>_end) < dim_src_<width,height> */
  184. const int32_t kernel_y_end = MIN(kernel_y, input_y - base_idx_y);
  185. const int32_t kernel_x_end = MIN(kernel_x, input_x - base_idx_x);
  186. int count = 0;
  187. for (int k_y = ker_y_start; k_y < kernel_y_end; k_y++)
  188. {
  189. for (int k_x = ker_x_start; k_x < kernel_x_end; k_x++)
  190. {
  191. const int8_t *start = src + channel_in * (k_x + base_idx_x + (k_y + base_idx_y) * input_x);
  192. if (count == 0)
  193. {
  194. arm_memcpy_s8(dst, start, channel_in);
  195. count++;
  196. }
  197. else
  198. {
  199. compare_and_replace_if_larger_q7(dst, start, channel_in);
  200. }
  201. }
  202. }
  203. /* 'count' is expected to be non-zero here. */
  204. dst += channel_in;
  205. }
  206. }
  207. clamp_output(dst_base, output_x * output_y * channel_in, act_min, act_max);
  208. src += batch_size;
  209. batch_cnt--;
  210. }
  211. return ARM_CMSIS_NN_SUCCESS;
  212. }
  213. /**
  214. * @} end of Pooling group
  215. */