1 /* Copyright 2017 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/grappler/optimizers/graph_optimizer_stage.h"
17 #include "tensorflow/core/graph/tensor_id.h"
18
19 namespace tensorflow {
20 namespace grappler {
21
ParseNodeScopeAndName(const string & node_name)22 const NodeScopeAndName ParseNodeScopeAndName(const string& node_name) {
23 auto pos = node_name.find_last_of('/');
24 if (pos == string::npos) {
25 return {"", node_name};
26 } else {
27 return {node_name.substr(0, pos), node_name.substr(pos + 1)};
28 }
29 };
30
GetInputNode(const GraphOptimizerContext & ctx,const string & input,NodeDef ** node)31 Status GetInputNode(const GraphOptimizerContext& ctx, const string& input,
32 NodeDef** node) {
33 string node_name = NodeName(input);
34 NodeDef* node_by_name = ctx.node_map->GetNode(node_name);
35 if (node_by_name == nullptr) {
36 return errors::FailedPrecondition("Node ", node_name,
37 " doesn't exists in a node map");
38 }
39 *node = node_by_name;
40 return Status::OK();
41 }
42
GetTensorProperties(const GraphOptimizerContext & ctx,const string & tensor,const OpInfo::TensorProperties ** properties)43 Status GetTensorProperties(const GraphOptimizerContext& ctx,
44 const string& tensor,
45 const OpInfo::TensorProperties** properties) {
46 if (ctx.graph_properties == nullptr) {
47 return errors::InvalidArgument("Graph properties are unknown.");
48 }
49
50 // TODO(ezhulenev): Make it TensorId when graph properties will support
51 // absl::string_view lookup.
52 SafeTensorId tensor_id = ParseTensorName(tensor);
53
54 if (tensor_id.index() < 0) {
55 return errors::InvalidArgument(
56 "Can't get tensor properties of control dependency ", tensor);
57 }
58
59 const auto& output_properties =
60 ctx.graph_properties->GetOutputProperties(tensor_id.node());
61 int num_outputs = output_properties.size();
62
63 if (num_outputs == 0 || tensor_id.index() > num_outputs - 1) {
64 return errors::InvalidArgument(
65 "Node ", tensor_id.node(),
66 " is missing output properties at position :", tensor_id.index(),
67 " (num_outputs=", num_outputs, ")");
68 }
69
70 *properties = &output_properties[tensor_id.index()];
71 return Status::OK();
72 }
73
AddCopyNode(const GraphOptimizerContext & ctx,const string & name,const NodeDef * node_to_copy)74 NodeDef* AddCopyNode(const GraphOptimizerContext& ctx, const string& name,
75 const NodeDef* node_to_copy) {
76 CHECK(node_to_copy != nullptr);
77 CHECK(!ctx.node_map->NodeExists(name))
78 << "Node " << name << " already exists in a graph";
79 NodeDef* new_node = ctx.optimized_graph->add_node();
80 *new_node = *node_to_copy;
81 new_node->set_name(name);
82 ctx.node_map->AddNode(name, new_node);
83 return new_node;
84 }
85
AddEmptyNode(const GraphOptimizerContext & ctx,const string & name)86 NodeDef* AddEmptyNode(const GraphOptimizerContext& ctx, const string& name) {
87 std::string new_name = name;
88 for (int count = 0; ctx.node_map->NodeExists(new_name); ++count) {
89 LOG(WARNING) << name << " already exists in the graph.";
90 new_name = absl::StrCat(name, "_", count);
91 }
92 NodeDef* new_node = ctx.optimized_graph->add_node();
93 new_node->set_name(new_name);
94 ctx.node_map->AddNode(new_name, new_node);
95 return new_node;
96 }
97
MakeOptimizedNodeName(const NodeScopeAndName & node,const string & sub_scope,const string & prefix)98 const string MakeOptimizedNodeName(const NodeScopeAndName& node,
99 const string& sub_scope,
100 const string& prefix) {
101 CHECK(!sub_scope.empty() || !prefix.empty())
102 << "Either optimized node name prefix or sub-scope must be non-empty";
103 string optimized_node_name;
104 if (!node.scope.empty()) {
105 strings::StrAppend(&optimized_node_name, node.scope, "/");
106 }
107 if (!sub_scope.empty()) {
108 strings::StrAppend(&optimized_node_name, sub_scope, "/");
109 }
110 if (!prefix.empty()) {
111 strings::StrAppend(&optimized_node_name, prefix, "_");
112 }
113 strings::StrAppend(&optimized_node_name, node.name);
114 return optimized_node_name;
115 }
116
MakeOptimizedNodeName(const NodeScopeAndName & root,const std::vector<string> node_names,const string & sub_scope,const string & prefix)117 const string MakeOptimizedNodeName(const NodeScopeAndName& root,
118 const std::vector<string> node_names,
119 const string& sub_scope,
120 const string& prefix) {
121 string optimized_node_name = MakeOptimizedNodeName(root, sub_scope, prefix);
122 for (const string& node_name : node_names) {
123 auto name_and_scope = ParseNodeScopeAndName(node_name);
124 strings::StrAppend(&optimized_node_name, "_", name_and_scope.name);
125 }
126 return optimized_node_name;
127 }
128
129 } // end namespace grappler
130 } // end namespace tensorflow
131