class MicroInterpreterThe lifetime of the model, op resolver, tensor arena, error reporter, resource variables, and profiler must be at least as long as that of the interpreter obje…
tflite::MicroInterpreter::MicroInterpreter( const Model *model, const MicroOpResolver &op_resolver, uint8_t *tensor_arena, size_t tensor_arena_size, MicroResourceVariables *resource_variables = nullptr, MicroProfilerInterface *profiler = nullptr, bool preserve_all_tensors = false)The lifetime of the model, op resolver, tensor arena, error reporter, resource variables, and profiler must be at least as long as that of the interpreter object, since the interpreter may need to access them at any time. This means that you should usually create them with the same scope as each other, for example having them all allocated on the stack as local variables through a top-level function. The interpreter doesn’t do any deallocation of any of the pointed-to objects, ownership remains with the caller.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
model | const Model * | Required | |
op_resolver | const MicroOpResolver & | Required | |
tensor_arena | uint8_t * | Required | |
tensor_arena_size | size_t | Required | |
resource_variables | MicroResourceVariables * | nullptr | |
profiler | MicroProfilerInterface * | nullptr | |
preserve_all_tensors | bool | false |
Create an interpreter instance using an existing MicroAllocator instance.
tflite::MicroInterpreter::MicroInterpreter( const Model *model, const MicroOpResolver &op_resolver, MicroAllocator *allocator, MicroResourceVariables *resource_variables = nullptr, MicroProfilerInterface *profiler = nullptr)Create an interpreter instance using an existing MicroAllocator instance. This constructor should be used when creating an allocator that needs to have allocation handled in more than one interpreter or for recording allocations inside the interpreter. The lifetime of the allocator must be as long as that of the interpreter object.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
model | const Model * | Required | |
op_resolver | const MicroOpResolver & | Required | |
allocator | MicroAllocator * | Required | |
resource_variables | MicroResourceVariables * | nullptr | |
profiler | MicroProfilerInterface * | nullptr |
tflite::MicroInterpreter::~MicroInterpreter()Runs through the model and allocates all necessary input, output and intermediate tensors.
TfLiteStatus tflite::MicroInterpreter::AllocateTensors()Runs through the model and allocates all necessary input, output and intermediate tensors.
Invoke
C++In order to support partial graph runs for strided models, this can return values other than kTfLiteOk and kTfLiteError.
TfLiteStatus tflite::MicroInterpreter::Invoke()In order to support partial graph runs for strided models, this can return values other than kTfLiteOk and kTfLiteError. TODO(b/149795762): Add this to the TfLiteStatus enum.
This is the recommended API for an application to pass an external payload pointer as an external context to kernels.
TfLiteStatus tflite::MicroInterpreter::SetMicroExternalContext(void *external_context_payload)This is the recommended API for an application to pass an external payload pointer as an external context to kernels. The life time of the payload pointer should be at least as long as this interpreter. TFLM supports only one external context.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
external_context_payload | void * | Required |
input
C++TfLiteTensor * tflite::MicroInterpreter::input(size_t index)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
index | size_t | Required |
inputs_size
C++size_t tflite::MicroInterpreter::inputs_size() constinputs
C++const flatbuffers::Vector< int32_t > & tflite::MicroInterpreter::inputs() constinput_tensor
C++TfLiteTensor * tflite::MicroInterpreter::input_tensor(size_t index)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
index | size_t | Required |
template <class T>T * tflite::MicroInterpreter::typed_input_tensor(int tensor_index)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
tensor_index | int | Required |
output
C++TfLiteTensor * tflite::MicroInterpreter::output(size_t index)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
index | size_t | Required |
outputs_size
C++size_t tflite::MicroInterpreter::outputs_size() constoutputs
C++const flatbuffers::Vector< int32_t > & tflite::MicroInterpreter::outputs() constTfLiteTensor * tflite::MicroInterpreter::output_tensor(size_t index)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
index | size_t | Required |
template <class T>T * tflite::MicroInterpreter::typed_output_tensor(int tensor_index)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
tensor_index | int | Required |
GetTensor
C++Returns a pointer to the tensor for the corresponding tensorindex.
TfLiteEvalTensor * tflite::MicroInterpreter::GetTensor(int tensor_index, int subgraph_index = 0)Returns a pointer to the tensor for the corresponding tensor_index.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
tensor_index | int | Required | |
subgraph_index | int | 0 |
Zeros out a single variable tensor in a specified subgraph in the model.
TfLiteStatus tflite::MicroInterpreter::ResetVariableTensor(int tensor_index, int subgraph_index = 0)Zeros out a single variable tensor in a specified subgraph in the model.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
tensor_index | int | Required | |
subgraph_index | int | 0 |
Reset
C++Reset the state to be what you would expect when the interpreter is first created.
TfLiteStatus tflite::MicroInterpreter::Reset()Reset the state to be what you would expect when the interpreter is first created. i.e. after Init and Prepare is called for the very first time.
TfLiteStatus tflite::MicroInterpreter::initialization_status() constPopulates node and registration pointers representing the inference graph of the model from values inside the flatbuffer (loaded from the TfLiteModel instance).
TfLiteStatus tflite::MicroInterpreter::PrepareNodeAndRegistrationDataFromFlatbuffer()Populates node and registration pointers representing the inference graph of the model from values inside the flatbuffer (loaded from the TfLiteModel instance). Persistent data (e.g. operator data) is allocated from the arena.
For debugging only.
size_t tflite::MicroInterpreter::arena_used_bytes() constFor debugging only. Returns the actual used arena in bytes. This method gives the optimal arena size. It’s only available after AllocateTensors has been called. Note that normally tensor_arena requires 16 bytes alignment to fully utilize the space. If it’s not the case, the optimal arena size would be arena_used_bytes() + 16.
Returns True if all Tensors are being preserved TODO(b/297106074) : revisit making C++ example or test for preservealltensors
bool tflite::MicroInterpreter::preserve_all_tensors() constReturns True if all Tensors are being preserved TODO(b/297106074) : revisit making C++ example or test for preserve_all_tensors
Set the alternate MicroProfilerInterface.
TfLiteStatus tflite::MicroInterpreter::SetAlternateProfiler(MicroProfilerInterface *alt_profiler)Set the alternate MicroProfilerInterface. This value is passed through to the MicroContext. This can be used to profile subsystems simultaneously with the profiling of kernels during the Eval phase. See (b/379584353). The alternate MicroProfilerInterface is currently used by the tensor decompression subsystem.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
alt_profiler | MicroProfilerInterface * | Required |
Set the alternate decompression memory regions.
TfLiteStatus tflite::MicroInterpreter::SetDecompressionMemory( const MicroContext::AlternateMemoryRegion *regions, size_t count)Set the alternate decompression memory regions. Can only be called during the MicroInterpreter kInit state (i.e. must be called before MicroInterpreter::AllocateTensors). The regions pointer argument is the start of a MicroContext::AlternateMemoryRegion array where the length of the array is given by the count argument. The lifetime of the MicroContext::AlternateMemoryRegion array must be at least that of the MicroInterpreter.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
regions | const MicroContext::AlternateMemoryRegion * | Required | |
count | size_t | Required |
allocator
C++const MicroAllocator & tflite::MicroInterpreter::allocator() constcontext
C++const TfLiteContext & tflite::MicroInterpreter::context() const