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 <memory> 20 #include <string> 21 #include <vector> 22 23 #include "tensorflow/lite/model.h" 24 #include "tensorflow/lite/profiling/profile_summarizer.h" 25 #include "tensorflow/lite/tools/benchmark/benchmark_model.h" 26 27 namespace tflite { 28 namespace benchmark { 29 30 // Dumps profiling events if profiling is enabled. 31 class ProfilingListener : public BenchmarkListener { 32 public: ProfilingListener()33 explicit ProfilingListener() : interpreter_(nullptr), has_profiles_(false) {} 34 35 void SetInterpreter(Interpreter* interpreter); 36 37 void OnSingleRunStart(RunType run_type) override; 38 39 void OnSingleRunEnd() override; 40 41 void OnBenchmarkEnd(const BenchmarkResults& results) override; 42 43 private: 44 Interpreter* interpreter_; 45 profiling::Profiler profiler_; 46 profiling::ProfileSummarizer summarizer_; 47 bool has_profiles_; 48 }; 49 50 // Dumps gemmlowp profiling events if gemmlowp profiling is enabled. 51 class GemmlowpProfilingListener : public BenchmarkListener { 52 public: ~GemmlowpProfilingListener()53 virtual ~GemmlowpProfilingListener() {} 54 55 void OnBenchmarkStart(const BenchmarkParams& params) override; 56 57 void OnBenchmarkEnd(const BenchmarkResults& results) override; 58 }; 59 60 // Benchmarks a TFLite model by running tflite interpreter. 61 class BenchmarkTfLiteModel : public BenchmarkModel { 62 public: 63 struct InputLayerInfo { 64 std::string name; 65 std::vector<int> shape; 66 }; 67 68 BenchmarkTfLiteModel(); 69 explicit BenchmarkTfLiteModel(BenchmarkParams params); 70 virtual ~BenchmarkTfLiteModel(); 71 72 std::vector<Flag> GetFlags() override; 73 void LogParams() override; 74 bool ValidateParams() override; 75 uint64_t ComputeInputBytes() override; 76 void Init() override; 77 void RunImpl() override; SetDelegates(std::vector<std::unique_ptr<TfLiteDelegate>> delegates)78 void SetDelegates(std::vector<std::unique_ptr<TfLiteDelegate>> delegates) { 79 delegates_ = std::move(delegates); 80 } 81 82 protected: 83 static BenchmarkParams DefaultParams(); 84 void PrepareInputData() override; 85 void ResetInputsAndOutputs() override; 86 void CleanUp(); 87 88 // Allows installation of custom delegates during initialization 89 virtual void ApplyDelegates(); 90 91 std::unique_ptr<tflite::FlatBufferModel> model; 92 std::unique_ptr<tflite::Interpreter> interpreter; 93 94 private: 95 struct InputTensorData { 96 TfLitePtrUnion data; 97 size_t bytes; 98 }; 99 std::vector<InputLayerInfo> inputs; 100 std::vector<InputTensorData> inputs_data_; 101 ProfilingListener profiling_listener_; 102 GemmlowpProfilingListener gemmlowp_profiling_listener_; 103 std::vector<std::unique_ptr<TfLiteDelegate>> delegates_; 104 }; 105 106 } // namespace benchmark 107 } // namespace tflite 108 109 #endif // TENSORFLOW_LITE_TOOLS_BENCHMARK_BENCHMARK_TFLITE_MODEL_H_ 110