• 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 // Dumps the given execution options if dumping is enabled. Exactly
59 // where and in what formats it's dumped is determined by the debug options.
60 void DumpExecutionOptions(const ExecutionOptions& execution_options,
61                           const DebugOptions& debug_options);
62 
63 // Dumps the given HLO module if dumping is enabled for the module.  Exactly
64 // where and in what formats it's dumped is determined by the module's config.
65 //
66 // If you pass an HloExecutionProfile, note that currently only DOT-based output
67 // formats (i.e. --xla_dump_as_{dot,html,url}) are able to incorporate it into
68 // their output.  Other formats will just ignore the profile.
69 void DumpHloModuleIfEnabled(const HloModule& module, absl::string_view name);
70 void DumpHloModuleIfEnabled(const HloModule& module,
71                             const BufferAssignment& buffer_assn,
72                             absl::string_view name);
73 void DumpHloModuleIfEnabled(const HloModule& module,
74                             const HloExecutionProfile& profile,
75                             absl::string_view name);
76 
77 // Dumps the given HLO module after running one HLO pass and before running
78 // another, if that's enabled. Returns the full file paths of all dumps of the
79 // module, or an empty vector if nothing was dumped.
80 std::vector<std::string> DumpHloModuleBetweenPassesIfEnabled(
81     absl::string_view pipeline_name, absl::string_view before_pass_name,
82     absl::string_view after_pass_name, const HloModule& module);
83 
84 // Dumps the given HLO module during the given HLO pass, if that's enabled.
85 //
86 // "step" is a human-readable description of where we are in the middle of this
87 // pass.  For example, "before-assigning-layouts".
88 void DumpHloModuleDuringPassIfEnabled(absl::string_view pass_name,
89                                       absl::string_view step,
90                                       const HloModule& module);
91 
92 // Dumps the given HloSnapshot to the module's xla_dump_dir, if this is enabled.
93 //
94 // Prefer the first overload below, as this will give filenames that are
95 // consistent with the other methods here.  The second overload (which doesn't
96 // take an HloModule) is useful in the cases when you're dumping an HloSnapshot
97 // and simply don't have an HloModule.
98 void DumpHloSnapshotIfEnabled(const HloModule& module,
99                               const HloSnapshot& snapshot);
100 void DumpHloSnapshotIfEnabled(const HloSnapshot& snapshot,
101                               const DebugOptions& opts);
102 
103 void DumpHloModuleMetadataIfEnabled(const std::vector<HloModule*>& modules);
104 
105 // Returns true if we should dump data for an HloModule.  This is useful if you
106 // want to check if DumpToFileInDir{,OrStdout} will do anything before
107 // generating an expensive string.
108 bool DumpingEnabledForHloModule(absl::string_view hlo_module_name,
109                                 const DebugOptions& opts);
DumpingEnabledForHloModule(const HloModule & module)110 inline bool DumpingEnabledForHloModule(const HloModule& module) {
111   return DumpingEnabledForHloModule(module.name(),
112                                     module.config().debug_options());
113 }
114 
115 // Returns true if DumpToFileInDirOrStdout and DumpHloModuleIfEnabled will write
116 // to stdout, rather than to a file on disk.
117 //
118 // This is useful if you want to do something different when writing to stdout.
119 // For example, maybe you have (almost-)duplicate data that you wouldn't mind
120 // writing to two files, but you don't want to print twice.
121 bool DumpingToStdout(const DebugOptions& opts);
122 
123 }  // namespace xla
124 
125 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_DUMP_H_
126