Home | History | Annotate | Download | only in profiler
      1 /* Copyright 2017 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 #ifndef TENSORFLOW_CC_PROFILER_PROFILER_H_
     17 #define TENSORFLOW_CC_PROFILER_PROFILER_H_
     18 
     19 #include "tensorflow/core/framework/graph.pb.h"
     20 #include "tensorflow/core/lib/core/status.h"
     21 #include "tensorflow/core/profiler/internal/tfprof_stats.h"
     22 #include "tensorflow/core/profiler/tfprof_options.h"
     23 #include "tensorflow/core/profiler/tfprof_output.pb.h"
     24 
     25 namespace tensorflow {
     26 namespace tfprof {
     27 
     28 /// @addtogroup core
     29 /// @{
     30 
     31 /// A `Profiler` object lets the caller profile the execution of a graph.
     32 ///
     33 /// Example:
     34 ///     // First build a graph and run tracing.
     35 ///     Scope root = Scope::NewRootScope();
     36 ///     auto a = Placeholder(root, DT_INT32);
     37 ///     auto c = Add(root, a, {41});
     38 ///
     39 ///     ClientSession session(root);
     40 ///     std::vector<Tensor> outputs;
     41 ///     RunOptions run_options;
     42 ///     run_options.set_trace_level(RunOptions::FULL_TRACE);
     43 ///     RunMetadata run_meta;
     44 ///     Status s = session.Run(run_options, { {a, {1}} }, {c}, &outputs,
     45 ///                            &run_meta);
     46 ///     if (!s.ok()) { ... }
     47 ///
     48 ///     // Then create profiler to do profiling.
     49 ///     GraphDef graph;
     50 ///     root.ToGraphDef(&graph);
     51 ///     Profiler profiler(graph);
     52 ///     profiler.AddStep(0, run_meta);
     53 ///     Options opts = ...  // TODO(xpan): Support option building API.
     54 ///     MultiGraphNodeProto r = profiler.ProfileOperations(opts);
     55 ///
     56 class Profiler {
     57  public:
     58   /// `graph` is the model's GraphDef.
     59   Profiler(const GraphDef& graph);
     60 
     61   /// Adds tracing information `run_meta` to profiler. A `run_meta` is
     62   /// generated by a TensorFlow session run call. `step` is the key
     63   /// to the `run_meta`. When calling ProfileXXX methods, caller can specify
     64   /// `step` in `options` to seletively profile the corresponding `run_meta`.
     65   /// Multiple different `run_meta` can be keyed by the same `step` in order
     66   /// to group them together.
     67   void AddStep(int64 step, const RunMetadata& run_meta);
     68 
     69   /// Profiles the model by organizing nodes in graph structure.
     70   /// Each node is an op and the nodes are contected by the op inputs/outputs.
     71   GraphNodeProto ProfileGraph(const Options& options);
     72 
     73   /// Profiles the model by organizing nodes in name scope structure.
     74   /// Each node is an op, and nodes are organized by the ops' name
     75   /// scope, similar to a filesystem tree.
     76   /// E.g. /foo is the root of operation /foo/matmul_1 and foo/conv_2.
     77   GraphNodeProto ProfileNameScope(const Options& options);
     78 
     79   /// Profiles the model by organizing nodes by operation types.
     80   /// Each node is an operation type (e.g. Conv2D or MatMul), containing all
     81   /// ops belonging to that type in the model.
     82   MultiGraphNodeProto ProfileOperations(const Options& options);
     83 
     84   /// Serialize the profile content (ProfileProto) into a binary string,
     85   /// User can write the string to file for offline analysis by
     86   /// tfprof command-line tools or graphical user interface.
     87   Status SerializeToString(string* content);
     88 
     89  private:
     90   std::unique_ptr<TFStats> stats_;
     91 };
     92 /// @}
     93 
     94 }  // namespace tfprof
     95 }  // namespace tensorflow
     96 
     97 #endif  // TENSORFLOW_CC_PROFILER_PROFILER_H_
     98