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 // Core API of tfprof.
     17 // 1. Load protos generated from a tensorflow model.
     18 // 2. Build in-memory representations of the tensorflow model, annotate the
     19 //    representation with various stats, such as params,times,memory,etc.
     20 // 3. Accept command and options to selectively aggregate stats for analysis
     21 //    and print out the results.
     22 
     23 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_STATS_H_
     24 #define TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_STATS_H_
     25 
     26 #include <map>
     27 #include <memory>
     28 #include <set>
     29 #include <string>
     30 
     31 #include "tensorflow/c/checkpoint_reader.h"
     32 #include "tensorflow/core/framework/attr_value.pb.h"
     33 #include "tensorflow/core/framework/graph.pb.h"
     34 #include "tensorflow/core/framework/step_stats.pb.h"
     35 #include "tensorflow/core/lib/core/errors.h"
     36 #include "tensorflow/core/lib/strings/stringprintf.h"
     37 #include "tensorflow/core/profiler/internal/tfprof_code.h"
     38 #include "tensorflow/core/profiler/internal/tfprof_graph.h"
     39 #include "tensorflow/core/profiler/internal/tfprof_node.h"
     40 #include "tensorflow/core/profiler/internal/tfprof_op.h"
     41 #include "tensorflow/core/profiler/internal/tfprof_scope.h"
     42 #include "tensorflow/core/profiler/internal/tfprof_show.h"
     43 #include "tensorflow/core/profiler/internal/tfprof_utils.h"
     44 #include "tensorflow/core/profiler/tfprof_log.pb.h"
     45 #include "tensorflow/core/profiler/tfprof_options.h"
     46 #include "tensorflow/core/profiler/tfprof_output.pb.h"
     47 #include "tensorflow/core/protobuf/config.pb.h"
     48 
     49 namespace tensorflow {
     50 namespace tfprof {
     51 
     52 class TFStats {
     53  public:
     54   TFStats(std::unique_ptr<GraphDef> graph,
     55           std::unique_ptr<RunMetadata> run_meta,
     56           std::unique_ptr<OpLogProto> op_log,
     57           std::unique_ptr<checkpoint::CheckpointReader> ckpt_reader);
     58 
     59   TFStats(const string& filename,
     60           std::unique_ptr<checkpoint::CheckpointReader> ckpt_reader);
     61 
     62   ~TFStats() {}
     63 
     64   const std::map<string, std::unique_ptr<TFGraphNode>>& nodes() const {
     65     return nodes_map_;
     66   }
     67   const std::set<int64>& steps() const { return steps_; }
     68   bool has_code_traces() const { return has_code_traces_; }
     69   double run_coverage() const {
     70     return covered_nodes_.size() / (nodes_map_.size() + 1e-10);
     71   }
     72 
     73   void BuildView(const string& cmd);
     74   void BuildAllViews();
     75 
     76   // Note: Must first BuildView(view_foo) before ShowXXX(view_foo) methods.
     77   //
     78   // Organize the TensorFlow model as different types of views, and generate
     79   // outputs for profiling.
     80   // TODO(xpan): Should it return reference here?
     81   const GraphNodeProto& ShowGraphNode(const string& cmd,
     82                                       const Options& opts) const;
     83   const MultiGraphNodeProto& ShowMultiGraphNode(const string& cmd,
     84                                                 const Options& opts) const;
     85 
     86   // Add a (partial) graph to existing graph.
     87   void AddGraph(std::unique_ptr<GraphDef> graph);
     88 
     89   // Add a step of run time meta data.
     90   void AddRunMeta(int64 step, std::unique_ptr<RunMetadata> run_meta);
     91   // Add tfprof operation meta data, such as customized op type, float_ops,
     92   // and code traces.
     93   void AddOpLogProto(std::unique_ptr<OpLogProto> op_log);
     94 
     95   void SerializeToString(string* content);
     96   void WriteProfile(const string& filename);
     97 
     98   // For test purpose only.
     99   void AddNodeForTest(int64 step, std::unique_ptr<TFGraphNode> node);
    100 
    101  private:
    102   bool Validate(const Options& opts) const;
    103   string MaybeReportMissingTrace() const;
    104 
    105   std::set<int64> steps_;
    106   bool has_code_traces_;
    107   bool miss_accelerator_stream_;
    108   std::unique_ptr<TFScope> scope_view_;
    109   std::unique_ptr<TFGraph> graph_view_;
    110   std::unique_ptr<TFCode> code_view_;
    111   std::unique_ptr<TFOp> op_view_;
    112   std::unique_ptr<checkpoint::CheckpointReader> ckpt_reader_;
    113   // TODO(xpan): Store TFGraphNode instead of TFGraphNode* to avoid large
    114   // number of dynamic alloc.
    115   // Maps from graph node name to TFGraphNode.
    116   std::map<string, std::unique_ptr<TFGraphNode>> nodes_map_;
    117   GraphNodeProto empty_graph_node_;
    118   MultiGraphNodeProto empty_multi_graph_node_;
    119 
    120   std::map<int64, string> id_to_string_;
    121   // Graph nodes covered by RunMetadata, that is traced with run time stats.
    122   std::set<int64> covered_nodes_;
    123 };
    124 
    125 }  // namespace tfprof
    126 }  // namespace tensorflow
    127 
    128 #endif  // TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_STATS_H_
    129