arm_depthwise_conv_u8_basic_ver1.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_u8_basic_ver1.c
  21. * Description: u8 depthwise convolution function
  22. *
  23. * $Date: 09. October 2020
  24. * $Revision: V.1.1.1
  25. *
  26. * Target : 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_u8_mult_4(const uint8_t *input,
  39. const int32_t input_x,
  40. const int32_t input_y,
  41. const int32_t input_ch,
  42. const uint8_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. uint8_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 filter_offset,
  60. const int32_t output_activation_min,
  61. const int32_t output_activation_max)
  62. {
  63. for (int32_t in_h = -pad_y, out_h = 0, out_idx = 0; out_h < output_y; in_h += stride_y, ++out_h)
  64. {
  65. 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)
  66. {
  67. for (int32_t in_ch = 0, out_ch = 0, ker_w_start = MAX(0, -in_w); out_ch < output_ch;
  68. ++in_ch, out_ch += ch_mult)
  69. {
  70. for (int mult_tile = 0; mult_tile < ch_mult; mult_tile += 4)
  71. {
  72. int32_t out_buff[4];
  73. out_buff[0] = 0;
  74. out_buff[1] = 0;
  75. out_buff[2] = 0;
  76. out_buff[3] = 0;
  77. for (int32_t ker_h = ker_h_start; ker_h < MIN(kernel_y, input_y - in_h); ++ker_h)
  78. {
  79. int32_t ker_idx = ker_h * (output_ch * kernel_x) + ker_w_start * output_ch + out_ch;
  80. int32_t in_idx = (in_h + ker_h) * (input_ch * input_x) + in_w * input_ch + in_ch;
  81. for (int32_t ker_w = ker_w_start; ker_w < MIN(kernel_x, input_x - in_w);
  82. ++ker_w, ker_idx += output_ch)
  83. {
  84. int32_t in_val = input[in_idx + ker_w * input_ch] + input_offset;
  85. out_buff[0] += in_val * (kernel[ker_idx + 0 + mult_tile] + filter_offset);
  86. out_buff[1] += in_val * (kernel[ker_idx + 1 + mult_tile] + filter_offset);
  87. out_buff[2] += in_val * (kernel[ker_idx + 2 + mult_tile] + filter_offset);
  88. out_buff[3] += in_val * (kernel[ker_idx + 3 + mult_tile] + filter_offset);
  89. }
  90. }
  91. if (bias != NULL)
  92. {
  93. out_buff[0] += bias[out_ch + 0 + mult_tile];
  94. out_buff[1] += bias[out_ch + 1 + mult_tile];
  95. out_buff[2] += bias[out_ch + 2 + mult_tile];
  96. out_buff[3] += bias[out_ch + 3 + mult_tile];
  97. }
  98. out_buff[0] = arm_nn_requantize(out_buff[0], output_mult, output_shift);
  99. out_buff[1] = arm_nn_requantize(out_buff[1], output_mult, output_shift);
  100. out_buff[2] = arm_nn_requantize(out_buff[2], output_mult, output_shift);
  101. out_buff[3] = arm_nn_requantize(out_buff[3], output_mult, output_shift);
  102. out_buff[0] += output_offset;
  103. out_buff[1] += output_offset;
  104. out_buff[2] += output_offset;
  105. out_buff[3] += output_offset;
  106. out_buff[0] = MIN(MAX(out_buff[0], output_activation_min), output_activation_max);
  107. out_buff[1] = MIN(MAX(out_buff[1], output_activation_min), output_activation_max);
  108. out_buff[2] = MIN(MAX(out_buff[2], output_activation_min), output_activation_max);
  109. out_buff[3] = MIN(MAX(out_buff[3], output_activation_min), output_activation_max);
  110. output[out_idx++] = (uint8_t)out_buff[0];
  111. output[out_idx++] = (uint8_t)out_buff[1];
  112. output[out_idx++] = (uint8_t)out_buff[2];
  113. output[out_idx++] = (uint8_t)out_buff[3];
  114. }
  115. }
  116. }
  117. }
  118. }
  119. static void depthwise_conv_u8_generic(const uint8_t *input,
  120. const int32_t input_x,
  121. const int32_t input_y,
  122. const int32_t input_ch,
  123. const uint8_t *kernel,
  124. const int32_t output_ch,
  125. const int32_t ch_mult,
  126. const int32_t kernel_x,
  127. const int32_t kernel_y,
  128. const int32_t pad_x,
  129. const int32_t pad_y,
  130. const int32_t stride_x,
  131. const int32_t stride_y,
  132. const int32_t *bias,
  133. uint8_t *output,
  134. const int32_t output_shift,
  135. const int32_t output_mult,
  136. const int32_t output_x,
  137. const int32_t output_y,
  138. const int32_t output_offset,
  139. const int32_t input_offset,
  140. const int32_t filter_offset,
  141. const int32_t output_activation_min,
  142. const int32_t output_activation_max)
  143. {
  144. (void)output_ch;
  145. int i_out = 0;
  146. for (int i_out_y = 0; i_out_y < output_y; i_out_y++)
  147. {
  148. const int16_t base_idx_y = (i_out_y * stride_y) - pad_y;
  149. for (int i_out_x = 0; i_out_x < output_x; i_out_x++)
  150. {
  151. const int16_t base_idx_x = (i_out_x * stride_x) - pad_x;
  152. for (int i_input_ch = 0; i_input_ch < input_ch; i_input_ch++)
  153. {
  154. for (int i_ch_mult = 0; i_ch_mult < ch_mult; i_ch_mult++)
  155. {
  156. const int idx_out_ch = i_ch_mult + i_input_ch * ch_mult;
  157. int32_t acc_0;
  158. /* Condition for kernel start dimension: (base_idx_<x,y> + ker_<x,y>_start) >= 0 */
  159. const int ker_y_start = MAX(0, -base_idx_y);
  160. const int ker_x_start = MAX(0, -base_idx_x);
  161. /* Condition for kernel end dimension: (base_idx_<x,y> + ker_<x,y>_end) < input_<x,y> */
  162. const int ker_y_end = MIN(kernel_y, input_y - base_idx_y);
  163. const int ker_x_end = MIN(kernel_x, input_x - base_idx_x);
  164. acc_0 = 0;
  165. for (int i_ker_y = ker_y_start; i_ker_y < ker_y_end; i_ker_y++)
  166. {
  167. const int32_t idx_y = base_idx_y + i_ker_y;
  168. for (int i_ker_x = ker_x_start; i_ker_x < ker_x_end; i_ker_x++)
  169. {
  170. const int32_t idx_x = base_idx_x + i_ker_x;
  171. int32_t idx_0 = (idx_y * input_x + idx_x) * input_ch + i_input_ch;
  172. int32_t ker_idx_0 = (i_ker_y * kernel_x + i_ker_x) * (input_ch * ch_mult) + idx_out_ch;
  173. acc_0 += (input[idx_0] + input_offset) * (kernel[ker_idx_0] + filter_offset);
  174. }
  175. }
  176. if (bias != NULL)
  177. {
  178. acc_0 += bias[idx_out_ch];
  179. }
  180. /* Requantize and clamp output to provided range */
  181. acc_0 = arm_nn_requantize(acc_0, output_mult, output_shift);
  182. acc_0 += output_offset;
  183. acc_0 = MAX(acc_0, output_activation_min);
  184. acc_0 = MIN(acc_0, output_activation_max);
  185. output[i_out++] = acc_0;
  186. }
  187. }
  188. }
  189. }
  190. }
  191. /**
  192. * @brief uint8 depthwise convolution function with asymmetric quantization
  193. *
  194. * @param[in] input Pointer to input tensor
  195. * @param[in] input_x Width of input tensor
  196. * @param[in] input_y Height of input tensor
  197. * @param[in] input_ch Channels in input tensor
  198. * @param[in] kernel Pointer to kernel weights
  199. * @param[in] kernel_x Width of kernel
  200. * @param[in] kernel_y Height of kernel
  201. * @param[in] ch_mult Number of channel multiplier
  202. * @param[in] pad_x Padding sizes x
  203. * @param[in] pad_y Padding sizes y
  204. * @param[in] stride_x Convolution stride along the width
  205. * @param[in] stride_y Convolution stride along the height
  206. * @param[in] dilation_x Dilation along width. Not used and intended for future enhancement.
  207. * @param[in] dilation_y Dilation along height. Not used and intended for future enhancement.
  208. * @param[in] bias Pointer to optional bias values. If no bias is
  209. * availble, NULL is expected
  210. * @param[in] input_offset Input tensor zero offset
  211. * @param[in] filter_offset Kernel tensor zero offset
  212. * @param[in] output_offset Output tensor zero offset
  213. * @param[in,out] output Pointer to output tensor
  214. * @param[in] output_x Width of output tensor
  215. * @param[in] output_y Height of output tensor
  216. * @param[in] output_activation_min Minimum value to clamp the output to. Range : {0, 255}
  217. * @param[in] output_activation_max Minimum value to clamp the output to. Range : {0, 255}
  218. * @param[in] output_shift Amount of right-shift for output
  219. * @param[in] output_mult Output multiplier for requantization
  220. * @return The function returns one of the following
  221. * <code>ARM_MATH_SIZE_MISMATCH</code> - Not supported dimension of tensors
  222. * <code>ARM_MATH_SUCCESS</code> - Successful operation
  223. * <code>ARM_MATH_ARGUMENT_ERROR</code> - Implementation not available
  224. *
  225. *
  226. */
  227. arm_status arm_depthwise_conv_u8_basic_ver1(const uint8_t *input,
  228. const uint16_t input_x,
  229. const uint16_t input_y,
  230. const uint16_t input_ch,
  231. const uint8_t *kernel,
  232. const uint16_t kernel_x,
  233. const uint16_t kernel_y,
  234. const int16_t ch_mult,
  235. const int16_t pad_x,
  236. const int16_t pad_y,
  237. const int16_t stride_x,
  238. const int16_t stride_y,
  239. const int16_t dilation_x,
  240. const int16_t dilation_y,
  241. const int32_t *bias,
  242. const int32_t input_offset,
  243. const int32_t filter_offset,
  244. const int32_t output_offset,
  245. uint8_t *output,
  246. const uint16_t output_x,
  247. const uint16_t output_y,
  248. const int32_t output_activation_min,
  249. const int32_t output_activation_max,
  250. const int32_t output_shift,
  251. const int32_t output_mult)
  252. {
  253. (void)dilation_x;
  254. (void)dilation_y;
  255. if (ch_mult % 4 == 0)
  256. {
  257. depthwise_conv_u8_mult_4(input,
  258. input_x,
  259. input_y,
  260. input_ch,
  261. kernel,
  262. ch_mult * input_ch,
  263. ch_mult,
  264. kernel_x,
  265. kernel_y,
  266. pad_x,
  267. pad_y,
  268. stride_x,
  269. stride_y,
  270. bias,
  271. output,
  272. output_shift,
  273. output_mult,
  274. output_x,
  275. output_y,
  276. output_offset,
  277. input_offset,
  278. filter_offset,
  279. output_activation_min,
  280. output_activation_max);
  281. }
  282. else
  283. {
  284. depthwise_conv_u8_generic(input,
  285. input_x,
  286. input_y,
  287. input_ch,
  288. kernel,
  289. ch_mult * input_ch,
  290. ch_mult,
  291. kernel_x,
  292. kernel_y,
  293. pad_x,
  294. pad_y,
  295. stride_x,
  296. stride_y,
  297. bias,
  298. output,
  299. output_shift,
  300. output_mult,
  301. output_x,
  302. output_y,
  303. output_offset,
  304. input_offset,
  305. filter_offset,
  306. output_activation_min,
  307. output_activation_max);
  308. }
  309. /* Return to application */
  310. return ARM_MATH_SUCCESS;
  311. }
  312. /**
  313. * @} end of NNConv group
  314. */