For AidliteSDK, it handles different models from different frameworks, and each model has its own different input data types and different output data types. In the aforementioned usage workflow, you need to set the input and output data types of the model, which requires using this data type.
Member Variable Name
Type
Value
Description
TYPE_DEFAULT
int
0
Invalid DataType type
TYPE_UINT8
int
1
Unsigned byte data
TYPE_INT8
int
2
Byte data
TYPE_UINT32
int
3
Unsigned int32 data
TYPE_FLOAT32
int
4
Float data
TYPE_INT32
int
5
Int32 data
TYPE_INT64
int
6
Int64 data
TYPE_UINT64
int
7
Uint64 data
TYPE_INT16
int
8
Int16 data
TYPE_UINT16
int
9
Uint16 data
TYPE_FLOAT16
int
10
Float16 data
TYPE_BOOL
int
11
Bool data
Inference Implementation Type class ImplementType
Member Variable Name
Type
Value
Description
TYPE_DEFAULT
int
0
Invalid ImplementType type
TYPE_MMKV
int
1
Implementation through MMKV (currently no related implementation, may be removed later)
As mentioned earlier, Aidlite SDK integrates multiple deep learning inference frameworks, so in the aforementioned usage workflow, you need to set which framework's model is currently being used, which requires using this framework type.
Member Variable Name
Type
Value
Description
TYPE_DEFAULT
int
0
Invalid FrameworkType type
TYPE_SNPE
int
1
SNPE 1.x (DLC) model
TYPE_TFLite
int
2
TFLite model
TYPE_RKNN
int
3
RKNN model
TYPE_QNN
int
4
QNN model
TYPE_SNPE2
int
5
SNPE 2.x (DLC) model
TYPE_NCNN
int
6
NCNN model
TYPE_MNN
int
7
MNN model
TYPE_TNN
int
8
TNN model
TYPE_PADDLE
int
9
Paddle model
TYPE_MS
int
10
MindSpore model
TYPE_ONNX
uint8_t
11
ONNX model
TYPE_QNN216
uint8_t
101
QNN2.16 model
TYPE_SNPE216
uint8_t
102
SNPE2.16 model
TYPE_QNN223
uint8_t
103
QNN2.23 model
TYPE_SNPE223
uint8_t
104
SNPE2.23 model
TYPE_QNN229
uint8_t
105
QNN2.29 model
TYPE_SNPE229
uint8_t
106
SNPE2.29 model
TYPE_QNN231
uint8_t
107
QNN2.31 model
TYPE_SNPE231
uint8_t
1068
SNPE2.31 model
💡Note
Special notes:
If the user environment has multiple backend implementation software packages such as AidLite-QNN216/AidLite-QNN223/AidLite-QNN229 installed simultaneously, and the FrameworkType in the code is set to TYPE_QNN (i.e., without specifying which specific version of QNN backend to use), then the actual running QNN backend should be the one with the highest QNN version (i.e., AidLite-QNN229 will be prioritized to complete inference).
Different QNN version backend software packages cannot be used simultaneously in the same program, which means if the program first specifies TYPE_QNN216 to complete inference, then TYPE_QNN223/TYPE_QNN229 cannot be used for inference.
Inference Acceleration Hardware Type class AccelerateType
For each deep learning inference framework, it may support running on different acceleration hardware units (such as SNPE/QNN models running on Qualcomm DSP computing units, RKNN models running on Rockchip NPU computing units), so in the aforementioned usage workflow, you need to set which computing unit the current model is expected to run on, which requires using this acceleration hardware unit type.
AidliteSDK provides an interface for setting logs (which will be introduced later), which requires providing the interface with which log level to use currently, so this log level needs to be used.
Each model has its own inputs and outputs. For models that developers are not familiar with, during development they may need to query the input and output Tensor information of the model to facilitate developers in performing pre and post-processing operations for model inference. Aidlite provides developers with an interface to query detailed information about model inputs and outputs.
As mentioned earlier, before creating an inference interpreter, you need to set specific detailed parameters related to the model. The Model class is mainly used to record the model's file information, structural information, and model-related content during runtime.
To set detailed information related to the model, you naturally need to have a model instance object first. This function is used to create a Model instance object.
API
create_instance
Description
Constructs a Model type instance object by passing the path name of the model file
Parameters
model_path: Path name of the model file
Return Value
Normal: Model instance object
Exception: None
python
# Create model object using inceptionv3_float32.dlc file in current path, error if return value is emptymodel = aidlite.Model.create_instance(model_path=r"./inceptionv3.dlc")if model is None: print("Create model failed !") return False
After the model instance object is created successfully, you need to set the input and output data types of the model and the shape information of the input and output tensor data.
API
set_model_properties
Description
Set model properties, input and output data shape and data types.
Parameters
input_shapes: Shape array of input tensors, two-dimensional array structure.
input_data_type: Data type of input tensors, DataType enumeration type.
output_shapes: Shape array of output tensors, two-dimensional array structure.
output_data_type: Data type of output tensors, DataType enumeration type.
Return Value
Model file path string object corresponding to the Model object
python
# Data type uses the aforementioned DataType, input and output shapes are two-dimensional arraysinput_shapes=[[1,640,640,3]]output_shapes=[[1,10,10,255], [1,20,20,255], [1,40,40,255]]model.set_model_properties(input_shapes=input_shapes, input_data_type=aidlite.DataType.TYPE_FLOAT32, output_shapes=output_shapes, output_data_type=aidlite.DataType.TYPE_FLOAT32)
As mentioned earlier, before creating an inference interpreter, in addition to setting specific Model information, you also need to set some configuration information for inference. The Config class is used to record configuration options that need to be set in advance, and these configuration items will be used at runtime.
To set runtime configuration information, you naturally need to have a configuration instance object first. This function is used to create a Config instance object.
API
create_instance
Description
Used to construct an instance object of the Config class
Parameters
snpe_out_names: List of model output node names (optional)
number_of_threads: Number of threads, valid when greater than 0 (optional)
is_quantify_model: Whether it's a quantized model, 1 indicates quantized model for FAST (optional)
fast_timeout: Interface timeout (milliseconds, valid when greater than 0) for FAST (optional)
accelerate_type: Type of acceleration hardware (optional)
framework_type: Type of underlying deep learning framework (optional)
Return Value
Normal: Config instance object
Exception: None
python
# Create configuration instance object, error if return value is Noneconfig = aidlite.Config.create_instance()if config is None: print("Create config failed !") return False
Used to store runtime context involved in the execution process, including Model objects and Config objects, and may be extended to include runtime data in the future.
The Interpreter class object instance is the main entity that executes inference operations and is used to perform specific inference processes. In the inference workflow mentioned earlier, after creating the interpreter object, all operations are completed based on the interpreter object, making it the absolute core content of AidliteSDK.
To execute inference-related operations, the inference interpreter is essential. This function is used to build an instance object of the inference interpreter.
API
create_instance
Description
Use the various data managed by the Context object to construct an Interpreter type object.
Parameters
None
Return Value
Normal: Interpreter instance object
Exception: None
python
# Create interpreter object, return value of None indicates errorinterpreter = aidlite.Interpreter.create_instance()if interpreter is None: print("Create Interpreter failed !") return False
After the interpreter object is created, some initialization operations need to be performed (such as environment checking, resource construction, etc.).
API
create_instance
Description
Use the various data managed by the Context object to construct an Interpreter type object.
Parameters
context: Context object instance. It manages Model and Config objects, which contain model data, configuration data, etc.
Return Value
Normal: 0
Exception: Non-zero
python
# Initialize interpreter, non-zero return value indicates errorresult = aidlite.interpreter.init()if result != 0: printf("sample : interpreter->init() failed !") return False
After the interpreter object completes the initialization operation, the required model file can be loaded for the interpreter to complete the model loading work. The subsequent inference process uses the loaded model resources.
API
load_model
Description
Complete model loading related work. Since the model file path has been set in the Model object previously, the model loading operation can be executed directly
Parameters
None
Return Value
Normal: 0
Exception: Non-zero
python
# Load model with interpreter, non-zero return value indicates errorresult = interpreter.load_model()if result != 0: print("sample : interpreter->load_model() failed !") return False
Get Input Tensor Details get_input_tensor_info()
If you want to get detailed information about the model's input Tensor, you can use this interface to obtain it.
API
get_input_tensor_info
Description
Get detailed information about input Tensors in the model
Return Value
Normal: Two-dimensional array of TensorInfo type data
Exception: None
Special Note
This interface is supported from version 2.2.5 onwards
python
# Get detailed information about model input Tensorsinput_tensor_info = onnx_interpreter.get_input_tensor_info()if len(input_tensor_info) == 0 : printf("interpreter get_input_tensor_info() failed !\n") return Falsefor gi, graph_tensor_info in enumerate(input_tensor_info): for ti, tensor_info in enumerate(graph_tensor_info): print( f"Input tensor : Graph[{gi}]-Tensor[{ti}]-name[{tensor_info.name}]" f"-element_count[{tensor_info.element_count}]-element_type[{tensor_info.element_type}]" f"-dimensions[{tensor_info.dimensions}]-shape{tensor_info.shape}")
Get Output Tensor Details get_output_tensor_info()
If you want to get detailed information about the model's output Tensor, you can use this interface to obtain it.
API
get_output_tensor_info
Description
Get detailed information about output Tensors in the model
Return Value
Normal: Two-dimensional array of TensorInfo type data
Exception: None
Special Note
This interface is supported from version 2.2.5 onwards
python
# Get detailed information about model output Tensorsoutput_tensor_info = onnx_interpreter.get_output_tensor_info()if len(output_tensor_info) == 0 : printf("interpreter get_output_tensor_info() failed !\n") return Falsefor gi, graph_tensor_info in enumerate(output_tensor_info): for ti, tensor_info in enumerate(graph_tensor_info): print( f"Output tensor : Graph[{gi}]-Tensor[{ti}]-name[{tensor_info.name}]" f"-element_count[{tensor_info.element_count}]-element_type[{tensor_info.element_type}]" f"-dimensions[{tensor_info.dimensions}]-shape{tensor_info.shape}")
As mentioned when introducing the workflow, before setting input data, different preprocessing operations need to be completed for different models to adapt to specific models.
API
set_input_tensor
Description
Execute inference computation process
Parameters
in_tensor_tag: Index value or name string of input tensor, type is int or str
input_data: Binary data of input tensor, type is passed according to actual situation
Return Value
Normal: 0
Exception: Non-zero
Special Note
Parameter in_tensor_tag supports name string from version 2.2.5 onwards
python
# Set input data for inference, non-zero return value indicates errorresult = interpreter.set_input_tensor(in_tensor_tag=1, input_data=obj)#result = interpreter.set_input_tensor(in_tensor_tag="input_tensor_name", input_data=obj)if result != 0: print("interpreter->set_input_tensor() failed !") return False
As mentioned when introducing the workflow, after setting the input data, the next step is naturally to execute the inference running process on the input data.
After inference is completed, the result data obtained from inference needs to be extracted. As mentioned when introducing the workflow, after extracting the result data, this result data can be processed to determine whether the result is correct.
API
get_output_tensor
Description
After successful inference, get inference result data
Parameters
out_tensor_tag: Index value or name string of result output tensor, type is int or str
output_type: Type of result output, optional parameter, defaults to aidlite.DataType.TYPE_FLOAT32 if not passed
Return Value
Normal: float result data.
Exception: None.
Special Note
Parameter out_tensor_tag supports name string from version 2.2.5 onwards
python
# Get inference result data, return value of None indicates errorout_data = interpreter.get_output_tensor(out_tensor_tag=1, output_type=aidlite.DataType.TYPE_INT32)#out_data = interpreter.get_output_tensor(out_tensor_tag="output_tensor_name", output_type=aidlite.DataType.TYPE_INT32)if out_data is None: print("interpreter->get_output_tensor() failed !") return False
As mentioned earlier, the interpreter object needs to perform init initialization operations and load model operations. Correspondingly, the interpreter also needs to perform some release operations to destroy the previously created resources.
Build inference interpreter object, can provide different parameters, the simplest is to provide only the path and name of the model file.
API
build_interpretper_from_path
Description
Directly create corresponding interpreter object through the path name of the model file. All related parameters use default values
Parameters
path: Path name of the model file
Return Value
Normal: Interpreter object instance
Exception: None
python
# Build interpreter by passing Model file path, return value of None indicates errorinterpreter = aidlite.InterpreterBuilder.build_interpretper_from_path(path=r"./640.dlc")if interpreter is None: print("Create Interpreter failed !") return False
Build inference interpreter object, in addition to providing the model file path, you can also provide a Model object, so that you can not only set the path name of the model file, but also set the input and output data types of the model and the shape information of input and output data.
API
build_interpretper_from_model
Description
Create corresponding interpreter object by passing Model object. Config related parameters all use default values
Parameters
model: Model type object containing model-related data
Return Value
Normal: Interpreter object instance
Exception: None
python
# Build interpreter by passing Model object, return value of None indicates errorinterpreter = aidlite.InterpreterBuilder.build_interpretper_from_model(model=model)if interpreter is None: print("Create Interpreter failed !") return False
Build inference interpreter object, in addition to the two methods mentioned above, you can also provide both Model object and Config object, so that you can not only provide model-related information, but also provide more runtime configuration parameters.
API
build_interpretper_from_model_and_config
Description
Create corresponding interpreter object by passing Model object and Config.
Parameters
model: Model type object containing model-related data
config: Config type object containing some configuration parameters
Return Value
Normal: Interpreter object instance
Exception: None
python
# Build interpreter by passing Model and Config objects, return value of None indicates errorinterpreter = aidlite.InterpreterBuilder.build_interpretper_from_model_and_config(model=model, config=config)if interpreter is None: print("Create Interpreter failed !") return False