arm_depthwise_conv_s8.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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: March 20, 2020
  24. * $Revision: V.1.1.1
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_math.h"
  30. #include "arm_nnfunctions.h"
  31. #include "arm_nnsupportfunctions.h"
  32. /**
  33. * @ingroup groupNN
  34. */
  35. /**
  36. * @addtogroup NNConv
  37. * @{
  38. */
  39. static void depthwise_conv_s8_mult_4(const int8_t *input,
  40. const int32_t input_x,
  41. const int32_t input_y,
  42. const int32_t input_ch,
  43. const int8_t *kernel,
  44. const int32_t output_ch,
  45. const int32_t ch_mult,
  46. const int32_t kernel_x,
  47. const int32_t kernel_y,
  48. const int32_t pad_x,
  49. const int32_t pad_y,
  50. const int32_t stride_x,
  51. const int32_t stride_y,
  52. const int32_t *bias,
  53. int8_t *output,
  54. const int32_t *output_shift,
  55. const int32_t *output_mult,
  56. const int32_t output_x,
  57. const int32_t output_y,
  58. const int32_t output_offset,
  59. const int32_t input_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; ++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); ++ker_w, ker_idx += output_ch)
  81. {
  82. int32_t in_val = input[in_idx + ker_w * input_ch] + input_offset;
  83. out_buff[0] += in_val * kernel[ker_idx + 0 + mult_tile];
  84. out_buff[1] += in_val * kernel[ker_idx + 1 + mult_tile];
  85. out_buff[2] += in_val * kernel[ker_idx + 2 + mult_tile];
  86. out_buff[3] += in_val * kernel[ker_idx + 3 + mult_tile];
  87. }
  88. }
  89. #if defined(ARM_MATH_MVEI)
  90. (void)out_idx;
  91. int32x4_t res = vldrwq_s32(out_buff);
  92. res = arm_requantize_mve_32x4(res, vldrwq_s32(&output_mult[out_ch + mult_tile]), vldrwq_s32(&output_shift[out_ch + mult_tile]));
  93. res = vaddq_n_s32(res, output_offset);
  94. res = vmaxq_s32(res, vdupq_n_s32(output_activation_min));
  95. res = vminq_s32(res, vdupq_n_s32(output_activation_max));
  96. vstrbq_s32(output, res);
  97. output += 4;
  98. #else
  99. out_buff[0] = arm_nn_requantize(out_buff[0], output_mult[out_ch + 0 + mult_tile], output_shift[out_ch + 0 + mult_tile]);
  100. out_buff[1] = arm_nn_requantize(out_buff[1], output_mult[out_ch + 1 + mult_tile], output_shift[out_ch + 1 + mult_tile]);
  101. out_buff[2] = arm_nn_requantize(out_buff[2], output_mult[out_ch + 2 + mult_tile], output_shift[out_ch + 2 + mult_tile]);
  102. out_buff[3] = arm_nn_requantize(out_buff[3], output_mult[out_ch + 3 + mult_tile], output_shift[out_ch + 3 + mult_tile]);
  103. out_buff[0] += output_offset;
  104. out_buff[1] += output_offset;
  105. out_buff[2] += output_offset;
  106. out_buff[3] += output_offset;
  107. out_buff[0] = MIN(MAX(out_buff[0], output_activation_min), output_activation_max);
  108. out_buff[1] = MIN(MAX(out_buff[1], output_activation_min), output_activation_max);
  109. out_buff[2] = MIN(MAX(out_buff[2], output_activation_min), output_activation_max);
  110. out_buff[3] = MIN(MAX(out_buff[3], output_activation_min), output_activation_max);
  111. output[out_idx++] = (int8_t)out_buff[0];
  112. output[out_idx++] = (int8_t)out_buff[1];
  113. output[out_idx++] = (int8_t)out_buff[2];
  114. output[out_idx++] = (int8_t)out_buff[3];
  115. #endif
  116. }
  117. }
  118. }
  119. }
  120. }
  121. static void depthwise_conv_s8_generic(const q7_t *input,
  122. const uint16_t input_x,
  123. const uint16_t input_y,
  124. const uint16_t input_ch,
  125. const q7_t *kernel,
  126. const uint16_t output_ch,
  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 int32_t *bias,
  135. q7_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_offset,
  141. const int32_t input_offset,
  142. const int32_t output_activation_min,
  143. const int32_t output_activation_max)
  144. {
  145. (void)output_ch;
  146. int i_out = 0;
  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. int32_t acc_0;
  159. /* Condition for kernel start dimension: (base_idx_<x,y> + ker_<x,y>_start) >= 0 */
  160. const int ker_y_start = MAX(0, -base_idx_y);
  161. const int ker_x_start = MAX(0, -base_idx_x);
  162. /* Condition for kernel end dimension: (base_idx_<x,y> + ker_<x,y>_end) < input_<x,y> */
  163. const int ker_y_end = MIN(kernel_y, input_y - base_idx_y);
  164. const int ker_x_end = MIN(kernel_x, input_x - base_idx_x);
  165. acc_0 = bias[idx_out_ch];
  166. for (int i_ker_y = ker_y_start; i_ker_y < ker_y_end; i_ker_y++)
  167. {
  168. const int32_t idx_y = base_idx_y + i_ker_y;
  169. for (int i_ker_x = ker_x_start; i_ker_x < ker_x_end; i_ker_x++)
  170. {
  171. const int32_t idx_x = base_idx_x + i_ker_x;
  172. int32_t idx_0 = (idx_y * input_x + idx_x) * input_ch + i_input_ch;
  173. int32_t ker_idx_0 = (i_ker_y * kernel_x + i_ker_x) * (input_ch * ch_mult) + idx_out_ch;
  174. acc_0 += (input[idx_0] + input_offset) * kernel[ker_idx_0];
  175. }
  176. }
  177. /* Requantize and clamp output to provided range */
  178. acc_0 = arm_nn_requantize(acc_0, output_mult[idx_out_ch], output_shift[idx_out_ch]);
  179. acc_0 += output_offset;
  180. acc_0 = MAX(acc_0, output_activation_min);
  181. acc_0 = MIN(acc_0, output_activation_max);
  182. output[i_out++] = acc_0;
  183. }
  184. }
  185. }
  186. }
  187. }
  188. /*
  189. * Basic s8 depthwise convolution function.
  190. *
  191. * Refer header file for details.
  192. * Optimization using DSP extension is not available for the generic case where channel multiplier is > 1.
  193. *
  194. */
  195. arm_status arm_depthwise_conv_s8(const q7_t *input,
  196. const uint16_t input_x,
  197. const uint16_t input_y,
  198. const uint16_t input_ch,
  199. const q7_t *kernel,
  200. const uint16_t output_ch,
  201. const uint16_t ch_mult,
  202. const uint16_t kernel_x,
  203. const uint16_t kernel_y,
  204. const uint16_t pad_x,
  205. const uint16_t pad_y,
  206. const uint16_t stride_x,
  207. const uint16_t stride_y,
  208. const int32_t *bias,
  209. q7_t *output,
  210. const int32_t *output_shift,
  211. const int32_t *output_mult,
  212. const uint16_t output_x,
  213. const uint16_t output_y,
  214. const int32_t output_offset,
  215. const int32_t input_offset,
  216. const int32_t output_activation_min,
  217. const int32_t output_activation_max,
  218. const uint16_t dilation_x,
  219. const uint16_t dilation_y,
  220. q15_t *buffer_a)
  221. {
  222. (void)dilation_x;
  223. (void)dilation_y;
  224. (void)buffer_a;
  225. if(ch_mult % 4 == 0)
  226. {
  227. depthwise_conv_s8_mult_4(input, input_x, input_y, input_ch, kernel, output_ch, ch_mult, kernel_x, kernel_y, pad_x, pad_y, stride_x, stride_y, bias,
  228. output, output_shift, output_mult, output_x, output_y, output_offset, input_offset, output_activation_min, output_activation_max);
  229. }
  230. else
  231. {
  232. depthwise_conv_s8_generic(input, input_x, input_y, input_ch, kernel, output_ch, ch_mult, kernel_x, kernel_y, pad_x, pad_y, stride_x, stride_y, bias,
  233. output, output_shift, output_mult, output_x, output_y, output_offset, input_offset, output_activation_min, output_activation_max);
  234. }
  235. /* Return to application */
  236. return ARM_MATH_SUCCESS;
  237. }
  238. /**
  239. * @} end of NNConv group
  240. */