arm_depthwise_conv_s8.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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: 11. May 2021
  24. * $Revision: V.2.5.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 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_batches,
  130. const uint16_t input_x,
  131. const uint16_t input_y,
  132. const uint16_t input_ch,
  133. const q7_t *kernel,
  134. const uint16_t output_ch,
  135. const uint16_t ch_mult,
  136. const uint16_t kernel_x,
  137. const uint16_t kernel_y,
  138. const uint16_t pad_x,
  139. const uint16_t pad_y,
  140. const uint16_t stride_x,
  141. const uint16_t stride_y,
  142. const int32_t *bias,
  143. q7_t *output,
  144. const int32_t *output_shift,
  145. const int32_t *output_mult,
  146. const uint16_t output_x,
  147. const uint16_t output_y,
  148. const int32_t output_offset,
  149. const int32_t input_offset,
  150. const int32_t output_activation_min,
  151. const int32_t output_activation_max)
  152. {
  153. (void)output_ch;
  154. int i_out = 0;
  155. int i_batch;
  156. for (i_batch = 0; i_batch < input_batches; i_batch++)
  157. {
  158. for (int i_out_y = 0; i_out_y < output_y; i_out_y++)
  159. {
  160. const int16_t base_idx_y = (i_out_y * stride_y) - pad_y;
  161. for (int i_out_x = 0; i_out_x < output_x; i_out_x++)
  162. {
  163. const int16_t base_idx_x = (i_out_x * stride_x) - pad_x;
  164. for (int i_input_ch = 0; i_input_ch < input_ch; i_input_ch++)
  165. {
  166. for (int i_ch_mult = 0; i_ch_mult < ch_mult; i_ch_mult++)
  167. {
  168. const int idx_out_ch = i_ch_mult + i_input_ch * ch_mult;
  169. int32_t acc_0;
  170. /* Condition for kernel start dimension: (base_idx_<x,y> + ker_<x,y>_start) >= 0 */
  171. const int ker_y_start = MAX(0, -base_idx_y);
  172. const int ker_x_start = MAX(0, -base_idx_x);
  173. /* Condition for kernel end dimension: (base_idx_<x,y> + ker_<x,y>_end) < input_<x,y> */
  174. const int ker_y_end = MIN(kernel_y, input_y - base_idx_y);
  175. const int ker_x_end = MIN(kernel_x, input_x - base_idx_x);
  176. acc_0 = bias[idx_out_ch];
  177. for (int i_ker_y = ker_y_start; i_ker_y < ker_y_end; i_ker_y++)
  178. {
  179. const int32_t idx_y = base_idx_y + i_ker_y;
  180. for (int i_ker_x = ker_x_start; i_ker_x < ker_x_end; i_ker_x++)
  181. {
  182. const int32_t idx_x = base_idx_x + i_ker_x;
  183. int32_t idx_0 = (idx_y * input_x + idx_x) * input_ch + i_input_ch;
  184. int32_t ker_idx_0 = (i_ker_y * kernel_x + i_ker_x) * (input_ch * ch_mult) + idx_out_ch;
  185. acc_0 += (input[idx_0] + input_offset) * kernel[ker_idx_0];
  186. }
  187. }
  188. /* Requantize and clamp output to provided range */
  189. acc_0 = arm_nn_requantize(acc_0, output_mult[idx_out_ch], output_shift[idx_out_ch]);
  190. acc_0 += output_offset;
  191. acc_0 = MAX(acc_0, output_activation_min);
  192. acc_0 = MIN(acc_0, output_activation_max);
  193. output[i_out++] = acc_0;
  194. }
  195. }
  196. }
  197. }
  198. /* Advance to the next batch */
  199. input += (input_x * input_y * input_ch);
  200. }
  201. }
  202. /*
  203. * Basic s8 depthwise convolution function.
  204. *
  205. * Refer header file for details.
  206. * Optimization using DSP extension is not available for the generic case where channel multiplier is > 1.
  207. *
  208. */
  209. arm_status arm_depthwise_conv_s8(const cmsis_nn_context *ctx,
  210. const cmsis_nn_dw_conv_params *dw_conv_params,
  211. const cmsis_nn_per_channel_quant_params *quant_params,
  212. const cmsis_nn_dims *input_dims,
  213. const q7_t *input,
  214. const cmsis_nn_dims *filter_dims,
  215. const q7_t *kernel,
  216. const cmsis_nn_dims *bias_dims,
  217. const int32_t *bias,
  218. const cmsis_nn_dims *output_dims,
  219. q7_t *output)
  220. {
  221. (void)dw_conv_params->dilation;
  222. (void)bias_dims;
  223. (void)ctx;
  224. if (dw_conv_params->ch_mult % 4 == 0 && input_dims->n == 1)
  225. {
  226. depthwise_conv_s8_mult_4(input,
  227. input_dims->w,
  228. input_dims->h,
  229. input_dims->c,
  230. kernel,
  231. output_dims->c,
  232. dw_conv_params->ch_mult,
  233. filter_dims->w,
  234. filter_dims->h,
  235. dw_conv_params->padding.w,
  236. dw_conv_params->padding.h,
  237. dw_conv_params->stride.w,
  238. dw_conv_params->stride.h,
  239. bias,
  240. output,
  241. quant_params->shift,
  242. quant_params->multiplier,
  243. output_dims->w,
  244. output_dims->h,
  245. dw_conv_params->output_offset,
  246. dw_conv_params->input_offset,
  247. dw_conv_params->activation.min,
  248. dw_conv_params->activation.max);
  249. }
  250. else
  251. {
  252. depthwise_conv_s8_generic(input,
  253. input_dims->n,
  254. input_dims->w,
  255. input_dims->h,
  256. input_dims->c,
  257. kernel,
  258. output_dims->c,
  259. dw_conv_params->ch_mult,
  260. filter_dims->w,
  261. filter_dims->h,
  262. dw_conv_params->padding.w,
  263. dw_conv_params->padding.h,
  264. dw_conv_params->stride.w,
  265. dw_conv_params->stride.h,
  266. bias,
  267. output,
  268. quant_params->shift,
  269. quant_params->multiplier,
  270. output_dims->w,
  271. output_dims->h,
  272. dw_conv_params->output_offset,
  273. dw_conv_params->input_offset,
  274. dw_conv_params->activation.min,
  275. dw_conv_params->activation.max);
  276. }
  277. /* Return to application */
  278. return ARM_MATH_SUCCESS;
  279. }
  280. /**
  281. * @} end of NNConv group
  282. */