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