tflite-micro/python/tflite_micro/interpreter_wrapper.h
Ryan Kuester 9664901486
feat(python): add alt decompression memory parameter to interpreter (#3614)
Add alt_decompression_memory_size parameter to the Python interpreter
API. When non-zero, allocates a separate memory region for DECODE
operator outputs and calls SetDecompressionMemory before AllocateTensors.

BUG=part of #3256
2026-07-06 16:38:39 +00:00

70 lines
2.7 KiB
C++

/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_LITE_MICRO_TOOLS_PYTHON_INTERPRETER_WRAPPER_H_
#define TENSORFLOW_LITE_MICRO_TOOLS_PYTHON_INTERPRETER_WRAPPER_H_
#include <Python.h>
#include "python/tflite_micro/python_ops_resolver.h"
#include "tensorflow/lite/micro/micro_allocator.h"
#include "tensorflow/lite/micro/micro_context.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/recording_micro_allocator.h"
namespace tflite {
// Allocation Recording is mutually exclusive from the PreserveAllTensors
// debugging feature because PreserveAllTensors uses the LinearMemoryPlanner.
// This means that the Allocations recorded by the RecordingMicroAllocator
// wouldn't be accurate because the GreedyMemoryPlanner would have to be used.
// So this Enum was made to represent the two possible modes/configs you can use
// the python interpreter for.
enum InterpreterConfig {
kAllocationRecording = 0,
kPreserveAllTensors = 1,
};
class InterpreterWrapper {
public:
InterpreterWrapper(
PyObject* model_data, const std::vector<std::string>& registerers_by_name,
size_t arena_size, int num_resource_variables,
InterpreterConfig config = InterpreterConfig::kAllocationRecording,
size_t alt_decompression_memory_size = 0);
~InterpreterWrapper();
void PrintAllocations();
int Invoke();
int Reset();
void SetInputTensor(PyObject* data, size_t index);
PyObject* GetOutputTensor(size_t index) const;
PyObject* GetInputTensorDetails(size_t index) const;
PyObject* GetOutputTensorDetails(size_t index) const;
PyObject* GetTensor(size_t tensor_index, size_t subgraph_index = 0);
private:
tflite::MicroAllocator* allocator_ = nullptr;
tflite::RecordingMicroAllocator* recording_allocator_ = nullptr;
const PyObject* model_;
std::unique_ptr<uint8_t[]> memory_arena_;
std::unique_ptr<uint8_t[]> alt_decompression_memory_;
tflite::MicroContext::AlternateMemoryRegion alt_decompression_region_;
tflite::PythonOpsResolver python_ops_resolver_;
tflite::MicroInterpreter* interpreter_;
};
} // namespace tflite
#endif // TENSORFLOW_LITE_MICRO_TOOLS_PYTHON_INTERPRETER_WRAPPER_H_