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