Fbs API Reference
The esp-dl model utilizes FlatBuffers to store information about parameters and the computation graph. Taking into account the encryption requirements of some models, this part has not been open-sourced. However, we provide a set of APIs to facilitate users in loading and parsing esp-dl models.
Header File
Classes
-
class FbsLoader
Class for parser the flatbuffers.
Public Functions
-
FbsLoader(const char *rodata_address_or_partition_label_or_path = nullptr, model_location_type_t location = MODEL_LOCATION_IN_FLASH_RODATA)
Construct a new FbsLoader object.
- Parameters
rodata_address_or_partition_label_or_path – The address of model data while location is MODEL_LOCATION_IN_FLASH_RODATA. The label of partition while location is MODEL_LOCATION_IN_FLASH_PARTITION. The path of model while location is MODEL_LOCATION_IN_SDCARD.
location – The model location.
-
FbsModel *load(const uint8_t *key = nullptr, bool param_copy = true)
Load the model. If there are multiple sub-models, the first sub-model will be loaded.
- Parameters
key – NULL or a 128-bit AES key, like {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
param_copy – Set to false to avoid copy model parameters from FLASH to PSRAM. Only set this param to false when your PSRAM resource is very tight. This saves PSRAM and sacrifices the performance of model inference because the frequency of PSRAM is higher than FLASH. Only takes effect when MODEL_LOCATION_IN_FLASH_RODATA(CONFIG_SPIRAM_RODATA not set) or MODEL_LOCATION_IN_FLASH_PARTITION.
- Returns
Return nullptr if loading fails. Otherwise return the pointer of FbsModel.
-
FbsModel *load(const int model_index, const uint8_t *key = nullptr, bool param_copy = true)
Load the model by model index.
- Parameters
model_index – The index of model.
key – NULL or a 128-bit AES key, like {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}.
param_copy – Set to false to avoid copy model parameters from FLASH to PSRAM. Only set this param to false when your PSRAM resource is very tight. This saves PSRAM and sacrifices the performance of model inference because the frequency of PSRAM is higher than FLASH. Only takes effect when MODEL_LOCATION_IN_FLASH_RODATA(CONFIG_SPIRAM_RODATA not set) or MODEL_LOCATION_IN_FLASH_PARTITION.
- Returns
Return nullptr if loading fails. Otherwise return the pointer of FbsModel.
-
FbsModel *load(const char *model_name, const uint8_t *key = nullptr, bool param_copy = true)
Load the model by model name.
- Parameters
model_name – The name of model.
key – NULL or a 128-bit AES key, like {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
param_copy – Set to false to avoid copy model parameters from FLASH to PSRAM. Only set this param to false when your PSRAM resource is very tight. This saves PSRAM and sacrifices the performance of model inference because the frequency of PSRAM is higher than FLASH. Only takes effect when MODEL_LOCATION_IN_FLASH_RODATA(CONFIG_SPIRAM_RODATA not set) or MODEL_LOCATION_IN_FLASH_PARTITION.
- Returns
Return nullptr if loading fails. Otherwise return the pointer of FbsModel.
-
int get_model_num()
Get the number of models.
- Returns
The number of models
-
void list_models()
List all model’s name.
-
const char *get_model_location_string()
Get the model location string.
- Returns
The model location string.
-
FbsLoader(const char *rodata_address_or_partition_label_or_path = nullptr, model_location_type_t location = MODEL_LOCATION_IN_FLASH_RODATA)
Header File
Classes
-
class FbsModel
Flatbuffer model object.
Public Functions
-
FbsModel(const void *data, size_t size, model_location_type_t location, bool encrypt, bool rodata_move, bool auto_free, bool param_copy)
Construct a new FbsModel object.
- Parameters
data – The data of model flatbuffers.
size – The size of model flatbuffers in bytes.
location – The location of model flatbuffers.
encrypt – Whether the model flatbuffers is encrypted or not.
rodata_move – Whether the model flatbuffers is moved from FLASH rodata to PSRAM.
auto_free – Whether to free the model flatbuffers data when destroy this class instance.
param_copy – Whether to copy the parameter in flatbuffers.
-
void print()
Print the model information.
-
std::vector<std::string> topological_sort()
Return vector of node name in the order of execution.
- Returns
topological sort of node name.
-
esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, int &ret_value)
Get the attribute of node.
- Parameters
node_name – The name of operation.
attribute_name – The name of attribute.
ret_value – The attribute value.
- Returns
esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.
-
esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, float &ret_value)
Get the attribute of node.
- Parameters
node_name – The name of operation.
attribute_name – The name of attribute.
ret_value – The attribute value.
- Returns
esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.
-
esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, std::string &ret_value)
Get the attribute of node.
- Parameters
node_name – The name of operation.
attribute_name – The name of attribute.
ret_value – The attribute value.
- Returns
esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.
-
esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, std::vector<int> &ret_value)
Get the attribute of node.
- Parameters
node_name – The name of operation.
attribute_name – The name of attribute.
ret_value – The attribute value.
- Returns
esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.
-
esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, std::vector<float> &ret_value)
Get the attribute of node.
- Parameters
node_name – The name of operation.
attribute_name – The name of attribute.
ret_value – The attribute value.
- Returns
esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.
-
esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, dl::quant_type_t &ret_value)
Get the attribute of node.
- Parameters
node_name – The name of operation.
attribute_name – The name of attribute.
ret_value – The attribute value.
- Returns
esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.
-
esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, dl::activation_type_t &ret_value)
Get the attribute of node.
- Parameters
node_name – The name of operation.
attribute_name – The name of attribute.
ret_value – The attribute value.
- Returns
esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.
-
esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, dl::resize_mode_t &ret_value)
Get the attribute of node.
- Parameters
node_name – The name of operation.
attribute_name – The name of attribute.
ret_value – The attribute value.
- Returns
esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.
-
esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, dl::TensorBase *&ret_value)
Get the attribute of node.
- Parameters
node_name – The name of operation.
attribute_name – The name of attribute.
ret_value – The attribute value.
- Returns
esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.
-
esp_err_t get_operation_output_shape(std::string node_name, int index, std::vector<int> &ret_value)
Get operation output shape.
- Parameters
node_name – The name of operation.
index – The index of outputs
ret_value – Return shape value.
- Returns
esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.
-
esp_err_t get_operation_inputs_and_outputs(std::string node_name, std::vector<std::string> &inputs, std::vector<std::string> &outputs)
Get the attribute of node.
- Parameters
node_name – The name of operation.
inputs – The vector of operation inputs.
outputs – The vector of operation outputs.
- Returns
esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.
-
std::string get_operation_type(std::string node_name)
Get operation type, “Conv”, “Linear” etc.
- Parameters
node_name – The name of operation
- Returns
The type of operation.
-
dl::TensorBase *get_operation_parameter(std::string node_name, int index = 1, uint32_t caps = MALLOC_CAP_DEFAULT)
Return if the variable is a parameter.
- Parameters
node_name – The name of operation
index – The index of the variable
caps – Bitwise OR of MALLOC_CAP_* flags indicating the type of memory to be returned
- Returns
-
dl::TensorBase *get_operation_lut(std::string node_name, uint32_t caps = MALLOC_CAP_DEFAULT, std::string attribute_name = "lut")
Get LUT(Look Up Table) if the operation has LUT.
- Parameters
node_name – The name of operation
caps – Bitwise OR of MALLOC_CAP_* flags indicating the type of memory to be returned
attribute_name – The name of LUT attribute
- Returns
-
bool is_parameter(std::string name)
return true if the variable is a parameter
- Parameters
name – Variable name
- Returns
true if the variable is a parameter else false
-
const void *get_tensor_raw_data(std::string tensor_name)
Get the raw data of FlatBuffers::Dl::Tensor.
- Parameters
tensor_name – The name of Tensor.
- Returns
uint8_t * The pointer of raw data.
-
dl::dtype_t get_tensor_dtype(std::string tensor_name)
Get the element type of tensor tensor.
- Parameters
tensor_name – The tensor name.
- Returns
FlatBuffers::Dl::TensorDataType
-
std::vector<int> get_tensor_shape(std::string tensor_name)
Get the shape of tensor.
- Parameters
tensor_name – The name of tensor.
- Returns
std::vector<int> The shape of tensor.
-
std::vector<int> get_tensor_exponents(std::string tensor_name)
Get the exponents of tensor.
Warning
When quantization is PER_CHANNEL, the size of exponents is same as out_channels. When quantization is PER_TENSOR, the size of exponents is 1.
- Parameters
tensor_name – The name of tensor.
- Returns
The exponents of tensor.
-
dl::dtype_t get_value_info_dtype(std::string var_name)
Get the element type of value_info.
- Parameters
var_name – The value_info name.
- Returns
dl::dtype_t
-
std::vector<int> get_value_info_shape(std::string var_name)
Get the shape of value_info.
- Parameters
var_name – The value_info name.
- Returns
the shape of value_info.
-
int get_value_info_exponent(std::string var_name)
Get the exponent of value_info. Only support PER_TENSOR quantization.
- Parameters
var_name – The value_info name.
- Returns
the exponent of value_info
-
const void *get_test_input_tensor_raw_data(std::string tensor_name)
Get the raw data of test input tensor.
- Parameters
tensor_name – The name of test input tensor.
- Returns
uint8_t * The pointer of raw data.
-
const void *get_test_output_tensor_raw_data(std::string tensor_name)
Get the raw data of test output tensor.
- Parameters
tensor_name – The name of test output tensor.
- Returns
uint8_t * The pointer of raw data.
-
dl::TensorBase *get_test_input_tensor(std::string tensor_name)
Get the test input tensor.
- Parameters
tensor_name – The name of test input tensor.
- Returns
The pointer of tensor.
-
dl::TensorBase *get_test_output_tensor(std::string tensor_name)
Get the test output tensor.
- Parameters
tensor_name – The name of test output tensor.
- Returns
The pointer of tensor.
-
std::vector<std::string> get_test_outputs_name()
Get the name of test outputs.
- Returns
the name of test outputs
-
std::vector<std::string> get_graph_inputs()
Get the graph inputs.
- Returns
the name of inputs
-
std::vector<std::string> get_graph_outputs()
Get the graph outputs.
- Returns
the name of ounputs
-
void clear_map()
Clear all map.
-
void load_map()
Load all map.
-
std::string get_model_name()
Get the model name.
- Returns
the name of model
-
int64_t get_model_version()
Get the model version.
- Returns
The version of model
-
std::string get_model_doc_string()
Get the model doc string.
- Returns
The doc string of model
-
std::string get_model_metadata_prop(const std::string &key)
Get the model’s metadata prop.
- Parameters
key – The key of metadata prop
- Returns
The value of metadata prop
-
void get_model_size(size_t *internal_size, size_t *psram_size, size_t *psram_rodata_size, size_t *flash_size)
Get the model size.
- Parameters
internal_size – Flatbuffers model internal RAM usage
psram_size – Flatbuffers model PSRAM usage
psram_rodata_size – Flatbuffers model PSRAM rodate usage. If CONFIG_SPIRAM_RODATA option is on, \ Flatbuffers model in FLASH rodata will be copied to PSRAM
flash_size – Flatbuffers model FLASH usage
Public Members
-
bool m_param_copy
copy flatbuffers param or not.
-
FbsModel(const void *data, size_t size, model_location_type_t location, bool encrypt, bool rodata_move, bool auto_free, bool param_copy)