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 #include "tensorflow/lite/profiling/profile_summary_formatter.h"
17
18 #include <memory>
19 #include <sstream>
20 #include <string>
21
22 namespace tflite {
23 namespace profiling {
24
GetOutputString(const std::map<uint32_t,std::unique_ptr<tensorflow::StatsCalculator>> & stats_calculator_map,const tensorflow::StatsCalculator & delegate_stats_calculator) const25 std::string ProfileSummaryDefaultFormatter::GetOutputString(
26 const std::map<uint32_t, std::unique_ptr<tensorflow::StatsCalculator>>&
27 stats_calculator_map,
28 const tensorflow::StatsCalculator& delegate_stats_calculator) const {
29 return GenerateReport("profile", /*include_output_string*/ true,
30 stats_calculator_map, delegate_stats_calculator);
31 }
32
GetShortSummary(const std::map<uint32_t,std::unique_ptr<tensorflow::StatsCalculator>> & stats_calculator_map,const tensorflow::StatsCalculator & delegate_stats_calculator) const33 std::string ProfileSummaryDefaultFormatter::GetShortSummary(
34 const std::map<uint32_t, std::unique_ptr<tensorflow::StatsCalculator>>&
35 stats_calculator_map,
36 const tensorflow::StatsCalculator& delegate_stats_calculator) const {
37 return GenerateReport("summary", /*include_output_string*/ false,
38 stats_calculator_map, delegate_stats_calculator);
39 }
40
GenerateReport(const std::string & tag,bool include_output_string,const std::map<uint32_t,std::unique_ptr<tensorflow::StatsCalculator>> & stats_calculator_map,const tensorflow::StatsCalculator & delegate_stats_calculator) const41 std::string ProfileSummaryDefaultFormatter::GenerateReport(
42 const std::string& tag, bool include_output_string,
43 const std::map<uint32_t, std::unique_ptr<tensorflow::StatsCalculator>>&
44 stats_calculator_map,
45 const tensorflow::StatsCalculator& delegate_stats_calculator) const {
46 std::stringstream stream;
47 bool has_non_primary_graph =
48 (stats_calculator_map.size() - stats_calculator_map.count(0)) > 0;
49 for (const auto& stats_calc : stats_calculator_map) {
50 auto subgraph_index = stats_calc.first;
51 auto subgraph_stats = stats_calc.second.get();
52 if (has_non_primary_graph) {
53 if (subgraph_index == 0) {
54 stream << "Primary graph " << tag << ":" << std::endl;
55 } else {
56 stream << "Subgraph (index: " << subgraph_index << ") " << tag << ":"
57 << std::endl;
58 }
59 }
60 if (include_output_string) {
61 stream << subgraph_stats->GetOutputString();
62 }
63 if (subgraph_index != 0) {
64 stream << "Subgraph (index: " << subgraph_index << ") ";
65 }
66 stream << subgraph_stats->GetShortSummary() << std::endl;
67 }
68
69 if (delegate_stats_calculator.num_runs() > 0) {
70 stream << "Delegate internal: " << std::endl;
71 if (include_output_string) {
72 stream << delegate_stats_calculator.GetOutputString();
73 }
74 stream << delegate_stats_calculator.GetShortSummary() << std::endl;
75 }
76
77 return stream.str();
78 }
79
80 tensorflow::StatSummarizerOptions
GetStatSummarizerOptions() const81 ProfileSummaryDefaultFormatter::GetStatSummarizerOptions() const {
82 auto options = tensorflow::StatSummarizerOptions();
83 // Summary will be manually handled per subgraphs in order to keep the
84 // compatibility.
85 options.show_summary = false;
86 options.show_memory = false;
87 return options;
88 }
89
90 tensorflow::StatSummarizerOptions
GetStatSummarizerOptions() const91 ProfileSummaryCSVFormatter::GetStatSummarizerOptions() const {
92 auto options = ProfileSummaryDefaultFormatter::GetStatSummarizerOptions();
93 options.format_as_csv = true;
94 return options;
95 }
96
97 } // namespace profiling
98 } // namespace tflite
99