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_PROFILING_PROFILE_SUMMARIZER_H_
17 #define TENSORFLOW_LITE_PROFILING_PROFILE_SUMMARIZER_H_
18 
19 #include <functional>
20 #include <memory>
21 #include <vector>
22 
23 #include "tensorflow/core/util/stats_calculator.h"
24 #include "tensorflow/lite/interpreter.h"
25 #include "tensorflow/lite/profiling/profile_buffer.h"
26 #include "tensorflow/lite/profiling/profile_summary_formatter.h"
27 
28 namespace tflite {
29 namespace profiling {
30 
31 // Creates a summary of operator invocations in the interpreter.
32 class ProfileSummarizer {
33  public:
34   explicit ProfileSummarizer(
35       std::shared_ptr<ProfileSummaryFormatter> summary_formatter =
36           std::make_shared<ProfileSummaryDefaultFormatter>());
~ProfileSummarizer()37   virtual ~ProfileSummarizer() {}
38 
39   // Process profile events to update statistics for operator invocations.
40   void ProcessProfiles(const std::vector<const ProfileEvent*>& profile_stats,
41                        const tflite::Interpreter& interpreter);
42 
43   // Returns a string detailing the accumulated runtime stats in the format of
44   // summary_formatter_.
GetOutputString()45   std::string GetOutputString() {
46     return summary_formatter_->GetOutputString(stats_calculator_map_,
47                                                *delegate_stats_calculator_);
48   }
49 
GetShortSummary()50   std::string GetShortSummary() {
51     return summary_formatter_->GetShortSummary(stats_calculator_map_,
52                                                *delegate_stats_calculator_);
53   }
54 
55   tensorflow::StatsCalculator* GetStatsCalculator(uint32_t subgraph_index);
56 
HasProfiles()57   bool HasProfiles() {
58     for (auto& stats_calc : stats_calculator_map_) {
59       auto subgraph_stats = stats_calc.second.get();
60       if (subgraph_stats->num_runs() >= 1) return true;
61     }
62     return false;
63   }
64 
65  private:
66   // Map storing stats per subgraph.
67   std::map<uint32_t, std::unique_ptr<tensorflow::StatsCalculator>>
68       stats_calculator_map_;
69 
70   std::unique_ptr<tensorflow::StatsCalculator> delegate_stats_calculator_;
71 
72   // Summary formatter for customized output formats.
73   std::shared_ptr<ProfileSummaryFormatter> summary_formatter_;
74 };
75 
76 }  // namespace profiling
77 }  // namespace tflite
78 
79 #endif  // TENSORFLOW_LITE_PROFILING_PROFILE_SUMMARIZER_H_
80