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 // Renames all nodes not uses as graph inputs or outputs to short numerical
29 // forms.
ObfuscateNames(const GraphDef & input_graph_def,const TransformFuncContext & context,GraphDef * output_graph_def)30 Status ObfuscateNames(const GraphDef& input_graph_def,
31 const TransformFuncContext& context,
32 GraphDef* output_graph_def) {
33 std::unordered_set<string> required_nodes;
34 for (const string& input : context.input_names) {
35 required_nodes.insert(input);
36 }
37 for (const string& output : context.output_names) {
38 required_nodes.insert(output);
39 }
40
41 const string valid_chars =
42 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
43 const int64_t chars_size = valid_chars.size();
44
45 std::map<string, string> new_names;
46 int64_t name_index = 0;
47 for (const NodeDef& input_node : input_graph_def.node()) {
48 const string& old_name = input_node.name();
49 string new_name;
50 if (required_nodes.count(old_name)) {
51 new_name = old_name;
52 } else {
53 do {
54 int64_t remaining = name_index;
55 new_name = "";
56 while (true) {
57 const int64_t remainder = (remaining % chars_size);
58 const char current_char = valid_chars[remainder];
59 new_name = current_char + new_name;
60 remaining /= chars_size;
61 if (remaining <= 0) {
62 break;
63 }
64 }
65 ++name_index;
66 } while (required_nodes.count(new_name));
67 }
68 new_names[old_name] = new_name;
69 }
70
71 output_graph_def->Clear();
72 for (const NodeDef& input_node : input_graph_def.node()) {
73 NodeDef* node = output_graph_def->mutable_node()->Add();
74 *node = input_node;
75 const string& old_name = input_node.name();
76 node->set_name(new_names[old_name]);
77 node->mutable_input()->Clear();
78 for (const string& input_name : input_node.input()) {
79 string prefix;
80 string input_node_name;
81 string suffix;
82 NodeNamePartsFromInput(input_name, &prefix, &input_node_name, &suffix);
83 if (new_names.count(input_node_name) == 0) {
84 return errors::InvalidArgument("No node named ", input_node_name,
85 " for input to ", old_name);
86 }
87 string new_input_name = prefix + new_names[input_node_name] + suffix;
88 *(node->mutable_input()->Add()) = new_input_name;
89 }
90 }
91
92 return Status::OK();
93 }
94
95 REGISTER_GRAPH_TRANSFORM("obfuscate_names", ObfuscateNames);
96
97 } // namespace graph_transforms
98 } // namespace tensorflow
99