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/graph/control_flow.h" 17 18 #include <deque> 19 #include <vector> 20 21 #include "tensorflow/core/framework/types.h" 22 #include "tensorflow/core/graph/node_builder.h" 23 #include "tensorflow/core/lib/core/errors.h" 24 25 namespace tensorflow { 26 27 Status BuildControlFlowInfo(Graph* g, std::vector<ControlFlowInfo>* info) { 28 info->clear(); 29 info->resize(g->num_node_ids()); 30 31 std::vector<const Node*> parent_nodes; 32 parent_nodes.resize(g->num_node_ids()); 33 34 Node* src_node = g->source_node(); 35 ControlFlowInfo& src_info = (*info)[src_node->id()]; 36 src_info.frame = src_node; 37 src_info.parent_frame = src_node; 38 39 string frame_name; 40 std::deque<Node*> ready; 41 ready.push_back(src_node); 42 while (!ready.empty()) { 43 Node* curr_node = ready.front(); 44 ready.pop_front(); 45 const ControlFlowInfo& curr_info = (*info)[curr_node->id()]; 46 const Node* frame = curr_info.frame; 47 const Node* parent = curr_info.parent_frame; 48 frame_name = curr_info.frame_name; 49 50 if (IsExit(curr_node)) { 51 // Exit to the parent frame. 52 const ControlFlowInfo& parent_info = (*info)[parent->id()]; 53 frame = parent_info.frame; 54 parent = parent_info.parent_frame; 55 frame_name = parent_info.frame_name; 56 } 57 58 for (const Edge* out_edge : curr_node->out_edges()) { 59 Node* out = out_edge->dst(); 60 int out_id = out->id(); 61 ControlFlowInfo* out_info = &(*info)[out_id]; 62 const Node* out_parent = out_info->parent_frame; 63 bool is_visited = (parent_nodes[out_id] != nullptr); 64 65 // Skip Sink/Source nodes. 66 if (!out->IsOp()) continue; 67 68 // Add to ready queue if not seen. 69 if (!is_visited) { 70 parent_nodes[out->id()] = curr_node; 71 ready.push_back(out); 72 } 73 74 // Process the node 'out'. 75 if (IsEnter(out)) { 76 if (is_visited) { 77 const string& parent_frame = (*info)[out_parent->id()].frame_name; 78 if (parent_frame != frame_name) { 79 return errors::InvalidArgument( 80 "The node '", out->name(), 81 "' has inputs from different " 82 "frames. The input '", 83 curr_node->name(), "' is in frame '", frame_name, 84 "'. The input '", parent_nodes[out->id()]->name(), 85 "' is in frame '", parent_frame, "'."); 86 } 87 } else { 88 out_info->frame = out; 89 out_info->parent_frame = frame; 90 TF_RETURN_IF_ERROR( 91 GetNodeAttr(out->attrs(), "frame_name", &out_info->frame_name)); 92 if (out_info->frame_name.empty()) { 93 return errors::InvalidArgument("The Enter node ", out->name(), 94 " must have a frame name."); 95 } 96 } 97 } else { 98 if (is_visited) { 99 if (out_info->frame_name != frame_name) { 100 return errors::InvalidArgument( 101 "The node '", out->name(), 102 "' has inputs from different " 103 "frames. The input '", 104 curr_node->name(), "' is in frame '", frame_name, 105 "'. The input '", parent_nodes[out->id()]->name(), 106 "' is in frame '", out_info->frame_name, "'."); 107 } 108 } else { 109 out_info->frame = frame; 110 out_info->parent_frame = parent; 111 out_info->frame_name = frame_name; 112 } 113 } 114 } 115 } 116 return Status::OK(); 117 } 118 119 } // namespace tensorflow 120