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 /* ----------------------------------------------------------------------
20  * Project:      CMSIS NN Library
21  * Title:        arm_nn_types.h
22  * Description:  Public header file to contain the CMSIS-NN structs for the
23  *               TensorFlowLite micro compliant functions
24  *
25  * $Date:        21 Oct 2024
26  * $Revision:    V.3.5.0
27  *
28  * Target :  Arm(R) M-Profile Architecture
29  * -------------------------------------------------------------------- */
30 
31 #ifndef ARM_NN_TYPES_H
32 #define ARM_NN_TYPES_H
33 
34 #include <stdbool.h>
35 #include <stdint.h>
36 
37 /**
38  * @defgroup genPubTypes Structure Types
39  * @ingroup Public
40  * @brief Enums and Data Structures used in public API
41  * @{
42  */
43 
44 /** Enum for specifying activation function types */
45 typedef enum
46 {
47     ARM_SIGMOID = 0, /**< Sigmoid activation function */
48     ARM_TANH = 1,    /**< Tanh activation function */
49 } arm_nn_activation_type;
50 
51 /** Function return codes */
52 typedef enum
53 {
54     ARM_CMSIS_NN_SUCCESS = 0,        /**< No error */
55     ARM_CMSIS_NN_ARG_ERROR = -1,     /**< One or more arguments are incorrect */
56     ARM_CMSIS_NN_NO_IMPL_ERROR = -2, /**<  No implementation available */
57     ARM_CMSIS_NN_FAILURE = -3,       /**<  Logical error */
58 } arm_cmsis_nn_status;
59 
60 /** CMSIS-NN object to contain the width and height of a tile */
61 typedef struct
62 {
63     int32_t w; /**< Width */
64     int32_t h; /**< Height */
65 } cmsis_nn_tile;
66 
67 /** CMSIS-NN object used for the function context. */
68 typedef struct
69 {
70     void *buf;    /**< Pointer to a buffer needed for the optimization */
71     int32_t size; /**< Buffer size */
72 } cmsis_nn_context;
73 
74 /** CMSIS-NN object used to hold bias data for int16 variants. */
75 typedef struct
76 {
77     const void *data;         /**< Pointer to bias data */
78     const bool is_int32_bias; /**< Indicate type of bias data. True means int32 else int64 */
79 } cmsis_nn_bias_data;
80 
81 /** CMSIS-NN object to contain the dimensions of the tensors */
82 typedef struct
83 {
84     int32_t n; /**< Generic dimension to contain either the batch size or output channels.
85                      Please refer to the function documentation for more information */
86     int32_t h; /**< Height */
87     int32_t w; /**< Width */
88     int32_t c; /**< Input channels */
89 } cmsis_nn_dims;
90 
91 /** CMSIS-NN object to contain LSTM specific input parameters related to dimensions */
92 typedef struct
93 {
94     int32_t max_time;
95     int32_t num_inputs;
96     int32_t num_batches;
97     int32_t num_outputs;
98 } cmsis_nn_lstm_dims;
99 
100 /** CMSIS-NN object for the per-channel quantization parameters */
101 typedef struct
102 {
103     int32_t *multiplier; /**< Multiplier values */
104     int32_t *shift;      /**< Shift values */
105 } cmsis_nn_per_channel_quant_params;
106 
107 /** CMSIS-NN object for the per-tensor quantization parameters */
108 typedef struct
109 {
110     int32_t multiplier; /**< Multiplier value */
111     int32_t shift;      /**< Shift value */
112 } cmsis_nn_per_tensor_quant_params;
113 
114 /** CMSIS-NN object for quantization parameters.
115  *  This struct supports both per-tensor and per-channels requantization
116  *  and is recommended for new operators.
117  */
118 typedef struct
119 {
120     int32_t *multiplier;    /**< Multiplier values */
121     int32_t *shift;         /**< Shift values */
122     int32_t is_per_channel; /** Indicating if per channel or per tensor quantization */
123 } cmsis_nn_quant_params;
124 
125 /** CMSIS-NN object for the quantized Relu activation */
126 typedef struct
127 {
128     int32_t min; /**< Min value used to clamp the result */
129     int32_t max; /**< Max value used to clamp the result */
130 } cmsis_nn_activation;
131 
132 /** CMSIS-NN object for the convolution layer parameters */
133 typedef struct
134 {
135     int32_t input_offset;  /**< The negative of the zero value for the input tensor */
136     int32_t output_offset; /**< The negative of the zero value for the output tensor */
137     cmsis_nn_tile stride;
138     cmsis_nn_tile padding;
139     cmsis_nn_tile dilation;
140     cmsis_nn_activation activation;
141 } cmsis_nn_conv_params;
142 
143 /** CMSIS-NN object for the transpose convolution layer parameters */
144 typedef struct
145 {
146     int32_t input_offset;  /**< The negative of the zero value for the input tensor */
147     int32_t output_offset; /**< The negative of the zero value for the output tensor */
148     cmsis_nn_tile stride;
149     cmsis_nn_tile padding;
150     cmsis_nn_tile padding_offsets;
151     cmsis_nn_tile dilation;
152     cmsis_nn_activation activation;
153 } cmsis_nn_transpose_conv_params;
154 
155 /** CMSIS-NN object for the depthwise convolution layer parameters */
156 typedef struct
157 {
158     int32_t input_offset;  /**< The negative of the zero value for the input tensor */
159     int32_t output_offset; /**< The negative of the zero value for the output tensor */
160     int32_t ch_mult;       /**< Channel Multiplier. ch_mult * in_ch = out_ch */
161     cmsis_nn_tile stride;
162     cmsis_nn_tile padding;
163     cmsis_nn_tile dilation;
164     cmsis_nn_activation activation;
165 } cmsis_nn_dw_conv_params;
166 
167 /** CMSIS-NN object for pooling layer parameters */
168 typedef struct
169 {
170     cmsis_nn_tile stride;
171     cmsis_nn_tile padding;
172     cmsis_nn_activation activation;
173 } cmsis_nn_pool_params;
174 
175 /** CMSIS-NN object for Fully Connected layer parameters */
176 typedef struct
177 {
178     int32_t input_offset;  /**< The negative of the zero value for the input tensor */
179     int32_t filter_offset; /**< The negative of the zero value for the filter tensor */
180     int32_t output_offset; /**< The negative of the zero value for the output tensor */
181     cmsis_nn_activation activation;
182 } cmsis_nn_fc_params;
183 
184 /** CMSIS-NN object for Batch Matmul layer parameters */
185 typedef struct
186 {
187     const bool adj_x;
188     const bool adj_y;
189     cmsis_nn_fc_params fc_params;
190 } cmsis_nn_bmm_params;
191 
192 /** CMSIS-NN object for Transpose layer parameters */
193 typedef struct
194 {
195     const int32_t num_dims;
196     const uint32_t *permutations; /**< The dimensions applied to the input dimensions */
197 } cmsis_nn_transpose_params;
198 
199 /** CMSIS-NN object for SVDF layer parameters */
200 typedef struct
201 {
202     int32_t rank;
203     int32_t input_offset;  /**< The negative of the zero value for the input tensor */
204     int32_t output_offset; /**< The negative of the zero value for the output tensor */
205     cmsis_nn_activation input_activation;
206     cmsis_nn_activation output_activation;
207 } cmsis_nn_svdf_params;
208 
209 /** CMSIS-NN object for Softmax s16 layer parameters */
210 typedef struct
211 {
212     const int16_t *exp_lut;
213     const int16_t *one_by_one_lut;
214 } cmsis_nn_softmax_lut_s16;
215 
216 /** CMSIS-NN object for quantization parameters */
217 typedef struct
218 {
219     int32_t multiplier; /**< Multiplier value */
220     int32_t shift;      /**< Shift value */
221 } cmsis_nn_scaling;
222 
223 /** CMSIS-NN object for LSTM gate parameters*/
224 typedef struct
225 {
226     int32_t input_multiplier;
227     int32_t input_shift;
228     const void *input_weights;
229     const void *input_effective_bias; /**< Bias added with precomputed kernel_sum * lhs_offset*/
230 
231     int32_t hidden_multiplier;
232     int32_t hidden_shift;
233     const void *hidden_weights;
234     const void *hidden_effective_bias; /**< Precomputed kernel_sum * lhs_offset*/
235 
236     const void *bias;
237     arm_nn_activation_type activation_type;
238 } cmsis_nn_lstm_gate;
239 
240 /** CMSIS-NN object for LSTM parameters*/
241 typedef struct
242 {
243     int32_t time_major; /**< 0 if first dimension is batch, else first dimension is time */
244     int32_t batch_size;
245     int32_t time_steps;
246     int32_t input_size; /**< Size of new data input into the LSTM cell*/
247     int32_t
248         hidden_size; /**< Size of output from the LSTM cell, used as output and recursively into the next time step*/
249 
250     int32_t input_offset;
251 
252     int32_t forget_to_cell_multiplier;
253     int32_t forget_to_cell_shift;
254     int32_t input_to_cell_multiplier;
255     int32_t input_to_cell_shift;
256     int32_t cell_clip; /**< Min/max value of cell output*/
257     int32_t cell_scale_power;
258 
259     int32_t output_multiplier;
260     int32_t output_shift;
261     int32_t output_offset;
262 
263     cmsis_nn_lstm_gate forget_gate;
264     cmsis_nn_lstm_gate input_gate;
265     cmsis_nn_lstm_gate cell_gate;
266     cmsis_nn_lstm_gate output_gate;
267 } cmsis_nn_lstm_params;
268 
269 /** CMSIS-NN object for LSTM scratch buffers*/
270 typedef struct
271 {
272     void *temp1;
273     void *temp2;
274     void *cell_state;
275 } cmsis_nn_lstm_context;
276 
277 /**
278  * @} // end group genPubTypes
279  */
280 
281 #endif /* ARM_NN_TYPES_H */
282