• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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_COMPILER_XLA_SERVICE_DUMP_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_DUMP_H_
18 
19 #include "absl/strings/string_view.h"
20 #include "tensorflow/compiler/xla/service/hlo_module.h"
21 #include "tensorflow/compiler/xla/status.h"
22 #include "tensorflow/compiler/xla/xla.pb.h"
23 
24 // Consolidated utilities for logging information during compilation, usually
25 // based on the options specified in the DebugOptions proto.
26 //
27 // Most functions here take an HloModule and read the DebugOptions from the
28 // module's config.
29 
30 namespace xla {
31 
32 class BufferAssignment;
33 class HloExecutionProfile;
34 class HloSnapshot;
35 
36 // Get a timestamp which we can use as a filename prefix specific to this
37 // module.
38 string TimestampFor(const HloModule& module);
39 
40 // Create the filename we will use to dump in DumpToFileInDir.
41 string FilenameFor(const HloModule& module, absl::string_view prefix,
42                    absl::string_view suffix);
43 
44 // Writes the given string to a file in the xla_dump_to directory specified by
45 // module's DebugOptions.
46 //
47 // If module doesn't have an xla_dump_to directory, does nothing.
48 void DumpToFileInDir(const HloModule& module, absl::string_view file_prefix,
49                      absl::string_view file_suffix, absl::string_view contents);
50 
51 // Like DumpToFileInDir, except if module doesn't have an xla_dump_to directory
52 // specified, or if that directory is equal to "-", writes to stdout instead.
53 void DumpToFileInDirOrStdout(const HloModule& module,
54                              absl::string_view file_prefix,
55                              absl::string_view file_suffix,
56                              absl::string_view contents);
57 
58 // Like DumpToFileInDir, except if debug_options doesn't have an xla_dump_to
59 // directory specified, or if that directory is equal to "-", writes to stdout
60 // instead.
61 void DumpToFileInDirOrStdout(const DebugOptions& debug_options, int unique_id,
62                              absl::string_view module_name,
63                              absl::string_view file_prefix,
64                              absl::string_view file_suffix,
65                              absl::string_view contents);
66 
67 // Dumps the given execution options if dumping is enabled. Exactly
68 // where and in what formats it's dumped is determined by the debug options.
69 void DumpExecutionOptions(const ExecutionOptions& execution_options,
70                           const DebugOptions& debug_options);
71 
72 // Dumps the given HLO module if dumping is enabled for the module. Exactly
73 // where and in what formats it's dumped is determined by the module's config.
74 //
75 // If you pass an HloExecutionProfile, note that currently only DOT-based output
76 // formats (i.e. --xla_dump_as_{dot,html,url}) are able to incorporate it into
77 // their output.  Other formats will just ignore the profile.
78 void DumpHloModuleIfEnabled(const HloModule& module, absl::string_view name);
79 void DumpHloModuleIfEnabled(const HloModule& module,
80                             const BufferAssignment& buffer_assn,
81                             absl::string_view name);
82 void DumpHloModuleIfEnabled(const HloModule& module,
83                             const HloExecutionProfile& profile,
84                             absl::string_view name);
85 
86 // Dumps the given HLO module after running one HLO pass and before running
87 // another, if that's enabled. Returns the full file paths of all dumps of the
88 // module, or an empty vector if nothing was dumped.
89 std::vector<std::string> DumpHloModuleBetweenPassesIfEnabled(
90     absl::string_view pipeline_name, absl::string_view before_pass_name,
91     absl::string_view after_pass_name, const HloModule& module);
92 
93 // Dumps the given HLO module during the given HLO pass, if that's enabled.
94 //
95 // "step" is a human-readable description of where we are in the middle of this
96 // pass.  For example, "before-assigning-layouts".
97 void DumpHloModuleDuringPassIfEnabled(absl::string_view pass_name,
98                                       absl::string_view step,
99                                       const HloModule& module);
100 
101 // Dumps the given HloSnapshot to the module's xla_dump_dir, if this is enabled.
102 //
103 // Prefer the first overload below, as this will give filenames that are
104 // consistent with the other methods here.  The second overload (which doesn't
105 // take an HloModule) is useful in the cases when you're dumping an HloSnapshot
106 // and simply don't have an HloModule.
107 void DumpHloSnapshotIfEnabled(const HloModule& module,
108                               const HloSnapshot& snapshot);
109 void DumpHloSnapshotIfEnabled(const HloSnapshot& snapshot,
110                               const DebugOptions& opts);
111 
112 void DumpHloModuleMetadataIfEnabled(const std::vector<HloModule*>& modules);
113 
114 // Returns true if we should dump data for an HloModule.  This is useful if you
115 // want to check if DumpToFileInDir{,OrStdout} will do anything before
116 // generating an expensive string.
117 bool DumpingEnabledForHloModule(absl::string_view hlo_module_name,
118                                 const DebugOptions& opts);
DumpingEnabledForHloModule(const HloModule & module)119 inline bool DumpingEnabledForHloModule(const HloModule& module) {
120   return DumpingEnabledForHloModule(module.name(),
121                                     module.config().debug_options());
122 }
123 
124 // Returns true if DumpToFileInDirOrStdout and DumpHloModuleIfEnabled will write
125 // to stdout, rather than to a file on disk.
126 //
127 // This is useful if you want to do something different when writing to stdout.
128 // For example, maybe you have (almost-)duplicate data that you wouldn't mind
129 // writing to two files, but you don't want to print twice.
130 bool DumpingToStdout(const DebugOptions& opts);
131 
132 }  // namespace xla
133 
134 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_DUMP_H_
135