arm_convolve_1x1_s8_fast.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2010-2020 Arm Limited or its affiliates. All rights reserved.
  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_1x1_s8_fast.c
  21. * Description: Fast q7 version of 1x1 convolution (non-square shape)
  22. *
  23. * $Date: 09. October 2020
  24. * $Revision: V.2.0.3
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. #define DIM_KER_X (1U)
  32. #define DIM_KER_Y (1U)
  33. /**
  34. * @ingroup groupNN
  35. */
  36. /**
  37. * @addtogroup NNConv
  38. * @{
  39. */
  40. /*
  41. * Fast s8 version for 1x1 convolution (non-square shape)
  42. *
  43. * Refer header file for details.
  44. *
  45. */
  46. arm_status arm_convolve_1x1_s8_fast(const cmsis_nn_context *ctx,
  47. const cmsis_nn_conv_params *conv_params,
  48. const cmsis_nn_per_channel_quant_params *quant_params,
  49. const cmsis_nn_dims *input_dims,
  50. const q7_t *input_data,
  51. const cmsis_nn_dims *filter_dims,
  52. const q7_t *filter_data,
  53. const cmsis_nn_dims *bias_dims,
  54. const int32_t *bias_data,
  55. const cmsis_nn_dims *output_dims,
  56. q7_t *output_data)
  57. {
  58. if (input_dims->c % 4 != 0 || conv_params->padding.w != 0 || conv_params->padding.h != 0 ||
  59. conv_params->stride.w != 1 || conv_params->stride.h != 1)
  60. {
  61. return ARM_MATH_SIZE_MISMATCH;
  62. }
  63. (void)ctx;
  64. (void)filter_dims;
  65. (void)bias_dims;
  66. #if defined(ARM_MATH_MVEI)
  67. const int32_t col_len = input_dims->w * input_dims->h * input_dims->n;
  68. const int32_t output_ch = output_dims->c;
  69. const int32_t input_ch = input_dims->c;
  70. const int32_t input_offset = conv_params->input_offset;
  71. const int32_t out_offset = conv_params->output_offset;
  72. const int32_t out_activation_min = conv_params->activation.min;
  73. const int32_t out_activation_max = conv_params->activation.max;
  74. int32_t *output_mult = quant_params->multiplier;
  75. int32_t *output_shift = quant_params->shift;
  76. for (int i_items = 0; i_items <= (col_len - 4); i_items += 4)
  77. {
  78. for (int i_out_ch = 0; i_out_ch < output_ch; i_out_ch++)
  79. {
  80. int32_t sum_row = 0;
  81. int32_t temp_out[4];
  82. (void)arm_nn_mat_mul_core_4x_s8(input_ch,
  83. input_ch,
  84. input_data + i_items * input_ch,
  85. filter_data + i_out_ch * input_ch,
  86. &sum_row,
  87. temp_out);
  88. int32x4_t res = vldrwq_s32(temp_out);
  89. if (bias_data)
  90. {
  91. res = vaddq_n_s32(res, bias_data[i_out_ch]);
  92. }
  93. sum_row = sum_row * input_offset;
  94. res = vaddq_n_s32(res, sum_row);
  95. res = arm_requantize_mve(res, output_mult[i_out_ch], output_shift[i_out_ch]);
  96. res = vaddq_n_s32(res, out_offset);
  97. res = vmaxq_s32(res, vdupq_n_s32(out_activation_min));
  98. res = vminq_s32(res, vdupq_n_s32(out_activation_max));
  99. const uint32x4_t scatter_offset = {
  100. 0, (uint32_t)output_ch, (uint32_t)output_ch * 2, (uint32_t)output_ch * 3};
  101. vstrbq_scatter_offset_s32(output_data, scatter_offset, res);
  102. output_data++;
  103. }
  104. output_data += (3 * output_ch);
  105. }
  106. /* Handle left over elements */
  107. for (int i_items = (col_len & ~0x3); i_items < col_len; i_items++)
  108. {
  109. for (int i_out_ch = 0; i_out_ch < output_ch; i_out_ch++)
  110. {
  111. int32_t sum_row = 0;
  112. int32_t acc;
  113. (void)arm_nn_mat_mul_core_1x_s8(
  114. input_ch, input_data + i_items * input_ch, filter_data + i_out_ch * input_ch, &sum_row, &acc);
  115. if (bias_data)
  116. {
  117. acc += bias_data[i_out_ch];
  118. }
  119. sum_row = (sum_row * input_offset);
  120. acc += sum_row;
  121. acc = arm_nn_requantize(acc, output_mult[i_out_ch], output_shift[i_out_ch]);
  122. acc += out_offset;
  123. acc = MAX(acc, out_activation_min);
  124. acc = MIN(acc, out_activation_max);
  125. *output_data++ = acc;
  126. }
  127. }
  128. #else
  129. /* Run the following code as reference implementation for Cortex-M processors with or without DSP extension */
  130. const int32_t lhs_rows = input_dims->w * input_dims->h * input_dims->n;
  131. const int32_t rhs_rows = output_dims->c;
  132. const int32_t rhs_cols = input_dims->c;
  133. arm_nn_mat_mult_nt_t_s8(input_data,
  134. filter_data,
  135. bias_data,
  136. output_data,
  137. quant_params->multiplier,
  138. quant_params->shift,
  139. lhs_rows,
  140. rhs_rows,
  141. rhs_cols,
  142. conv_params->input_offset,
  143. conv_params->output_offset,
  144. conv_params->activation.min,
  145. conv_params->activation.max);
  146. #endif
  147. /* Return to application */
  148. return ARM_MATH_SUCCESS;
  149. }
  150. int32_t arm_convolve_1x1_s8_fast_get_buffer_size(const cmsis_nn_dims *input_dims)
  151. {
  152. (void)input_dims;
  153. return 0;
  154. }
  155. /**
  156. * @} end of NNConv group
  157. */