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/profiler/internal/tfprof_constants.h" 29 #include "tensorflow/core/profiler/internal/tfprof_node.h" 30 #include "tensorflow/core/profiler/internal/tfprof_node_show.h" 31 #include "tensorflow/core/profiler/internal/tfprof_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 41 class TFMultiShow { 42 public: TFMultiShow()43 explicit TFMultiShow() {} ~TFMultiShow()44 virtual ~TFMultiShow() {} 45 virtual void AddNode(TFGraphNode* node) = 0; 46 virtual void Build() = 0; 47 const MultiGraphNodeProto& Show(const string& prefix, const Options& opts); 48 49 protected: 50 virtual const ShowMultiNode* ShowInternal(const Options& opts, 51 Timeline* timeline) = 0; 52 53 bool LookUpCheckPoint(const string& name, 54 std::unique_ptr<TFProfTensor>* tensor); 55 56 // Overridden by subclass if extra requirements need to be met. ShouldShowIfExtra(const ShowMultiNode * node,const Options & opts,int depth)57 virtual bool ShouldShowIfExtra(const ShowMultiNode* node, const Options& opts, 58 int depth) const { 59 return true; 60 } 61 62 bool ShouldShow(const ShowMultiNode* node, const Options& opts, 63 int depth) const; 64 65 bool ShouldTrim(const ShowMultiNode* node, 66 const std::vector<string>& regexes) const; 67 68 bool ReAccount(ShowMultiNode* node, const Options& opts); 69 70 string FormatLegend(const Options& opts) const; 71 string FormatInputShapes(const MultiGraphNodeProto& proto) const; 72 std::vector<string> FormatTimes(const ShowMultiNode* node, 73 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 } else if (opts.order_by == kOrderBy[10]) { 116 return n1->node->graph_nodes().size() > 117 n2->node->graph_nodes().size(); 118 } 119 return name_cmp; 120 }); 121 return sorted_nodes; 122 } 123 }; 124 125 } // namespace tfprof 126 } // namespace tensorflow 127 128 #endif // TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_SHOW_MULTI_H_ 129