# microinterpreter

Allocate tensors, invoke a model and access inputs/outputs.

## tflite::MicroInterpreter

`class` · `cpp`

```cpp
class MicroInterpreter
```

Source: `tensorflow/lite/micro/micro_interpreter.h:46`

### tflite::MicroInterpreter::MicroInterpreter--classtflite_1_1MicroInterpreter_1a67f9a6dd9023a79d9270692896c0490a

`method` · `cpp`

```cpp
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 |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:56`

### tflite::MicroInterpreter::MicroInterpreter--classtflite_1_1MicroInterpreter_1a04a785a56d4e367ee9b41639f33af726

`method` · `cpp`

```cpp
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 |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:67`

### tflite::MicroInterpreter::~MicroInterpreter

`method` · `cpp`

```cpp
tflite::MicroInterpreter::~MicroInterpreter()
```

Source: `tensorflow/lite/micro/micro_interpreter.h:72`

### tflite::MicroInterpreter::AllocateTensors

`method` · `cpp`

```cpp
TfLiteStatus tflite::MicroInterpreter::AllocateTensors()
```

Runs through the model and allocates all necessary input, output and intermediate tensors.

Source: `tensorflow/lite/micro/micro_interpreter.h:76`

### tflite::MicroInterpreter::Invoke

`method` · `cpp`

```cpp
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.

Source: `tensorflow/lite/micro/micro_interpreter.h:81`

### tflite::MicroInterpreter::SetMicroExternalContext

`method` · `cpp`

```cpp
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 |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:87`

### tflite::MicroInterpreter::input

`method` · `cpp`

```cpp
TfLiteTensor * tflite::MicroInterpreter::input(size_t index)
```

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| index | size_t | Required |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:89`

### tflite::MicroInterpreter::inputs_size

`method` · `cpp`

```cpp
size_t tflite::MicroInterpreter::inputs_size() const
```

Source: `tensorflow/lite/micro/micro_interpreter.h:90`

### tflite::MicroInterpreter::inputs

`method` · `cpp`

```cpp
const flatbuffers::Vector< int32_t > & tflite::MicroInterpreter::inputs() const
```

Source: `tensorflow/lite/micro/micro_interpreter.h:93`

### tflite::MicroInterpreter::input_tensor

`method` · `cpp`

```cpp
TfLiteTensor * tflite::MicroInterpreter::input_tensor(size_t index)
```

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| index | size_t | Required |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:96`

### tflite::MicroInterpreter::typed_input_tensor

`method` · `cpp`

```cpp
template <class T>
T * tflite::MicroInterpreter::typed_input_tensor(int tensor_index)
```

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| tensor_index | int | Required |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:98`

### tflite::MicroInterpreter::output

`method` · `cpp`

```cpp
TfLiteTensor * tflite::MicroInterpreter::output(size_t index)
```

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| index | size_t | Required |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:107`

### tflite::MicroInterpreter::outputs_size

`method` · `cpp`

```cpp
size_t tflite::MicroInterpreter::outputs_size() const
```

Source: `tensorflow/lite/micro/micro_interpreter.h:108`

### tflite::MicroInterpreter::outputs

`method` · `cpp`

```cpp
const flatbuffers::Vector< int32_t > & tflite::MicroInterpreter::outputs() const
```

Source: `tensorflow/lite/micro/micro_interpreter.h:111`

### tflite::MicroInterpreter::output_tensor

`method` · `cpp`

```cpp
TfLiteTensor * tflite::MicroInterpreter::output_tensor(size_t index)
```

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| index | size_t | Required |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:114`

### tflite::MicroInterpreter::typed_output_tensor

`method` · `cpp`

```cpp
template <class T>
T * tflite::MicroInterpreter::typed_output_tensor(int tensor_index)
```

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| tensor_index | int | Required |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:116`

### tflite::MicroInterpreter::GetTensor

`method` · `cpp`

```cpp
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 |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:126`

### tflite::MicroInterpreter::ResetVariableTensor

`method` · `cpp`

```cpp
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 |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:129`

### tflite::MicroInterpreter::Reset

`method` · `cpp`

```cpp
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.

Source: `tensorflow/lite/micro/micro_interpreter.h:133`

### tflite::MicroInterpreter::initialization_status

`method` · `cpp`

```cpp
TfLiteStatus tflite::MicroInterpreter::initialization_status() const
```

Source: `tensorflow/lite/micro/micro_interpreter.h:135`

### tflite::MicroInterpreter::PrepareNodeAndRegistrationDataFromFlatbuffer

`method` · `cpp`

```cpp
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.

Source: `tensorflow/lite/micro/micro_interpreter.h:141`

### tflite::MicroInterpreter::arena_used_bytes

`method` · `cpp`

```cpp
size_t tflite::MicroInterpreter::arena_used_bytes() const
```

For 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.

Source: `tensorflow/lite/micro/micro_interpreter.h:149`

### tflite::MicroInterpreter::preserve_all_tensors

`method` · `cpp`

```cpp
bool tflite::MicroInterpreter::preserve_all_tensors() const
```

Returns True if all Tensors are being preserved TODO(b/297106074) : revisit making C++ example or test for preserve_all_tensors

Source: `tensorflow/lite/micro/micro_interpreter.h:154`

### tflite::MicroInterpreter::SetAlternateProfiler

`method` · `cpp`

```cpp
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 |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:164`

### tflite::MicroInterpreter::SetDecompressionMemory

`method` · `cpp`

```cpp
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 |  |

Source: `tensorflow/lite/micro/micro_interpreter.h:174`

### tflite::MicroInterpreter::allocator

`method` · `cpp`

```cpp
const MicroAllocator & tflite::MicroInterpreter::allocator() const
```

Source: `tensorflow/lite/micro/micro_interpreter.h:178`

### tflite::MicroInterpreter::context

`method` · `cpp`

```cpp
const TfLiteContext & tflite::MicroInterpreter::context() const
```

Source: `tensorflow/lite/micro/micro_interpreter.h:179`
