1 /* Copyright 2020 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_PROFILING_LISTENER_H_ 17 #define TENSORFLOW_LITE_TOOLS_BENCHMARK_PROFILING_LISTENER_H_ 18 19 #include <memory> 20 21 #include "tensorflow/lite/profiling/buffered_profiler.h" 22 #include "tensorflow/lite/profiling/profile_summarizer.h" 23 #include "tensorflow/lite/profiling/profile_summary_formatter.h" 24 #include "tensorflow/lite/tools/benchmark/benchmark_model.h" 25 26 namespace tflite { 27 namespace benchmark { 28 29 // Dumps profiling events if profiling is enabled. 30 class ProfilingListener : public BenchmarkListener { 31 public: 32 ProfilingListener( 33 Interpreter* interpreter, uint32_t max_num_entries, 34 const std::string& csv_file_path = "", 35 std::shared_ptr<profiling::ProfileSummaryFormatter> summarizer_formatter = 36 std::make_shared<profiling::ProfileSummaryDefaultFormatter>()); 37 38 void OnBenchmarkStart(const BenchmarkParams& params) override; 39 40 void OnSingleRunStart(RunType run_type) override; 41 42 void OnSingleRunEnd() override; 43 44 void OnBenchmarkEnd(const BenchmarkResults& results) override; 45 46 protected: 47 profiling::ProfileSummarizer run_summarizer_; 48 profiling::ProfileSummarizer init_summarizer_; 49 std::string csv_file_path_; 50 51 private: 52 void WriteOutput(const std::string& header, const string& data, 53 std::ostream* stream); 54 Interpreter* interpreter_; 55 profiling::BufferedProfiler profiler_; 56 }; 57 58 } // namespace benchmark 59 } // namespace tflite 60 61 #endif // TENSORFLOW_LITE_TOOLS_BENCHMARK_PROFILING_LISTENER_H_ 62