arm_convolve_s16.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2010-2022 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_s16.c
  21. * Description: s16 version of convolution using symmetric quantization.
  22. *
  23. * $Date: January 13, 2022
  24. * $Revision: V.1.1.0
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. /**
  32. * @ingroup groupNN
  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_status arm_convolve_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 q15_t *input_data,
  50. const cmsis_nn_dims *filter_dims,
  51. const q7_t *filter_data,
  52. const cmsis_nn_dims *bias_dims,
  53. const int64_t *bias_data,
  54. const cmsis_nn_dims *output_dims,
  55. q15_t *output_data)
  56. {
  57. (void)bias_dims;
  58. (void)ctx;
  59. const int32_t input_batches = input_dims->n;
  60. const int32_t input_x = input_dims->w;
  61. const int32_t input_y = input_dims->h;
  62. const int32_t input_ch = input_dims->c;
  63. const int32_t kernel_x = filter_dims->w;
  64. const int32_t kernel_y = filter_dims->h;
  65. const int32_t output_x = output_dims->w;
  66. const int32_t output_y = output_dims->h;
  67. const int32_t output_ch = output_dims->c;
  68. const int32_t pad_x = conv_params->padding.w;
  69. const int32_t pad_y = conv_params->padding.h;
  70. const int32_t stride_x = conv_params->stride.w;
  71. const int32_t stride_y = conv_params->stride.h;
  72. const int32_t dilation_x = conv_params->dilation.w;
  73. const int32_t dilation_y = conv_params->dilation.h;
  74. const int32_t out_activation_min = conv_params->activation.min;
  75. const int32_t out_activation_max = conv_params->activation.max;
  76. int32_t *output_mult = quant_params->multiplier;
  77. int32_t *output_shift = quant_params->shift;
  78. for (int i_batch = 0; i_batch < input_batches; i_batch++)
  79. {
  80. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  81. for (int32_t i_out_ch = 0; i_out_ch < output_ch; i_out_ch++)
  82. {
  83. const q31_t reduced_multiplier = REDUCE_MULTIPLIER(output_mult[i_out_ch]);
  84. for (int32_t base_idx_y = -pad_y, i_out_y = 0; i_out_y < output_y; base_idx_y += stride_y, i_out_y++)
  85. {
  86. for (int32_t base_idx_x = -pad_x, i_out_x = 0; i_out_x < output_x; base_idx_x += stride_x, i_out_x++)
  87. {
  88. int64_t conv_out_acc = 0;
  89. const int32_t start_y_max = (-base_idx_y + dilation_y - 1) / dilation_y;
  90. const int32_t ker_y_start = MAX(0, start_y_max);
  91. const int32_t start_x_max = (-base_idx_x + dilation_x - 1) / dilation_x;
  92. const int32_t ker_x_start = MAX(0, start_x_max);
  93. const int32_t end_min_y = (input_y - base_idx_y + dilation_y - 1) / dilation_y;
  94. const int32_t ker_y_end = MIN(kernel_y, end_min_y);
  95. const int32_t end_min_x = (input_x - base_idx_x + dilation_x - 1) / dilation_x;
  96. const int32_t ker_x_end = MIN(kernel_x, end_min_x);
  97. for (int32_t i_ker_y = ker_y_start; i_ker_y < ker_y_end; i_ker_y++)
  98. {
  99. for (int32_t i_ker_x = ker_x_start; i_ker_x < ker_x_end; i_ker_x++)
  100. {
  101. const int32_t in_row = base_idx_y + dilation_y * i_ker_y;
  102. const int32_t in_col = base_idx_x + dilation_x * i_ker_x;
  103. for (int32_t i_input_ch = 0; i_input_ch < input_ch; i_input_ch++)
  104. {
  105. conv_out_acc += input_data[(in_row * input_x + in_col) * input_ch + i_input_ch] *
  106. filter_data[i_out_ch * input_ch * kernel_y * kernel_x +
  107. (i_ker_y * kernel_x + i_ker_x) * input_ch + i_input_ch];
  108. }
  109. }
  110. }
  111. if (bias_data)
  112. {
  113. conv_out_acc += bias_data[i_out_ch];
  114. }
  115. int32_t conv_out = arm_nn_requantize_s64(conv_out_acc, reduced_multiplier, output_shift[i_out_ch]);
  116. conv_out = MAX(conv_out, out_activation_min);
  117. conv_out = MIN(conv_out, out_activation_max);
  118. output_data[i_out_ch + (i_out_y * output_x + i_out_x) * output_ch] = (int16_t)conv_out;
  119. }
  120. }
  121. }
  122. /* Advance to the next batch */
  123. input_data += (input_x * input_y * input_ch);
  124. output_data += (output_x * output_y * output_ch);
  125. }
  126. /* Return to application */
  127. return ARM_MATH_SUCCESS;
  128. }
  129. int32_t arm_convolve_s16_get_buffer_size(const cmsis_nn_dims *input_dims, const cmsis_nn_dims *filter_dims)
  130. {
  131. (void)input_dims;
  132. (void)filter_dims;
  133. return 0;
  134. }
  135. /**
  136. * @} end of NNConv group
  137. */