arm_convolve_fast_s16.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_convolve_fast_s16.c
  21. * Description: Optimized s16 version of convolution.
  22. *
  23. * $Date: 23 March 2023
  24. * $Revision: V.2.3.0
  25. *
  26. * Target : Arm(R) M-Profile Architecture
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. /**
  32. * @ingroup Public
  33. */
  34. /**
  35. * @addtogroup NNConv
  36. * @{
  37. */
  38. /*
  39. * Basic s16 convolution function.
  40. *
  41. * Refer header file for details. Optimal use case for the DSP/MVE implementation is when input and output channels
  42. * are multiples of 4 or atleast greater than 4.
  43. *
  44. */
  45. arm_cmsis_nn_status arm_convolve_fast_s16(const cmsis_nn_context *ctx,
  46. const cmsis_nn_conv_params *conv_params,
  47. const cmsis_nn_per_channel_quant_params *quant_params,
  48. const cmsis_nn_dims *input_dims,
  49. const int16_t *input_data,
  50. const cmsis_nn_dims *filter_dims,
  51. const int8_t *filter_data,
  52. const cmsis_nn_dims *bias_dims,
  53. const int64_t *bias_data,
  54. const cmsis_nn_dims *output_dims,
  55. int16_t *output_data)
  56. {
  57. (void)bias_dims;
  58. if (filter_dims->w * filter_dims->h * input_dims->c >= 512)
  59. {
  60. return ARM_CMSIS_NN_ARG_ERROR;
  61. }
  62. if (ctx->buf == NULL && arm_convolve_s8_get_buffer_size(input_dims, filter_dims) > 0)
  63. {
  64. return ARM_CMSIS_NN_ARG_ERROR;
  65. }
  66. int16_t *buffer_a = (int16_t *)ctx->buf;
  67. const int32_t input_batches = input_dims->n;
  68. const int32_t input_x = input_dims->w;
  69. const int32_t input_y = input_dims->h;
  70. const int32_t input_ch = input_dims->c;
  71. const int32_t kernel_x = filter_dims->w;
  72. const int32_t kernel_y = filter_dims->h;
  73. const int32_t output_x = output_dims->w;
  74. const int32_t output_y = output_dims->h;
  75. const int32_t output_ch = output_dims->c;
  76. const int32_t rhs_cols = input_ch * kernel_y * kernel_x;
  77. const int32_t pad_x = conv_params->padding.w;
  78. const int32_t pad_y = conv_params->padding.h;
  79. const int32_t stride_x = conv_params->stride.w;
  80. const int32_t stride_y = conv_params->stride.h;
  81. const int16_t out_activation_min = conv_params->activation.min;
  82. const int16_t out_activation_max = conv_params->activation.max;
  83. int32_t *output_mult = quant_params->multiplier;
  84. int32_t *output_shift = quant_params->shift;
  85. for (int i_batch = 0; i_batch < input_batches; i_batch++)
  86. {
  87. #if defined(ARM_MATH_DSP) && !defined(ARM_MATH_MVEI)
  88. /* Generate two columns from the input tensor a GEMM computation */
  89. int16_t *two_column_buf = buffer_a;
  90. int16_t *out = output_data;
  91. /* This part implements the im2col function */
  92. for (int32_t i_out_y = 0; i_out_y < output_y; i_out_y++)
  93. {
  94. for (int32_t i_out_x = 0; i_out_x < output_x; i_out_x++)
  95. {
  96. for (int32_t i_ker_y = i_out_y * stride_y - pad_y; i_ker_y < i_out_y * stride_y - pad_y + kernel_y;
  97. i_ker_y++)
  98. {
  99. for (int32_t i_ker_x = i_out_x * stride_x - pad_x; i_ker_x < i_out_x * stride_x - pad_x + kernel_x;
  100. i_ker_x++)
  101. {
  102. if (i_ker_y < 0 || i_ker_y >= input_y || i_ker_x < 0 || i_ker_x >= input_x)
  103. {
  104. /* Filling 0 for out-of-bound paddings */
  105. arm_memset_s8((int8_t *)two_column_buf, 0, sizeof(int16_t) * input_ch);
  106. }
  107. else
  108. {
  109. arm_memcpy_s8((int8_t *)two_column_buf,
  110. (const int8_t *)(input_data + (i_ker_y * input_x + i_ker_x) * input_ch),
  111. input_ch * sizeof(int16_t));
  112. }
  113. two_column_buf += input_ch;
  114. }
  115. }
  116. /* Computation is filed for every 2 columns */
  117. if (two_column_buf == buffer_a + 2 * rhs_cols)
  118. {
  119. out = arm_nn_mat_mult_kernel_s16(filter_data,
  120. buffer_a,
  121. output_ch,
  122. output_shift,
  123. output_mult,
  124. out_activation_min,
  125. out_activation_max,
  126. rhs_cols,
  127. bias_data,
  128. out);
  129. /* Counter reset */
  130. two_column_buf = buffer_a;
  131. }
  132. }
  133. }
  134. /* Left-over because odd number of output pixels */
  135. if (two_column_buf != buffer_a)
  136. {
  137. const int8_t *ker_a = filter_data;
  138. int i;
  139. for (i = 0; i < output_ch; i++)
  140. {
  141. /* Init the accumulator*/
  142. int32_t sum = 0;
  143. /* Point to the beginning of the im2col buffer where the input is available as a rearranged column */
  144. const int16_t *ip_as_col = buffer_a;
  145. /* 4 multiply and accumulates are done in one loop. */
  146. int32_t col_count = rhs_cols >> 2;
  147. while (col_count)
  148. {
  149. int32_t ker_a1, ker_a2;
  150. int32_t ip_b1, ip_b2;
  151. ker_a = read_and_pad(ker_a, &ker_a1, &ker_a2);
  152. ip_b1 = arm_nn_read_q15x2_ia(&ip_as_col);
  153. sum = SMLAD(ker_a1, ip_b1, sum);
  154. ip_b2 = arm_nn_read_q15x2_ia(&ip_as_col);
  155. sum = SMLAD(ker_a2, ip_b2, sum);
  156. col_count--;
  157. }
  158. /* Handle left over mac */
  159. col_count = rhs_cols & 0x3;
  160. while (col_count)
  161. {
  162. int8_t ker_a1 = *ker_a++;
  163. int16_t ip_b1 = *ip_as_col++;
  164. sum += ker_a1 * ip_b1;
  165. col_count--;
  166. }
  167. if (bias_data)
  168. {
  169. int32_t reduced_multiplier = REDUCE_MULTIPLIER(output_mult[i]);
  170. int64_t acc_64 = sum + bias_data[i];
  171. sum = arm_nn_requantize_s64(acc_64, reduced_multiplier, output_shift[i]);
  172. }
  173. else
  174. {
  175. sum = arm_nn_requantize(sum, output_mult[i], output_shift[i]);
  176. }
  177. sum = MAX(sum, out_activation_min);
  178. sum = MIN(sum, out_activation_max);
  179. *out++ = (int16_t)sum;
  180. }
  181. }
  182. #else
  183. (void)input_data;
  184. (void)output_data;
  185. (void)bias_data;
  186. (void)filter_data;
  187. (void)buffer_a;
  188. (void)kernel_x;
  189. (void)kernel_y;
  190. (void)pad_x;
  191. (void)pad_y;
  192. (void)stride_x;
  193. (void)stride_y;
  194. (void)out_activation_min;
  195. (void)out_activation_max;
  196. (void)output_mult;
  197. (void)output_shift;
  198. (void)rhs_cols;
  199. return ARM_CMSIS_NN_ARG_ERROR;
  200. #endif
  201. /* Advance to the next batch */
  202. input_data += (input_x * input_y * input_ch);
  203. output_data += (output_x * output_y * output_ch);
  204. }
  205. /* Return to application */
  206. return ARM_CMSIS_NN_SUCCESS;
  207. }
  208. /**
  209. * @} end of NNConv group
  210. */