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_TFGRAPH_BUILDER_H_
     17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_TFGRAPH_BUILDER_H_
     18 
     19 #include "tensorflow/compiler/xla/service/hlo_computation.h"
     20 #include "tensorflow/compiler/xla/xla.pb.h"
     21 #include "tensorflow/core/framework/graph.pb.h"
     22 #include "tensorflow/core/framework/node_def.pb.h"
     23 
     24 namespace xla {
     25 namespace hlo_graph_dumper {
     26 
     27 // This constructs a tensorflow graph for HLO computations.
     28 class HloTfGraphBuilder {
     29  public:
     30   HloTfGraphBuilder(const DebugOptions& debug_options = DebugOptions());
     31 
     32   // Adds a computation to the graph.
     33   Status AddComputation(const HloComputation& computation);
     34 
     35   const tensorflow::GraphDef& GetGraphDef() const;
     36 
     37  private:
     38   // Gets the node name of an instruction. The node name is hierarchical. For
     39   // example, if an instruction is fused, it will be put in a subgraph of the
     40   // fusion instruction.
     41   const string& GetNodeNameForInstruction(const HloInstruction* instruction);
     42 
     43   void SetNodeAttrs(const HloInstruction* instruction,
     44                     tensorflow::NodeDef* node_def) const;
     45 
     46   Status AddInstruction(const HloInstruction* instruction);
     47 
     48   DebugOptions debug_options_;
     49   tensorflow::GraphDef graph_def_;
     50   // This records instructions that have been visited.
     51   std::unordered_set<const HloInstruction*> visited_instructions_;
     52   // A cache that maps instruction to the node name.
     53   std::unordered_map<const HloInstruction*, string> instruction_to_node_name_;
     54 };
     55 
     56 }  // namespace hlo_graph_dumper
     57 }  // namespace xla
     58 
     59 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_TFGRAPH_BUILDER_H_
     60