esp_nn_esp32s3.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @file Header definitions to include for esp_nn optimized functions for
  16. * the ESP32-S3 platform
  17. */
  18. #pragma once
  19. #include "esp_nn_defs.h"
  20. #include "esp_nn_ansi_headers.h"
  21. /************************** Basic math functions *****************************/
  22. /**
  23. * @brief elementwise addition
  24. *
  25. * @note inputs type: int8_t, output: int8_t
  26. * input offsets: although int32_t, they are contained in 8 bits [-128, 127]
  27. *
  28. * shift values are expected to be <= 0
  29. */
  30. void esp_nn_add_elementwise_s8_esp32s3(const int8_t *input1_data,
  31. const int8_t *input2_data,
  32. const int32_t input1_offset,
  33. const int32_t input2_offset,
  34. const int32_t input1_mult,
  35. const int32_t input2_mult,
  36. const int32_t input1_shift,
  37. const int32_t input2_shift,
  38. const int32_t left_shift,
  39. int8_t *output,
  40. const int32_t out_offset,
  41. const int32_t out_mult,
  42. const int32_t out_shift,
  43. const int32_t activation_min,
  44. const int32_t activation_max,
  45. const int32_t size);
  46. /**
  47. * @brief elementwise multiplication
  48. *
  49. * @note inputs type: int8_t, output: int8_t
  50. * input offsets: although int32_t, they are contained in 8 bits [-128, 127]
  51. *
  52. * output shift is expected to be <= 0
  53. */
  54. void esp_nn_mul_elementwise_s8_esp32s3(const int8_t *input1_data,
  55. const int8_t *input2_data,
  56. const int32_t input1_offset,
  57. const int32_t input2_offset,
  58. int8_t *output,
  59. const int32_t out_offset,
  60. const int32_t out_mult,
  61. const int32_t out_shift,
  62. const int32_t activation_min,
  63. const int32_t activation_max,
  64. const int32_t size);
  65. /************************** Convolution functions *****************************/
  66. /**
  67. * @brief depthwise convolution per channel
  68. *
  69. * @note inputs type: int8_t, output: int8_t
  70. * Version used in tflite is per channel.
  71. * This version follows the same footsprints.
  72. * Meaning, it has per out_channel shift and multiplier for
  73. * requantization
  74. *
  75. * optimization notes: Though input_offset is int32 type,
  76. * offset values are contained in 8 bits [-128, 127]
  77. */
  78. void esp_nn_depthwise_conv_s8_esp32s3(const data_dims_t *input_dims,
  79. const int8_t *input_data,
  80. const data_dims_t *filter_dims,
  81. const int8_t *filter_data,
  82. const int32_t *bias,
  83. const data_dims_t *output_dims,
  84. int8_t *output_data,
  85. const dw_conv_params_t *conv_params,
  86. const quant_data_t *quant_data);
  87. /**
  88. * @brief 2d - convolution channelwise
  89. *
  90. * @note operation: result += (input + offset) * filter
  91. *
  92. * inputs type: int8_t, output: int8_t
  93. * input offsets: although int32_t, they are contained in 8 bits [-128, 127]
  94. */
  95. void esp_nn_conv_s8_esp32s3(const data_dims_t *input_dims,
  96. const int8_t *input_data,
  97. const data_dims_t *filter_dims,
  98. const int8_t *filter_data,
  99. const int32_t *bias,
  100. const data_dims_t *output_dims,
  101. int8_t *output_data,
  102. const conv_params_t *conv_params,
  103. const quant_data_t *quant_data);
  104. int esp_nn_get_conv_scratch_size_esp32s3(const data_dims_t *input_dims,
  105. const data_dims_t *filter_dims,
  106. const data_dims_t *output_dims,
  107. const conv_params_t *conv_params);
  108. void esp_nn_set_conv_scratch_buf_esp32s3(const void *buf);
  109. int esp_nn_get_depthwise_conv_scratch_size_esp32s3(const data_dims_t *input_dims,
  110. const data_dims_t *filter_dims,
  111. const data_dims_t *output_dims,
  112. const dw_conv_params_t *conv_params);
  113. void esp_nn_set_depthwise_conv_scratch_buf_esp32s3(const void *buf);
  114. /************************** Pooling functions *****************************/
  115. /**
  116. * @brief max_pool
  117. *
  118. * @note inputs type: int8_t, output: int8_t
  119. * input offsets: although int32_t, they are contained in 8 bits [-128, 127]
  120. */
  121. void esp_nn_max_pool_s8_esp32s3(const int8_t *input,
  122. const uint16_t input_wd,
  123. const uint16_t input_ht,
  124. int8_t *output,
  125. const uint16_t output_wd,
  126. const uint16_t output_ht,
  127. const uint16_t stride_wd,
  128. const uint16_t stride_ht,
  129. const uint16_t filter_wd,
  130. const uint16_t filter_ht,
  131. const uint16_t pad_wd,
  132. const uint16_t pad_ht,
  133. const int32_t activation_min,
  134. const int32_t activation_max,
  135. const uint16_t channels);
  136. /**
  137. * @brief avg_pool
  138. *
  139. * @note inputs type: int8_t, output: int8_t
  140. * input offsets: although int32_t, they are contained in 8 bits [-128, 127]
  141. */
  142. void esp_nn_avg_pool_s8_esp32s3(const int8_t *input,
  143. const uint16_t input_wd,
  144. const uint16_t input_ht,
  145. int8_t *output,
  146. const uint16_t output_wd,
  147. const uint16_t output_ht,
  148. const uint16_t stride_wd,
  149. const uint16_t stride_ht,
  150. const uint16_t filter_wd,
  151. const uint16_t filter_ht,
  152. const uint16_t pad_wd,
  153. const uint16_t pad_ht,
  154. const int32_t activation_min,
  155. const int32_t activation_max,
  156. const uint16_t channels);
  157. /************************** Fully connected functions *****************************/
  158. /**
  159. * @brief fully connected
  160. *
  161. * @note inputs type: int8_t, output: int8_t
  162. * input offsets: although int32_t, they are contained in 8 bits [-128, 127]
  163. *
  164. * Current version works only on aligned input.
  165. * row_len and channels should both be multiple of 8.
  166. */
  167. void esp_nn_fully_connected_s8_esp32s3(const int8_t *input_data,
  168. const int32_t input_offset,
  169. const uint16_t row_len,
  170. const int8_t *filter_data,
  171. const int32_t filter_offset,
  172. const int32_t *bias,
  173. int8_t *out_data,
  174. const uint16_t out_channels,
  175. const int32_t out_offset,
  176. const int32_t out_shift,
  177. const int32_t out_mult,
  178. const int32_t activation_min,
  179. const int32_t activation_max);
  180. /**
  181. * @brief relu6
  182. *
  183. * @note inout: int8_t
  184. */
  185. void esp_nn_relu6_s8_esp32s3(int8_t *data, uint16_t size);
  186. /********************** function defines ***************************/
  187. #define esp_nn_add_elementwise_s8 esp_nn_add_elementwise_s8_esp32s3
  188. #define esp_nn_mul_elementwise_s8 esp_nn_mul_elementwise_s8_esp32s3
  189. #define esp_nn_depthwise_conv_s8 esp_nn_depthwise_conv_s8_esp32s3
  190. #define esp_nn_get_conv_scratch_size esp_nn_get_conv_scratch_size_esp32s3
  191. #define esp_nn_set_conv_scratch_buf esp_nn_set_conv_scratch_buf_esp32s3
  192. #define esp_nn_get_depthwise_conv_scratch_size esp_nn_get_depthwise_conv_scratch_size_esp32s3
  193. #define esp_nn_set_depthwise_conv_scratch_buf esp_nn_set_depthwise_conv_scratch_buf_esp32s3
  194. #define esp_nn_conv_s8 esp_nn_conv_s8_esp32s3
  195. #define esp_nn_relu6_s8 esp_nn_relu6_s8_esp32s3
  196. #define esp_nn_avg_pool_s8 esp_nn_avg_pool_s8_esp32s3
  197. #define esp_nn_max_pool_s8 esp_nn_max_pool_s8_esp32s3
  198. #define esp_nn_fully_connected_s8 esp_nn_fully_connected_s8_esp32s3
  199. #define esp_nn_get_softmax_scratch_size esp_nn_get_softmax_scratch_size_opt
  200. #define esp_nn_set_softmax_scratch_buf esp_nn_set_softmax_scratch_buf_opt
  201. #define esp_nn_softmax_s8 esp_nn_softmax_s8_opt