aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/hlo_tfgraph_builder.cc
blob: 487653344976a10e18ba667085525ba1ecbb8612 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* 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.
==============================================================================*/

#include "tensorflow/compiler/xla/service/hlo_tfgraph_builder.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "tensorflow/compiler/xla/layout_util.h"
#include "tensorflow/compiler/xla/literal.h"
#include "tensorflow/compiler/xla/service/hlo_opcode.h"
#include "tensorflow/compiler/xla/shape_util.h"
#include "tensorflow/core/framework/attr_value.pb.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/tensor_shape.pb.h"

namespace xla {
namespace hlo_graph_dumper {
namespace {

using absl::StrAppend;
using absl::StrCat;
using tensorflow::GraphDef;
using tensorflow::NodeDef;
using tensorflow::TensorShapeProto;

string GetOpDefName(const HloInstruction* instruction) {
  string name = StrCat("hlo-", HloOpcodeString(instruction->opcode()));
  tensorflow::str_util::TitlecaseString(&name, "-");  // non-absl ok
  name.erase(std::remove(name.begin(), name.end(), '-'), name.end());

  if (instruction->opcode() == HloOpcode::kFusion) {
    string fusion_name = ToString(instruction->fusion_kind());
    StrAppend(&name, absl::string_view(fusion_name).substr(1));
  }
  return name;
}

TensorShapeProto GetTensorShape(const HloInstruction* instruction) {
  TensorShapeProto tensor_shape;
  const Shape& shape = instruction->shape();
  for (auto dim : shape.dimensions()) {
    tensor_shape.add_dim()->set_size(dim);
  }
  return tensor_shape;
}

string GetDeviceName(int device) { return StrCat("/device/XLA:", device); }

void CleanNodeName(string* name) {
  name->erase(std::remove(name->begin(), name->end(), '%'), name->end());
  const string chars_to_replace = "<>[]";
  auto pred = [&](char c) {
    return std::find(chars_to_replace.begin(), chars_to_replace.end(), c) !=
           chars_to_replace.end();
  };
  std::replace_if(name->begin(), name->end(), pred, '_');
}

}  // namespace

HloTfGraphBuilder::HloTfGraphBuilder(const DebugOptions& debug_options)
    : debug_options_(debug_options) {}

Status HloTfGraphBuilder::AddComputation(const HloComputation& computation) {
  VLOG(2) << "Adding computation " << computation.name();
  for (auto embedded : computation.MakeEmbeddedComputationsList()) {
    for (auto* instruction : embedded->instructions()) {
      TF_RETURN_IF_ERROR(AddInstruction(instruction));
    }
  }
  for (auto* instruction : computation.instructions()) {
    TF_RETURN_IF_ERROR(AddInstruction(instruction));
  }
  return Status::OK();
}

const GraphDef& HloTfGraphBuilder::GetGraphDef() const { return graph_def_; }

const string& HloTfGraphBuilder::GetNodeNameForInstruction(
    const HloInstruction* instruction) {
  if (ContainsKey(instruction_to_node_name_, instruction)) {
    return instruction_to_node_name_[instruction];
  }
  auto append = [](string* str, const string& other) {
    if (str->empty()) {
      *str = other;
    } else if (!other.empty()) {
      StrAppend(str, "/", other);
    }
  };
  string node_name;
  if (debug_options_.xla_hlo_tfgraph_device_scopes()) {
    auto device = instruction->sharding_unique_device();
    if (device) {
      node_name = StrCat("dev", *device);
    }
  }
  // If an instruction is fused, put it in the subgraph of the fusion;
  // otherwise, put it in the computation subgraph.
  const HloComputation* computation = instruction->parent();
  if (computation->IsFusionComputation()) {
    append(&node_name,
           GetNodeNameForInstruction(computation->FusionInstruction()));
  } else {
    append(&node_name, computation->name());
    if (!instruction->metadata().op_name().empty()) {
      // Always make computations contain TF ops but not the other way around.
      append(&node_name, instruction->metadata().op_name());
    }
  }
  string instruction_name = instruction->name();
  if (instruction->opcode() == HloOpcode::kParameter) {
    StrAppend(&instruction_name, ".", instruction->parameter_number());
  }
  append(&node_name, instruction_name);
  CleanNodeName(&node_name);
  auto ret =
      instruction_to_node_name_.insert(std::make_pair(instruction, node_name));
  CHECK(ret.second);
  return ret.first->second;
}

void HloTfGraphBuilder::SetNodeAttrs(const HloInstruction* instruction,
                                     NodeDef* node_def) const {
  auto& attrs = *node_def->mutable_attr();

  // Set the number of arguments for instructions that have variadic operands.
  if (HloOpcodeIsVariadic(instruction->opcode())) {
    tensorflow::AttrValue attr_value;
    attr_value.set_i(instruction->operands().size());
    attrs["arg_num"] = attr_value;
  }

  // Set the node type.
  attrs["type"].set_s(
      xla::PrimitiveType_Name(instruction->shape().element_type()));

  // Set the framework op (e.g. Tensorflow op) that generated this XLA op.
  attrs["tf_op_type"].set_s(instruction->metadata().op_type());
  attrs["tf_op_name"].set_s(instruction->metadata().op_name());

  // Set the shape of the output tensor. "_output_shapes" is a special attribute
  // name used by Tensorboard for shapes of output tensors.
  tensorflow::AttrValue shapes;
  *shapes.mutable_list()->add_shape() = GetTensorShape(instruction);
  attrs["_output_shapes"] = shapes;

  // Set the layout.
  if (LayoutUtil::HasLayout(instruction->shape())) {
    string layout_string;
    if (ShapeUtil::IsTuple(instruction->shape())) {
      // For tuples, emit the full shape because the layout of a tuple is not
      // represented in a single Layout field.
      layout_string = ShapeUtil::HumanStringWithLayout(instruction->shape());
    } else {
      layout_string = StrCat(
          "{",
          absl::StrJoin(LayoutUtil::MinorToMajor(instruction->shape()), ","),
          "}");
    }
    attrs["layout"].set_s(layout_string);
  }

  // Set op-specific attributes.
  switch (instruction->opcode()) {
    case HloOpcode::kConcatenate:
    case HloOpcode::kBroadcast:
    case HloOpcode::kReduce:
    case HloOpcode::kReverse:
    case HloOpcode::kTranspose:
      for (auto dim : instruction->dimensions()) {
        attrs["dims"].mutable_list()->add_i(dim);
      }
      break;
    case HloOpcode::kGetTupleElement:
      attrs["index"].set_i(instruction->tuple_index());
      break;
    case HloOpcode::kRng:
      attrs["dist"].set_s(
          RandomDistribution_Name(instruction->random_distribution()));
      break;
    case HloOpcode::kConstant:
      if (ShapeUtil::IsScalar(instruction->shape())) {
        attrs["value"].set_s(instruction->literal().GetAsString({}));
      }
      break;
    case HloOpcode::kCustomCall:
      attrs["custom_call_target"].set_s(instruction->custom_call_target());
      break;
    case HloOpcode::kSend:
    case HloOpcode::kRecv:
      attrs["channel_id"].set_i(instruction->channel_id());
      break;
    default:
      break;
  }
}

Status HloTfGraphBuilder::AddInstruction(const HloInstruction* instruction) {
  if (!visited_instructions_.insert(instruction).second) {
    // Skip instructions that have already been added.
    return Status::OK();
  }

  NodeDef* node_def = graph_def_.add_node();
  node_def->set_name(GetNodeNameForInstruction(instruction));
  node_def->set_op(GetOpDefName(instruction));

  auto device = instruction->sharding_unique_device();
  if (device) {
    node_def->set_device(GetDeviceName(*device));
  }
  SetNodeAttrs(instruction, node_def);
  if (instruction->opcode() == HloOpcode::kFusion) {
    for (auto* fused_instruction : instruction->fused_instructions()) {
      TF_RETURN_IF_ERROR(AddInstruction(fused_instruction));
    }
  }
  // Add all edges including control edges.
  for (unsigned i = 0; i < instruction->operands().size(); ++i) {
    *node_def->add_input() = GetNodeNameForInstruction(instruction->operand(i));
  }
  // Called computations are control dependencies.
  for (const auto* called_computation : instruction->called_computations()) {
    *node_def->add_input() = StrCat(
        "^", GetNodeNameForInstruction(called_computation->root_instruction()));
  }
  return Status::OK();
}

}  // namespace hlo_graph_dumper
}  // namespace xla