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 // Build a graph structure based on op inputs/outputs. The graph is a directed 17 // acyclic graph pointing *from outputs to inputs*. 18 19 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_GRAPH_H_ 20 #define TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_GRAPH_H_ 21 22 #include <deque> 23 #include <map> 24 #include <memory> 25 #include <set> 26 #include <string> 27 #include <vector> 28 29 #include "tensorflow/core/lib/core/errors.h" 30 #include "tensorflow/core/profiler/internal/tfprof_node.h" 31 #include "tensorflow/core/profiler/internal/tfprof_show.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 // Organize tensorflow ops in a graph structure, pointing from output ops 40 // to input ops. 41 class TFGraph : public TFShow { 42 public: TFGraph(checkpoint::CheckpointReader * ckpt_reader)43 explicit TFGraph(checkpoint::CheckpointReader* ckpt_reader) 44 : TFShow(ckpt_reader), root_(nullptr) {} ~TFGraph()45 ~TFGraph() override {} 46 47 void AddNode(TFGraphNode* node) override; 48 49 void Build() override; 50 51 private: 52 const ShowNode* ShowInternal(const Options& opts, 53 Timeline* timeline) override; 54 ShouldShowIfExtra(const ShowNode * node,const Options & opts,int depth)55 bool ShouldShowIfExtra(const ShowNode* node, const Options& opts, 56 int depth) const override { 57 return true; 58 } 59 60 GraphNode* CreateParentNode(const string& name); 61 62 std::vector<GraphNode*> SearchRoot(const std::vector<GraphNode*>& roots, 63 const std::vector<string>& regexes, 64 std::set<string>* visited); 65 66 std::vector<GraphNode*> PrintGraph(const std::vector<GraphNode*> roots, 67 const Options& opts, int depth, 68 int last_ident, std::set<string>* visits); 69 70 std::vector<GraphNode*> Account(const std::vector<GraphNode*>& roots, 71 const Options& opts, 72 std::set<string>* visits); 73 74 void Format(const std::vector<GraphNode*> roots, string* display_str, 75 GraphNodeProto* proto); 76 77 MemoryTracker memory_tracker_; 78 GraphNode* root_; 79 std::vector<std::unique_ptr<NodeDef>> node_defs_; 80 std::map<string, std::unique_ptr<TFGraphNode>> parent_nodes_; 81 std::map<string, std::unique_ptr<GraphNode>> nodes_map_; 82 }; 83 84 } // namespace tfprof 85 } // namespace tensorflow 86 87 #endif // TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_GRAPH_H_ 88