1/* Copyright 2016 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
16#include "tensorflow/tools/graph_transforms/fold_constants_lib.h"
17
18#include "tensorflow/core/common_runtime/constant_folding.h"
19#include "tensorflow/core/graph/graph_constructor.h"
20#include "tensorflow/core/graph/node_builder.h"
21#include "tensorflow/core/graph/subgraph.h"
22#include "tensorflow/core/platform/init_main.h"
23#include "tensorflow/core/public/session.h"
24#include "tensorflow/core/util/command_line_flags.h"
25#include "tensorflow/tools/graph_transforms/transform_utils.h"
26
27namespace tensorflow {
28namespace graph_transforms {
29
30Status RenameAttribute(const GraphDef& input_graph_def,
31                       const TransformFuncContext& context,
32                       GraphDef* output_graph_def) {
33  if (!context.params.count("old_attribute_name") ||
34      (context.params.at("old_attribute_name").size() != 1) ||
35      !context.params.count("new_attribute_name") ||
36      (context.params.at("new_attribute_name").size() != 1)) {
37    return errors::InvalidArgument(
38        "remove_nodes expects exactly one 'old_attribute_name' and one "
39        "'new_attribute_name' argument, e.g. "
40        "remove_attribute(old_attribute_name=foo, new_attribute_name=bar)");
41  }
42
43  string op_name;
44  if (context.params.count("op_name")) {
45    op_name = context.params.at("op_name")[0];
46  } else {
47    op_name = "*";
48  }
49
50  const string old_attribute_name = context.params.at("old_attribute_name")[0];
51  const string new_attribute_name = context.params.at("new_attribute_name")[0];
52  output_graph_def->Clear();
53  for (const NodeDef& node : input_graph_def.node()) {
54    NodeDef* new_node = output_graph_def->mutable_node()->Add();
55    *new_node = node;
56    if (((op_name == "*") || (op_name == node.op())) &&
57        (node.attr().count(old_attribute_name))) {
58      AttrValue attribute_value = node.attr().at(old_attribute_name);
59      new_node->mutable_attr()->erase(old_attribute_name);
60      new_node->mutable_attr()->insert({new_attribute_name, attribute_value});
61    }
62  }
63
64  return Status::OK();
65}
66
67REGISTER_GRAPH_TRANSFORM("rename_attribute", RenameAttribute);
68
69}  // namespace graph_transforms
70}  // namespace tensorflow
71