arm_convolve_s4.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 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_s4.c
  21. * Description: s8 version of convolution using symmetric quantization with 4 bit weights.
  22. *
  23. * $Date: 01 November 2023
  24. * $Revision: V.1.0.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 s8 convolution function with int4 weights.
  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 at least greater than 4.
  43. *
  44. */
  45. arm_cmsis_nn_status arm_convolve_s4(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 int8_t *input_data,
  50. const cmsis_nn_dims *filter_dims,
  51. const int8_t *packed_filter_data,
  52. const cmsis_nn_dims *bias_dims,
  53. const int32_t *bias_data,
  54. const cmsis_nn_dims *output_dims,
  55. int8_t *output_data)
  56. {
  57. (void)bias_dims;
  58. if (ctx->buf == NULL)
  59. {
  60. return ARM_CMSIS_NN_ARG_ERROR;
  61. }
  62. int16_t *buffer_a = (int16_t *)ctx->buf;
  63. const int32_t input_batches = input_dims->n;
  64. const uint16_t input_x = input_dims->w;
  65. const uint16_t input_y = input_dims->h;
  66. const uint16_t input_ch = input_dims->c;
  67. const uint16_t kernel_x = filter_dims->w;
  68. const uint16_t kernel_y = filter_dims->h;
  69. const uint16_t output_x = output_dims->w;
  70. const uint16_t output_y = output_dims->h;
  71. const uint16_t output_ch = output_dims->c;
  72. const uint16_t pad_x = conv_params->padding.w;
  73. const uint16_t pad_y = conv_params->padding.h;
  74. const uint16_t stride_x = conv_params->stride.w;
  75. const uint16_t stride_y = conv_params->stride.h;
  76. const int32_t dilation_x = conv_params->dilation.w;
  77. const int32_t dilation_y = conv_params->dilation.h;
  78. const int32_t out_offset = conv_params->output_offset;
  79. const int32_t out_activation_min = conv_params->activation.min;
  80. const int32_t out_activation_max = conv_params->activation.max;
  81. const int32_t rhs_cols = kernel_x * kernel_y * input_ch;
  82. const int32_t input_offset = conv_params->input_offset;
  83. int32_t *output_mult = quant_params->multiplier;
  84. int32_t *output_shift = quant_params->shift;
  85. int i_batch;
  86. for (i_batch = 0; i_batch < input_batches; i_batch++)
  87. {
  88. int16_t *two_column_buf = buffer_a;
  89. int8_t *out = output_data;
  90. int32_t lhs_rows = 0;
  91. /* This part implements the im2col function */
  92. for (int i_out_y = 0; i_out_y < output_y; i_out_y++)
  93. {
  94. for (int i_out_x = 0; i_out_x < output_x; i_out_x++)
  95. {
  96. const int32_t base_idx_x = stride_x * i_out_x - pad_x;
  97. const int32_t base_idx_y = stride_y * i_out_y - pad_y;
  98. for (int32_t i_ker_y = 0; i_ker_y < kernel_y; i_ker_y++)
  99. {
  100. for (int32_t i_ker_x = 0; i_ker_x < kernel_x; i_ker_x++)
  101. {
  102. const int32_t k_y = base_idx_y + dilation_y * i_ker_y;
  103. const int32_t k_x = base_idx_x + dilation_x * i_ker_x;
  104. if (k_y < 0 || k_y >= input_y || k_x < 0 || k_x >= input_x)
  105. {
  106. /* Filling 0 for out-of-bound paddings */
  107. memset(two_column_buf, 0, sizeof(int16_t) * input_ch);
  108. }
  109. else
  110. {
  111. /* Copying the pixel data to column */
  112. arm_q7_to_q15_with_offset(
  113. input_data + (k_y * input_x + k_x) * input_ch, two_column_buf, input_ch, input_offset);
  114. }
  115. two_column_buf += input_ch;
  116. }
  117. }
  118. lhs_rows++;
  119. /* Computation is filed for every 2 columns */
  120. if (lhs_rows == 2)
  121. {
  122. out = arm_nn_mat_mult_kernel_s4_s16(packed_filter_data,
  123. buffer_a,
  124. output_ch,
  125. output_shift,
  126. output_mult,
  127. out_offset,
  128. out_activation_min,
  129. out_activation_max,
  130. rhs_cols,
  131. bias_data,
  132. out);
  133. /* counter reset */
  134. two_column_buf = buffer_a;
  135. lhs_rows = 0;
  136. }
  137. }
  138. if (out == NULL)
  139. {
  140. return ARM_CMSIS_NN_NO_IMPL_ERROR;
  141. }
  142. }
  143. /* Handle left over columns */
  144. if (lhs_rows != 0)
  145. {
  146. const int8_t *ker_a_ptr = packed_filter_data;
  147. int i;
  148. int8_t spilled_ker_a = 0;
  149. for (i = 0; i < output_ch; i++)
  150. {
  151. /* Load the accumulator with bias first */
  152. int32_t sum = 0;
  153. if (bias_data)
  154. {
  155. sum = bias_data[i];
  156. }
  157. const int16_t *ip_as_col = buffer_a;
  158. if (rhs_cols % 2 && (i % 2))
  159. {
  160. int16_t ip_b0 = *ip_as_col++;
  161. sum += spilled_ker_a * ip_b0;
  162. }
  163. #if defined(ARM_MATH_DSP)
  164. /* 4 multiply and accumulates are done in one loop. */
  165. uint16_t col_count = rhs_cols / 4;
  166. while (col_count)
  167. {
  168. int32_t ker_a1, ker_a2;
  169. int32_t ip_b1, ip_b2;
  170. read_and_pad_s4_ordered(ker_a_ptr, &ker_a1, &ker_a2);
  171. ker_a_ptr += 2;
  172. ip_b1 = arm_nn_read_q15x2_ia(&ip_as_col);
  173. sum = SMLAD(ker_a1, ip_b1, sum);
  174. ip_b2 = arm_nn_read_q15x2_ia(&ip_as_col);
  175. sum = SMLAD(ker_a2, ip_b2, sum);
  176. col_count--;
  177. }
  178. col_count = (rhs_cols & 0x3) >> 1;
  179. #else
  180. uint16_t col_count = rhs_cols >> 1;
  181. #endif
  182. while (col_count)
  183. {
  184. int8_t ker_a0 = (int8_t)(*ker_a_ptr << 4) >> 4;
  185. int8_t ker_a1 = *ker_a_ptr >> 4;
  186. ker_a_ptr++;
  187. int16_t ip_b0 = *ip_as_col++;
  188. sum += ker_a0 * ip_b0;
  189. ip_b0 = *ip_as_col++;
  190. sum += ker_a1 * ip_b0;
  191. col_count--;
  192. }
  193. if (rhs_cols % 2 && !(i % 2))
  194. {
  195. int8_t ker_a0 = (int8_t)(*ker_a_ptr << 4) >> 4;
  196. spilled_ker_a = *ker_a_ptr >> 4;
  197. ker_a_ptr++;
  198. int16_t ip_b0 = *ip_as_col;
  199. sum += ker_a0 * ip_b0;
  200. }
  201. sum = arm_nn_requantize(sum, output_mult[i], output_shift[i]);
  202. sum += out_offset;
  203. sum = MAX(sum, out_activation_min);
  204. sum = MIN(sum, out_activation_max);
  205. *out++ = (int8_t)sum;
  206. }
  207. }
  208. /* Advance to the next batch */
  209. input_data += (input_x * input_y * input_ch);
  210. output_data += (output_x * output_y * output_ch);
  211. }
  212. /* Return to application */
  213. return ARM_CMSIS_NN_SUCCESS;
  214. }
  215. /**
  216. * @} end of NNConv group
  217. */