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 }; 54 55 struct HloRenderOptions { 56 // Include the backend config string in the rendered graph. 57 bool show_backend_config = false; 58 59 // Include the fusion subcomputations in the rendered graph. 60 bool show_fusion_subcomputations = true; 61 }; 62 63 // Renders an HLO module as a human-readable visual graph. 64 // 65 // Note that this only works well for relatively small graphs (no more than a 66 // few hundred nodes). Beyond that, the dot is usually unrenderable, 67 // unreadable, or both. To view such graphs, use a tool such as 68 // interactive_graphviz, which calls RenderNeighborhoodAround to render subsets 69 // of a graph. 70 StatusOr<std::string> RenderGraph( 71 const HloComputation& computation, absl::string_view label, 72 const DebugOptions& debug_options, RenderedGraphFormat format, 73 const HloExecutionProfile* hlo_execution_profile = nullptr, 74 HloRenderOptions hlo_render_options = {}); 75 76 // Like RenderGraph, but renders only nodes "near" the given node in the graph. 77 // 78 // The number of nodes dumped is controlled by the radius parameter, which 79 // (roughly) corresponds to the max distance a node may be from the primary node 80 // before it's omitted from the graph. 81 // 82 // The optional boundary specifies a set of boundary nodes, beyond which nodes 83 // will be omitted even if they are within the radius. 84 StatusOr<std::string> RenderNeighborhoodAround( 85 const HloInstruction& node, int radius, RenderedGraphFormat format, 86 HloRenderOptions hlo_render_options = {}, 87 const absl::flat_hash_set<const HloInstruction*>& boundary = {}); 88 89 // Renders nodes on any of the paths from `from` to `to`. If there are more 90 // than max_nodes on all paths, restricts to the max_nodes nodes on the shortest 91 // paths. 92 StatusOr<std::string> RenderAllPathsFromTo( 93 const HloInstruction& from, const HloInstruction& to, int64_t max_nodes, 94 RenderedGraphFormat format, HloRenderOptions hlo_render_options = {}); 95 96 // Registers the fusion state of the graph for future visualization using 97 // the kFusionVisulization render format. 98 // 99 // The `consumer` node defines the area which should be rendered: if left null, 100 // computation root is used by default. 101 // 102 // The `producer` remains `nullptr` if it's fused, or is set if the desire is to 103 // highlight it. 104 void RegisterFusionState(const HloComputation& computation, 105 absl::string_view label, 106 const HloInstruction& consumer, 107 const HloInstruction* producer = nullptr); 108 109 // Registers a function which implements RenderedGraphFormat::kUrl. 110 // 111 // The input to the function is dot, and the output should be a URL or an error. 112 // 113 // There can only be one active renderer, and the last call to this function 114 // wins. 115 void RegisterGraphToURLRenderer( 116 std::function<StatusOr<std::string>(absl::string_view dot)> renderer); 117 118 // Generates a fusion explorer for the given computation using the data in 119 // fusion_visualizer_state. 120 StatusOr<std::string> WrapFusionExplorer(const HloComputation& computation); 121 122 } // namespace xla 123 124 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_GRAPH_DUMPER_H_ 125