16 #ifndef TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_TFLITE_INTERPRETER_POOL_H_
17 #define TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_TFLITE_INTERPRETER_POOL_H_
22 #include "absl/base/thread_annotations.h"
23 #include "absl/synchronization/mutex.h"
24 #include "tensorflow/core/framework/tensor.h"
25 #include "tensorflow/core/lib/gtl/array_slice.h"
26 #include "tensorflow/core/platform/cpu_info.h"
27 #include "tensorflow/core/platform/tstring.h"
28 #include "tensorflow/core/public/session_options.h"
29 #include "tensorflow/lite/c/common.h"
30 #include "tensorflow/lite/model.h"
32 #ifndef TFLITE_PROFILE_EVENTS
33 #define TFLITE_PROFILE_EVENTS 2000
35 #include "tensorflow/lite/profiling/buffered_profiler.h"
36 #include "tensorflow/lite/profiling/profile_summarizer.h"
37 #include "tensorflow/lite/profiling/profile_summary_formatter.h"
39 #include "tensorflow/lite/string_util.h"
41 namespace tensorflow {
45 constexpr
int kInitialBatchSize = 500;
53 static Status CreateTfLiteInterpreterWrapper(
54 const tflite::FlatBufferModel& model,
55 const tensorflow::SessionOptions& options,
56 std::unique_ptr<TfLiteInterpreterWrapper>& wrapper);
60 std::unique_ptr<tflite::ExternalCpuBackendContext> external_context,
61 std::unique_ptr<tflite::Interpreter> interpreter);
67 tflite::Interpreter* Get() {
return interpreter_.get(); }
70 int GetBatchSize() {
return batch_size_; }
73 void SetBatchSize(
int batch_size) { batch_size_ = batch_size; }
76 TfLiteStatus Invoke();
78 void WriteOutput(
const std::string& header,
const string& data,
79 std::ostream* stream) {
80 (*stream) << header << std::endl;
81 (*stream) << data << std::endl;
84 void WriteProfileData() {
85 if (run_summarizer_.HasProfiles()) {
86 WriteOutput(
"Operator-wise Profiling Info for Regular Benchmark Runs:",
87 run_summarizer_.GetOutputString(), &std::cout);
97 tensorflow::Status SetStringData(
const std::vector<const Tensor*>& tensors,
98 TfLiteTensor* tflite_tensor,
99 int tensor_index,
int batch_size);
103 std::unique_ptr<tflite::ExternalCpuBackendContext> external_context_;
104 std::unique_ptr<tflite::Interpreter> interpreter_;
106 std::map<int, std::unique_ptr<char>> tensor_buffer_;
107 std::map<int, size_t> tensor_buffer_max_bytes_;
108 std::vector<int32_t> offset_;
109 #ifdef TFLITE_PROFILE
110 int max_num_entries_;
111 tflite::profiling::ProfileSummarizer run_summarizer_;
112 tflite::profiling::BufferedProfiler profiler_;
113 int invocation_count_ = 0;
124 static tensorflow::Status CreateTfLiteInterpreterPool(
125 const tflite::FlatBufferModel* model,
126 const tensorflow::SessionOptions& options,
int pool_size,
127 std::unique_ptr<TfLiteInterpreterPool>& interpreter_pool);
131 std::unique_ptr<TfLiteInterpreterWrapper> GetInterpreter() {
132 auto interpreter_available = [
this]() ABSL_SHARED_LOCKS_REQUIRED(mutex_) {
133 return !this->available_.empty();
135 mutex_.LockWhen(absl::Condition(&interpreter_available));
136 auto pool = std::move(available_.back());
137 available_.pop_back();
143 void ReturnInterpreter(
144 std::unique_ptr<TfLiteInterpreterWrapper> interpreter) {
145 absl::MutexLock l(&mutex_);
146 available_.emplace_back(std::move(interpreter));
151 std::vector<std::unique_ptr<TfLiteInterpreterWrapper>> interpreters)
152 : available_(std::move(interpreters)) {}
153 mutable absl::Mutex mutex_;
154 std::vector<std::unique_ptr<TfLiteInterpreterWrapper>> available_
155 ABSL_GUARDED_BY(mutex_);