| 55 | } |
| 56 | |
| 57 | TEST(LowerWhileOpTest, Simple) { |
| 58 | std::unique_ptr<Graph> graph(new Graph(OpRegistry::Global())); |
| 59 | |
| 60 | // Add test functions for cond and body. |
| 61 | FunctionDefLibrary f_lib_proto; |
| 62 | *f_lib_proto.add_function() = test::function::XTimesTwo(); |
| 63 | *f_lib_proto.add_function() = test::function::LessThanOrEqualToN(8); |
| 64 | |
| 65 | Scope root = Scope::NewRootScope().ExitOnError(); |
| 66 | TF_ASSERT_OK(root.graph()->AddFunctionLibrary(f_lib_proto)); |
| 67 | auto a = ops::Placeholder(root.WithOpName("A"), DT_INT32); |
| 68 | Node* while_node; |
| 69 | std::vector<NodeBuilder::NodeOut> inputs({NodeBuilder::NodeOut(a.node())}); |
| 70 | AttrValue cond_func; |
| 71 | cond_func.mutable_func()->set_name("LessThanOrEqualToN"); |
| 72 | AttrValue body_func; |
| 73 | body_func.mutable_func()->set_name("XTimesTwo"); |
| 74 | TF_ASSERT_OK( |
| 75 | NodeBuilder("while", "While", &root.graph()->flib_def()) |
| 76 | .Input(inputs) |
| 77 | .Attr("T", {DT_INT32}) |
| 78 | .Attr("cond", cond_func) |
| 79 | .Attr("body", body_func) |
| 80 | .Attr("parallel_iterations", 100) |
| 81 | .Attr(LowerFunctionalOpsPass::kLowerUsingSwitchMergeAttr, true) |
| 82 | .Finalize(root.graph(), &while_node)); |
| 83 | auto c = ops::Identity( |
| 84 | root.WithOpName("C").WithControlDependencies(Output(while_node)), |
| 85 | Output(while_node)); |
| 86 | TF_ASSERT_OK(root.DoShapeInference(while_node)); |
| 87 | TF_ASSERT_OK(root.ToGraph(graph.get())); |
| 88 | |
| 89 | // The input graph has no lower level control flow primitives. |
| 90 | int node_called_while_count = 0; |
| 91 | for (const auto* op : graph->op_nodes()) { |
| 92 | ASSERT_FALSE(op->IsEnter()); |
| 93 | ASSERT_FALSE(op->IsExit()); |
| 94 | ASSERT_FALSE(op->IsSwitch()); |
| 95 | ASSERT_FALSE(op->IsMerge()); |
| 96 | ASSERT_FALSE(op->IsNextIteration()); |
| 97 | ASSERT_FALSE(op->IsLoopCond()); |
| 98 | if (op->name() == "while") { |
| 99 | node_called_while_count++; |
| 100 | } |
| 101 | } |
| 102 | ASSERT_EQ(node_called_while_count, 1); |
| 103 | |
| 104 | TF_ASSERT_OK(Rewrite(&graph)); |
| 105 | |
| 106 | int enter_count = 0; |
| 107 | int exit_count = 0; |
| 108 | int switch_count = 0; |
| 109 | int merge_count = 0; |
| 110 | int next_iteration_count = 0; |
| 111 | node_called_while_count = 0; |
| 112 | int less_than_or_equan_to_n_count = 0; |
| 113 | int x_times_two_count = 0; |
| 114 |
nothing calls this directly
no test coverage detected