arm_convolve_get_buffer_sizes_s8.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_get_buffer_sizes_s8.c
  21. * Description: Collection of get buffer size functions for the various s8 convolution layer functions.
  22. *
  23. * $Date: 8 March 2023
  24. * $Revision: V.1.1.0
  25. *
  26. * Target : Arm(R) M-Profile Architecture
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "Internal/arm_nn_compiler.h"
  30. #include "arm_nnfunctions.h"
  31. /**
  32. * @ingroup NNConv
  33. */
  34. /**
  35. * @addtogroup GetBufferSizeNNConv
  36. * @{
  37. */
  38. __STATIC_INLINE int32_t arm_convolve_s8_get_buffer_size_mve(const cmsis_nn_dims *input_dims,
  39. const cmsis_nn_dims *filter_dims)
  40. {
  41. int32_t col_length = input_dims->c * filter_dims->w * filter_dims->h;
  42. // Get number of complete int16 lanes(multiple of 8) for given col_length. This is dependent on
  43. // implementation of arm_nn_mat_mult_s8
  44. col_length = (col_length + 7) / 8;
  45. // 4 -> number of im2col buffers, 8 -> 8 elements per Q register
  46. return 4 * col_length * 8 * (int32_t)sizeof(int8_t);
  47. }
  48. __STATIC_INLINE int32_t arm_convolve_1_x_n_s8_get_buffer_size_mve(const cmsis_nn_dims *input_dims,
  49. const cmsis_nn_dims *filter_dims)
  50. {
  51. (void)input_dims;
  52. (void)filter_dims;
  53. return 0;
  54. }
  55. int32_t arm_convolve_s8_get_buffer_size(const cmsis_nn_dims *input_dims, const cmsis_nn_dims *filter_dims)
  56. {
  57. #if defined(ARM_MATH_MVEI)
  58. return arm_convolve_s8_get_buffer_size_mve(input_dims, filter_dims);
  59. #else
  60. return (2 * input_dims->c * filter_dims->w * filter_dims->h) * (int32_t)sizeof(int16_t);
  61. #endif
  62. }
  63. int32_t arm_convolve_1_x_n_s8_get_buffer_size(const cmsis_nn_dims *input_dims, const cmsis_nn_dims *filter_dims)
  64. {
  65. #if !defined(ARM_MATH_MVEI)
  66. return arm_convolve_s8_get_buffer_size(input_dims, filter_dims);
  67. #else
  68. return arm_convolve_1_x_n_s8_get_buffer_size_mve(input_dims, filter_dims);
  69. #endif
  70. }
  71. int32_t arm_convolve_1x1_s8_fast_get_buffer_size(const cmsis_nn_dims *input_dims)
  72. {
  73. (void)input_dims;
  74. return 0;
  75. }
  76. /*
  77. * Get the required buffer size for arm_convolve_wrapper_s8. This is the recommended function convolve wrapper s8
  78. * function.
  79. *
  80. * Refer to header file for details.
  81. *
  82. */
  83. int32_t arm_convolve_wrapper_s8_get_buffer_size(const cmsis_nn_conv_params *conv_params,
  84. const cmsis_nn_dims *input_dims,
  85. const cmsis_nn_dims *filter_dims,
  86. const cmsis_nn_dims *output_dims)
  87. {
  88. #if defined(ARM_MATH_MVEI)
  89. return arm_convolve_wrapper_s8_get_buffer_size_mve(conv_params, input_dims, filter_dims, output_dims);
  90. #else
  91. (void)output_dims;
  92. if ((conv_params->padding.w == 0) && (conv_params->padding.h == 0) && (filter_dims->w == 1) &&
  93. (filter_dims->h == 1) && (conv_params->dilation.w == 1 && conv_params->dilation.h == 1))
  94. {
  95. if ((conv_params->stride.w == 1) && (conv_params->stride.h == 1))
  96. {
  97. return arm_convolve_1x1_s8_fast_get_buffer_size(input_dims);
  98. }
  99. else
  100. {
  101. return 0;
  102. }
  103. }
  104. else if ((input_dims->h == 1) && (conv_params->dilation.w == 1) && (filter_dims->h == 1) &&
  105. (conv_params->stride.w * input_dims->c % 4 == 0))
  106. {
  107. return arm_convolve_1_x_n_s8_get_buffer_size(input_dims, filter_dims);
  108. }
  109. else
  110. {
  111. return arm_convolve_s8_get_buffer_size(input_dims, filter_dims);
  112. }
  113. #endif
  114. }
  115. int32_t arm_convolve_wrapper_s8_get_buffer_size_mve(const cmsis_nn_conv_params *conv_params,
  116. const cmsis_nn_dims *input_dims,
  117. const cmsis_nn_dims *filter_dims,
  118. const cmsis_nn_dims *output_dims)
  119. {
  120. (void)output_dims;
  121. if ((conv_params->padding.w == 0) && (conv_params->padding.h == 0) && (filter_dims->w == 1) &&
  122. (filter_dims->h == 1) && (conv_params->dilation.w == 1 && conv_params->dilation.h == 1))
  123. {
  124. if ((conv_params->stride.w == 1) && (conv_params->stride.h == 1))
  125. {
  126. return arm_convolve_1x1_s8_fast_get_buffer_size(input_dims);
  127. }
  128. else
  129. {
  130. return 0;
  131. }
  132. }
  133. else if ((input_dims->h == 1) && (conv_params->dilation.w == 1) && (filter_dims->h == 1) &&
  134. (conv_params->stride.w * input_dims->c % 4 == 0))
  135. {
  136. return arm_convolve_1_x_n_s8_get_buffer_size_mve(input_dims, filter_dims);
  137. }
  138. else
  139. {
  140. return arm_convolve_s8_get_buffer_size_mve(input_dims, filter_dims);
  141. }
  142. }
  143. int32_t arm_convolve_wrapper_s8_get_buffer_size_dsp(const cmsis_nn_conv_params *conv_params,
  144. const cmsis_nn_dims *input_dims,
  145. const cmsis_nn_dims *filter_dims,
  146. const cmsis_nn_dims *output_dims)
  147. {
  148. return arm_convolve_wrapper_s8_get_buffer_size(conv_params, input_dims, filter_dims, output_dims);
  149. }
  150. /**
  151. * @} end of GetBufferSizeNNConv group
  152. */