ML Inference Engine
MTB Machine Learning Inference Engine Library
cy_ml_inference.h
1 /******************************************************************************
2 * File Name: cy_ml_inference.h
3 *
4 * Description: This file contains public interface for MTB ML inference engine
5 *
6 * Related Document: See README.md
7 *
8 *******************************************************************************
9 * (c) 2019-2021, Cypress Semiconductor Corporation. All rights reserved.
10 *******************************************************************************
11 * This software, including source code, documentation and related materials
12 * ("Software"), is owned by Cypress Semiconductor Corporation or one of its
13 * subsidiaries ("Cypress") and is protected by and subject to worldwide patent
14 * protection (United States and foreign), United States copyright laws and
15 * international treaty provisions. Therefore, you may use this Software only
16 * as provided in the license agreement accompanying the software package from
17 * which you obtained this Software ("EULA").
18 *
19 * If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
20 * non-transferable license to copy, modify, and compile the Software source
21 * code solely for use in connection with Cypress's integrated circuit products.
22 * Any reproduction, modification, translation, compilation, or representation
23 * of this Software except as specified above is prohibited without the express
24 * written permission of Cypress.
25 *
26 * Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
29 * reserves the right to make changes to the Software without notice. Cypress
30 * does not assume any liability arising out of the application or use of the
31 * Software or any product or circuit described in the Software. Cypress does
32 * not authorize its products for use in any products where a malfunction or
33 * failure of the Cypress product may reasonably be expected to result in
34 * significant property damage, injury or death ("High Risk Product"). By
35 * including Cypress's product in a High Risk Product, the manufacturer of such
36 * system or application assumes all risk of such use and in doing so agrees to
37 * indemnify Cypress against all liability.
38 *******************************************************************************/
39 
40 /*******************************************************************************
41 * Include guard
42 *******************************************************************************/
43 #ifndef __CY_ML_INFERENCE_H
44 #define __CY_ML_INFERENCE_H
45 
46 /*******************************************************************************
47 * Include header file
48 *******************************************************************************/
49 #include <stdint.h>
50 #include <stdbool.h>
51 /*******************************************************************************
52 * Compile-time flags
53 *******************************************************************************/
54 
55 /*******************************************************************************
56 * NN data type
57 *******************************************************************************/
58 #if defined(COMPONENT_ML_FLOAT32)
59  typedef float CY_ML_DATA_TYPE_T;
60 #elif defined(COMPONENT_ML_INT16x16) || defined(COMPONENT_ML_INT16x8)
61  typedef int16_t CY_ML_DATA_TYPE_T;
62 #elif defined(COMPONENT_ML_INT8x8)
63  typedef int8_t CY_ML_DATA_TYPE_T;
64 #else
65  #error Unsupported data type
66 #endif
67 
68 /******************************************************************************
69  * Defines
70  *****************************************************************************/
71 #define CY_ML_INFERENCE_VERSION_MAJOR 2
72 #define CY_ML_INFERENCE_VERSION_MINOR 0
73 #define CY_ML_INFERENCE_VERSION_PATCH 0
74 #define CY_ML_INFERENCE_VERSION 200
75 
76 #define CY_ML_SUCCESS (0)
77 
78 #define CY_ML_LINE_SHIFT (16u)
79 #define CY_ML_LINE_MASK (0xFFFF0000)
80 
81 #define CY_ML_LAYER_ID_SHIFT (8u)
82 #define CY_ML_LAYER_ID_MASK (0x0000FF00)
83 #define CY_ML_LAYER_ID_INVALID (255u)
84 
85 #define CY_ML_ERR_CODE_MASK (0x000000FF)
86 #define CY_ML_ERR_CORE_TOOL_VERSION (0x01)
87 #define CY_ML_ERR_EXCEED_MAX_SCRATCH_MEM (0x02)
88 #define CY_ML_ERR_LAYER_NOT_SUPPORTED (0x03)
89 #define CY_ML_ERR_ACT_NOT_SUPPORTED (0x04)
90 #define CY_ML_ERR_INPUT_DIMENTION (0x05)
91 #define CY_ML_ERR_CONNECTION (0x06)
92 #define CY_ML_ERR_OTHER_INPUT_MISSING (0x07)
93 #define CY_ML_ERR_INVALID_ARGUMENT (0x08)
94 #define CY_ML_ERR_MISMATCH_DATA_TYPE (0x09)
95 #define CY_ML_ERR_MISMATCH_PARM_CHECKSUM (0x0A)
96 #define CY_ML_ERR_INVALID_SCRATCH_MEM_OPT (0x0B)
97 
98 #define CY_ML_ERROR(x, y) ((__LINE__ << CY_ML_LINE_SHIFT) | \
99  (((x) << CY_ML_LAYER_ID_SHIFT) & CY_ML_LAYER_ID_MASK) | \
100  ((y) & CY_ML_ERR_CODE_MASK))
101 #define CY_ML_ERR_CODE(x) (uint8_t)((x) & CY_ML_ERR_CODE_MASK)
102 #define CY_ML_ERR_LAYER_INDEX(x) (uint8_t)(((x) & CY_ML_LAYER_ID_MASK) >> CY_ML_LAYER_ID_SHIFT)
103 #define CY_ML_ERR_LINE_NUMBER(x) (uint16_t)(((x) & CY_ML_LINE_MASK) >> CY_ML_LINE_SHIFT)
104 
105 #define CY_ML_PROFILE_FRAME (0x01)
106 #define CY_ML_PROFILE_LAYER (0x02)
107 #define CY_ML_PROFILE_MODEL (0x04)
108 #define CY_ML_LOG_MODEL_OUTPUT (0x10)
109 
110 #define CY_ML_MODEL_OBJECT_SIZE (360)
111 #define CY_ML_RES_CONN_OBJECT_SIZE (8)
112 #if defined(COMPONENT_ML_FLOAT32)
113  #define CY_ML_LAYER_OBJECT_SIZE (80)
114 #else
115  #define CY_ML_LAYER_OBJECT_SIZE (88)
116 #endif
117 
118 /*******************************************************************************
119 * Structures and enumerations
120 *******************************************************************************/
121 typedef enum
122 {
123  CY_ML_DATA_UNKNOWN = 0u,
124  CY_ML_DATA_INT8 = 1u,
125  CY_ML_DATA_INT16 = 2u,
126  CY_ML_DATA_FLOAT = 3u,
127 } cy_en_ml_data_type_t;
128 
130 typedef enum
131 {
132  CY_ML_PROFILE_DISABLE = 0,
133  CY_ML_PROFILE_ENABLE_MODEL = CY_ML_PROFILE_MODEL,
134  CY_ML_PROFILE_ENABLE_LAYER = CY_ML_PROFILE_LAYER,
135  CY_ML_PROFILE_ENABLE_MODEL_PER_FRAME = (CY_ML_PROFILE_MODEL | CY_ML_PROFILE_FRAME),
136  CY_ML_PROFILE_ENABLE_LAYER_PER_FRAME = (CY_ML_PROFILE_LAYER | CY_ML_PROFILE_FRAME),
137  CY_ML_LOG_ENABLE_MODEL_LOG = CY_ML_LOG_MODEL_OUTPUT
138 } cy_en_ml_profile_config_t;
139 
143 typedef struct
144 {
155  cy_en_ml_data_type_t libml_input_type;
156  cy_en_ml_data_type_t libml_weight_type;
159 
160 /******************************************************************************
161 * Function prototype
162 ******************************************************************************/
163 
164 typedef int (*cy_ml_cb_fun) (void* arg, char* buf, cy_en_ml_profile_config_t type);
165 
198 extern int Cy_ML_Model_Inference(void *modelPt, void *input, void *output,
199  int* in_out_q);
200 
220 extern int Cy_ML_Model_Parse(char *fn_prms , cy_stc_ml_model_info_t *mdl_infoPt);
221 
247 extern int Cy_ML_Model_Init(void **dPt_container
248  , char *fn_prms, char *fn_ptr
249  ,char *persistent_mem, char* scratch_mem, cy_stc_ml_model_info_t *mdl_infoPt);
250 
268 extern int Cy_ML_Rnn_State_Control(void* modelPt, int rnn_status, int window_size);
269 
280 int Cy_ML_Profile_Get_Tsc(uint32_t *val);
281 
296 int Cy_ML_Profile_Init(void *modelPt, cy_en_ml_profile_config_t config, cy_ml_cb_fun cb_func, void *cb_arg);
297 
309 int Cy_ML_Profile_Control(void *modelPt, cy_en_ml_profile_config_t config);
310 
319 int Cy_ML_Profile_Print(void *modelPt);
320 
325 #endif /*__CY_ML_INFERENCE_H */
326 
327 /* [] END OF FILE */
int Cy_ML_Profile_Print(void *modelPt)
: Print profile log.
int libml_version
Definition: cy_ml_inference.h:153
cy_en_ml_data_type_t libml_input_type
Definition: cy_ml_inference.h:155
int num_of_res_conns
Definition: cy_ml_inference.h:151
int Cy_ML_Profile_Control(void *modelPt, cy_en_ml_profile_config_t config)
: Update profile configuration.
int Cy_ML_Profile_Get_Tsc(uint32_t *val)
: Cy_ML_Profile_Get_Tsc() is an API function to read time stamp counter (TSC) .
uint32_t ml_coretool_version
Definition: cy_ml_inference.h:154
int Cy_ML_Model_Inference(void *modelPt, void *input, void *output, int *in_out_q)
: Cy_ML_Model_Inference() is the API function to perform NN inference.
int Cy_ML_Profile_Init(void *modelPt, cy_en_ml_profile_config_t config, cy_ml_cb_fun cb_func, void *cb_arg)
: Initialize profile configuration.
int scratch_mem
Definition: cy_ml_inference.h:147
Definition: cy_ml_inference.h:143
int input_size
Definition: cy_ml_inference.h:149
int Cy_ML_Rnn_State_Control(void *modelPt, int rnn_status, int window_size)
: Cy_ML_Rnn_State_Control() is the API function to rest recurrent NN state, inference engine will res...
int persistent_mem
Definition: cy_ml_inference.h:148
cy_en_ml_data_type_t libml_weight_type
Definition: cy_ml_inference.h:156
int output_size
Definition: cy_ml_inference.h:146
int Cy_ML_Model_Parse(char *fn_prms, cy_stc_ml_model_info_t *mdl_infoPt)
: Cy_ML_Model_Parse() is the API function to parse NN model parameters to get basic info...
int num_of_layers
Definition: cy_ml_inference.h:150
int Cy_ML_Model_Init(void **dPt_container, char *fn_prms, char *fn_ptr, char *persistent_mem, char *scratch_mem, cy_stc_ml_model_info_t *mdl_infoPt)
: Cy_ML_Model_Init() is the API function to parse NN model and initialize CY data container before st...
int recurrent_ts_size
Definition: cy_ml_inference.h:152