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
BuildRetvalNode(Graph * graph,DataType type,int index)33 StatusOr<Node*> BuildRetvalNode(Graph* graph, DataType type, int index) {
34 const char* const kRetValOp = "_Retval";
35 NodeDef ret_def;
36 ret_def.set_op(kRetValOp);
37 ret_def.set_name(absl::StrCat(kRetValOp, index));
38 AddNodeAttr("T", type, &ret_def);
39 AddNodeAttr("index", index, &ret_def);
40 return graph->AddNode(ret_def);
41 }
42
ExtractWhileLoopFrames(const std::vector<ControlFlowInfo> & cf_info,const Graph * graph,std::unordered_map<string,WhileLoopFrame> * frames,const NodeFilter & node_filter)43 Status ExtractWhileLoopFrames(
44 const std::vector<ControlFlowInfo>& cf_info, const Graph* graph,
45 std::unordered_map<string, WhileLoopFrame>* frames,
46 const NodeFilter& node_filter) {
47 for (Node* node : graph->op_nodes()) {
48 const ControlFlowInfo& cf = cf_info[node->id()];
49
50 VLOG(2) << "node: " << node->name() << " (" << node->id()
51 << ") frame_name: " << cf.frame_name
52 << " frame: " << (cf.frame ? cf.frame->name() : "---")
53 << " parent_frame: "
54 << (cf.parent_frame ? cf.parent_frame->name() : "---");
55 TF_RET_CHECK(cf.frame != nullptr && cf.parent_frame != nullptr);
56
57 WhileLoopFrame& frame = (*frames)[cf.frame_name];
58 WhileLoopFrame* parent =
59 &(*frames)[cf_info[cf.parent_frame->id()].frame_name];
60 if (frame.parent == nullptr) {
61 frame.parent = parent;
62 frame.name = cf.frame_name;
63 ++parent->num_children;
64 }
65
66 if (IsEnter(node)) {
67 WhileLoopArg arg;
68 arg.enter = node;
69 TF_RETURN_IF_ERROR(GetNodeAttr(arg.enter->attrs(), "is_constant",
70 &arg.is_loop_invariant));
71 frame.args.push_back(arg);
72 } else if (IsLoopCond(node)) {
73 frame.loop_cond = node;
74 }
75 frame.nodes.insert(node);
76 if (node->IsControlFlow() && node_filter && !node_filter(node)) {
77 frame.should_be_functionalized = false;
78 }
79 }
80
81 return OkStatus();
82 }
83
84 // Check that the graph has no cycle containing the given node.
CheckNodeNotInCycle(const Node * node,const int num_nodes)85 Status CheckNodeNotInCycle(const Node* node, const int num_nodes) {
86 std::vector<const Node*> ready;
87 ready.push_back(node);
88 std::vector<bool> visited(num_nodes);
89 while (!ready.empty()) {
90 const Node* current_node = ready.back();
91 ready.pop_back();
92 visited[current_node->id()] = true;
93 for (const Edge* out : current_node->out_edges()) {
94 if (out->dst() == node) {
95 return errors::Internal("Detected a cycle: ", FormatNodeForError(*node),
96 " (", node->def().op(), ") feeds into itself.");
97 } else if (!visited[out->dst()->id()]) {
98 ready.push_back(out->dst());
99 }
100 }
101 }
102 return OkStatus();
103 }
104
105 } // namespace tensorflow
106