• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations 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/tools/graph_transforms/transform_utils.h"
25 
26 namespace tensorflow {
27 namespace graph_transforms {
28 
29 // Changes the op type of a specified op.
RenameOp(const GraphDef & input_graph_def,const TransformFuncContext & context,GraphDef * output_graph_def)30 Status RenameOp(const GraphDef& input_graph_def,
31                 const TransformFuncContext& context,
32                 GraphDef* output_graph_def) {
33   if (!context.params.count("old_op_name") ||
34       (context.params.at("old_op_name").size() != 1) ||
35       !context.params.count("new_op_name") ||
36       (context.params.at("new_op_name").size() != 1)) {
37     return errors::InvalidArgument(
38         "rename_op expects exactly one 'old_op_name' and 'new_op_name' "
39         "argument, e.g. rename_op(old_op_name=Mul, new_op_name=Multiply)");
40   }
41 
42   const string old_op_name = context.params.at("old_op_name")[0];
43   const string new_op_name = context.params.at("new_op_name")[0];
44   output_graph_def->Clear();
45   for (const NodeDef& node : input_graph_def.node()) {
46     NodeDef* new_node = output_graph_def->mutable_node()->Add();
47     *new_node = node;
48     if (node.op() == old_op_name) {
49       new_node->set_op(new_op_name);
50     }
51   }
52 
53   return Status::OK();
54 }
55 
56 REGISTER_GRAPH_TRANSFORM("rename_op", RenameOp);
57 
58 }  // namespace graph_transforms
59 }  // namespace tensorflow
60