aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tools/tfprof/internal/tfprof_show.h
blob: 2c61b4fd732c5945d7eb82f845f7390c8e01e6ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Copyright 2016 The TensorFlow Authors All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// Parent class and utilities for tfprof_graph and tfprof_scope.

#ifndef THIRD_PARTY_TENSORFLOW_TOOLS_TFPROF_INTERNAL_TFPROF_SHOW_H_
#define THIRD_PARTY_TENSORFLOW_TOOLS_TFPROF_INTERNAL_TFPROF_SHOW_H_

#include <algorithm>
#include <string>
#include <vector>

#include "tensorflow/c/checkpoint_reader.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/tools/tfprof/internal/tfprof_constants.h"
#include "tensorflow/tools/tfprof/internal/tfprof_node.h"
#include "tensorflow/tools/tfprof/internal/tfprof_node_show.h"
#include "tensorflow/tools/tfprof/internal/tfprof_options.h"
#include "tensorflow/tools/tfprof/internal/tfprof_tensor.h"
#include "tensorflow/tools/tfprof/internal/tfprof_timeline.h"
#include "tensorflow/tools/tfprof/internal/tfprof_utils.h"
#include "tensorflow/tools/tfprof/tfprof_output.pb.h"

namespace tensorflow {
namespace tfprof {
class TFShow {
 public:
  explicit TFShow(checkpoint::CheckpointReader* ckpt_reader)
      : ckpt_reader_(ckpt_reader) {}
  virtual ~TFShow() {}
  virtual void AddNode(TFGraphNode* node) = 0;
  virtual void Build() = 0;
  const TFGraphNodeProto& Show(const Options& opts);

 protected:
  virtual const ShowNode* ShowInternal(const Options& opts,
                                       Timeline* timeline) = 0;

  bool LookUpCheckPoint(const string& name,
                        std::unique_ptr<TFProfTensor>* tensor);

  // Overridden by subclass if extra requirements need to be met.
  virtual bool ShouldShowIfExtra(ShowNode* node, const Options& opts,
                                 int depth) {
    return true;
  }

  bool ShouldShow(ShowNode* node, const Options& opts, int depth);

  bool ShouldTrim(ShowNode* node, const std::vector<string>& regexes);

  bool ReAccount(ShowNode* node, const Options& opts);

  string FormatNode(ShowNode* node, const Options& opts);

  string FormatLegend(const Options& opts);

  template <typename T>
  std::vector<T*> SortNodes(const std::vector<T*>& nodes, const Options& opts) {
    if (opts.order_by.empty() || nodes.empty()) {
      return nodes;
    }
    std::vector<T*> sorted_nodes = nodes;
    std::sort(sorted_nodes.begin(), sorted_nodes.end(), [&opts](const T* n1,
                                                                const T* n2) {
      if (n1->name() == kTFProfRoot) return true;
      if (n2->name() == kTFProfRoot) return false;
      bool name_cmp = n1->name() < n2->name();
      if (opts.order_by == kOrderBy[0]) {
        return name_cmp;
      } else if (opts.order_by == kOrderBy[1]) {
        return n1->proto().total_requested_bytes() >
               n2->proto().total_requested_bytes();
      } else if (opts.order_by == kOrderBy[2]) {
        return n1->proto().total_exec_micros() >
               n2->proto().total_exec_micros();
      } else if (opts.order_by == kOrderBy[3]) {
        return n1->proto().total_parameters() > n2->proto().total_parameters();
      } else if (opts.order_by == kOrderBy[4]) {
        return n1->proto().total_float_ops() > n2->proto().total_float_ops();
      }
      return name_cmp;
    });
    return sorted_nodes;
  }

  checkpoint::CheckpointReader* ckpt_reader_;
};

template <typename T>
string FormatTotalExecTime(const T* node, const Options& opts) {
  string time = FormatTime(node->proto().total_exec_micros());
  if (node->account) {
    time = FormatTime(node->proto().exec_micros()) + "/" + time;
  } else {
    time = "--/" + time;
  }
  return time;
}
template <typename T>
string FormatCPUExecTime(const T* node, const Options& opts) {
  string time = FormatTime(node->proto().total_cpu_exec_micros());
  if (node->account) {
    time = FormatTime(node->proto().cpu_exec_micros()) + "/" + time;
  } else {
    time = "--/" + time;
  }
  return time;
}
template <typename T>
string FormatAcceleratorExecTime(const T* node, const Options& opts) {
  string time = FormatTime(node->proto().total_accelerator_exec_micros());
  if (node->account) {
    time = FormatTime(node->proto().accelerator_exec_micros()) + "/" + time;
  } else {
    time = "--/" + time;
  }
  return time;
}
}  // namespace tfprof
}  // namespace tensorflow

#endif  // THIRD_PARTY_TENSORFLOW_TOOLS_TFPROF_INTERNAL_TFPROF_SHOW_H_