1/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#include "tensorflow/core/common_runtime/graph_constructor.h"
16#include "tensorflow/core/graph/node_builder.h"
17#include "tensorflow/tools/graph_transforms/transform_utils.h"
18
19namespace tensorflow {
20namespace graph_transforms {
21
22// Remove control dependencies in preparation for inference.
23// In the tensorflow graph, control dependencies are represented as extra
24// inputs which are referenced with "^tensor_name".
25// See node_def.proto for more details.
26Status RemoveControlDependencies(const GraphDef& input_graph_def,
27 const TransformFuncContext& context,
28 GraphDef* output_graph_def) {
29 output_graph_def->Clear();
30 for (const NodeDef& node : input_graph_def.node()) {
31 NodeDef* new_node = output_graph_def->mutable_node()->Add();
32 *new_node = node;
33 new_node->clear_input();
34 for (const auto& input : node.input()) {
35 if (input[0] != '^') {
36 new_node->add_input(input);
37 }
38 }
39 }
40 return OkStatus();
41}
42
43REGISTER_GRAPH_TRANSFORM("remove_control_dependencies", RemoveControlDependencies);
44
45} // namespace graph_transforms
46} // namespace tensorflow
47