• 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/core/common_runtime/constant_folding.h"
17 #include "tensorflow/core/common_runtime/graph_constructor.h"
18 #include "tensorflow/core/graph/node_builder.h"
19 #include "tensorflow/core/graph/subgraph.h"
20 #include "tensorflow/core/platform/init_main.h"
21 #include "tensorflow/core/public/session.h"
22 #include "tensorflow/tools/graph_transforms/fold_constants_lib.h"
23 #include "tensorflow/tools/graph_transforms/transform_utils.h"
24 
25 namespace tensorflow {
26 namespace graph_transforms {
27 
28 // Deletes a given attribute from the specified nodes.
RemoveAttribute(const GraphDef & input_graph_def,const TransformFuncContext & context,GraphDef * output_graph_def)29 Status RemoveAttribute(const GraphDef& input_graph_def,
30                        const TransformFuncContext& context,
31                        GraphDef* output_graph_def) {
32   if (!context.params.count("attribute_name") ||
33       (context.params.at("attribute_name").size() != 1)) {
34     return errors::InvalidArgument(
35         "remove_attribute expects exactly one 'attribute_name' "
36         "argument, e.g. remove_attribute(op_name=Mul, attribute_name=foo)");
37   }
38 
39   string op_name;
40   if (context.params.count("op_name")) {
41     if (context.params.at("op_name").size() != 1) {
42       return errors::InvalidArgument(
43           "remove_attribute expects a single op_name argument, but found ",
44           context.params.at("op_name").size());
45     }
46     op_name = context.params.at("op_name")[0];
47   } else {
48     op_name = "*";
49   }
50 
51   const string attribute_name = context.params.at("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(attribute_name))) {
58       new_node->mutable_attr()->erase(attribute_name);
59     }
60   }
61 
62   return Status::OK();
63 }
64 
65 REGISTER_GRAPH_TRANSFORM("remove_attribute", RemoveAttribute);
66 
67 }  // namespace graph_transforms
68 }  // namespace tensorflow
69