• 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/compiler/tf2xla/functionalize_control_flow_util.h"
17 
18 #include "tensorflow/core/framework/node_def.pb.h"
19 #include "tensorflow/core/graph/graph_node_util.h"
20 
21 namespace tensorflow {
22 
operator ()(const Node * lhs,const Node * rhs) const23 bool NodeCmpByNameResourcesLast::operator()(const Node* lhs,
24                                             const Node* rhs) const {
25   bool lhs_is_resource =
26       lhs->num_inputs() > 0 ? (lhs->input_type(0) == DT_RESOURCE) : false;
27   bool rhs_is_resource =
28       rhs->num_inputs() > 0 ? (rhs->input_type(0) == DT_RESOURCE) : false;
29   return std::tie(lhs_is_resource, lhs->name()) <
30          std::tie(rhs_is_resource, rhs->name());
31 }
32 
AddNodeDefToGraph(const NodeDef & node_def,Graph * graph)33 xla::StatusOr<Node*> AddNodeDefToGraph(const NodeDef& node_def, Graph* graph) {
34   Status status;
35   Node* inserted_node = graph->AddNode(node_def, &status);
36   if (!status.ok()) {
37     return status;
38   }
39   return inserted_node;
40 }
41 
BuildRetvalNode(Graph * graph,DataType type,int index)42 xla::StatusOr<Node*> BuildRetvalNode(Graph* graph, DataType type, int index) {
43   const char* const kRetValOp = "_Retval";
44   NodeDef ret_def;
45   ret_def.set_op(kRetValOp);
46   ret_def.set_name(absl::StrCat(kRetValOp, index));
47   AddNodeAttr("T", type, &ret_def);
48   AddNodeAttr("index", index, &ret_def);
49   return AddNodeDefToGraph(ret_def, graph);
50 }
51 
ExtractWhileLoopFrames(const std::vector<ControlFlowInfo> & cf_info,const Graph * graph,std::unordered_map<string,WhileLoopFrame> * frames,const NodeFilter & node_filter)52 Status ExtractWhileLoopFrames(
53     const std::vector<ControlFlowInfo>& cf_info, const Graph* graph,
54     std::unordered_map<string, WhileLoopFrame>* frames,
55     const NodeFilter& node_filter) {
56   for (Node* node : graph->op_nodes()) {
57     const ControlFlowInfo& cf = cf_info[node->id()];
58 
59     VLOG(2) << "node: " << node->name() << " (" << node->id()
60             << ") frame_name: " << cf.frame_name
61             << " frame: " << (cf.frame ? cf.frame->name() : "---")
62             << " parent_frame: "
63             << (cf.parent_frame ? cf.parent_frame->name() : "---");
64     TF_RET_CHECK(cf.frame != nullptr && cf.parent_frame != nullptr);
65 
66     WhileLoopFrame& frame = (*frames)[cf.frame_name];
67     WhileLoopFrame* parent =
68         &(*frames)[cf_info[cf.parent_frame->id()].frame_name];
69     if (frame.parent == nullptr) {
70       frame.parent = parent;
71       frame.name = cf.frame_name;
72       ++parent->num_children;
73     }
74 
75     if (IsEnter(node)) {
76       WhileLoopArg arg;
77       arg.enter = node;
78       TF_RETURN_IF_ERROR(GetNodeAttr(arg.enter->attrs(), "is_constant",
79                                      &arg.is_loop_invariant));
80       frame.args.push_back(arg);
81     } else if (IsLoopCond(node)) {
82       frame.loop_cond = node;
83     }
84     frame.nodes.insert(node);
85     if (node->IsControlFlow() && node_filter && !node_filter(node)) {
86       frame.should_be_functionalized = false;
87     }
88   }
89 
90   return Status::OK();
91 }
92 
93 // Check that the graph has no cycle containing the given node.
CheckNodeNotInCycle(const Node * node,const int num_nodes)94 Status CheckNodeNotInCycle(const Node* node, const int num_nodes) {
95   std::vector<const Node*> ready;
96   ready.push_back(node);
97   std::vector<bool> visited(num_nodes);
98   while (!ready.empty()) {
99     const Node* current_node = ready.back();
100     ready.pop_back();
101     visited[current_node->id()] = true;
102     for (const Edge* out : current_node->out_edges()) {
103       if (out->dst() == node) {
104         return errors::Internal("Detected a cycle: ", FormatNodeForError(*node),
105                                 " (", node->def().op(), ") feeds into itself.");
106       } else if (!visited[out->dst()->id()]) {
107         ready.push_back(out->dst());
108       }
109     }
110   }
111   return Status::OK();
112 }
113 
114 }  // namespace tensorflow
115