Home | History | Annotate | Download | only in service
      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 namespace xla {
     27 namespace hlo_graph_dumper {
     28 
     29 // Abstract interface for classes that render HLO graphs (e.g. DOT graph,
     30 // tensorflow GraphDef).
     31 class GraphRendererInterface {
     32  public:
     33   enum GraphKind {
     34     DOT_GRAPH,
     35     TF_GRAPHDEF,
     36   };
     37 
     38   virtual ~GraphRendererInterface() = default;
     39 
     40   // Renders a DOT graph, returning a description of the rendered output
     41   // (e.g., a URL)
     42   virtual string RenderGraph(const string& graph, GraphKind graph_kind,
     43                              const DebugOptions& debug_options) = 0;
     44 };
     45 
     46 // Dump the given HLO module if a dump is requested in its debug options. Based
     47 // on the debug options, either a graph dump, a text dump or both may be
     48 // generated. If a graph dump is generated, the description (e.g. an URL) is
     49 // returned; otherwise an empty string is returned.
     50 string MaybeDumpHloModule(const HloModule& module, const string& label,
     51                           const HloExecutionProfile* profile = nullptr);
     52 
     53 // Dumps a graph of the computation and returns a description of the rendered
     54 // graph (e.g., a URL) based on the renderer. The "best" renderer in the
     55 // registry is used.
     56 string DumpGraph(const HloComputation& computation, const string& label,
     57                  const DebugOptions& debug_options,
     58                  const HloExecutionProfile* hlo_execution_profile = nullptr,
     59                  bool show_metadata = false);
     60 
     61 // Like DumpGraph, but renders only nodes "near" the given node in the graph.
     62 //
     63 // The number of nodes dumped is controlled by the radius parameter, which
     64 // (roughly) corresponds to the max distance a node may be from the primary node
     65 // before it's omitted from the graph.
     66 string DumpNeighborhoodAround(const HloInstruction& node, int radius,
     67                               bool show_metadata = false);
     68 
     69 // Dumps the HloModule::ToString() as a file into the provided directory path
     70 // suffixed with the provided label.
     71 //
     72 // If do_prefix is true, a timestamp will be prepended onto the label to
     73 // construct a filename in the directory path; otherwise, the label is used
     74 // as the filename directly.
     75 void DumpText(const HloModule& module, const string& label,
     76               const string& directory_path, bool do_prefix = true);
     77 
     78 // Graph renderers may be added using a registration mechanism, e.g.:
     79 // XLA_REGISTER_GRAPH_RENDERER(AGraphRendererClass, 100)
     80 // The renderer with the highest numeric priority value is used.
     81 
     82 #define XLA_REGISTER_GRAPH_RENDERER(factory, ...) \
     83   XLA_INTERNAL_REGISTER_GRAPH_RENDERER(factory, __COUNTER__, ##__VA_ARGS__)
     84 
     85 // Internal implementation details below this point.
     86 
     87 // Class that registers a graph renderer.
     88 class Registrar {
     89  public:
     90   Registrar(GraphRendererInterface* dumper);
     91 };
     92 
     93 #define XLA_INTERNAL_REGISTER_GRAPH_RENDERER(factory, ctr, ...)   \
     94   static ::xla::hlo_graph_dumper::Registrar                       \
     95       XLA_INTERNAL_REGISTER_GRAPH_RENDERER_NAME(ctr)(new factory, \
     96                                                      ##__VA_ARGS__)
     97 
     98 // __COUNTER__ must go through another macro to be properly expanded
     99 #define XLA_INTERNAL_REGISTER_GRAPH_RENDERER_NAME(ctr) ___##ctr##__object_
    100 
    101 }  // namespace hlo_graph_dumper
    102 }  // namespace xla
    103 
    104 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_GRAPH_DUMPER_H_
    105