xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/tools/benchmark/benchmark_tflite_model.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_LITE_TOOLS_BENCHMARK_BENCHMARK_TFLITE_MODEL_H_
17 #define TENSORFLOW_LITE_TOOLS_BENCHMARK_BENCHMARK_TFLITE_MODEL_H_
18 
19 #include <algorithm>
20 #include <map>
21 #include <memory>
22 #include <random>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include "tensorflow/lite/model.h"
28 #include "tensorflow/lite/profiling/profiler.h"
29 #include "tensorflow/lite/tools/benchmark/benchmark_model.h"
30 #include "tensorflow/lite/tools/utils.h"
31 
32 namespace tflite {
33 namespace benchmark {
34 
35 // Splits the input_layer_name and input_layer_value_files and stores them in
36 // the name_file_pair. In the case of failures, return an error status, and the
37 // the state of name_file_pair is unchanged.
38 //
39 // BenchmarkTfLiteModel takes --input_layer_value_files flag, which is a comma-
40 // separated list of input_layer_name:input_value_file_path pairs,
41 // e.g. input1:/tmp/path.
42 //
43 // As TensorFlow allows ':' in the tensor names (e.g. input:0 to denote the
44 // output index), having ':' as the delimiter can break the benchmark code
45 // unexpectedly. To avoid this issue, we allow escaping ':' char with '\:' for
46 // this particular flag only. This function handles splitting the name and file
47 // path that contains escaped colon.
48 //
49 // For example, "input\:0:/tmp/path" will be divided into input:0 and /tmp/path.
50 TfLiteStatus SplitInputLayerNameAndValueFile(
51     const std::string& name_and_value_file,
52     std::pair<std::string, std::string>& name_file_pair);
53 
54 // Benchmarks a TFLite model by running tflite interpreter.
55 class BenchmarkTfLiteModel : public BenchmarkModel {
56  public:
57   struct InputLayerInfo {
InputLayerInfoInputLayerInfo58     InputLayerInfo() : has_value_range(false), low(0), high(0) {}
59 
60     std::string name;
61     std::vector<int> shape;
62 
63     // The input value is randomly generated when benchmarking the NN model.
64     // However, the NN model might require the value be limited to a certain
65     // range [low, high] for this particular input layer. For simplicity,
66     // support integer value first.
67     bool has_value_range;
68     int low;
69     int high;
70 
71     // The input value will be loaded from 'input_file_path' INSTEAD OF being
72     // randomly generated. Note the input file will be opened in binary mode.
73     std::string input_file_path;
74   };
75 
76   explicit BenchmarkTfLiteModel(BenchmarkParams params = DefaultParams());
77   ~BenchmarkTfLiteModel() override;
78 
79   std::vector<Flag> GetFlags() override;
80   void LogParams() override;
81   TfLiteStatus ValidateParams() override;
82   uint64_t ComputeInputBytes() override;
83   TfLiteStatus Init() override;
84   TfLiteStatus RunImpl() override;
85   static BenchmarkParams DefaultParams();
86 
87  protected:
88   TfLiteStatus PrepareInputData() override;
89   TfLiteStatus ResetInputsAndOutputs() override;
90 
91   int64_t MayGetModelFileSize() override;
92 
93   virtual TfLiteStatus LoadModel();
94 
95   // Allow subclasses to create a customized Op resolver during init.
96   virtual std::unique_ptr<tflite::OpResolver> GetOpResolver() const;
97 
98   // Allow subclass to initialize a customized tflite interpereter.
99   virtual TfLiteStatus InitInterpreter();
100 
101   // Create a BenchmarkListener that's specifically for TFLite profiling if
102   // necessary.
103   virtual std::unique_ptr<BenchmarkListener> MayCreateProfilingListener() const;
104 
105   void CleanUp();
106 
107   utils::InputTensorData LoadInputTensorData(
108       const TfLiteTensor& t, const std::string& input_file_path);
109 
110   std::vector<InputLayerInfo> inputs_;
111   std::vector<utils::InputTensorData> inputs_data_;
112   std::unique_ptr<tflite::FlatBufferModel> model_;
113   std::unique_ptr<tflite::Interpreter> interpreter_;
114   std::unique_ptr<tflite::ExternalCpuBackendContext> external_context_;
115 
116  private:
117   utils::InputTensorData CreateRandomTensorData(
118       const TfLiteTensor& t, const InputLayerInfo* layer_info);
119 
AddOwnedListener(std::unique_ptr<BenchmarkListener> listener)120   void AddOwnedListener(std::unique_ptr<BenchmarkListener> listener) {
121     if (listener == nullptr) return;
122     owned_listeners_.emplace_back(std::move(listener));
123     AddListener(owned_listeners_.back().get());
124   }
125 
126   std::vector<std::unique_ptr<BenchmarkListener>> owned_listeners_;
127   std::mt19937 random_engine_;
128   std::vector<Interpreter::TfLiteDelegatePtr> owned_delegates_;
129   // Always TFLITE_LOG the benchmark result.
130   BenchmarkLoggingListener log_output_;
131 };
132 
133 }  // namespace benchmark
134 }  // namespace tflite
135 
136 #endif  // TENSORFLOW_LITE_TOOLS_BENCHMARK_BENCHMARK_TFLITE_MODEL_H_
137