| 29 | namespace { |
| 30 | |
| 31 | TEST(ConstAnalysisTest, Basics) { |
| 32 | Scope root = Scope::NewRootScope(); |
| 33 | |
| 34 | auto arg0 = ops::_Arg(root.WithOpName("Arg0"), DT_INT32, 0); |
| 35 | auto arg1 = ops::_Arg(root.WithOpName("Arg1"), DT_INT32, 1); |
| 36 | auto arg2 = ops::_Arg(root.WithOpName("Arg2"), DT_INT32, 2); |
| 37 | auto arg3 = ops::_Arg(root.WithOpName("Arg3"), DT_INT32, 3); |
| 38 | auto a = ops::Shape(root, arg0); |
| 39 | auto b = ops::Add(root, a, arg1); |
| 40 | auto c = ops::Reshape(root, arg2, b); |
| 41 | auto d = ops::Mul(root, c, ops::Sum(root, arg3, arg3)); |
| 42 | |
| 43 | FixupSourceAndSinkEdges(root.graph()); |
| 44 | |
| 45 | std::vector<bool> const_args(4, false); |
| 46 | std::vector<bool> const_nodes(root.graph()->num_node_ids(), false); |
| 47 | TF_ASSERT_OK(BackwardsConstAnalysis(*root.graph(), &const_args, &const_nodes, |
| 48 | /*flib_runtime=*/nullptr)); |
| 49 | |
| 50 | // Arg 0 doesn't need to be constant since the graph only uses its shape. |
| 51 | // Arg 1 must be constant because it flows to the shape argument of a Reshape. |
| 52 | // Arg 2 is used only as the value input to a Reshape and need not be const. |
| 53 | // Arg 3 is used as the reduction-indices argument to Sum and must be const. |
| 54 | EXPECT_EQ(const_args, std::vector<bool>({false, true, false, true})); |
| 55 | |
| 56 | EXPECT_FALSE(const_nodes[arg0.node()->id()]); |
| 57 | EXPECT_TRUE(const_nodes[arg1.node()->id()]); |
| 58 | EXPECT_FALSE(const_nodes[arg2.node()->id()]); |
| 59 | EXPECT_TRUE(const_nodes[arg3.node()->id()]); |
| 60 | } |
| 61 | |
| 62 | // Regression test for a case where the backward const analysis did |
| 63 | // not visit nodes in topological order. |
nothing calls this directly
no test coverage detected