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