• 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 three 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 // This last option is not implemented by default, but you can add a plugin to
40 // implement it via RegisterGraphToURLRenderer.
41 //
42 // TODO(jlebar): Rename this file to hlo_graph_renderer.
43 
44 namespace xla {
45 
46 // Different formats that a graph can be packaged as.
47 enum class RenderedGraphFormat {
48   kDot,
49   kHtml,
50   kUrl,
51 };
52 
53 // Renders an HLO module as a human-readable visual graph.
54 //
55 // Note that this only works well for relatively small graphs (no more than a
56 // few hundred nodes).  Beyond that, the dot is usually unrenderable,
57 // unreadable, or both.  To view such graphs, use a tool such as
58 // interactive_graphviz, which calls RenderNeighborhoodAround to render subsets
59 // of a graph.
60 StatusOr<string> RenderGraph(
61     const HloComputation& computation, absl::string_view label,
62     const DebugOptions& debug_options, RenderedGraphFormat format,
63     const HloExecutionProfile* hlo_execution_profile = nullptr,
64     bool show_backend_config = false);
65 
66 // Like RenderGraph, but renders only nodes "near" the given node in the graph.
67 //
68 // The number of nodes dumped is controlled by the radius parameter, which
69 // (roughly) corresponds to the max distance a node may be from the primary node
70 // before it's omitted from the graph.
71 //
72 // The optional boundary specifies a set of boundary nodes, beyond which nodes
73 // will be omitted even if they are within the radius.
74 StatusOr<string> RenderNeighborhoodAround(
75     const HloInstruction& node, int radius, RenderedGraphFormat format,
76     bool show_backend_config = false,
77     const absl::flat_hash_set<const HloInstruction*>& boundary = {});
78 
79 // Renders nodes on any of the paths from `from` to `to`.  If there are more
80 // than max_nodes on all paths, restricts to the max_nodes nodes on the shortest
81 // paths.
82 StatusOr<string> RenderAllPathsFromTo(const HloInstruction& from,
83                                       const HloInstruction& to, int64 max_nodes,
84                                       RenderedGraphFormat format,
85                                       bool show_backend_config = false);
86 
87 // Registers a function which implements RenderedGraphFormat::kUrl.
88 //
89 // The input to the function is dot, and the output should be a URL or an error.
90 //
91 // There can only be one active renderer, and the last call to this function
92 // wins.
93 void RegisterGraphToURLRenderer(
94     std::function<StatusOr<string>(absl::string_view dot)> renderer);
95 
96 }  // namespace xla
97 
98 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_GRAPH_DUMPER_H_
99