aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/common_runtime/parallel_concat_optimizer.cc
blob: 6af4ca4d961d96a46be67e3770434e380658f32a (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
/* Copyright 2016 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/core/common_runtime/graph_optimizer.h"

#include "tensorflow/core/common_runtime/constant_folding.h"
#include "tensorflow/core/common_runtime/function.h"
#include "tensorflow/core/common_runtime/optimization_registry.h"
#include "tensorflow/core/graph/algorithm.h"
#include "tensorflow/core/graph/node_builder.h"
#include "tensorflow/core/graph/optimizer_cse.h"

namespace tensorflow {
namespace {

// Replaces occurrences of parallel_concat with the implementation based on
// unsafe ops. Sets removed_any to true if any parallel_concats were removed;
// leaves it untouched otherwise.
class ParallelConcatRemovePass : public GraphOptimizationPass {
 public:
  Status Run(const GraphOptimizationPassOptions& options) override {
    if (options.graph == nullptr) {
      // TODO(apassos) returning OK feels weird here as we can't do anything
      // without a graph, but some tests require this.
      return Status::OK();
    }
    Graph* g = options.graph->get();
    if (g == nullptr) {
      return errors::Internal(
          "Parallel concat removal should happen before partitioning and a "
          "graph should be available.");
    }
    gtl::InlinedVector<Node*, 2> matches;
    for (Node* n : g->op_nodes()) {
      if (n->type_string() == "ParallelConcat") {
        matches.push_back(n);
      }
    }
    for (Node* n : matches) {
      AttrSlice n_attrs = n->attrs();
      auto base_make_node = [n, &n_attrs](const string& op,
                                          const string& name) {
        NodeBuilder node_builder(name, op);
        node_builder.Device(n->requested_device());
        string colo;
        if (GetNodeAttr(n_attrs, "_class", &colo).ok()) {
          node_builder.Attr("_class", colo);
        }
        return node_builder;
      };
      auto make_node = [n, g, &base_make_node](string op) {
        return base_make_node(
            op, g->NewName(strings::StrCat(n->name(), "/Internal")));
      };
      DataType dtype;
      TF_RETURN_IF_ERROR(GetNodeAttr(n_attrs, "T", &dtype));
      TensorShapeProto shape;
      TF_RETURN_IF_ERROR(GetNodeAttr(n_attrs, "shape", &shape));

      // Add the start node
      Node* start;
      TF_RETURN_IF_ERROR(make_node("_ParallelConcatStart")
                             .Attr("shape", shape)
                             .Attr("dtype", dtype)
                             .Finalize(g, &start));

      // Add all the inplace_updates.
      std::vector<Node*> control_nodes;
      int64 i = 0;
      for (const Edge* input_edge : n->in_edges()) {
        if (input_edge->IsControlEdge()) {
          g->AddControlEdge(input_edge->src(), start);
          continue;
        }

        Node* update;
        TF_RETURN_IF_ERROR(
            make_node("_ParallelConcatUpdate")
                .Attr("loc", i)
                .Input(start)
                .Input(input_edge->src(), input_edge->src_output())
                .Finalize(g, &update));
        control_nodes.push_back(update);

        ++i;
      }

      // Add the final identity.
      NodeBuilder identity_def = base_make_node("Identity", n->name());
      identity_def.Input(start, 0);
      for (Node* s : control_nodes) {
        identity_def.ControlInput(s);
      }
      Node* identity_node;
      TF_RETURN_IF_ERROR(identity_def.Finalize(g, &identity_node));

      // Remove the node and redirect edges.
      for (auto* e : n->out_edges()) {
        if (e->IsControlEdge()) {
          g->AddControlEdge(identity_node, e->dst());
        } else {
          g->AddEdge(identity_node, 0, e->dst(), e->dst_input());
        }
      }
      g->RemoveNode(n);
    }
    return Status::OK();
  }
};
REGISTER_OPTIMIZATION(OptimizationPassRegistry::PRE_PLACEMENT, 0,
                      ParallelConcatRemovePass);

}  // namespace
}  // namespace tensorflow