arm_nn_types.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2020-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
  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_nn_types.h
  21. * Description: Public header file to contain the CMSIS-NN structs for the
  22. * TensorFlowLite micro compliant functions
  23. *
  24. * $Date: 19 January 2024
  25. * $Revision: V.3.0.0
  26. *
  27. * Target : Arm(R) M-Profile Architecture
  28. * -------------------------------------------------------------------- */
  29. #ifndef ARM_NN_TYPES_H
  30. #define ARM_NN_TYPES_H
  31. #include <stdint.h>
  32. /**
  33. * @defgroup genPubTypes Structure Types
  34. * @ingroup Public
  35. * @brief Enums and Data Structures used in public API
  36. * @{
  37. */
  38. /** Enum for specifying activation function types */
  39. typedef enum
  40. {
  41. ARM_SIGMOID = 0, /**< Sigmoid activation function */
  42. ARM_TANH = 1, /**< Tanh activation function */
  43. } arm_nn_activation_type;
  44. /** Function return codes */
  45. typedef enum
  46. {
  47. ARM_CMSIS_NN_SUCCESS = 0, /**< No error */
  48. ARM_CMSIS_NN_ARG_ERROR = -1, /**< One or more arguments are incorrect */
  49. ARM_CMSIS_NN_NO_IMPL_ERROR = -2, /**< No implementation available */
  50. ARM_CMSIS_NN_FAILURE = -3, /**< Logical error */
  51. } arm_cmsis_nn_status;
  52. /** CMSIS-NN object to contain the width and height of a tile */
  53. typedef struct
  54. {
  55. int32_t w; /**< Width */
  56. int32_t h; /**< Height */
  57. } cmsis_nn_tile;
  58. /** CMSIS-NN object used for the function context. */
  59. typedef struct
  60. {
  61. void *buf; /**< Pointer to a buffer needed for the optimization */
  62. int32_t size; /**< Buffer size */
  63. } cmsis_nn_context;
  64. /** CMSIS-NN object to contain the dimensions of the tensors */
  65. typedef struct
  66. {
  67. int32_t n; /**< Generic dimension to contain either the batch size or output channels.
  68. Please refer to the function documentation for more information */
  69. int32_t h; /**< Height */
  70. int32_t w; /**< Width */
  71. int32_t c; /**< Input channels */
  72. } cmsis_nn_dims;
  73. /** CMSIS-NN object to contain LSTM specific input parameters related to dimensions */
  74. typedef struct
  75. {
  76. int32_t max_time;
  77. int32_t num_inputs;
  78. int32_t num_batches;
  79. int32_t num_outputs;
  80. } cmsis_nn_lstm_dims;
  81. /** CMSIS-NN object for the per-channel quantization parameters */
  82. typedef struct
  83. {
  84. int32_t *multiplier; /**< Multiplier values */
  85. int32_t *shift; /**< Shift values */
  86. } cmsis_nn_per_channel_quant_params;
  87. /** CMSIS-NN object for the per-tensor quantization parameters */
  88. typedef struct
  89. {
  90. int32_t multiplier; /**< Multiplier value */
  91. int32_t shift; /**< Shift value */
  92. } cmsis_nn_per_tensor_quant_params;
  93. /** CMSIS-NN object for the quantized Relu activation */
  94. typedef struct
  95. {
  96. int32_t min; /**< Min value used to clamp the result */
  97. int32_t max; /**< Max value used to clamp the result */
  98. } cmsis_nn_activation;
  99. /** CMSIS-NN object for the convolution layer parameters */
  100. typedef struct
  101. {
  102. int32_t input_offset; /**< The negative of the zero value for the input tensor */
  103. int32_t output_offset; /**< The negative of the zero value for the output tensor */
  104. cmsis_nn_tile stride;
  105. cmsis_nn_tile padding;
  106. cmsis_nn_tile dilation;
  107. cmsis_nn_activation activation;
  108. } cmsis_nn_conv_params;
  109. /** CMSIS-NN object for the transpose convolution layer parameters */
  110. typedef struct
  111. {
  112. int32_t input_offset; /**< The negative of the zero value for the input tensor */
  113. int32_t output_offset; /**< The negative of the zero value for the output tensor */
  114. cmsis_nn_tile stride;
  115. cmsis_nn_tile padding;
  116. cmsis_nn_tile padding_offsets;
  117. cmsis_nn_tile dilation;
  118. cmsis_nn_activation activation;
  119. } cmsis_nn_transpose_conv_params;
  120. /** CMSIS-NN object for the depthwise convolution layer parameters */
  121. typedef struct
  122. {
  123. int32_t input_offset; /**< The negative of the zero value for the input tensor */
  124. int32_t output_offset; /**< The negative of the zero value for the output tensor */
  125. int32_t ch_mult; /**< Channel Multiplier. ch_mult * in_ch = out_ch */
  126. cmsis_nn_tile stride;
  127. cmsis_nn_tile padding;
  128. cmsis_nn_tile dilation;
  129. cmsis_nn_activation activation;
  130. } cmsis_nn_dw_conv_params;
  131. /** CMSIS-NN object for pooling layer parameters */
  132. typedef struct
  133. {
  134. cmsis_nn_tile stride;
  135. cmsis_nn_tile padding;
  136. cmsis_nn_activation activation;
  137. } cmsis_nn_pool_params;
  138. /** CMSIS-NN object for Fully Connected layer parameters */
  139. typedef struct
  140. {
  141. int32_t input_offset; /**< The negative of the zero value for the input tensor */
  142. int32_t filter_offset; /**< The negative of the zero value for the filter tensor. Not used */
  143. int32_t output_offset; /**< The negative of the zero value for the output tensor */
  144. cmsis_nn_activation activation;
  145. } cmsis_nn_fc_params;
  146. /** CMSIS-NN object for SVDF layer parameters */
  147. typedef struct
  148. {
  149. int32_t rank;
  150. int32_t input_offset; /**< The negative of the zero value for the input tensor */
  151. int32_t output_offset; /**< The negative of the zero value for the output tensor */
  152. cmsis_nn_activation input_activation;
  153. cmsis_nn_activation output_activation;
  154. } cmsis_nn_svdf_params;
  155. /** CMSIS-NN object for Softmax s16 layer parameters */
  156. typedef struct
  157. {
  158. const int16_t *exp_lut;
  159. const int16_t *one_by_one_lut;
  160. } cmsis_nn_softmax_lut_s16;
  161. /** CMSIS-NN object for quantization parameters */
  162. typedef struct
  163. {
  164. int32_t multiplier; /**< Multiplier value */
  165. int32_t shift; /**< Shift value */
  166. } cmsis_nn_scaling;
  167. /** CMSIS-NN object for LSTM gate parameters*/
  168. typedef struct
  169. {
  170. int32_t input_multiplier;
  171. int32_t input_shift;
  172. const int8_t *input_weights;
  173. const int32_t *input_effective_bias; /**< Bias added with precomputed kernel_sum * lhs_offset*/
  174. int32_t hidden_multiplier;
  175. int32_t hidden_shift;
  176. const int8_t *hidden_weights;
  177. const int32_t *hidden_effective_bias; /**< Precomputed kernel_sum * lhs_offset*/
  178. const int32_t *bias;
  179. arm_nn_activation_type activation_type;
  180. } cmsis_nn_lstm_gate;
  181. /** CMSIS-NN object for LSTM parameters*/
  182. typedef struct
  183. {
  184. int32_t time_major; /**< 0 if first dimension is batch, else first dimension is time */
  185. int32_t batch_size;
  186. int32_t time_steps;
  187. int32_t input_size; /**< Size of new data input into the LSTM cell*/
  188. int32_t
  189. hidden_size; /**< Size of output from the LSTM cell, used as output and recursively into the next time step*/
  190. int32_t input_offset;
  191. int32_t forget_to_cell_multiplier;
  192. int32_t forget_to_cell_shift;
  193. int32_t input_to_cell_multiplier;
  194. int32_t input_to_cell_shift;
  195. int32_t cell_clip; /**< Min/max value of cell output*/
  196. int32_t cell_scale_power;
  197. int32_t output_multiplier;
  198. int32_t output_shift;
  199. int32_t output_offset;
  200. cmsis_nn_lstm_gate forget_gate;
  201. cmsis_nn_lstm_gate input_gate;
  202. cmsis_nn_lstm_gate cell_gate;
  203. cmsis_nn_lstm_gate output_gate;
  204. } cmsis_nn_lstm_params;
  205. /** CMSIS-NN object for LSTM scratch buffers*/
  206. typedef struct
  207. {
  208. void *temp1;
  209. void *temp2;
  210. void *cell_state;
  211. } cmsis_nn_lstm_context;
  212. /**
  213. * @} // end group genPubTypes
  214. */
  215. #endif /* ARM_NN_TYPES_H */