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 tree structure based on the TensorFlow op names. 17 // For example, 'name1/name2' is a child of 'name1'. 18 // Stats are aggregated from descendants to ancestors. 19 20 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_SCOPE_H_ 21 #define TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_SCOPE_H_ 22 23 #include <map> 24 #include <memory> 25 #include <string> 26 #include <vector> 27 28 #include "tensorflow/c/checkpoint_reader.h" 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 class TFScope : public TFShow { 40 public: TFScope(checkpoint::CheckpointReader * ckpt_reader)41 explicit TFScope(checkpoint::CheckpointReader* ckpt_reader) 42 : TFShow(ckpt_reader), root_(nullptr) {} ~TFScope()43 ~TFScope() override {} 44 45 void AddNode(TFGraphNode* node) override; 46 47 void Build() override; 48 49 private: 50 const ShowNode* ShowInternal(const Options& opts, 51 Timeline* timeline) override; 52 53 ScopeNode* CreateParentNode(const string& name); 54 55 std::vector<ScopeNode*> SearchRoot(std::vector<ScopeNode*> roots, 56 const std::vector<string>& regexes); 57 58 std::vector<ScopeNode*> PrintScope(const std::vector<ScopeNode*> roots, 59 const Options& opts, int depth, 60 int last_ident); 61 62 std::vector<ScopeNode*> Account(const std::vector<ScopeNode*>& roots, 63 const Options& opts); 64 65 void Format(const std::vector<ScopeNode*> roots, string* display_str, 66 GraphNodeProto* proto); 67 68 ScopeNode* root_; 69 std::vector<std::unique_ptr<NodeDef>> node_defs_; 70 std::map<string, std::unique_ptr<TFGraphNode>> parent_nodes_; 71 std::map<string, std::unique_ptr<ScopeNode>> nodes_map_; 72 }; 73 } // namespace tfprof 74 } // namespace tensorflow 75 76 #endif // TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_SCOPE_H_ 77