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"""Offline dump analyzer of TensorFlow Debugger (tfdbg)."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import argparse
21import sys
22
23# Google-internal import(s).
24
25from tensorflow.python.debug.cli import analyzer_cli
26from tensorflow.python.debug.lib import debug_data
27from tensorflow.python.platform import app
28
29
30def main(_):
31  if FLAGS.log_usage:
32    pass  # No logging for open-source.
33
34  if not FLAGS.dump_dir:
35    print("ERROR: dump_dir flag is empty.", file=sys.stderr)
36    sys.exit(1)
37
38  print("tfdbg offline: FLAGS.dump_dir = %s" % FLAGS.dump_dir)
39
40  debug_dump = debug_data.DebugDumpDir(
41      FLAGS.dump_dir, validate=FLAGS.validate_graph)
42  cli = analyzer_cli.create_analyzer_ui(
43      debug_dump,
44      tensor_filters={"has_inf_or_nan": debug_data.has_inf_or_nan},
45      ui_type=FLAGS.ui_type)
46
47  title = "tfdbg offline @ %s" % FLAGS.dump_dir
48  cli.run_ui(title=title, title_color="black_on_white", init_command="lt")
49
50
51if __name__ == "__main__":
52  parser = argparse.ArgumentParser()
53  parser.register("type", "bool", lambda v: v.lower() == "true")
54  parser.add_argument(
55      "--dump_dir", type=str, default="", help="tfdbg dump directory to load")
56  parser.add_argument(
57      "--log_usage",
58      type="bool",
59      nargs="?",
60      const=True,
61      default=True,
62      help="Whether the usage of this tool is to be logged")
63  parser.add_argument(
64      "--ui_type",
65      type=str,
66      default="curses",
67      help="Command-line user interface type (curses | readline)")
68  parser.add_argument(
69      "--validate_graph",
70      nargs="?",
71      const=True,
72      type="bool",
73      default=True,
74      help="""\
75      Whether the dumped tensors will be validated against the GraphDefs\
76      """)
77  FLAGS, unparsed = parser.parse_known_args()
78  app.run(main=main, argv=[sys.argv[0]] + unparsed)
79