arm_max_pool_s8.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (C) 2010-2021 Arm Limited or its affiliates.
  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: 20. July 2021
  24. * $Revision: V.2.0.3
  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(q7_t *base, const q7_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_m_s8(vuninitializedq_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. q7_t *dst = base;
  48. const q7_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_q7x4(dst);
  55. comp_max.word = arm_nn_read_q7x4_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_q7x4_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(q7_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. for (int i = 0; i < loop_count; i++)
  93. {
  94. mve_pred16_t p = vctp8q((uint32_t)length);
  95. length -= 16;
  96. const int8x16_t src = vldrbq_z_s8(source, p);
  97. const int8x16_t predicated_min = vdupq_m_n_s8(vuninitializedq_s8(), (int8_t)act_min, p);
  98. const int8x16_t predicated_max = vdupq_m_n_s8(vuninitializedq_s8(), (int8_t)act_max, p);
  99. int8x16_t res = vmaxq_m_s8(vuninitializedq_s8(), src, predicated_min, p);
  100. res = vminq_m_s8(vuninitializedq_s8(), res, predicated_max, 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_q7x4(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_q7x4_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 groupNN
  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_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 q7_t *src,
  149. const cmsis_nn_dims *filter_dims,
  150. const cmsis_nn_dims *output_dims,
  151. q7_t *dst)
  152. {
  153. const int32_t input_y = input_dims->h;
  154. const int32_t input_x = input_dims->w;
  155. const int32_t output_y = output_dims->h;
  156. const int32_t output_x = output_dims->w;
  157. const int32_t stride_y = pool_params->stride.h;
  158. const int32_t stride_x = pool_params->stride.w;
  159. const int32_t kernel_y = filter_dims->h;
  160. const int32_t kernel_x = filter_dims->w;
  161. const int32_t pad_y = pool_params->padding.h;
  162. const int32_t pad_x = pool_params->padding.w;
  163. const int32_t act_min = pool_params->activation.min;
  164. const int32_t act_max = pool_params->activation.max;
  165. const int32_t channel_in = input_dims->c;
  166. (void)ctx;
  167. q7_t *dst_base = dst;
  168. for (int i_y = 0, base_idx_y = -pad_y; i_y < output_y; base_idx_y += stride_y, i_y++)
  169. {
  170. for (int i_x = 0, base_idx_x = -pad_x; i_x < output_x; base_idx_x += stride_x, i_x++)
  171. {
  172. /* Condition for kernel start dimension: (base_idx_<x,y> + kernel_<x,y>_start) >= 0 */
  173. const int32_t ker_y_start = MAX(0, -base_idx_y);
  174. const int32_t ker_x_start = MAX(0, -base_idx_x);
  175. /* Condition for kernel end dimension: (base_idx_<x,y> + kernel_<x,y>_end) < dim_src_<width,height> */
  176. const int32_t kernel_y_end = MIN(kernel_y, input_y - base_idx_y);
  177. const int32_t kernel_x_end = MIN(kernel_x, input_x - base_idx_x);
  178. int count = 0;
  179. for (int k_y = ker_y_start; k_y < kernel_y_end; k_y++)
  180. {
  181. for (int k_x = ker_x_start; k_x < kernel_x_end; k_x++)
  182. {
  183. const q7_t *start = src + channel_in * (k_x + base_idx_x + (k_y + base_idx_y) * input_x);
  184. if (count == 0)
  185. {
  186. arm_memcpy_q7(dst, start, channel_in);
  187. count++;
  188. }
  189. else
  190. {
  191. compare_and_replace_if_larger_q7(dst, start, channel_in);
  192. }
  193. }
  194. }
  195. /* 'count' is expected to be non-zero here. */
  196. dst += channel_in;
  197. }
  198. }
  199. clamp_output(dst_base, output_x * output_y * channel_in, act_min, act_max);
  200. return ARM_MATH_SUCCESS;
  201. }
  202. /**
  203. * @} end of Pooling group
  204. */