arm_depthwise_conv_s8.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (C) 2010-2020 Arm Limited or its affiliates. All rights reserved.
  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_s8.c
  21. * Description: s8 version of depthwise convolution.
  22. *
  23. * $Date: 09. October 2020
  24. * $Revision: V.2.0.1
  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 depthwise_conv_s8_mult_4(const int8_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 int32_t *bias,
  52. int8_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_offset,
  58. const int32_t input_offset,
  59. const int32_t output_activation_min,
  60. const int32_t output_activation_max)
  61. {
  62. for (int32_t in_h = -pad_y, out_h = 0, out_idx = 0; out_h < output_y; in_h += stride_y, ++out_h)
  63. {
  64. 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)
  65. {
  66. for (int32_t in_ch = 0, out_ch = 0, ker_w_start = MAX(0, -in_w); out_ch < output_ch;
  67. ++in_ch, out_ch += ch_mult)
  68. {
  69. for (int mult_tile = 0; mult_tile < ch_mult; mult_tile += 4)
  70. {
  71. int32_t out_buff[4];
  72. out_buff[0] = bias[out_ch + 0 + mult_tile];
  73. out_buff[1] = bias[out_ch + 1 + mult_tile];
  74. out_buff[2] = bias[out_ch + 2 + mult_tile];
  75. out_buff[3] = bias[out_ch + 3 + mult_tile];
  76. for (int32_t ker_h = ker_h_start; ker_h < MIN(kernel_y, input_y - in_h); ++ker_h)
  77. {
  78. int32_t ker_idx = ker_h * (output_ch * kernel_x) + ker_w_start * output_ch + out_ch;
  79. int32_t in_idx = (in_h + ker_h) * (input_ch * input_x) + in_w * input_ch + in_ch;
  80. for (int32_t ker_w = ker_w_start; ker_w < MIN(kernel_x, input_x - in_w);
  81. ++ker_w, ker_idx += output_ch)
  82. {
  83. int32_t in_val = input[in_idx + ker_w * input_ch] + input_offset;
  84. out_buff[0] += in_val * kernel[ker_idx + 0 + mult_tile];
  85. out_buff[1] += in_val * kernel[ker_idx + 1 + mult_tile];
  86. out_buff[2] += in_val * kernel[ker_idx + 2 + mult_tile];
  87. out_buff[3] += in_val * kernel[ker_idx + 3 + mult_tile];
  88. }
  89. }
  90. #if defined(ARM_MATH_MVEI)
  91. (void)out_idx;
  92. int32x4_t res = vldrwq_s32(out_buff);
  93. res = arm_requantize_mve_32x4(res,
  94. vldrwq_s32(&output_mult[out_ch + mult_tile]),
  95. vldrwq_s32(&output_shift[out_ch + mult_tile]));
  96. res = vaddq_n_s32(res, output_offset);
  97. res = vmaxq_s32(res, vdupq_n_s32(output_activation_min));
  98. res = vminq_s32(res, vdupq_n_s32(output_activation_max));
  99. vstrbq_s32(output, res);
  100. output += 4;
  101. #else
  102. out_buff[0] = arm_nn_requantize(
  103. out_buff[0], output_mult[out_ch + 0 + mult_tile], output_shift[out_ch + 0 + mult_tile]);
  104. out_buff[1] = arm_nn_requantize(
  105. out_buff[1], output_mult[out_ch + 1 + mult_tile], output_shift[out_ch + 1 + mult_tile]);
  106. out_buff[2] = arm_nn_requantize(
  107. out_buff[2], output_mult[out_ch + 2 + mult_tile], output_shift[out_ch + 2 + mult_tile]);
  108. out_buff[3] = arm_nn_requantize(
  109. out_buff[3], output_mult[out_ch + 3 + mult_tile], output_shift[out_ch + 3 + mult_tile]);
  110. out_buff[0] += output_offset;
  111. out_buff[1] += output_offset;
  112. out_buff[2] += output_offset;
  113. out_buff[3] += output_offset;
  114. out_buff[0] = MIN(MAX(out_buff[0], output_activation_min), output_activation_max);
  115. out_buff[1] = MIN(MAX(out_buff[1], output_activation_min), output_activation_max);
  116. out_buff[2] = MIN(MAX(out_buff[2], output_activation_min), output_activation_max);
  117. out_buff[3] = MIN(MAX(out_buff[3], output_activation_min), output_activation_max);
  118. output[out_idx++] = (int8_t)out_buff[0];
  119. output[out_idx++] = (int8_t)out_buff[1];
  120. output[out_idx++] = (int8_t)out_buff[2];
  121. output[out_idx++] = (int8_t)out_buff[3];
  122. #endif
  123. }
  124. }
  125. }
  126. }
  127. }
  128. static void depthwise_conv_s8_generic(const q7_t *input,
  129. const uint16_t input_x,
  130. const uint16_t input_y,
  131. const uint16_t input_ch,
  132. const q7_t *kernel,
  133. const uint16_t output_ch,
  134. const uint16_t ch_mult,
  135. const uint16_t kernel_x,
  136. const uint16_t kernel_y,
  137. const uint16_t pad_x,
  138. const uint16_t pad_y,
  139. const uint16_t stride_x,
  140. const uint16_t stride_y,
  141. const int32_t *bias,
  142. q7_t *output,
  143. const int32_t *output_shift,
  144. const int32_t *output_mult,
  145. const uint16_t output_x,
  146. const uint16_t output_y,
  147. const int32_t output_offset,
  148. const int32_t input_offset,
  149. const int32_t output_activation_min,
  150. const int32_t output_activation_max)
  151. {
  152. (void)output_ch;
  153. int i_out = 0;
  154. for (int i_out_y = 0; i_out_y < output_y; i_out_y++)
  155. {
  156. const int16_t base_idx_y = (i_out_y * stride_y) - pad_y;
  157. for (int i_out_x = 0; i_out_x < output_x; i_out_x++)
  158. {
  159. const int16_t base_idx_x = (i_out_x * stride_x) - pad_x;
  160. for (int i_input_ch = 0; i_input_ch < input_ch; i_input_ch++)
  161. {
  162. for (int i_ch_mult = 0; i_ch_mult < ch_mult; i_ch_mult++)
  163. {
  164. const int idx_out_ch = i_ch_mult + i_input_ch * ch_mult;
  165. int32_t acc_0;
  166. /* Condition for kernel start dimension: (base_idx_<x,y> + ker_<x,y>_start) >= 0 */
  167. const int ker_y_start = MAX(0, -base_idx_y);
  168. const int ker_x_start = MAX(0, -base_idx_x);
  169. /* Condition for kernel end dimension: (base_idx_<x,y> + ker_<x,y>_end) < input_<x,y> */
  170. const int ker_y_end = MIN(kernel_y, input_y - base_idx_y);
  171. const int ker_x_end = MIN(kernel_x, input_x - base_idx_x);
  172. acc_0 = bias[idx_out_ch];
  173. for (int i_ker_y = ker_y_start; i_ker_y < ker_y_end; i_ker_y++)
  174. {
  175. const int32_t idx_y = base_idx_y + i_ker_y;
  176. for (int i_ker_x = ker_x_start; i_ker_x < ker_x_end; i_ker_x++)
  177. {
  178. const int32_t idx_x = base_idx_x + i_ker_x;
  179. int32_t idx_0 = (idx_y * input_x + idx_x) * input_ch + i_input_ch;
  180. int32_t ker_idx_0 = (i_ker_y * kernel_x + i_ker_x) * (input_ch * ch_mult) + idx_out_ch;
  181. acc_0 += (input[idx_0] + input_offset) * kernel[ker_idx_0];
  182. }
  183. }
  184. /* Requantize and clamp output to provided range */
  185. acc_0 = arm_nn_requantize(acc_0, output_mult[idx_out_ch], output_shift[idx_out_ch]);
  186. acc_0 += output_offset;
  187. acc_0 = MAX(acc_0, output_activation_min);
  188. acc_0 = MIN(acc_0, output_activation_max);
  189. output[i_out++] = acc_0;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. /*
  196. * Basic s8 depthwise convolution function.
  197. *
  198. * Refer header file for details.
  199. * Optimization using DSP extension is not available for the generic case where channel multiplier is > 1.
  200. *
  201. */
  202. arm_status arm_depthwise_conv_s8(const cmsis_nn_context *ctx,
  203. const cmsis_nn_dw_conv_params *dw_conv_params,
  204. const cmsis_nn_per_channel_quant_params *quant_params,
  205. const cmsis_nn_dims *input_dims,
  206. const q7_t *input,
  207. const cmsis_nn_dims *filter_dims,
  208. const q7_t *kernel,
  209. const cmsis_nn_dims *bias_dims,
  210. const int32_t *bias,
  211. const cmsis_nn_dims *output_dims,
  212. q7_t *output)
  213. {
  214. (void)dw_conv_params->dilation;
  215. (void)bias_dims;
  216. (void)ctx;
  217. if (dw_conv_params->ch_mult % 4 == 0)
  218. {
  219. depthwise_conv_s8_mult_4(input,
  220. input_dims->w,
  221. input_dims->h,
  222. input_dims->c,
  223. kernel,
  224. output_dims->c,
  225. dw_conv_params->ch_mult,
  226. filter_dims->w,
  227. filter_dims->h,
  228. dw_conv_params->padding.w,
  229. dw_conv_params->padding.h,
  230. dw_conv_params->stride.w,
  231. dw_conv_params->stride.h,
  232. bias,
  233. output,
  234. quant_params->shift,
  235. quant_params->multiplier,
  236. output_dims->w,
  237. output_dims->h,
  238. dw_conv_params->output_offset,
  239. dw_conv_params->input_offset,
  240. dw_conv_params->activation.min,
  241. dw_conv_params->activation.max);
  242. }
  243. else
  244. {
  245. depthwise_conv_s8_generic(input,
  246. input_dims->w,
  247. input_dims->h,
  248. input_dims->c,
  249. kernel,
  250. output_dims->c,
  251. dw_conv_params->ch_mult,
  252. filter_dims->w,
  253. filter_dims->h,
  254. dw_conv_params->padding.w,
  255. dw_conv_params->padding.h,
  256. dw_conv_params->stride.w,
  257. dw_conv_params->stride.h,
  258. bias,
  259. output,
  260. quant_params->shift,
  261. quant_params->multiplier,
  262. output_dims->w,
  263. output_dims->h,
  264. dw_conv_params->output_offset,
  265. dw_conv_params->input_offset,
  266. dw_conv_params->activation.min,
  267. dw_conv_params->activation.max);
  268. }
  269. /* Return to application */
  270. return ARM_MATH_SUCCESS;
  271. }
  272. /**
  273. * @} end of NNConv group
  274. */