• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,OpInfo::TensorProperties * properties)43 Status GetTensorProperties(const GraphOptimizerContext& ctx,
44                            const string& tensor,
45                            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   auto 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->CopyFrom(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   CHECK(!ctx.node_map->NodeExists(name))
88       << "Node " << name << " already exists in a graph";
89   NodeDef* new_node = ctx.optimized_graph->add_node();
90   new_node->set_name(name);
91   ctx.node_map->AddNode(name, new_node);
92   return new_node;
93 }
94 
MakeOptimizedNodeName(const NodeScopeAndName & node,const string & sub_scope,const string & prefix)95 const string MakeOptimizedNodeName(const NodeScopeAndName& node,
96                                    const string& sub_scope,
97                                    const string& prefix) {
98   CHECK(!sub_scope.empty() || !prefix.empty())
99       << "Either optimized node name prefix or sub-scope must be non-empty";
100   string optimized_node_name;
101   if (!node.scope.empty()) {
102     strings::StrAppend(&optimized_node_name, node.scope, "/");
103   }
104   if (!sub_scope.empty()) {
105     strings::StrAppend(&optimized_node_name, sub_scope, "/");
106   }
107   if (!prefix.empty()) {
108     strings::StrAppend(&optimized_node_name, prefix, "_");
109   }
110   strings::StrAppend(&optimized_node_name, node.name);
111   return optimized_node_name;
112 }
113 
MakeOptimizedNodeName(const NodeScopeAndName & root,const std::vector<string> node_names,const string & sub_scope,const string & prefix)114 const string MakeOptimizedNodeName(const NodeScopeAndName& root,
115                                    const std::vector<string> node_names,
116                                    const string& sub_scope,
117                                    const string& prefix) {
118   string optimized_node_name = MakeOptimizedNodeName(root, sub_scope, prefix);
119   for (const string& node_name : node_names) {
120     auto name_and_scope = ParseNodeScopeAndName(node_name);
121     strings::StrAppend(&optimized_node_name, "_", name_and_scope.name);
122   }
123   return optimized_node_name;
124 }
125 
126 }  // end namespace grappler
127 }  // end namespace tensorflow
128