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 // Node classes used for different views. They are wrappers with "show" 17 // methods. 18 // 19 // ScopeNode is for scope view. GraphNode is for graph view, CodeNode 20 // is for code view and OpNode for op view. 21 // ScopeNode and GraphNode each maps to one TFGraphNode. 22 // CodeNode and OpNode each maps to one TFMultiGraphNode. 23 24 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_NODE_SHOW_H_ 25 #define TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_NODE_SHOW_H_ 26 27 #include <algorithm> 28 #include <string> 29 #include <vector> 30 31 #include "tensorflow/core/lib/core/errors.h" 32 #include "tensorflow/core/profiler/internal/tfprof_constants.h" 33 #include "tensorflow/core/profiler/internal/tfprof_node.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 ShowNode { 42 public: 43 explicit ShowNode(const TFGraphNode* node); ~ShowNode()44 virtual ~ShowNode() {} 45 name()46 const string& name() const { return node->name(); } 47 GraphNodeProto* mutable_proto(); 48 const GraphNodeProto& proto() const; 49 50 void ReInit(int64_t step); 51 52 void AggregateTotalStats(ShowNode* node); 53 54 void AddSelfToTotalStats(); 55 56 void ResetTotalStats(); 57 58 const TFGraphNode* node; 59 bool account; 60 string formatted_str; 61 62 protected: 63 GraphNodeProto proto_; 64 }; 65 66 class GraphNode : public ShowNode { 67 public: GraphNode(TFGraphNode * node)68 explicit GraphNode(TFGraphNode* node) : ShowNode(node) {} 69 Trackable(int64_t step)70 bool Trackable(int64_t step) const { return node->trackable(step); } 71 72 std::vector<GraphNode*> children; 73 std::vector<GraphNode*> show_children; 74 }; 75 76 class ScopeNode : public ShowNode { 77 public: ScopeNode(const TFGraphNode * node)78 explicit ScopeNode(const TFGraphNode* node) : ShowNode(node) {} ~ScopeNode()79 ~ScopeNode() override {} 80 81 std::vector<ScopeNode*> children; 82 std::vector<ScopeNode*> show_children; 83 }; 84 85 class ShowMultiNode { 86 public: 87 explicit ShowMultiNode(TFMultiGraphNode* node); ~ShowMultiNode()88 virtual ~ShowMultiNode() {} 89 90 bool ReInit(int64_t step, const std::vector<string>& type_regexes); 91 name()92 const string& name() const { return node->name(); } 93 MultiGraphNodeProto* mutable_proto(); 94 const MultiGraphNodeProto& proto() const; 95 96 void AggregateTotalStats(ShowMultiNode* node); 97 98 void AddSelfToTotalStats(); 99 100 void ResetTotalStats(); 101 102 TFMultiGraphNode* node; 103 bool account; 104 bool show; 105 string formatted_str; 106 107 protected: 108 MultiGraphNodeProto proto_; 109 }; 110 111 class CodeNode : public ShowMultiNode { 112 public: CodeNode(TFMultiGraphNode * node,const CallStack::Trace * trace,const string & suffix)113 CodeNode(TFMultiGraphNode* node, const CallStack::Trace* trace, 114 const string& suffix) 115 : ShowMultiNode(node), trace_(trace), suffix_(suffix) {} ~CodeNode()116 ~CodeNode() override {} 117 AddChildren(const string & name,const CallStack::Trace * trace,const string suffix)118 CodeNode* AddChildren(const string& name, const CallStack::Trace* trace, 119 const string suffix) { 120 auto it = children_.find(name); 121 if (it != children_.end()) { 122 return it->second.get(); 123 } 124 125 graph_children_.push_back( 126 std::unique_ptr<TFMultiGraphNode>(new TFMultiGraphNode(name))); 127 auto child = &children_[name]; 128 child->reset(new CodeNode(graph_children_.back().get(), trace, suffix)); 129 children.push_back(child->get()); 130 return child->get(); 131 } 132 has_trace()133 bool has_trace() const { return trace_ != nullptr; } lineno()134 const int32 lineno() const { return trace_->lineno(); } file()135 string file() const { return trace_->file(); } function()136 string function() const { return trace_->function() + suffix_; } func_start_line()137 int32 func_start_line() const { return trace_->func_start_line(); } 138 139 std::vector<CodeNode*> children; 140 std::vector<CodeNode*> show_children; 141 142 private: 143 const CallStack::Trace* trace_; 144 string suffix_; 145 std::vector<std::unique_ptr<TFMultiGraphNode>> graph_children_; 146 std::map<string, std::unique_ptr<CodeNode>> children_; 147 }; 148 149 class OpNode : public ShowMultiNode { 150 public: OpNode(TFMultiGraphNode * node)151 explicit OpNode(TFMultiGraphNode* node) : ShowMultiNode(node) {} ~OpNode()152 ~OpNode() override {} 153 }; 154 155 } // namespace tfprof 156 } // namespace tensorflow 157 158 #endif // TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_NODE_SHOW_H_ 159