xref: /aosp_15_r20/external/tensorflow/tensorflow/core/profiler/internal/tfprof_show_multi.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_code.
17 
18 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_SHOW_MULTI_H_
19 #define TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_SHOW_MULTI_H_
20 
21 #include <algorithm>
22 #include <string>
23 #include <vector>
24 
25 #include "tensorflow/core/lib/core/errors.h"
26 #include "tensorflow/core/profiler/internal/tfprof_constants.h"
27 #include "tensorflow/core/profiler/internal/tfprof_node.h"
28 #include "tensorflow/core/profiler/internal/tfprof_node_show.h"
29 #include "tensorflow/core/profiler/internal/tfprof_show.h"
30 #include "tensorflow/core/profiler/internal/tfprof_tensor.h"
31 #include "tensorflow/core/profiler/internal/tfprof_timeline.h"
32 #include "tensorflow/core/profiler/internal/tfprof_utils.h"
33 #include "tensorflow/core/profiler/tfprof_options.h"
34 #include "tensorflow/core/profiler/tfprof_output.pb.h"
35 
36 namespace tensorflow {
37 namespace tfprof {
38 
39 class TFMultiShow {
40  public:
TFMultiShow()41   explicit TFMultiShow() {}
~TFMultiShow()42   virtual ~TFMultiShow() {}
43   virtual void AddNode(TFGraphNode* node) = 0;
44   virtual void Build() = 0;
45   const MultiGraphNodeProto& Show(const string& prefix, const Options& opts);
46 
47  protected:
48   virtual const ShowMultiNode* ShowInternal(const Options& opts,
49                                             Timeline* timeline) = 0;
50 
51   bool LookUpCheckPoint(const string& name,
52                         std::unique_ptr<TFProfTensor>* tensor);
53 
54   // Overridden by subclass if extra requirements need to be met.
ShouldShowIfExtra(const ShowMultiNode * node,const Options & opts,int depth)55   virtual bool ShouldShowIfExtra(const ShowMultiNode* node, const Options& opts,
56                                  int depth) const {
57     return true;
58   }
59 
60   bool ShouldShow(const ShowMultiNode* node, const Options& opts,
61                   int depth) const;
62 
63   bool ShouldTrim(const ShowMultiNode* node,
64                   const std::vector<string>& regexes) const;
65 
66   bool ReAccount(ShowMultiNode* node, const Options& opts);
67 
68   string FormatLegend(const Options& opts) const;
69   string FormatInputShapes(const MultiGraphNodeProto& proto) const;
70   std::vector<string> FormatTimes(const ShowMultiNode* node,
71                                   const Options& opts) const;
72 
73   template <typename T>
SortNodes(const std::vector<T * > & nodes,const Options & opts)74   std::vector<T*> SortNodes(const std::vector<T*>& nodes, const Options& opts) {
75     if (opts.order_by.empty() || nodes.empty()) {
76       return nodes;
77     }
78     std::vector<T*> sorted_nodes = nodes;
79     std::stable_sort(sorted_nodes.begin(), sorted_nodes.end(),
80                      [&opts](const T* n1, const T* n2) {
81                        if (n1->name() == kTFProfRoot) return true;
82                        if (n2->name() == kTFProfRoot) return false;
83                        bool name_cmp = n1->name() < n2->name();
84                        if (opts.order_by == kOrderBy[0]) {
85                          return name_cmp;
86                        } else if (opts.order_by == kOrderBy[1]) {
87                          return n1->proto().total_requested_bytes() >
88                                 n2->proto().total_requested_bytes();
89                        } else if (opts.order_by == kOrderBy[2]) {
90                          return n1->proto().total_peak_bytes() >
91                                 n2->proto().total_peak_bytes();
92                        } else if (opts.order_by == kOrderBy[3]) {
93                          return n1->proto().total_residual_bytes() >
94                                 n2->proto().total_residual_bytes();
95                        } else if (opts.order_by == kOrderBy[4]) {
96                          return n1->proto().total_output_bytes() >
97                                 n2->proto().total_output_bytes();
98                        } else if (opts.order_by == kOrderBy[5]) {
99                          return n1->proto().total_exec_micros() >
100                                 n2->proto().total_exec_micros();
101                        } else if (opts.order_by == kOrderBy[6]) {
102                          return n1->proto().total_accelerator_exec_micros() >
103                                 n2->proto().total_accelerator_exec_micros();
104                        } else if (opts.order_by == kOrderBy[7]) {
105                          return n1->proto().total_cpu_exec_micros() >
106                                 n2->proto().total_cpu_exec_micros();
107                        } else if (opts.order_by == kOrderBy[8]) {
108                          return n1->proto().total_parameters() >
109                                 n2->proto().total_parameters();
110                        } else if (opts.order_by == kOrderBy[9]) {
111                          return n1->proto().total_float_ops() >
112                                 n2->proto().total_float_ops();
113                        } else if (opts.order_by == kOrderBy[10]) {
114                          return n1->node->graph_nodes().size() >
115                                 n2->node->graph_nodes().size();
116                        }
117                        return name_cmp;
118                      });
119     return sorted_nodes;
120   }
121 };
122 
123 }  // namespace tfprof
124 }  // namespace tensorflow
125 
126 #endif  // TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_SHOW_MULTI_H_
127