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