tflite::impl::Interpreter

Summary

Constructors and Destructors

Interpreter(ErrorReporter *error_reporter)
Interpreter(const Interpreter &)
~Interpreter()

Public types

TfLiteDelegatePtr using
std::unique_ptr< TfLiteDelegate, void(*)(TfLiteDelegate *)>

Public static attributes

kTensorsCapacityHeadroom = 16
constexpr int
The capacity headroom of tensors_ vector before calling ops' prepare and invoke function.
kTensorsReservedCapacity = 128
constexpr int

Friend classes

tflite::impl::InterpreterBuilder
friend class

Public functions

AddProfiler(Profiler *profiler)
void
\warning This is an experimental API and subject to change.
AddProfiler(std::unique_ptr< Profiler > profiler)
void
\warning This is an experimental API and subject to change.
AllocateTensors()
TfLiteStatus
Update allocations for all tensors.
ApplyOptions(InterpreterOptions *options)
TfLiteStatus
\warning This is an experimental API and subject to change.
Cancel()
TfLiteStatus
\warning This is an experimental API and subject to change.
EnsureTensorDataIsReadable(int tensor_index)
TfLiteStatus
\warning This is an experimental API and subject to change.
GetAllowFp16PrecisionForFp32() const
bool
\warning Experimental interface, subject to change.
GetAsyncSignatureRunner(const char *signature_key)
async::AsyncSignatureRunner *
\warning Experimental interface, subject to change.
GetBufferHandle(int tensor_index, TfLiteBufferHandle *buffer_handle, TfLiteDelegate **delegate)
TfLiteStatus
\warning This is an experimental API and subject to change.
GetInputName(int index) const
const char *
Return the name of a given input.
GetOutputName(int index) const
const char *
Return the name of a given output.
GetProfiler()
Profiler *
\warning This is an experimental API and subject to change.
GetSignatureRunner(const char *signature_key)
SignatureRunner *
\warning Experimental interface, subject to change.
GetSubgraphIndexFromSignature(const char *signature_key) const
int
\warning Experimental interface, subject to change.
Invoke()
TfLiteStatus
Invoke the interpreter (run the whole graph in dependency order).
ModifyGraphWithDelegate(TfLiteDelegate *delegate)
TfLiteStatus
Allow a delegate to look at the graph and modify the graph to handle parts of the graph themselves.
ModifyGraphWithDelegate(std::unique_ptr< Delegate, Deleter > delegate)
TfLiteStatus
\warning This is an experimental API and subject to change.
ModifyGraphWithDelegate(std::unique_ptr< TfLiteDelegate > delegate)=delete
TfLiteStatus
This overload is never OK.
OpProfilingString(const TfLiteRegistration & op_reg, const TfLiteNode *node) const
const char *
Retrieve an operator's description of its work, for profiling purposes.
ReleaseNonPersistentMemory()
TfLiteStatus
\warning Experimental interface, subject to change.
ResetVariableTensors()
TfLiteStatus
\warning This is an experimental API and subject to change.
ResizeInputTensor(int tensor_index, const std::vector< int > & dims)
TfLiteStatus
Change the dimensionality of a given tensor.
ResizeInputTensorStrict(int tensor_index, const std::vector< int > & dims)
TfLiteStatus
Change the dimensionality of a given tensor.
SetAllowBufferHandleOutput(bool allow_buffer_handle_output)
void
\warning This is an experimental API and subject to change.
SetAllowFp16PrecisionForFp32(bool allow)
void
Allow float16 precision for FP32 calculation when possible.
SetBufferHandle(int tensor_index, TfLiteBufferHandle buffer_handle, TfLiteDelegate *delegate)
TfLiteStatus
\warning This is an experimental API and subject to change.
SetBufferHandle(TfLiteTensor *tensor, TfLiteBufferHandle buffer_handle, TfLiteDelegate *delegate)
TfLiteStatus
\warning This is an experimental API and subject to change.
SetCancellationFunction(void *data, bool(*)(void *) check_cancelled_func)
void
\warning This is an experimental API and subject to change.
SetCustomAllocationForTensor(int tensor_index, const TfLiteCustomAllocation & allocation, int64_t flags)
TfLiteStatus
Assigns (or reassigns) a custom memory allocation for the given tensor.
SetExternalContext(TfLiteExternalContextType type, TfLiteExternalContext *ctx)
void
SetNumThreads(int num_threads)
TfLiteStatus
Set the number of threads available to the interpreter.
SetProfiler(Profiler *profiler)
void
\warning This is an experimental API and subject to change.
SetProfiler(std::unique_ptr< Profiler > profiler)
void
\warning This is an experimental API and subject to change.
error_reporter() const
\warning Experimental interface, subject to change.
execution_plan() const
const std::vector< int > &
\warning Experimental interface, subject to change.
input_tensor(size_t index)
TfLiteTensor *
Return a mutable pointer to the given input tensor.
input_tensor(size_t index) const
const TfLiteTensor *
Return an immutable pointer to the given input tensor.
input_tensor_by_signature(const char *signature_input_name, const char *signature_key)
TfLiteTensor *
\warning Experimental interface, subject to change.
inputs() const
const std::vector< int > &
Read only access to list of inputs.
node_and_registration(int node_index) const
const std::pair< TfLiteNode, TfLiteRegistration > *
Returns a pointer to an operation and registration data structure if in bounds from the primary subgraph(subgraph_[0]).
node_and_registration(int subgraph_index, int node_index) const
const std::pair< TfLiteNode, TfLiteRegistration > *
Returns a pointer to an operation and registration data structure if in bounds.
nodes_size() const
size_t
Return the number of ops in the model.
operator=(const Interpreter &)=delete
output_tensor(size_t index)
TfLiteTensor *
Return a mutable pointer to the given output tensor.
output_tensor(size_t index) const
const TfLiteTensor *
Return an immutable pointer to the given output tensor.
output_tensor_by_signature(const char *signature_output_name, const char *signature_key) const
const TfLiteTensor *
\warning Experimental interface, subject to change.
outputs() const
const std::vector< int > &
Read only access to list of outputs.
signature_inputs(const char *signature_key) const
const std::map< std::string, uint32_t > &
\warning Experimental interface, subject to change.
signature_keys() const
std::vector< const std::string * >
\warning Experimental interface, subject to change.
signature_outputs(const char *signature_key) const
const std::map< std::string, uint32_t > &
\warning Experimental interface, subject to change.
tensor(int tensor_index)
TfLiteTensor *
Get a mutable tensor data structure.
tensor(int tensor_index) const
const TfLiteTensor *
Get an immutable tensor data structure.
tensors_size() const
size_t
Return the number of tensors in the model.
typed_input_tensor(int index)
T *
Return a mutable pointer into the data of a given input tensor.
typed_input_tensor(int index) const
const T *
Return an immutable pointer into the data of a given input tensor.
typed_output_tensor(int index)
T *
Return a mutable pointer into the data of a given output tensor.
typed_output_tensor(int index) const
const T *
Return an immutable pointer into the data of a given output tensor.
typed_tensor(int tensor_index)
T *
Perform a checked cast to the appropriate tensor type (mutable pointer version).
typed_tensor(int tensor_index) const
const T *
Perform a checked cast to the appropriate tensor type (immutable pointer version).
variables() const
const std::vector< int > &
Read only access to list of variable tensors.

Public types

TfLiteDelegatePtr

std::unique_ptr< TfLiteDelegate, void(*)(TfLiteDelegate *)> TfLiteDelegatePtr

Public static attributes

kTensorsCapacityHeadroom

constexpr int kTensorsCapacityHeadroom = 16

The capacity headroom of tensors_ vector before calling ops' prepare and invoke function.

In these functions, it's guaranteed allocating up to kTensorsCapacityHeadroom more tensors won't invalidate pointers to existing tensors.

kTensorsReservedCapacity

constexpr int kTensorsReservedCapacity = 128

Friend classes

tflite::impl::InterpreterBuilder

friend class tflite::impl::InterpreterBuilder

Public functions

AddProfiler

void AddProfiler(
  Profiler *profiler
)

\warning This is an experimental API and subject to change.

\n Adds the profiler to tracing execution. The caller retains ownership of the profiler and must ensure its validity. nullptr profiler will be ignored.

AddProfiler

void AddProfiler(
  std::unique_ptr< Profiler > profiler
)

\warning This is an experimental API and subject to change.

\n Adds the profiler to tracing execution. Transfers ownership of the profiler to the interpreter. nullptr profiler will be ignored.

AllocateTensors

TfLiteStatus AllocateTensors()

Update allocations for all tensors.

This will redim dependent tensors using the input tensor dimensionality as given. This is relatively expensive. This must be called after the interpreter has been created and before running inference (and accessing tensor buffers), and must be called again if (and only if) an input tensor is resized. Returns status of success or failure. Will fail if any of the ops in the model (other than those which were rewritten by delegates, if any) are not supported by the Interpreter's OpResolver.

ApplyOptions

TfLiteStatus ApplyOptions(
  InterpreterOptions *options
)

\warning This is an experimental API and subject to change.

\n Apply InterpreterOptions which tunes behavior of the interpreter.

Cancel

TfLiteStatus Cancel()

\warning This is an experimental API and subject to change.

\n Attempts to cancel in flight invocation if any. This will not affect Invokes that happends after the cancellation. Non blocking. Thread safe. Returns kTfLiteError if cancellation is not enabled, otherwise returns kTfLiteOk.

EnsureTensorDataIsReadable

TfLiteStatus EnsureTensorDataIsReadable(
  int tensor_index
)

\warning This is an experimental API and subject to change.

\n Ensure the data in tensor.data is readable. In case delegate is used, it might require to copy the data from delegate buffer to raw memory.

GetAllowFp16PrecisionForFp32

bool GetAllowFp16PrecisionForFp32() const 

\warning Experimental interface, subject to change.

\n Get the half precision flag.

GetAsyncSignatureRunner

async::AsyncSignatureRunner * GetAsyncSignatureRunner(
  const char *signature_key
)

\warning Experimental interface, subject to change.

\n Returns a pointer to the AsyncSignatureRunner instance to run the part of the graph identified by a SignatureDef. The nullptr is returned if the given signature key is not valid. The async delegate should be applied before calling this function.

GetBufferHandle

TfLiteStatus GetBufferHandle(
  int tensor_index,
  TfLiteBufferHandle *buffer_handle,
  TfLiteDelegate **delegate
)

\warning This is an experimental API and subject to change.

\n Get the delegate buffer handle, and the delegate which can process the buffer handle.

GetInputName

const char * GetInputName(
  int index
) const 

Return the name of a given input.

The given index must be between 0 and inputs().size().

GetOutputName

const char * GetOutputName(
  int index
) const 

Return the name of a given output.

The given index must be between 0 and outputs().size().

GetProfiler

Profiler * GetProfiler()

\warning This is an experimental API and subject to change.

\n Gets the profiler used for op tracing.

GetSignatureRunner

SignatureRunner * GetSignatureRunner(
  const char *signature_key
)

\warning Experimental interface, subject to change.

\n Returns a pointer to the SignatureRunner instance to run the part of the graph identified by a SignatureDef. The nullptr is returned if the given signature key is not valid. If you need to specify delegates, you have to do that before calling this function. This function will additionally apply default delegates. Thus, applying delegates after that might lead to undesirable behaviors. Note, the pointed instance has lifetime same as the Interpreter object and the SignatureRunner class is not thread-safe.

GetSubgraphIndexFromSignature

int GetSubgraphIndexFromSignature(
  const char *signature_key
) const 

\warning Experimental interface, subject to change.

\n Return the subgraph index that corresponds to a SignatureDef, defined by 'signature_key'. If invalid name passed, -1 will be returned.

Interpreter

 Interpreter(
  ErrorReporter *error_reporter
)

Interpreter

 Interpreter(
  const Interpreter &
)=delete

Invoke

TfLiteStatus Invoke()

Invoke the interpreter (run the whole graph in dependency order).

NOTE: It is possible that the interpreter is not in a ready state to evaluate (i.e. if a ResizeTensor() has been performed without an AllocateTensors(). Returns status of success or failure.

ModifyGraphWithDelegate

TfLiteStatus ModifyGraphWithDelegate(
  TfLiteDelegate *delegate
)

Allow a delegate to look at the graph and modify the graph to handle parts of the graph themselves.

After this is called, the graph may contain new nodes that replace 1 more nodes. 'delegate' must outlive the interpreter. Returns one of the following status codes:

  1. kTfLiteOk: Success.
  2. kTfLiteDelegateError: Delegation failed due to an error in the delegate, or the delegate parameter was null. The Interpreter has been restored to its pre-delegation state. NOTE: This undoes all delegates previously applied to the Interpreter.
  3. kTfLiteApplicationError : Delegation failed to be applied due to the incompatibility with the TfLite runtime, e.g., the model graph is already immutable when applying the delegate. However, the interpreter could still be invoked.
  4. kTfLiteUnresolvedOps: Delegation failed because the model has an operator that cannot be resolved. This can happen when the op is not registered or built with the TF Lite framework.
  5. kTfLiteError: Unexpected/runtime failure. \n \warning This is an experimental API and subject to change. \n

ModifyGraphWithDelegate

TfLiteStatus ModifyGraphWithDelegate(
  std::unique_ptr< Delegate, Deleter > delegate
)

\warning This is an experimental API and subject to change.

\n Same as ModifyGraphWithDelegate except this interpreter takes ownership of the provided delegate.

ModifyGraphWithDelegate

TfLiteStatus ModifyGraphWithDelegate(
  std::unique_ptr< TfLiteDelegate > delegate
)=delete

This overload is never OK.

TfLiteDelegate is a C structure, so it has no virtual destructor. The default deleter of the unique_ptr does not know how to delete C++ objects deriving from TfLiteDelegate.

OpProfilingString

const char * OpProfilingString(
  const TfLiteRegistration & op_reg,
  const TfLiteNode *node
) const 

Retrieve an operator's description of its work, for profiling purposes.

ReleaseNonPersistentMemory

TfLiteStatus ReleaseNonPersistentMemory()

\warning Experimental interface, subject to change.

\n This releases memory held by non-persistent tensors. It does NOT re-perform memory planning. AllocateTensors needs to be called before next invocation.

ResetVariableTensors

TfLiteStatus ResetVariableTensors()

\warning This is an experimental API and subject to change.

\n Reset all variable tensors to the default value. If a variable tensor doesn't have a buffer, reset it to zero. TODO(b/115961645): Implement - If a variable tensor has a buffer, reset it to the value of the buffer.

ResizeInputTensor

TfLiteStatus ResizeInputTensor(
  int tensor_index,
  const std::vector< int > & dims
)

Change the dimensionality of a given tensor.

Note, this is only acceptable for tensor indices that are inputs or variables. Returns status of failure or success. Note that this doesn't actually resize any existing buffers. A call to AllocateTensors() is required to change the tensor input buffer.

ResizeInputTensorStrict

TfLiteStatus ResizeInputTensorStrict(
  int tensor_index,
  const std::vector< int > & dims
)

Change the dimensionality of a given tensor.

This is only acceptable for tensor indices that are inputs or variables. Only unknown dimensions can be resized with this function. Unknown dimensions are indicated as -1 in the dims_signature attribute of a TfLiteTensor. Returns status of failure or success. Note that this doesn't actually resize any existing buffers. A call to AllocateTensors() is required to change the tensor input buffer.

SetAllowBufferHandleOutput

void SetAllowBufferHandleOutput(
  bool allow_buffer_handle_output
)

\warning This is an experimental API and subject to change.

\n Set if buffer handle output is allowed.

When using hardware delegation, Interpreter will make the data of output tensors available in tensor->data by default. If the application can consume the buffer handle directly (e.g. reading output from OpenGL texture), it can set this flag to false, so Interpreter won't copy the data from buffer handle to CPU memory.

SetAllowFp16PrecisionForFp32

void SetAllowFp16PrecisionForFp32(
  bool allow
)

Allow float16 precision for FP32 calculation when possible.

Default: not allow.

WARNING: This API is deprecated: prefer controlling this via delegate options, e.g. `tflite::StatefulNnApiDelegate::Options::allow_fp16' or TfLiteGpuDelegateOptionsV2::is_precision_loss_allowed. This method will be removed in a future release.

SetBufferHandle

TfLiteStatus SetBufferHandle(
  int tensor_index,
  TfLiteBufferHandle buffer_handle,
  TfLiteDelegate *delegate
)

\warning This is an experimental API and subject to change.

\n Set the delegate buffer handle to a tensor. It can be called in the following cases:

  1. Set the buffer handle to a tensor that's not being written by a delegate. For example, feeding an OpenGL texture as the input of the inference graph.
  2. Set the buffer handle to a tensor that uses the same delegate. For example, set an OpenGL texture as the output of inference, while the node which produces output is an OpenGL delegate node.

SetBufferHandle

TfLiteStatus SetBufferHandle(
  TfLiteTensor *tensor,
  TfLiteBufferHandle buffer_handle,
  TfLiteDelegate *delegate
)

\warning This is an experimental API and subject to change.

\n Set the delegate buffer handle to the given tensor.

SetCancellationFunction

void SetCancellationFunction(
  void *data,
  bool(*)(void *) check_cancelled_func
)

\warning This is an experimental API and subject to change.

\n Sets the cancellation function pointer in order to cancel a request in the middle of a call to Invoke(). The interpreter queries this function during inference, between op invocations; when it returns true, the interpreter will abort execution and return kTfLiteError. The data parameter contains any data used by the cancellation function, and if non-null, remains owned by the caller.

SetCustomAllocationForTensor

TfLiteStatus SetCustomAllocationForTensor(
  int tensor_index,
  const TfLiteCustomAllocation & allocation,
  int64_t flags
)

Assigns (or reassigns) a custom memory allocation for the given tensor.

flags is a bitmask, see TfLiteCustomAllocationFlags. The runtime does NOT take ownership of the underlying memory.

NOTE: User needs to call AllocateTensors() after this. Invalid/insufficient buffers will cause an error during AllocateTensors or Invoke (in case of dynamic shapes in the graph).

Parameters should satisfy the following conditions:

  1. tensor->allocation_type == kTfLiteArenaRw or kTfLiteArenaRwPersistent In general, this is true for I/O tensors & variable tensors.
  2. allocation->data has the appropriate permissions for runtime access (Read-only for inputs, Read-Write for others), and outlives Interpreter.
  3. allocation->bytes >= tensor->bytes. This condition is checked again if any tensors are resized.
  4. allocation->data should be aligned to kDefaultTensorAlignment defined in lite/util.h. (Currently 64 bytes) This check is skipped if kTfLiteCustomAllocationFlagsSkipAlignCheck is set through flags. \warning This is an experimental API and subject to change. \n

SetExternalContext

void SetExternalContext(
  TfLiteExternalContextType type,
  TfLiteExternalContext *ctx
)

SetNumThreads

TfLiteStatus SetNumThreads(
  int num_threads
)

Set the number of threads available to the interpreter.

NOTE: num_threads should be >= -1. Setting num_threads to 0 has the effect to disable multithreading, which is equivalent to setting num_threads to 1. If set to the value -1, the number of threads used will be implementation-defined and platform-dependent.

As TfLite interpreter could internally apply a TfLite delegate by default (i.e. XNNPACK), the number of threads that are available to the default delegate should be set via InterpreterBuilder APIs as follows:

std::unique_ptr interpreter;
tflite::InterpreterBuilder builder(tflite model, op resolver);
builder.SetNumThreads(...)
ASSERT_EQ(builder(&interpreter), kTfLiteOk);

WARNING: This API is deprecated: prefer using InterpreterBuilder::SetNumThreads, as documented above.

SetProfiler

void SetProfiler(
  Profiler *profiler
)

\warning This is an experimental API and subject to change.

\n Sets the profiler to tracing execution. The caller retains ownership of the profiler and must ensure its validity. Previously registered profilers will be unregistered. If profiler is nullptr, all previously installed profilers will be removed.

SetProfiler

void SetProfiler(
  std::unique_ptr< Profiler > profiler
)

\warning This is an experimental API and subject to change.

\n Same as SetProfiler except this interpreter takes ownership of the provided profiler. Previously registered profilers will be unregistered. If profiler is nullptr, all previously installed profilers will be removed.

error_reporter

ErrorReporter * error_reporter() const 

\warning Experimental interface, subject to change.

\n Get the error reporter associated with this interpreter.

execution_plan

const std::vector< int > & execution_plan() const 

\warning Experimental interface, subject to change.

input_tensor

TfLiteTensor * input_tensor(
  size_t index
)

Return a mutable pointer to the given input tensor.

The given index must be between 0 and inputs().size().

input_tensor

const TfLiteTensor * input_tensor(
  size_t index
) const 

Return an immutable pointer to the given input tensor.

The given index must be between 0 and inputs().size().

input_tensor_by_signature

TfLiteTensor * input_tensor_by_signature(
  const char *signature_input_name,
  const char *signature_key
)

\warning Experimental interface, subject to change.

\n Returns the input tensor identified by 'signature_input_name' in the signature identified by 'signature_key'. Returns nullptr if not found.

inputs

const std::vector< int > & inputs() const 

Read only access to list of inputs.

node_and_registration

const std::pair< TfLiteNode, TfLiteRegistration > * node_and_registration(
  int node_index
) const 

Returns a pointer to an operation and registration data structure if in bounds from the primary subgraph(subgraph_[0]).

node_and_registration

const std::pair< TfLiteNode, TfLiteRegistration > * node_and_registration(
  int subgraph_index,
  int node_index
) const 

Returns a pointer to an operation and registration data structure if in bounds.

nodes_size

size_t nodes_size() const 

Return the number of ops in the model.

operator=

Interpreter & operator=(
  const Interpreter &
)=delete

output_tensor

TfLiteTensor * output_tensor(
  size_t index
)

Return a mutable pointer to the given output tensor.

The given index must be between 0 and outputs().size().

output_tensor

const TfLiteTensor * output_tensor(
  size_t index
) const 

Return an immutable pointer to the given output tensor.

The given index must be between 0 and outputs().size().

output_tensor_by_signature

const TfLiteTensor * output_tensor_by_signature(
  const char *signature_output_name,
  const char *signature_key
) const 

\warning Experimental interface, subject to change.

\n Returns the output tensor identified by 'signature_output_name' in the signature identified by 'signature_key'. Returns nullptr if not found.

outputs

const std::vector< int > & outputs() const 

Read only access to list of outputs.

signature_inputs

const std::map< std::string, uint32_t > & signature_inputs(
  const char *signature_key
) const 

\warning Experimental interface, subject to change.

\n Returns the mapping of inputs to tensor index in the signature specified through 'signature_key'. If invalid name passed, an empty list will be returned.

signature_keys

std::vector< const std::string * > signature_keys() const 

\warning Experimental interface, subject to change.

\n Returns list of all keys of different method signatures defined in the model. Note, pointers returned have lifetime same as the Interpreter object.

signature_outputs

const std::map< std::string, uint32_t > & signature_outputs(
  const char *signature_key
) const 

\warning Experimental interface, subject to change.

\n Returns the mapping of outputs to tensor index in the signature specified through 'signature_key'. If invalid name passed, an empty list will be returned.

tensor

TfLiteTensor * tensor(
  int tensor_index
)

Get a mutable tensor data structure.

tensor

const TfLiteTensor * tensor(
  int tensor_index
) const 

Get an immutable tensor data structure.

tensors_size

size_t tensors_size() const 

Return the number of tensors in the model.

typed_input_tensor

T * typed_input_tensor(
  int index
)

Return a mutable pointer into the data of a given input tensor.

The given index must be between 0 and inputs().size().

typed_input_tensor

const T * typed_input_tensor(
  int index
) const 

Return an immutable pointer into the data of a given input tensor.

The given index must be between 0 and inputs().size().

typed_output_tensor

T * typed_output_tensor(
  int index
)

Return a mutable pointer into the data of a given output tensor.

The given index must be between 0 and outputs().size().

typed_output_tensor

const T * typed_output_tensor(
  int index
) const 

Return an immutable pointer into the data of a given output tensor.

The given index must be between 0 and outputs().size().

typed_tensor

T * typed_tensor(
  int tensor_index
)

Perform a checked cast to the appropriate tensor type (mutable pointer version).

typed_tensor

const T * typed_tensor(
  int tensor_index
) const 

Perform a checked cast to the appropriate tensor type (immutable pointer version).

variables

const std::vector< int > & variables() const 

Read only access to list of variable tensors.

~Interpreter

 ~Interpreter()