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 model's python code stacks. 17 // Stats are aggregated from descendants to ancestors. 18 19 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_CODE_H_ 20 #define TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_CODE_H_ 21 22 #include <map> 23 #include <memory> 24 #include <string> 25 #include <vector> 26 27 #include "tensorflow/core/lib/core/errors.h" 28 #include "tensorflow/core/profiler/internal/tfprof_node.h" 29 #include "tensorflow/core/profiler/internal/tfprof_show_multi.h" 30 #include "tensorflow/core/profiler/internal/tfprof_timeline.h" 31 #include "tensorflow/core/profiler/internal/tfprof_utils.h" 32 #include "tensorflow/core/profiler/profile.pb.h" 33 #include "tensorflow/core/profiler/tfprof_log.pb.h" 34 #include "tensorflow/core/profiler/tfprof_options.h" 35 #include "tensorflow/core/profiler/tfprof_output.pb.h" 36 37 namespace tensorflow { 38 namespace tfprof { 39 40 class PprofProfile { 41 public: ~PprofProfile()42 virtual ~PprofProfile() {} 43 44 virtual uint64 AddLocation(const CodeNode* callee, 45 const CodeNode* caller) = 0; 46 47 virtual void AddSample(const CodeNode* leaf, 48 std::vector<uint64>* call_ids) = 0; 49 50 virtual Status WritePprofProfile(const string& filename) = 0; 51 }; 52 53 class TFCode : public TFMultiShow { 54 public: TFCode()55 TFCode() {} ~TFCode()56 ~TFCode() override {} 57 58 // Add nodes to the code view. Called before Build() 59 void AddNode(TFGraphNode* node) override; 60 61 // Build the code view structure. Called after all nodes 62 // are added via AddNode(). 63 void Build() override; 64 65 private: 66 const ShowMultiNode* ShowInternal(const Options& opts, 67 Timeline* timeline) override; 68 69 std::vector<CodeNode*> SearchRoot(std::vector<CodeNode*> roots, 70 const std::vector<string>& regexes); 71 72 std::vector<CodeNode*> PrintScope(const std::vector<CodeNode*> roots, 73 const Options& opts, int depth, 74 int last_ident); 75 76 std::vector<CodeNode*> Account(const std::vector<CodeNode*>& roots, 77 const Options& opts); 78 79 void Format(const CodeNode* root, const std::vector<CodeNode*>& nodes, 80 const Options& opts, string* display_str, 81 MultiGraphNodeProto* proto, std::vector<uint64>* call_ids); 82 83 string FormatNode(CodeNode* node, const Options& opts, int64_t indent) const; 84 string FormatNodeMemory(CodeNode* node, int64_t bytes, 85 int64_t total_bytes) const; 86 87 std::unique_ptr<CodeNode> root_; 88 std::unique_ptr<TFMultiGraphNode> graph_root_; 89 std::unique_ptr<PprofProfile> pprof_profile_; 90 std::map<string, std::vector<TFGraphNode*>> grad_nodes_; 91 std::map<string, TFGraphNode*> forward_nodes_; 92 }; 93 } // namespace tfprof 94 } // namespace tensorflow 95 96 #endif // TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_CODE_H_ 97