| 79 | }; |
| 80 | |
| 81 | TEST_F(LoopOptimizerTest, Basic) { |
| 82 | GraphDef graph; |
| 83 | AddSimpleNode("In", "Identity", {}, &graph); |
| 84 | AddEnterNode("InvariantEnter", "while/while_context", true, 1, {"In"}, |
| 85 | &graph); |
| 86 | AddSimpleNode("InvariantAdd", "Add", {"InvariantEnter", "InvariantEnter"}, |
| 87 | &graph); |
| 88 | AddSimpleNode("VariantAdd", "Add", {"InvariantAdd", "Identity"}, &graph); |
| 89 | AddEnterNode("VariantEnter", "while/while_context", false, 1, {"In"}, &graph); |
| 90 | AddSimpleNode("Merge", "Merge", {"VariantEnter", "NextIteration"}, &graph); |
| 91 | AddSimpleNode("Less/y", "Const", {"^Identity"}, &graph); |
| 92 | AddSimpleNode("Less", "Less", {"VariantAdd", "Less/y"}, &graph); |
| 93 | AddSimpleNode("LoopCond", "LoopCond", {"Less"}, &graph); |
| 94 | AddSimpleNode("Switch", "Switch", {"Merge", "LoopCond"}, &graph); |
| 95 | AddSimpleNode("Identity", "Identity", {"Switch:1"}, &graph); |
| 96 | AddSimpleNode("NextIteration", "NextIteration", {"VariantAdd"}, &graph); |
| 97 | AddSimpleNode("Exit", "Exit", {"Switch"}, &graph); |
| 98 | AddSimpleNode("Out", "Identity", {"Exit"}, &graph); |
| 99 | |
| 100 | GrapplerItem item; |
| 101 | item.graph = graph; |
| 102 | |
| 103 | LoopOptimizer optimizer; |
| 104 | EnableOnlyLoopInvariantNodeMotion(&optimizer); |
| 105 | GraphDef output; |
| 106 | TF_EXPECT_OK(optimizer.Optimize(nullptr, item, &output)); |
| 107 | |
| 108 | { // Original graph. |
| 109 | Status status; |
| 110 | utils::GraphView view(&graph, &status); |
| 111 | TF_ASSERT_OK(status); |
| 112 | FrameView frames; |
| 113 | TF_EXPECT_OK(frames.InferFromGraphView(view)); |
| 114 | |
| 115 | EXPECT_EQ(frames.num_frames(), 1); |
| 116 | const auto* invariant_add_node = view.GetNode("InvariantAdd"); |
| 117 | ASSERT_NE(invariant_add_node, nullptr); |
| 118 | const auto* invariant_add_node_def = invariant_add_node->node(); |
| 119 | ASSERT_EQ(frames.Frames(*invariant_add_node_def).size(), 1); |
| 120 | EXPECT_EQ(frames.Frames(*invariant_add_node_def).back(), 0); |
| 121 | const auto* variant_add_node = view.GetNode("VariantAdd"); |
| 122 | ASSERT_NE(variant_add_node, nullptr); |
| 123 | const auto* variant_add_node_def = variant_add_node->node(); |
| 124 | ASSERT_EQ(frames.Frames(*variant_add_node_def).size(), 1); |
| 125 | EXPECT_EQ(frames.Frames(*variant_add_node_def).back(), 0); |
| 126 | } |
| 127 | |
| 128 | { // Optimized graph. |
| 129 | Status status; |
| 130 | utils::GraphView view(&output, &status); |
| 131 | TF_ASSERT_OK(status); |
| 132 | FrameView frames; |
| 133 | TF_EXPECT_OK(frames.InferFromGraphView(view)); |
| 134 | |
| 135 | EXPECT_EQ(frames.num_frames(), 1); |
| 136 | const auto* invariant_add_node = view.GetNode("InvariantAdd"); |
| 137 | ASSERT_NE(invariant_add_node, nullptr); |
| 138 | const auto* invariant_add_node_def = invariant_add_node->node(); |
nothing calls this directly
no test coverage detected