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