arm_transpose_conv_s8.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2023-2024 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_transpose_conv_s8.c
  21. * Description: s8 version of transpose convolution using symmetric quantization.
  22. *
  23. * $Date: 31 January 2024
  24. * $Revision: V.1.1.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 transpose convolution function.
  40. *
  41. * Refer header file for details.
  42. *
  43. */
  44. arm_cmsis_nn_status arm_transpose_conv_s8(const cmsis_nn_context *ctx,
  45. const cmsis_nn_context *output_ctx,
  46. const cmsis_nn_transpose_conv_params *transpose_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 *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 || output_ctx->buf == NULL)
  59. {
  60. return ARM_CMSIS_NN_ARG_ERROR;
  61. }
  62. const int32_t activation_min = transpose_conv_params->activation.min;
  63. const int32_t activation_max = transpose_conv_params->activation.max;
  64. const int32_t input_ch = input_dims->c;
  65. const int32_t input_size = input_dims->w * input_dims->h;
  66. const uint16_t kernel_x = filter_dims->w;
  67. const uint16_t kernel_y = filter_dims->h;
  68. const int32_t output_x = output_dims->w;
  69. const int32_t output_y = output_dims->h;
  70. const int32_t output_ch = output_dims->c;
  71. const int32_t pad_x = transpose_conv_params->padding.w;
  72. const int32_t pad_y = transpose_conv_params->padding.h;
  73. const int32_t pad_x_offset = transpose_conv_params->padding_offsets.w;
  74. const int32_t pad_y_offset = transpose_conv_params->padding_offsets.h;
  75. const int32_t stride_x = transpose_conv_params->stride.w;
  76. const int32_t stride_y = transpose_conv_params->stride.h;
  77. const int32_t filter_size = filter_dims->w * filter_dims->h;
  78. const int32_t *output_multiplier = quant_params->multiplier;
  79. const int32_t *output_shift = quant_params->shift;
  80. const int32_t out_offset = transpose_conv_params->output_offset;
  81. const int32_t input_offset = transpose_conv_params->input_offset;
  82. const int8_t *input_data_ptr = input_data;
  83. int8_t *output_data_ptr = output_data;
  84. int32_t *const col_data = (int32_t *)ctx->buf;
  85. const int32_t col_buf_size = arm_transpose_conv_s8_get_buffer_size(input_dims, filter_dims, output_dims);
  86. int32_t batch_cnt = input_dims->n;
  87. int32_t *const img_buf = output_ctx->buf;
  88. int32_t *img_buf_ptr = img_buf;
  89. while (batch_cnt)
  90. {
  91. if (bias_data == NULL)
  92. {
  93. arm_memset_s8((int8_t *)img_buf_ptr, 0, output_x * output_y * output_ch * sizeof(int32_t));
  94. }
  95. else
  96. {
  97. int32_t *img_data = img_buf;
  98. for (int i = 0; i < output_x * output_y; i++)
  99. {
  100. memcpy(img_data, bias_data, output_ch * sizeof(int32_t));
  101. img_data += output_ch;
  102. }
  103. }
  104. int32_t *col_data_ptr = col_data;
  105. const int8_t *filter_data_ptr = filter_data;
  106. arm_memset_s8((int8_t *)col_data_ptr, 0, col_buf_size);
  107. for (int i_output_ch = 0; i_output_ch < output_ch; i_output_ch++)
  108. {
  109. arm_nn_mat_mult_nt_t_s8_s32(input_data_ptr,
  110. filter_data_ptr,
  111. col_data_ptr,
  112. input_size,
  113. input_ch,
  114. filter_size,
  115. input_offset,
  116. output_ch);
  117. filter_data_ptr += (input_ch * filter_size);
  118. col_data_ptr++;
  119. }
  120. int32_t *col_buf = col_data;
  121. int32_t *img_data = img_buf_ptr;
  122. const int32_t col_y = (output_y + pad_y_offset + pad_y - kernel_y) / stride_y + 1;
  123. const int32_t col_x = (output_x + pad_x_offset + pad_x - kernel_x) / stride_x + 1;
  124. // Column to image
  125. for (int i_col_y = 0, i_pad_y = -pad_y; i_col_y < col_y; i_col_y++, i_pad_y += stride_y)
  126. {
  127. for (int i_col_x = 0, i_pad_x = -pad_x; i_col_x < col_x; i_col_x++, i_pad_x += stride_x)
  128. {
  129. int32_t *dst_data = img_data + (i_pad_y * output_x + i_pad_x) * output_ch;
  130. for (int32_t i_ker_y = i_pad_y; i_ker_y < i_pad_y + kernel_y; i_ker_y++)
  131. {
  132. for (int32_t i_ker_x = i_pad_x; i_ker_x < i_pad_x + kernel_x; i_ker_x++)
  133. {
  134. if (i_ker_y >= 0 && i_ker_y < output_y && i_ker_x >= 0 && i_ker_x < output_x)
  135. {
  136. for (int i_output_ch = 0; i_output_ch < output_ch; i_output_ch++)
  137. {
  138. dst_data[i_output_ch] += col_buf[i_output_ch];
  139. }
  140. }
  141. dst_data += output_ch;
  142. col_buf += output_ch;
  143. }
  144. dst_data += (output_x - kernel_x) * output_ch;
  145. }
  146. }
  147. }
  148. img_data = img_buf_ptr;
  149. for (int i = 0; i < output_x * output_y; i++)
  150. {
  151. #if defined(ARM_MATH_MVEI)
  152. int output_ch_idx = 0;
  153. int8_t *ip_out_data = output_data_ptr;
  154. for (int32_t i_channel_rmdr = output_ch; i_channel_rmdr > 0; i_channel_rmdr -= 4)
  155. {
  156. mve_pred16_t p = vctp32q((uint32_t)i_channel_rmdr);
  157. int32x4_t result = vldrwq_z_s32(&img_data[output_ch_idx], p);
  158. result = arm_requantize_mve_32x4(result,
  159. vldrwq_z_s32(&output_multiplier[output_ch_idx], p),
  160. vldrwq_z_s32(&output_shift[output_ch_idx], p));
  161. result = vaddq_n_s32(result, out_offset);
  162. result = vmaxq_s32(result, vdupq_n_s32(activation_min));
  163. result = vminq_s32(result, vdupq_n_s32(activation_max));
  164. vstrbq_p_s32(ip_out_data, result, p);
  165. ip_out_data += 4;
  166. output_ch_idx += 4;
  167. }
  168. output_data_ptr += output_ch;
  169. #else
  170. int i_output_ch = 0;
  171. for (; i_output_ch < output_ch; i_output_ch++)
  172. {
  173. int32_t result =
  174. arm_nn_requantize(img_data[i_output_ch], output_multiplier[i_output_ch], output_shift[i_output_ch]);
  175. result += out_offset;
  176. result = MAX(result, activation_min);
  177. result = MIN(result, activation_max);
  178. *output_data_ptr++ = (int8_t)result;
  179. }
  180. #endif
  181. img_data += output_ch;
  182. }
  183. input_data_ptr += (input_size * input_ch);
  184. batch_cnt--;
  185. }
  186. /* Return to application */
  187. return ARM_CMSIS_NN_SUCCESS;
  188. }
  189. /**
  190. * @} end of NNConv group
  191. */