aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tools/tfprof/tfprof_main.cc
blob: 7a4e7e85ffad496cf946d43dad56a22f854eb34a (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* 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.
==============================================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "linenoise.h"
#include "tensorflow/c/c_api.h"
#include "tensorflow/c/checkpoint_reader.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/init_main.h"
#include "tensorflow/core/platform/protobuf.h"
#include "tensorflow/core/protobuf/config.pb.h"
#include "tensorflow/core/util/command_line_flags.h"
#include "tensorflow/tools/tfprof/internal/tfprof_options.h"
#include "tensorflow/tools/tfprof/internal/tfprof_stats.h"
#include "tensorflow/tools/tfprof/internal/tfprof_utils.h"
#include "tensorflow/tools/tfprof/tfprof_log.pb.h"

namespace tensorflow {
namespace tfprof {
void completion(const char* buf, linenoiseCompletions* lc) {
  string buf_str = buf;
  if (buf_str.find(" ") == buf_str.npos) {
    for (const char* opt : kCmds) {
      if (string(opt).find(buf_str) == 0) {
        linenoiseAddCompletion(lc, opt);
      }
    }
    return;
  }

  string prefix;
  int last_dash = buf_str.find_last_of(' ');
  if (last_dash != string::npos) {
    prefix = buf_str.substr(0, last_dash + 1);
    buf_str = buf_str.substr(last_dash + 1, kint32max);
  }
  for (const char* opt : kOptions) {
    if (string(opt).find(buf_str) == 0) {
      linenoiseAddCompletion(lc, (prefix + opt).c_str());
    }
  }
}

int Run(int argc, char** argv) {
  string FLAGS_graph_path = "";
  string FLAGS_run_meta_path = "";
  string FLAGS_op_log_path = "";
  string FLAGS_checkpoint_path = "";
  int32 FLAGS_max_depth = 10;
  int64 FLAGS_min_bytes = 0;
  int64 FLAGS_min_micros = 0;
  int64 FLAGS_min_params = 0;
  int64 FLAGS_min_float_ops = 0;
  int64 FLAGS_min_occurrence = 0;
  int64 FLAGS_step = -1;
  string FLAGS_order_by = "name";
  string FLAGS_account_type_regexes = ".*";
  string FLAGS_start_name_regexes = ".*";
  string FLAGS_trim_name_regexes = "";
  string FLAGS_show_name_regexes = ".*";
  string FLAGS_hide_name_regexes;
  bool FLAGS_account_displayed_op_only = false;
  string FLAGS_select = "micros";
  string FLAGS_output = "";
  for (int i = 0; i < argc; i++) {
    fprintf(stderr, "%s\n", argv[i]);
  }

  std::vector<Flag> flag_list = {
      Flag("graph_path", &FLAGS_graph_path, "GraphDef proto text file name"),
      Flag("run_meta_path", &FLAGS_run_meta_path,
           "Comma-separated list of RunMetadata proto binary "
           "files. Each file is given step number 0,1,2,etc"),
      Flag("op_log_path", &FLAGS_op_log_path,
           "tensorflow::tfprof::OpLog proto binary file name"),
      Flag("checkpoint_path", &FLAGS_checkpoint_path,
           "TensorFlow Checkpoint file name"),
      Flag("max_depth", &FLAGS_max_depth, "max depth"),
      Flag("min_bytes", &FLAGS_min_bytes, "min_bytes"),
      Flag("min_micros", &FLAGS_min_micros, "min micros"),
      Flag("min_params", &FLAGS_min_params, "min params"),
      Flag("min_float_ops", &FLAGS_min_float_ops, "min float ops"),
      Flag("min_occurrence", &FLAGS_min_occurrence, "min occurrence"),
      Flag("step", &FLAGS_step,
           "The stats of which step to use. By default average"),
      Flag("order_by", &FLAGS_order_by, "order by"),
      Flag("account_type_regexes", &FLAGS_start_name_regexes,
           "start name regexes"),
      Flag("trim_name_regexes", &FLAGS_trim_name_regexes, "trim name regexes"),
      Flag("show_name_regexes", &FLAGS_show_name_regexes, "show name regexes"),
      Flag("hide_name_regexes", &FLAGS_hide_name_regexes, "hide name regexes"),
      Flag("account_displayed_op_only", &FLAGS_account_displayed_op_only,
           "account displayed op only"),
      Flag("select", &FLAGS_select, "select"),
      Flag("output", &FLAGS_output, "output"),
  };
  string usage = Flags::Usage(argv[0], flag_list);
  bool parse_ok = Flags::Parse(&argc, argv, flag_list);
  if (!parse_ok) {
    printf("%s", usage.c_str());
    return (2);
  }
  port::InitMain(argv[0], &argc, &argv);

  fprintf(stderr, "%s\n", FLAGS_graph_path.c_str());

  std::vector<string> account_type_regexes =
      str_util::Split(FLAGS_account_type_regexes, ',', str_util::SkipEmpty());
  std::vector<string> start_name_regexes =
      str_util::Split(FLAGS_start_name_regexes, ',', str_util::SkipEmpty());
  std::vector<string> trim_name_regexes =
      str_util::Split(FLAGS_trim_name_regexes, ',', str_util::SkipEmpty());
  std::vector<string> show_name_regexes =
      str_util::Split(FLAGS_show_name_regexes, ',', str_util::SkipEmpty());
  std::vector<string> hide_name_regexes =
      str_util::Split(FLAGS_hide_name_regexes, ',', str_util::SkipEmpty());
  std::vector<string> select =
      str_util::Split(FLAGS_select, ',', str_util::SkipEmpty());

  string output_type;
  std::map<string, string> output_options;
  Status s = ParseOutput(FLAGS_output, &output_type, &output_options);
  CHECK(s.ok()) << s.ToString();

  string cmd = "";
  if (argc == 1 && FLAGS_graph_path.empty()) {
    printf("1) go/tfprof: Tutorial.\n");
    printf("2) tfprof help: Detail help information.\n");
    printf(
        "3) tfprof --graph_path <GraphDef proto text file>: "
        "Profiling model structure, tensor shape and # parameters.\n");
    printf(
        "4) tfprof --graph_path <GraphDef proto text file> \\\n"
        "          --run_meta_path <RunMetadata proto binary file> \\\n"
        "          --op_log_path <tensorflow::tfprof::OpLog proto binary file> "
        "\\\n"
        "          --checkpoint_path <TensorFlow Checkpoint file>: "
        "Profiling everything!\n");
    return 0;
  } else if (argc > 1) {
    if (string(argv[1]) == kCmds[5]) {
      PrintHelp();
      return 0;
    }
    if (string(argv[1]) == kCmds[0] || string(argv[1]) == kCmds[1] ||
        string(argv[1]) == kCmds[2] || string(argv[1]) == kCmds[3]) {
      cmd = argv[1];
    }
  }

  printf("Reading Files...\n");
  std::unique_ptr<GraphDef> graph(new GraphDef());
  TF_CHECK_OK(
      ReadProtoFile(Env::Default(), FLAGS_graph_path, graph.get(), false));

  std::unique_ptr<OpLog> op_log(new OpLog());
  if (!FLAGS_op_log_path.empty()) {
    string op_log_str;
    s = ReadFileToString(Env::Default(), FLAGS_op_log_path, &op_log_str);
    if (!s.ok()) {
      fprintf(stderr, "Failed to read op_log_path: %s\n", s.ToString().c_str());
      return 1;
    }
    if (!ParseProtoUnlimited(op_log.get(), op_log_str)) {
      fprintf(stderr, "Failed to parse op_log_path\n");
      return 1;
    }
  }

  std::unique_ptr<checkpoint::CheckpointReader> ckpt_reader;
  TF_Status* status = TF_NewStatus();
  if (!FLAGS_checkpoint_path.empty()) {
    ckpt_reader.reset(
        new checkpoint::CheckpointReader(FLAGS_checkpoint_path, status));
    if (TF_GetCode(status) != TF_OK) {
      fprintf(stderr, "%s\n", TF_Message(status));
      TF_DeleteStatus(status);
      return 1;
    }
    TF_DeleteStatus(status);
  }

  TFStats tf_stat(std::move(graph), nullptr, std::move(op_log),
                  std::move(ckpt_reader));

  std::vector<string> run_meta_files =
      str_util::Split(FLAGS_run_meta_path, ',', str_util::SkipEmpty());
  for (int i = 0; i < run_meta_files.size(); ++i) {
    std::unique_ptr<RunMetadata> run_meta(new RunMetadata());
    s = ReadProtoFile(Env::Default(), run_meta_files[i], run_meta.get(), true);
    if (!s.ok()) {
      fprintf(stderr, "Failed to read run_meta_path %s. Status: %s\n",
              run_meta_files[i].c_str(), s.ToString().c_str());
      return 1;
    }
    tf_stat.ParseRunMeta(i, std::move(run_meta));
  }

  Options opts(FLAGS_max_depth, FLAGS_min_bytes, FLAGS_min_micros,
               FLAGS_min_params, FLAGS_min_float_ops, FLAGS_min_occurrence,
               FLAGS_step, FLAGS_order_by, account_type_regexes,
               start_name_regexes, trim_name_regexes, show_name_regexes,
               hide_name_regexes, FLAGS_account_displayed_op_only, select,
               output_type, output_options);

  if (cmd == kCmds[2] || cmd == kCmds[3]) {
    tf_stat.ShowMultiGraphNode(cmd, opts);
    return 0;
  } else if (cmd == kCmds[0] || cmd == kCmds[1]) {
    tf_stat.ShowGraphNode(cmd, opts);
    return 0;
  }

  linenoiseSetCompletionCallback(completion);
  linenoiseHistoryLoad(".tfprof_history.txt");

  for (char* line = nullptr; (line = linenoise("tfprof> ")) != nullptr;) {
    string line_s = line;
    free(line);

    if (line_s.empty()) {
      printf("%s", opts.ToString().c_str());
      continue;
    }
    linenoiseHistoryAdd(line_s.c_str());
    linenoiseHistorySave(".tfprof_history.txt");

    Options new_opts = opts;
    Status s = ParseCmdLine(line_s, &cmd, &new_opts);
    if (!s.ok()) {
      fprintf(stderr, "E: %s\n", s.ToString().c_str());
      continue;
    }
    if (cmd == kCmds[4]) {
      opts = new_opts;
    } else if (cmd == kCmds[5]) {
      PrintHelp();
    } else if (cmd == kCmds[2] || cmd == kCmds[3]) {
      tf_stat.ShowMultiGraphNode(cmd, new_opts);
    } else if (cmd == kCmds[0] || cmd == kCmds[1]) {
      tf_stat.ShowGraphNode(cmd, new_opts);
    }
  }
  return 0;
}
}  // namespace tfprof
}  // namespace tensorflow

int main(int argc, char** argv) { return tensorflow::tfprof::Run(argc, argv); }