arm_convolve_1x1_s8_fast.c 5.4 KB

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