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 #include <string> 21 22 #include "tensorflow/lite/profiling/buffered_profiler.h" 23 #include "tensorflow/lite/profiling/profile_summarizer.h" 24 #include "tensorflow/lite/profiling/profile_summary_formatter.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: 33 ProfilingListener( 34 Interpreter* interpreter, uint32_t max_num_initial_entries, 35 bool allow_dynamic_buffer_increase, const std::string& csv_file_path = "", 36 std::shared_ptr<profiling::ProfileSummaryFormatter> summarizer_formatter = 37 std::make_shared<profiling::ProfileSummaryDefaultFormatter>()); 38 39 void OnBenchmarkStart(const BenchmarkParams& params) override; 40 41 void OnSingleRunStart(RunType run_type) override; 42 43 void OnSingleRunEnd() override; 44 45 void OnBenchmarkEnd(const BenchmarkResults& results) override; 46 47 protected: 48 profiling::ProfileSummarizer run_summarizer_; 49 profiling::ProfileSummarizer init_summarizer_; 50 std::string csv_file_path_; 51 52 private: 53 void WriteOutput(const std::string& header, const string& data, 54 std::ostream* stream); 55 Interpreter* interpreter_; 56 profiling::BufferedProfiler profiler_; 57 }; 58 59 } // namespace benchmark 60 } // namespace tflite 61 62 #endif // TENSORFLOW_LITE_TOOLS_BENCHMARK_PROFILING_LISTENER_H_ 63