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:        11 April 2024
26  * $Revision:    V.3.2.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 the quantized Relu activation */
115 typedef struct
116 {
117     int32_t min; /**< Min value used to clamp the result */
118     int32_t max; /**< Max value used to clamp the result */
119 } cmsis_nn_activation;
120 
121 /** CMSIS-NN object for the convolution layer parameters */
122 typedef struct
123 {
124     int32_t input_offset;  /**< The negative of the zero value for the input tensor */
125     int32_t output_offset; /**< The negative of the zero value for the output tensor */
126     cmsis_nn_tile stride;
127     cmsis_nn_tile padding;
128     cmsis_nn_tile dilation;
129     cmsis_nn_activation activation;
130 } cmsis_nn_conv_params;
131 
132 /** CMSIS-NN object for the transpose 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 padding_offsets;
140     cmsis_nn_tile dilation;
141     cmsis_nn_activation activation;
142 } cmsis_nn_transpose_conv_params;
143 
144 /** CMSIS-NN object for the depthwise convolution layer parameters */
145 typedef struct
146 {
147     int32_t input_offset;  /**< The negative of the zero value for the input tensor */
148     int32_t output_offset; /**< The negative of the zero value for the output tensor */
149     int32_t ch_mult;       /**< Channel Multiplier. ch_mult * in_ch = out_ch */
150     cmsis_nn_tile stride;
151     cmsis_nn_tile padding;
152     cmsis_nn_tile dilation;
153     cmsis_nn_activation activation;
154 } cmsis_nn_dw_conv_params;
155 
156 /** CMSIS-NN object for pooling layer parameters */
157 typedef struct
158 {
159     cmsis_nn_tile stride;
160     cmsis_nn_tile padding;
161     cmsis_nn_activation activation;
162 } cmsis_nn_pool_params;
163 
164 /** CMSIS-NN object for Fully Connected layer parameters */
165 typedef struct
166 {
167     int32_t input_offset;  /**< The negative of the zero value for the input tensor */
168     int32_t filter_offset; /**< The negative of the zero value for the filter tensor. Not used */
169     int32_t output_offset; /**< The negative of the zero value for the output tensor */
170     cmsis_nn_activation activation;
171 } cmsis_nn_fc_params;
172 
173 /** CMSIS-NN object for SVDF layer parameters */
174 typedef struct
175 {
176     int32_t rank;
177     int32_t input_offset;  /**< The negative of the zero value for the input tensor */
178     int32_t output_offset; /**< The negative of the zero value for the output tensor */
179     cmsis_nn_activation input_activation;
180     cmsis_nn_activation output_activation;
181 } cmsis_nn_svdf_params;
182 
183 /** CMSIS-NN object for Softmax s16 layer parameters */
184 typedef struct
185 {
186     const int16_t *exp_lut;
187     const int16_t *one_by_one_lut;
188 } cmsis_nn_softmax_lut_s16;
189 
190 /** CMSIS-NN object for quantization parameters */
191 typedef struct
192 {
193     int32_t multiplier; /**< Multiplier value */
194     int32_t shift;      /**< Shift value */
195 } cmsis_nn_scaling;
196 
197 /** CMSIS-NN object for LSTM gate parameters*/
198 typedef struct
199 {
200     int32_t input_multiplier;
201     int32_t input_shift;
202     const void *input_weights;
203     const void *input_effective_bias; /**< Bias added with precomputed kernel_sum * lhs_offset*/
204 
205     int32_t hidden_multiplier;
206     int32_t hidden_shift;
207     const void *hidden_weights;
208     const void *hidden_effective_bias; /**< Precomputed kernel_sum * lhs_offset*/
209 
210     const void *bias;
211     arm_nn_activation_type activation_type;
212 } cmsis_nn_lstm_gate;
213 
214 /** CMSIS-NN object for LSTM parameters*/
215 typedef struct
216 {
217     int32_t time_major; /**< 0 if first dimension is batch, else first dimension is time */
218     int32_t batch_size;
219     int32_t time_steps;
220     int32_t input_size; /**< Size of new data input into the LSTM cell*/
221     int32_t
222         hidden_size; /**< Size of output from the LSTM cell, used as output and recursively into the next time step*/
223 
224     int32_t input_offset;
225 
226     int32_t forget_to_cell_multiplier;
227     int32_t forget_to_cell_shift;
228     int32_t input_to_cell_multiplier;
229     int32_t input_to_cell_shift;
230     int32_t cell_clip; /**< Min/max value of cell output*/
231     int32_t cell_scale_power;
232 
233     int32_t output_multiplier;
234     int32_t output_shift;
235     int32_t output_offset;
236 
237     cmsis_nn_lstm_gate forget_gate;
238     cmsis_nn_lstm_gate input_gate;
239     cmsis_nn_lstm_gate cell_gate;
240     cmsis_nn_lstm_gate output_gate;
241 } cmsis_nn_lstm_params;
242 
243 /** CMSIS-NN object for LSTM scratch buffers*/
244 typedef struct
245 {
246     void *temp1;
247     void *temp2;
248     void *cell_state;
249 } cmsis_nn_lstm_context;
250 
251 /**
252  * @} // end group genPubTypes
253  */
254 
255 #endif /* ARM_NN_TYPES_H */
256