• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 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_HLO_GRAPH_DUMPER_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_GRAPH_DUMPER_H_
18 
19 #include <string>
20 
21 #include "tensorflow/compiler/xla/service/hlo_computation.h"
22 #include "tensorflow/compiler/xla/service/hlo_execution_profile.h"
23 #include "tensorflow/compiler/xla/types.h"
24 #include "tensorflow/compiler/xla/xla.pb.h"
25 
26 // This file contains routines for rendering HLO computations into a
27 // human-readable graphical format.
28 //
29 // Fundamentally all graphs are rendered using the DOT language, but they can be
30 // packaged four different ways:
31 //
32 //  - as a raw DOT file, which can be rendered using `graphviz`.
33 //
34 //  - as an HTML file with an embedded DOT file, which can be viewed in a
35 //    browser using a version of graphviz compiled to JavaScript
36 //
37 //  - as a URL hosted somewhere which somehow embeds the DOT file.
38 //
39 //  - as an HTML page showing the fusion progress.
40 //
41 // Two last options are not implemented by default, but you can add a plugin to
42 // implement it via RegisterGraphToURLRenderer.
43 //
44 // TODO(jlebar): Rename this file to hlo_graph_renderer.
45 
46 namespace xla {
47 
48 // Different formats that a graph can be packaged as.
49 enum class RenderedGraphFormat {
50   kDot,
51   kHtml,
52   kUrl,
53   kFusionVisualization,
54 };
55 
56 struct HloRenderOptions {
57   // Include the backend config string in the rendered graph.
58   bool show_backend_config = false;
59 
60   // Include the fusion subcomputations in the rendered graph.
61   bool show_fusion_subcomputations = true;
62 };
63 
64 // Renders an HLO module as a human-readable visual graph.
65 //
66 // Note that this only works well for relatively small graphs (no more than a
67 // few hundred nodes).  Beyond that, the dot is usually unrenderable,
68 // unreadable, or both.  To view such graphs, use a tool such as
69 // interactive_graphviz, which calls RenderNeighborhoodAround to render subsets
70 // of a graph.
71 StatusOr<string> RenderGraph(
72     const HloComputation& computation, absl::string_view label,
73     const DebugOptions& debug_options, RenderedGraphFormat format,
74     const HloExecutionProfile* hlo_execution_profile = nullptr,
75     HloRenderOptions hlo_render_options = {});
76 
77 // Like RenderGraph, but renders only nodes "near" the given node in the graph.
78 //
79 // The number of nodes dumped is controlled by the radius parameter, which
80 // (roughly) corresponds to the max distance a node may be from the primary node
81 // before it's omitted from the graph.
82 //
83 // The optional boundary specifies a set of boundary nodes, beyond which nodes
84 // will be omitted even if they are within the radius.
85 StatusOr<string> RenderNeighborhoodAround(
86     const HloInstruction& node, int radius, RenderedGraphFormat format,
87     HloRenderOptions hlo_render_options = {},
88     const absl::flat_hash_set<const HloInstruction*>& boundary = {});
89 
90 // Renders nodes on any of the paths from `from` to `to`.  If there are more
91 // than max_nodes on all paths, restricts to the max_nodes nodes on the shortest
92 // paths.
93 StatusOr<string> RenderAllPathsFromTo(const HloInstruction& from,
94                                       const HloInstruction& to, int64 max_nodes,
95                                       RenderedGraphFormat format,
96                                       HloRenderOptions hlo_render_options = {});
97 
98 // Registers the fusion state of the graph for future visualization using
99 // the kFusionVisulization render format.
100 Status RegisterFusionState(const HloComputation& computation,
101                            absl::string_view label);
102 
103 // Registers a function which implements RenderedGraphFormat::kUrl.
104 //
105 // The input to the function is dot, and the output should be a URL or an error.
106 //
107 // There can only be one active renderer, and the last call to this function
108 // wins.
109 void RegisterGraphToURLRenderer(
110     std::function<StatusOr<string>(absl::string_view dot)> renderer);
111 
112 }  // namespace xla
113 
114 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_GRAPH_DUMPER_H_
115