1 | /* Copyright 2016 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_CORE_UTIL_STAT_SUMMARIZER_H_ |
17 | #define TENSORFLOW_CORE_UTIL_STAT_SUMMARIZER_H_ |
18 | |
19 | #include <stdlib.h> |
20 | |
21 | #include <cmath> |
22 | #include <limits> |
23 | #include <map> |
24 | #include <memory> |
25 | #include <sstream> |
26 | #include <string> |
27 | |
28 | #include "tensorflow/core/framework/tensor.h" |
29 | #include "tensorflow/core/framework/types.pb.h" |
30 | #include "tensorflow/core/platform/types.h" |
31 | #include "tensorflow/core/util/stat_summarizer_options.h" |
32 | #include "tensorflow/core/util/stats_calculator.h" |
33 | |
34 | namespace tensorflow { |
35 | |
36 | class GraphDef; |
37 | class StepStats; |
38 | class NodeExecStats; |
39 | |
40 | // A StatSummarizer assists in performance analysis of Graph executions. |
41 | // |
42 | // It summarizes time spent executing (on GPU/CPU), memory used etc. across |
43 | // multiple executions of a single Graph from the StepStats collected during |
44 | // graph execution. |
45 | // |
46 | // See tensorflow/tools/benchmark/benchmark_model.cc for an example usage. |
47 | class StatSummarizer { |
48 | public: |
49 | explicit StatSummarizer(const StatSummarizerOptions& options); |
50 | |
51 | // Deprecated: Use StatSummarizer(const StatSummarizerOptions&) instead. The |
52 | // GraphDef is not needed by the StatSummarizer. |
53 | explicit StatSummarizer(const tensorflow::GraphDef& tensorflow_graph); |
54 | |
55 | ~StatSummarizer(); |
56 | |
57 | // Adds another run's StepStats output to the aggregate counts. |
58 | void ProcessStepStats(const StepStats& step_stats); |
59 | |
60 | // Returns a string detailing the accumulated runtime stats in a tab-separated |
61 | // format which can be pasted into a spreadsheet for further analysis. |
62 | std::string GetOutputString() const { |
63 | return stats_calculator_->GetOutputString(); |
64 | } |
65 | |
66 | std::string ShortSummary() const { |
67 | return stats_calculator_->GetShortSummary(); |
68 | } |
69 | |
70 | // Prints the string returned by GetOutputString(). |
71 | void PrintStepStats() const; |
72 | |
73 | // Prints the output tensor sizes and types for each node. |
74 | void PrintOutputs() const; |
75 | |
76 | void ComputeStatsByType( |
77 | std::map<std::string, int64_t>* node_type_map_count, |
78 | std::map<std::string, int64_t>* node_type_map_time, |
79 | std::map<std::string, int64_t>* node_type_map_memory, |
80 | std::map<std::string, int64_t>* node_type_map_times_called, |
81 | int64_t* accumulated_us) const { |
82 | stats_calculator_->ComputeStatsByType( |
83 | node_type_map_count, node_type_map_time, node_type_map_memory, |
84 | node_type_map_times_called, accumulated_us); |
85 | } |
86 | |
87 | std::string GetStatsByNodeType() const { |
88 | return stats_calculator_->GetStatsByNodeType(); |
89 | } |
90 | |
91 | std::string GetStatsByMetric(const string& title, |
92 | StatsCalculator::SortingMetric sorting_metric, |
93 | int num_stats) const { |
94 | return stats_calculator_->GetStatsByMetric(title, sorting_metric, |
95 | num_stats); |
96 | } |
97 | |
98 | int num_runs() const { return stats_calculator_->num_runs(); } |
99 | |
100 | // Returns stats of total microseconds spent by all nodes in each run. |
101 | const Stat<int64_t>& run_total_us() const { |
102 | return stats_calculator_->run_total_us(); |
103 | } |
104 | |
105 | private: |
106 | void Validate(const std::vector<TensorDescription>* outputs, |
107 | const NodeExecStats& ns) const; |
108 | |
109 | std::map<std::string, std::vector<TensorDescription> > outputs_; |
110 | |
111 | std::unique_ptr<StatsCalculator> stats_calculator_; |
112 | }; |
113 | |
114 | } // namespace tensorflow |
115 | |
116 | #endif // TENSORFLOW_CORE_UTIL_STAT_SUMMARIZER_H_ |
117 | |