esp_nn_ansi_headers.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #pragma once
  15. /**
  16. * @file Header definitions to include for esp_nn reference functions
  17. */
  18. #include <stdint.h>
  19. /************************** Basic math functions ****************************/
  20. /**
  21. * @brief elementwise addition
  22. *
  23. * @note inputs type: int8_t, output: int8_t
  24. * input offsets: although int32_t, they are contained in 8 bits [-128, 127]
  25. *
  26. * shift values are expected to be <= 0
  27. */
  28. void esp_nn_add_elementwise_s8_ansi(const int8_t *input1_data,
  29. const int8_t *input2_data,
  30. const int32_t input1_offset,
  31. const int32_t input2_offset,
  32. const int32_t input1_mult,
  33. const int32_t input2_mult,
  34. const int32_t input1_shift,
  35. const int32_t input2_shift,
  36. const int32_t left_shift,
  37. int8_t *output,
  38. const int32_t out_offset,
  39. const int32_t out_mult,
  40. const int32_t out_shift,
  41. const int32_t activation_min,
  42. const int32_t activation_max,
  43. const int32_t size);
  44. /**
  45. * @brief elementwise multiplication
  46. *
  47. * @note inputs type: int8_t, output: int8_t
  48. * input offsets: although int32_t, they are contained in 8 bits [-128, 127]
  49. *
  50. * output shift is expected to be <= 0
  51. */
  52. void esp_nn_mul_elementwise_s8_ansi(const int8_t *input1_data,
  53. const int8_t *input2_data,
  54. const int32_t input1_offset,
  55. const int32_t input2_offset,
  56. int8_t *output,
  57. const int32_t out_offset,
  58. const int32_t out_mult,
  59. const int32_t out_shift,
  60. const int32_t activation_min,
  61. const int32_t activation_max,
  62. const int32_t size);
  63. /************************** Convolution functions *****************************/
  64. /**
  65. * @brief depthwise convolution per channel
  66. *
  67. * @note inputs type: int8_t, output: int8_t
  68. * Version used in tflite is per channel.
  69. * This version follows the same footsprints.
  70. * Meaning, it has per out_channel shift and multiplier for
  71. * requantization
  72. *
  73. * optimization notes: Though input_offset is int32 type,
  74. * offset values are contained in 8 bits [-128, 127]
  75. */
  76. void esp_nn_depthwise_conv_s8_ansi(const int8_t *input_data,
  77. const uint16_t input_wd,
  78. const uint16_t input_ht,
  79. const uint16_t channels,
  80. const int32_t input_offset,
  81. const uint16_t pad_wd,
  82. const uint16_t pad_ht,
  83. const uint16_t stride_wd,
  84. const uint16_t stride_ht,
  85. const uint16_t ch_mult,
  86. const int8_t *filter_data,
  87. const uint16_t filter_wd,
  88. const uint16_t filter_ht,
  89. const int32_t *bias,
  90. int8_t *out_data,
  91. const uint16_t out_wd,
  92. const uint16_t out_ht,
  93. const int32_t out_offset,
  94. const int32_t *out_shift,
  95. const int32_t *out_mult,
  96. const int32_t activation_min,
  97. const int32_t activation_max);
  98. /**
  99. * @brief 2d-convolution channelwise
  100. *
  101. * @note operation: result += (input + offset) * filter
  102. *
  103. * inputs type: int8_t, output: int8_t
  104. * input offsets: although int32_t, they are contained in 8 bits [-128, 127]
  105. */
  106. void esp_nn_conv_s8_ansi(const int8_t *input_data,
  107. const uint16_t input_wd,
  108. const uint16_t input_ht,
  109. const uint16_t in_channels,
  110. const int32_t input_offset,
  111. const uint16_t pad_wd,
  112. const uint16_t pad_ht,
  113. const uint16_t stride_wd,
  114. const uint16_t stride_ht,
  115. const int8_t *filter_data,
  116. const uint16_t filter_wd,
  117. const uint16_t filter_ht,
  118. const int32_t *bias,
  119. int8_t *out_data,
  120. const uint16_t out_wd,
  121. const uint16_t out_ht,
  122. const uint16_t out_channels,
  123. const int32_t out_offset,
  124. const int32_t *out_shift,
  125. const int32_t *out_mult,
  126. const int32_t activation_min,
  127. const int32_t activation_max);
  128. int esp_nn_get_conv_scratch_size_ansi(const uint16_t input_wd,
  129. const uint16_t input_ht,
  130. const uint16_t in_ch,
  131. const uint16_t out_ch,
  132. const uint16_t filter_wd,
  133. const uint16_t filter_ht);
  134. void esp_nn_set_conv_scratch_buf_ansi(const void *buf);
  135. int esp_nn_get_depthwise_conv_scratch_size_ansi(const uint16_t input_wd,
  136. const uint16_t input_ht,
  137. const uint16_t channels,
  138. const uint16_t ch_mult,
  139. const uint16_t filter_wd,
  140. const uint16_t filter_ht);
  141. void esp_nn_set_depthwise_conv_scratch_buf_ansi(const void *buf);
  142. /************************** Activation functions *****************************/
  143. /**
  144. * @brief relu6
  145. *
  146. * @note inout: int8_t
  147. */
  148. void esp_nn_relu6_s8_ansi(int8_t *data, uint16_t size);
  149. /************************** Pooling functions *****************************/
  150. /**
  151. * @brief max_pool
  152. *
  153. * @note inputs type: int8_t, output: int8_t
  154. * input offsets: although int32_t, they are contained in 8 bits [-128, 127]
  155. */
  156. void esp_nn_max_pool_s8_ansi(const int8_t *input,
  157. const uint16_t input_wd,
  158. const uint16_t input_ht,
  159. int8_t *output,
  160. const uint16_t output_wd,
  161. const uint16_t output_ht,
  162. const uint16_t stride_wd,
  163. const uint16_t stride_ht,
  164. const uint16_t filter_wd,
  165. const uint16_t filter_ht,
  166. const uint16_t pad_wd,
  167. const uint16_t pad_ht,
  168. const int32_t activation_min,
  169. const int32_t activation_max,
  170. const uint16_t channels);
  171. /**
  172. * @brief avg_pool
  173. *
  174. * @note inputs type: int8_t, output: int8_t
  175. * input offsets: although int32_t, they are contained in 8 bits [-128, 127]
  176. */
  177. void esp_nn_avg_pool_s8_ansi(const int8_t *input,
  178. const uint16_t input_wd,
  179. const uint16_t input_ht,
  180. int8_t *output,
  181. const uint16_t output_wd,
  182. const uint16_t output_ht,
  183. const uint16_t stride_wd,
  184. const uint16_t stride_ht,
  185. const uint16_t filter_wd,
  186. const uint16_t filter_ht,
  187. const uint16_t pad_wd,
  188. const uint16_t pad_ht,
  189. const int32_t activation_min,
  190. const int32_t activation_max,
  191. const uint16_t channels);
  192. /************************** Fully connected functions ***********************/
  193. /**
  194. * @brief fully connected
  195. *
  196. * @note inputs type: int8_t, output: int8_t
  197. * input offsets: although int32_t, they are contained in 8 bits [-128, 127]
  198. */
  199. void esp_nn_fully_connected_s8_ansi(const int8_t *input_data,
  200. const int32_t input_offset,
  201. const uint16_t row_len,
  202. const int8_t *filter_data,
  203. const int32_t filter_offset,
  204. const int32_t *bias,
  205. int8_t *out_data,
  206. const uint16_t out_channels,
  207. const int32_t out_offset,
  208. const int32_t out_shift,
  209. const int32_t out_mult,
  210. const int32_t activation_min,
  211. const int32_t activation_max);
  212. /**
  213. * @brief Get scratch buffer size needed by softmax function
  214. *
  215. * @param width
  216. * @param height
  217. * @return size in bytes
  218. *
  219. * @note buffer must be 4 byte aligned
  220. */
  221. int32_t esp_nn_get_softmax_scratch_size_ansi(const int32_t width, const int32_t height);
  222. /* ANSI C function to be hooked up when optimised version needed */
  223. int32_t esp_nn_get_softmax_scratch_size_opt(const int32_t width, const int32_t height);
  224. /**
  225. * @brief Set scratch buffer to be used by softmax function
  226. *
  227. * @param buffer this can be NULL if one needs to unset it
  228. * must be aligned to 4 bytes
  229. */
  230. void esp_nn_set_softmax_scratch_buf_ansi(void *buffer);
  231. /* ANSI C function to be hooked up when optimised version needed */
  232. void esp_nn_set_softmax_scratch_buf_opt(void *buffer);
  233. /**
  234. * @brief reference softmax function
  235. *
  236. * @note inputs type: int8_t, output: int8_t
  237. */
  238. void esp_nn_softmax_s8_ansi(const int8_t *input_data,
  239. const int32_t height,
  240. const int32_t width,
  241. const int32_t mult,
  242. const int32_t shift,
  243. const int32_t diff_min,
  244. int8_t *output_data);
  245. /**
  246. * @brief optimised version of softmax function
  247. *
  248. * @note the function uses extra buffer (4 * width bytes)
  249. * hence, scratch buffers must be set before calling this.
  250. */
  251. void esp_nn_softmax_s8_opt(const int8_t *input_data,
  252. const int32_t height,
  253. const int32_t width,
  254. const int32_t mult,
  255. const int32_t shift,
  256. const int32_t diff_min,
  257. int8_t *output_data);