aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/hlo_tfgraph_builder.h
blob: c4876b852e32d34693202f4023aa20ad2b301ffd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TENSORFLOW_COMPILER_XLA_SERVICE_HLO_TFGRAPH_BUILDER_H_
#define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_TFGRAPH_BUILDER_H_

#include "tensorflow/compiler/xla/service/hlo_computation.h"
#include "tensorflow/compiler/xla/xla.pb.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/node_def.pb.h"

namespace xla {
namespace hlo_graph_dumper {

// This constructs a tensorflow graph for HLO computations.
class HloTfGraphBuilder {
 public:
  HloTfGraphBuilder(const DebugOptions& debug_options = DebugOptions());

  // Adds a computation to the graph.
  Status AddComputation(const HloComputation& computation);

  const tensorflow::GraphDef& GetGraphDef() const;

 private:
  // Gets the node name of an instruction. The node name is hierarchical. For
  // example, if an instruction is fused, it will be put in a subgraph of the
  // fusion instruction.
  const string& GetNodeNameForInstruction(const HloInstruction* instruction);

  void SetNodeAttrs(const HloInstruction* instruction,
                    tensorflow::NodeDef* node_def) const;

  Status AddInstruction(const HloInstruction* instruction);

  DebugOptions debug_options_;
  tensorflow::GraphDef graph_def_;
  // This records instructions that have been visited.
  std::unordered_set<const HloInstruction*> visited_instructions_;
  // A cache that maps instruction to the node name.
  std::unordered_map<const HloInstruction*, string> instruction_to_node_name_;
};

}  // namespace hlo_graph_dumper
}  // namespace xla

#endif  // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_TFGRAPH_BUILDER_H_