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 // Parent class and utilities for tfprof_graph and tfprof_scope.
17 
18 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_SHOW_H_
19 #define TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_SHOW_H_
20 
21 #include <algorithm>
22 #include <string>
23 #include <vector>
24 
25 #include "tensorflow/c/checkpoint_reader.h"
26 #include "tensorflow/core/framework/graph.pb.h"
27 #include "tensorflow/core/lib/core/errors.h"
28 #include "tensorflow/core/lib/strings/stringprintf.h"
29 #include "tensorflow/core/profiler/internal/tfprof_constants.h"
30 #include "tensorflow/core/profiler/internal/tfprof_node.h"
31 #include "tensorflow/core/profiler/internal/tfprof_node_show.h"
32 #include "tensorflow/core/profiler/internal/tfprof_tensor.h"
33 #include "tensorflow/core/profiler/internal/tfprof_timeline.h"
34 #include "tensorflow/core/profiler/internal/tfprof_utils.h"
35 #include "tensorflow/core/profiler/tfprof_options.h"
36 #include "tensorflow/core/profiler/tfprof_output.pb.h"
37 
38 namespace tensorflow {
39 namespace tfprof {
40 class TFShow {
41  public:
TFShow(checkpoint::CheckpointReader * ckpt_reader)42   explicit TFShow(checkpoint::CheckpointReader* ckpt_reader)
43       : ckpt_reader_(ckpt_reader) {}
~TFShow()44   virtual ~TFShow() {}
45   virtual void AddNode(TFGraphNode* node) = 0;
46   virtual void Build() = 0;
47   virtual const GraphNodeProto& Show(const string& prefix,
48                                      const Options& opts) final;
49 
50  protected:
51   virtual const ShowNode* ShowInternal(const Options& opts,
52                                        Timeline* timeline) = 0;
53 
54   bool LookUpCheckPoint(const string& name,
55                         std::unique_ptr<TFProfTensor>* tensor);
56 
57   // Overridden by subclass if extra requirements need to be met.
ShouldShowIfExtra(const ShowNode * node,const Options & opts,int depth)58   virtual bool ShouldShowIfExtra(const ShowNode* node, const Options& opts,
59                                  int depth) const {
60     return true;
61   }
62 
63   bool ShouldShow(const ShowNode* node, const Options& opts, int depth) const;
64 
65   bool ShouldTrim(const ShowNode* node,
66                   const std::vector<string>& regexes) const;
67 
68   bool ReAccount(ShowNode* node, const Options& opts);
69 
70   string FormatNode(ShowNode* node, const Options& opts) const;
71   string FormatNodeMemory(ShowNode* node, int64 bytes, int64 total_bytes) const;
72 
73   string FormatLegend(const Options& opts) const;
74 
75   template <typename T>
SortNodes(const std::vector<T * > & nodes,const Options & opts)76   std::vector<T*> SortNodes(const std::vector<T*>& nodes, const Options& opts) {
77     if (opts.order_by.empty() || nodes.empty()) {
78       return nodes;
79     }
80     std::vector<T*> sorted_nodes = nodes;
81     std::sort(sorted_nodes.begin(), sorted_nodes.end(),
82               [&opts](const T* n1, const T* n2) {
83                 if (n1->name() == kTFProfRoot) return true;
84                 if (n2->name() == kTFProfRoot) return false;
85                 bool name_cmp = n1->name() < n2->name();
86                 if (opts.order_by == kOrderBy[0]) {
87                   return name_cmp;
88                 } else if (opts.order_by == kOrderBy[1]) {
89                   return n1->proto().total_requested_bytes() >
90                          n2->proto().total_requested_bytes();
91                 } else if (opts.order_by == kOrderBy[2]) {
92                   return n1->proto().total_peak_bytes() >
93                          n2->proto().total_peak_bytes();
94                 } else if (opts.order_by == kOrderBy[3]) {
95                   return n1->proto().total_residual_bytes() >
96                          n2->proto().total_residual_bytes();
97                 } else if (opts.order_by == kOrderBy[4]) {
98                   return n1->proto().total_output_bytes() >
99                          n2->proto().total_output_bytes();
100                 } else if (opts.order_by == kOrderBy[5]) {
101                   return n1->proto().total_exec_micros() >
102                          n2->proto().total_exec_micros();
103                 } else if (opts.order_by == kOrderBy[6]) {
104                   return n1->proto().total_accelerator_exec_micros() >
105                          n2->proto().total_accelerator_exec_micros();
106                 } else if (opts.order_by == kOrderBy[7]) {
107                   return n1->proto().total_cpu_exec_micros() >
108                          n2->proto().total_cpu_exec_micros();
109                 } else if (opts.order_by == kOrderBy[8]) {
110                   return n1->proto().total_parameters() >
111                          n2->proto().total_parameters();
112                 } else if (opts.order_by == kOrderBy[9]) {
113                   return n1->proto().total_float_ops() >
114                          n2->proto().total_float_ops();
115                 }
116                 return name_cmp;
117               });
118     return sorted_nodes;
119   }
120 
121   checkpoint::CheckpointReader* ckpt_reader_;
122 };
123 
124 template <typename T>
FormatTotalExecTime(const T * node,const Options & opts)125 string FormatTotalExecTime(const T* node, const Options& opts) {
126   string time = FormatTime(node->proto().total_exec_micros());
127   if (node->account) {
128     time = FormatTime(node->proto().exec_micros()) + "/" + time;
129   } else {
130     time = "--/" + time;
131   }
132   return time;
133 }
134 template <typename T>
FormatCPUExecTime(const T * node,const Options & opts)135 string FormatCPUExecTime(const T* node, const Options& opts) {
136   string time = FormatTime(node->proto().total_cpu_exec_micros());
137   if (node->account) {
138     time = FormatTime(node->proto().cpu_exec_micros()) + "/" + time;
139   } else {
140     time = "--/" + time;
141   }
142   return time;
143 }
144 template <typename T>
FormatAcceleratorExecTime(const T * node,const Options & opts)145 string FormatAcceleratorExecTime(const T* node, const Options& opts) {
146   string time = FormatTime(node->proto().total_accelerator_exec_micros());
147   if (node->account) {
148     time = FormatTime(node->proto().accelerator_exec_micros()) + "/" + time;
149   } else {
150     time = "--/" + time;
151   }
152   return time;
153 }
154 }  // namespace tfprof
155 }  // namespace tensorflow
156 
157 #endif  // TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_SHOW_H_
158