| 65 | } |
| 66 | |
| 67 | TEST(LowerCaseOpTest, Simple) { |
| 68 | std::unique_ptr<Graph> graph(new Graph(OpRegistry::Global())); |
| 69 | |
| 70 | // Add test functions for then and else branch. |
| 71 | FunctionDefLibrary f_lib_proto; |
| 72 | *(f_lib_proto.add_function()) = test::function::XTimesTwo(); |
| 73 | *(f_lib_proto.add_function()) = test::function::XTimesFour(); |
| 74 | *(f_lib_proto.add_function()) = test::function::XTimes16(); |
| 75 | |
| 76 | // Construct simple conditional that switches on `pred` and operates only on |
| 77 | // single input `A`. |
| 78 | Scope root = Scope::NewRootScope().ExitOnError(); |
| 79 | TF_ASSERT_OK(root.graph()->AddFunctionLibrary(f_lib_proto)); |
| 80 | auto a = ops::Placeholder(root.WithOpName("A"), DT_INT32); |
| 81 | auto branch_index = |
| 82 | ops::Placeholder(root.WithOpName("branch_index"), DT_INT32); |
| 83 | Node* written_if; |
| 84 | std::vector<NodeBuilder::NodeOut> inputs({NodeBuilder::NodeOut(a.node())}); |
| 85 | TF_ASSERT_OK( |
| 86 | NodeBuilder("case", "Case", &root.graph()->flib_def()) |
| 87 | .Input(branch_index.node()) |
| 88 | .Input(inputs) |
| 89 | .Attr("branches", |
| 90 | FuncListAttr({"XTimesTwo", "XTimesFour", "XTimes16"})) |
| 91 | .Attr(LowerFunctionalOpsPass::kLowerUsingSwitchMergeAttr, true) |
| 92 | .Attr("Tout", {DT_INT32}) |
| 93 | .Finalize(root.graph(), &written_if)); |
| 94 | TF_ASSERT_OK(root.DoShapeInference(written_if)); |
| 95 | TF_ASSERT_OK(root.ToGraph(graph.get())); |
| 96 | |
| 97 | // The input graph has no switch or merge nodes. |
| 98 | int node_called_case_count = 0; |
| 99 | for (const auto* op : graph->op_nodes()) { |
| 100 | ASSERT_FALSE(op->IsSwitch()); |
| 101 | ASSERT_FALSE(op->IsMerge()); |
| 102 | if (op->name() == "case") { |
| 103 | ++node_called_case_count; |
| 104 | } |
| 105 | } |
| 106 | ASSERT_EQ(node_called_case_count, 1); |
| 107 | |
| 108 | TF_ASSERT_OK(Rewrite(&graph)); |
| 109 | |
| 110 | // Verify the resultant graph has switch and merge nodes, and a node called |
| 111 | // `if` (but not If nodes). |
| 112 | int switch_count = 0; |
| 113 | int merge_count = 0; |
| 114 | node_called_case_count = 0; |
| 115 | for (const auto* op : graph->op_nodes()) { |
| 116 | if (op->IsSwitch()) { |
| 117 | ++switch_count; |
| 118 | } |
| 119 | if (op->IsMerge()) { |
| 120 | ++merge_count; |
| 121 | } |
| 122 | ASSERT_NE(op->type_string(), "Case"); |
| 123 | if (op->name() == "case") { |
| 124 | ++node_called_case_count; |
nothing calls this directly
no test coverage detected