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