ML Middleware
ModusToolbox Machine Learning Middleware Library
All Data Structures Functions Variables Modules
mtb_ml_model.h
1 /***************************************************************************/
39 #if !defined(__MTB_ML_MODEL_H__)
40 #define __MTB_ML_MODEL_H__
41 
42 #include "mtb_ml_common.h"
43 #include "mtb_ml_model_defs.h"
44 
45 #if defined(__cplusplus)
46 extern "C" {
47 #endif
48 
49 /******************************************************************************
50  * Macros
51  *****************************************************************************/
52 #define MEM_FLAG_SHIFT_PERSISTENT (0)
53 #define MEM_FLAG_SHIFT_SCRATCH (1)
54 
55 #define MTB_ML_MEM_DYNAMIC_PERSISTENT (1 << MEM_FLAG_SHIFT_PERSISTENT)
56 #define MTB_ML_MEM_DYNAMIC_SCRATCH (1 << MEM_FLAG_SHIFT_SCRATCH)
57 
58 #define MTB_ML_MODEL_NAME_LEN 64
59 /******************************************************************************
60  * Typedefs
61  *****************************************************************************/
62 
63 /******************************************************************************
64 * Public definitions
65 ******************************************************************************/
66 
67 /******************************************************************************
68 * Structures
69 ******************************************************************************/
73 typedef struct
74 {
78 
80  uint8_t* tensor_arena;
84 
88 typedef struct
89 {
94  char name[MTB_ML_MODEL_NAME_LEN];
96 #if defined(COMPONENT_ML_TFLM)
97 
100  const uint8_t * model_bin;
102  const unsigned int model_size;
103  const int arena_size;
104 #endif
106 #if defined(COMPONENT_ML_TFLM_LESS)
107 
110  tflm_rmf_apis_t rmf_bin;
112 #endif
115 
119 typedef struct
120 {
125  char name[MTB_ML_MODEL_NAME_LEN];
130  int lib_error;
131  MTB_ML_DATA_T *output;
132  MTB_ML_DATA_T *input;
133  void *tflm_obj;
134  int model_time_steps; /*< number of model time steps */
137  float input_scale;
139  float output_scale;
140  mtb_ml_profile_config_t profiling;
141  uint64_t m_cpu_cycles;
142  uint32_t m_sum_frames;
143  uint64_t m_cpu_sum_cycles;
144  uint32_t m_cpu_peak_frame;
145  uint64_t m_cpu_peak_cycles;
148 #if defined(COMPONENT_U55) || \
149  defined(COMPONENT_NNLITE2)
150 
154  uint64_t m_npu_cycles;
155  uint64_t m_npu_sum_cycles;
156  uint32_t m_npu_peak_frame;
157  uint64_t m_npu_peak_cycles;
159 #endif
160 #if defined(COMPONENT_ML_TFLM)
161 
165  uint8_t *arena_buffer;
166 #endif
167 #if defined(COMPONENT_ML_TFLM_LESS)
168 
172  tflm_rmf_apis_t rmf_apis;
174 #endif
176 
177 /******************************************************************************
178 * Function prototype
179 ******************************************************************************/
199 cy_rslt_t mtb_ml_model_init(const mtb_ml_model_bin_t *bin, const mtb_ml_model_buffer_t *buffer, mtb_ml_model_t **object);
200 
209 cy_rslt_t mtb_ml_model_deinit(mtb_ml_model_t *object);
210 
221 cy_rslt_t mtb_ml_model_run(mtb_ml_model_t *object, MTB_ML_DATA_T *input);
222 
232 
243 cy_rslt_t mtb_ml_model_get_output(const mtb_ml_model_t *object, MTB_ML_DATA_T **out_pptr, int* size_ptr);
244 
254 
264 
275 int mtb_ml_model_profile_get_tsc(uint64_t *val);
276 
286 cy_rslt_t mtb_ml_model_profile_config(mtb_ml_model_t *object, mtb_ml_profile_config_t config);
287 
296 cy_rslt_t mtb_ml_model_profile_log(mtb_ml_model_t *object);
297 
302 #if defined(__cplusplus)
303 }
304 #endif
305 
306 #endif /* __MTB_ML_MODEL_H__ */
int output_size
Definition: mtb_ml_model.h:129
int recurrent_ts_size
Definition: mtb_ml_model.h:135
uint64_t m_cpu_cycles
Definition: mtb_ml_model.h:141
bool is_rnn_streaming
Definition: mtb_ml_model.h:146
uint64_t m_cpu_peak_cycles
Definition: mtb_ml_model.h:145
Definition: mtb_ml_model.h:73
mtb_ml_profile_config_t profiling
Definition: mtb_ml_model.h:140
Definition: mtb_ml_model.h:88
uint8_t * arena_buffer
Definition: mtb_ml_model.h:165
const int arena_size
Definition: mtb_ml_model.h:103
cy_rslt_t mtb_ml_model_get_output(const mtb_ml_model_t *object, MTB_ML_DATA_T **out_pptr, int *size_ptr)
: Get NN model output buffer and size
cy_rslt_t mtb_ml_model_profile_log(mtb_ml_model_t *object)
: Generate MTB ML profiling log
cy_rslt_t mtb_ml_model_rnn_reset_all_parameters(mtb_ml_model_t *object)
: Reset model parameters
uint64_t m_cpu_sum_cycles
Definition: mtb_ml_model.h:143
cy_rslt_t mtb_ml_model_profile_config(mtb_ml_model_t *object, mtb_ml_profile_config_t config)
: Update MTB ML inference profiling setting
int mtb_ml_model_profile_get_tsc(uint64_t *val)
: Read time stamp counter (TSC) .
uint32_t m_sum_frames
Definition: mtb_ml_model.h:142
Definition: mtb_ml_model.h:119
size_t tensor_arena_size
Definition: mtb_ml_model.h:81
cy_rslt_t mtb_ml_model_init(const mtb_ml_model_bin_t *bin, const mtb_ml_model_buffer_t *buffer, mtb_ml_model_t **object)
: Allocate and initialize NN model runtime object based on model data. Only intended to be called onc...
MTB_ML_DATA_T * input
Definition: mtb_ml_model.h:132
MTB_ML_DATA_T * output
Definition: mtb_ml_model.h:131
float input_scale
Definition: mtb_ml_model.h:137
uint8_t * tensor_arena
Definition: mtb_ml_model.h:80
void * mtb_ml_model_get_inference_object(const mtb_ml_model_t *object)
: Get MTB ML inference runtime object
int input_size
Definition: mtb_ml_model.h:128
int output_zero_point
Definition: mtb_ml_model.h:138
int lib_error
Definition: mtb_ml_model.h:130
float output_scale
Definition: mtb_ml_model.h:139
int buffer_size
Definition: mtb_ml_model.h:127
uint32_t m_cpu_peak_frame
Definition: mtb_ml_model.h:144
int input_zero_point
Definition: mtb_ml_model.h:136
tflm_rmf_apis_t rmf_apis
Definition: mtb_ml_model.h:172
cy_rslt_t mtb_ml_model_run(mtb_ml_model_t *object, MTB_ML_DATA_T *input)
: Perform NN model inference
void * tflm_obj
Definition: mtb_ml_model.h:133
int mtb_ml_model_get_input_size(const mtb_ml_model_t *object)
: Get NN model input data size
const unsigned int model_size
Definition: mtb_ml_model.h:102
int model_size
Definition: mtb_ml_model.h:126
cy_rslt_t mtb_ml_model_deinit(mtb_ml_model_t *object)
: Delete NN model runtime object and free all dynamically allocated memory. Only intended to be calle...