| 222 | }; |
| 223 | |
| 224 | TEST_F(ConstantFoldingTest, SimpleFolding) { |
| 225 | // Build a simple graph with a few trivially prunable ops. |
| 226 | tensorflow::Scope s = tensorflow::Scope::NewRootScope(); |
| 227 | |
| 228 | Output a = ops::Const(s.WithOpName("a"), 1.0f, {1}); |
| 229 | Output b = ops::Const(s.WithOpName("b"), 2.0f, {1}); |
| 230 | Output c = ops::AddN(s.WithOpName("c").WithDevice("/CPU:0"), {a, b}); |
| 231 | Output d = ops::AddN(s.WithOpName("d"), {b, c}); |
| 232 | |
| 233 | GrapplerItem item; |
| 234 | item.fetch.push_back("d"); |
| 235 | TF_CHECK_OK(s.ToGraphDef(&item.graph)); |
| 236 | |
| 237 | ConstantFolding optimizer(/*cpu_device=*/nullptr); |
| 238 | GraphDef output; |
| 239 | Status status = optimizer.Optimize(/*cluster=*/nullptr, item, &output); |
| 240 | TF_EXPECT_OK(status); |
| 241 | |
| 242 | EXPECT_EQ(1, output.node_size()); |
| 243 | |
| 244 | const NodeDef& node_d = output.node(0); |
| 245 | EXPECT_EQ("d", node_d.name()); |
| 246 | EXPECT_EQ("Const", node_d.op()); |
| 247 | |
| 248 | std::vector<string> fetch = {"d"}; |
| 249 | auto tensors_expected = EvaluateNodes(item.graph, fetch); |
| 250 | auto tensors = EvaluateNodes(output, fetch); |
| 251 | EXPECT_EQ(1, tensors_expected.size()); |
| 252 | EXPECT_EQ(1, tensors.size()); |
| 253 | test::ExpectTensorEqual<float>(tensors_expected[0], tensors[0]); |
| 254 | } |
| 255 | |
| 256 | TEST_F(ConstantFoldingTest, AddTree) { |
| 257 | tensorflow::Scope s = tensorflow::Scope::NewRootScope(); |
nothing calls this directly
no test coverage detected