arm_depthwise_conv_s16.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 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_depthwise_conv_s16.c
  21. * Description: s16 version of depthwise convolution.
  22. *
  23. * $Date: 26. Jan 2022
  24. * $Revision: V.1.0.0
  25. *
  26. * Target Processor: Cortex-M CPUs
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. /**
  32. * @ingroup groupNN
  33. */
  34. /**
  35. * @addtogroup NNConv
  36. * @{
  37. */
  38. static void __attribute__((unused)) depthwise_conv_s16_mult_4_s16(const int16_t *input,
  39. const int32_t input_x,
  40. const int32_t input_y,
  41. const int32_t input_ch,
  42. const int8_t *kernel,
  43. const int32_t output_ch,
  44. const int32_t ch_mult,
  45. const int32_t kernel_x,
  46. const int32_t kernel_y,
  47. const int32_t pad_x,
  48. const int32_t pad_y,
  49. const int32_t stride_x,
  50. const int32_t stride_y,
  51. const int64_t *bias,
  52. int16_t *output,
  53. const int32_t *output_shift,
  54. const int32_t *output_mult,
  55. const int32_t output_x,
  56. const int32_t output_y,
  57. const int32_t output_activation_min,
  58. const int32_t output_activation_max)
  59. {
  60. for (int32_t in_h = -pad_y, out_h = 0, out_idx = 0; out_h < output_y; in_h += stride_y, ++out_h)
  61. {
  62. for (int32_t in_w = -pad_x, out_w = 0, ker_h_start = MAX(0, -in_h); out_w < output_x; in_w += stride_x, ++out_w)
  63. {
  64. for (int32_t in_ch = 0, out_ch = 0, ker_w_start = MAX(0, -in_w); out_ch < output_ch;
  65. ++in_ch, out_ch += ch_mult)
  66. {
  67. for (int mult_tile = 0; mult_tile < ch_mult; mult_tile += 4)
  68. {
  69. int32_t out_buff32[4] = {REDUCE_MULTIPLIER(output_mult[out_ch + 0 + mult_tile]),
  70. REDUCE_MULTIPLIER(output_mult[out_ch + 1 + mult_tile]),
  71. REDUCE_MULTIPLIER(output_mult[out_ch + 2 + mult_tile]),
  72. REDUCE_MULTIPLIER(output_mult[out_ch + 3 + mult_tile])};
  73. int64_t out_buff[4] = {0, 0, 0, 0};
  74. if (bias)
  75. {
  76. out_buff[0] = bias[out_ch + 0 + mult_tile];
  77. out_buff[1] = bias[out_ch + 1 + mult_tile];
  78. out_buff[2] = bias[out_ch + 2 + mult_tile];
  79. out_buff[3] = bias[out_ch + 3 + mult_tile];
  80. }
  81. for (int32_t ker_h = ker_h_start; ker_h < MIN(kernel_y, input_y - in_h); ++ker_h)
  82. {
  83. int32_t ker_idx = ker_h * (output_ch * kernel_x) + ker_w_start * output_ch + out_ch;
  84. int32_t in_idx = (in_h + ker_h) * (input_ch * input_x) + in_w * input_ch + in_ch;
  85. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  86. #pragma clang loop unroll(disable)
  87. #endif
  88. for (int32_t ker_w = ker_w_start; ker_w < MIN(kernel_x, input_x - in_w);
  89. ++ker_w, ker_idx += output_ch)
  90. {
  91. // TODO: Unroll of 4 with 64 bit accumulator will probably result in too much register
  92. // spills. Try with unroll of 2 when enabling this.
  93. int32_t in_val = input[in_idx + ker_w * input_ch];
  94. out_buff[0] += in_val * kernel[ker_idx + 0 + mult_tile];
  95. out_buff[1] += in_val * kernel[ker_idx + 1 + mult_tile];
  96. out_buff[2] += in_val * kernel[ker_idx + 2 + mult_tile];
  97. out_buff[3] += in_val * kernel[ker_idx + 3 + mult_tile];
  98. }
  99. }
  100. out_buff32[0] =
  101. arm_nn_requantize_s64(out_buff[0], out_buff32[0], output_shift[out_ch + 0 + mult_tile]);
  102. out_buff32[1] =
  103. arm_nn_requantize_s64(out_buff[1], out_buff32[1], output_shift[out_ch + 1 + mult_tile]);
  104. out_buff32[2] =
  105. arm_nn_requantize_s64(out_buff[2], out_buff32[2], output_shift[out_ch + 2 + mult_tile]);
  106. out_buff32[3] =
  107. arm_nn_requantize_s64(out_buff[3], out_buff32[3], output_shift[out_ch + 3 + mult_tile]);
  108. out_buff32[0] = MIN(MAX(out_buff32[0], output_activation_min), output_activation_max);
  109. out_buff32[1] = MIN(MAX(out_buff32[1], output_activation_min), output_activation_max);
  110. out_buff32[2] = MIN(MAX(out_buff32[2], output_activation_min), output_activation_max);
  111. out_buff32[3] = MIN(MAX(out_buff32[3], output_activation_min), output_activation_max);
  112. output[out_idx++] = (int16_t)out_buff32[0];
  113. output[out_idx++] = (int16_t)out_buff32[1];
  114. output[out_idx++] = (int16_t)out_buff32[2];
  115. output[out_idx++] = (int16_t)out_buff32[3];
  116. }
  117. }
  118. }
  119. }
  120. }
  121. static void depthwise_conv_s16_generic_s16(const int16_t *input,
  122. const uint16_t input_batches,
  123. const uint16_t input_x,
  124. const uint16_t input_y,
  125. const uint16_t input_ch,
  126. const int8_t *kernel,
  127. const uint16_t ch_mult,
  128. const uint16_t kernel_x,
  129. const uint16_t kernel_y,
  130. const uint16_t pad_x,
  131. const uint16_t pad_y,
  132. const uint16_t stride_x,
  133. const uint16_t stride_y,
  134. const int64_t *bias,
  135. int16_t *output,
  136. const int32_t *output_shift,
  137. const int32_t *output_mult,
  138. const uint16_t output_x,
  139. const uint16_t output_y,
  140. const int32_t output_activation_min,
  141. const int32_t output_activation_max,
  142. const uint16_t dilation_x,
  143. const uint16_t dilation_y)
  144. {
  145. for (int i_batch = 0; i_batch < input_batches; i_batch++)
  146. {
  147. for (int i_out_y = 0; i_out_y < output_y; i_out_y++)
  148. {
  149. const int16_t base_idx_y = (i_out_y * stride_y) - pad_y;
  150. for (int i_out_x = 0; i_out_x < output_x; i_out_x++)
  151. {
  152. const int16_t base_idx_x = (i_out_x * stride_x) - pad_x;
  153. for (int i_input_ch = 0; i_input_ch < input_ch; i_input_ch++)
  154. {
  155. for (int i_ch_mult = 0; i_ch_mult < ch_mult; i_ch_mult++)
  156. {
  157. const int idx_out_ch = i_ch_mult + i_input_ch * ch_mult;
  158. const q31_t reduced_multiplier = REDUCE_MULTIPLIER(output_mult[idx_out_ch]);
  159. int64_t acc_0 = 0;
  160. int ker_y_start;
  161. int ker_x_start;
  162. int ker_y_end;
  163. int ker_x_end;
  164. if (dilation_x > 1)
  165. {
  166. const int32_t start_x_max = (-base_idx_x + dilation_x - 1) / dilation_x;
  167. ker_x_start = MAX(0, start_x_max);
  168. const int32_t end_min_x = (input_x - base_idx_x + dilation_x - 1) / dilation_x;
  169. ker_x_end = MIN(kernel_x, end_min_x);
  170. }
  171. else
  172. {
  173. ker_x_start = MAX(0, -base_idx_x);
  174. ker_x_end = MIN(kernel_x, input_x - base_idx_x);
  175. }
  176. if (dilation_y > 1)
  177. {
  178. const int32_t start_y_max = (-base_idx_y + dilation_y - 1) / dilation_y;
  179. ker_y_start = MAX(0, start_y_max);
  180. const int32_t end_min_y = (input_y - base_idx_y + dilation_y - 1) / dilation_y;
  181. ker_y_end = MIN(kernel_y, end_min_y);
  182. }
  183. else
  184. {
  185. ker_y_start = MAX(0, -base_idx_y);
  186. ker_y_end = MIN(kernel_y, input_y - base_idx_y);
  187. }
  188. if (bias)
  189. {
  190. acc_0 = bias[idx_out_ch];
  191. }
  192. for (int i_ker_y = ker_y_start; i_ker_y < ker_y_end; i_ker_y++)
  193. {
  194. const int32_t idx_y = base_idx_y + dilation_y * i_ker_y;
  195. for (int i_ker_x = ker_x_start; i_ker_x < ker_x_end; i_ker_x++)
  196. {
  197. const int32_t idx_x = base_idx_x + dilation_x * i_ker_x;
  198. int32_t idx_0 = (idx_y * input_x + idx_x) * input_ch + i_input_ch;
  199. int32_t ker_idx_0 = (i_ker_y * kernel_x + i_ker_x) * (input_ch * ch_mult) + idx_out_ch;
  200. acc_0 += input[idx_0] * kernel[ker_idx_0];
  201. }
  202. }
  203. /* Requantize and clamp output to provided range */
  204. int32_t result = arm_nn_requantize_s64(acc_0, reduced_multiplier, output_shift[idx_out_ch]);
  205. result = MAX(result, output_activation_min);
  206. result = MIN(result, output_activation_max);
  207. *output++ = (int16_t)result;
  208. }
  209. }
  210. }
  211. }
  212. /* Advance to the next batch */
  213. input += (input_x * input_y * input_ch);
  214. }
  215. }
  216. /*
  217. * Basic s16 depthwise convolution function.
  218. *
  219. * Refer header file for details.
  220. *
  221. */
  222. arm_status arm_depthwise_conv_s16(const cmsis_nn_context *ctx,
  223. const cmsis_nn_dw_conv_params *dw_conv_params,
  224. const cmsis_nn_per_channel_quant_params *quant_params,
  225. const cmsis_nn_dims *input_dims,
  226. const q15_t *input,
  227. const cmsis_nn_dims *filter_dims,
  228. const q7_t *kernel,
  229. const cmsis_nn_dims *bias_dims,
  230. const int64_t *bias,
  231. const cmsis_nn_dims *output_dims,
  232. q15_t *output)
  233. {
  234. const uint16_t dilation_x = dw_conv_params->dilation.w;
  235. const uint16_t dilation_y = dw_conv_params->dilation.h;
  236. (void)bias_dims;
  237. (void)ctx;
  238. depthwise_conv_s16_generic_s16(input,
  239. input_dims->n,
  240. input_dims->w,
  241. input_dims->h,
  242. input_dims->c,
  243. kernel,
  244. dw_conv_params->ch_mult,
  245. filter_dims->w,
  246. filter_dims->h,
  247. dw_conv_params->padding.w,
  248. dw_conv_params->padding.h,
  249. dw_conv_params->stride.w,
  250. dw_conv_params->stride.h,
  251. bias,
  252. output,
  253. quant_params->shift,
  254. quant_params->multiplier,
  255. output_dims->w,
  256. output_dims->h,
  257. dw_conv_params->activation.min,
  258. dw_conv_params->activation.max,
  259. dilation_x,
  260. dilation_y);
  261. /* Return to application */
  262. return ARM_MATH_SUCCESS;
  263. }
  264. /**
  265. * @} end of NNConv group
  266. */