| 36 | class FunctionOptimizerTest : public GrapplerTest {}; |
| 37 | |
| 38 | TEST_F(FunctionOptimizerTest, InlineFunction_SimpleFunction) { |
| 39 | using test::function::NDef; |
| 40 | |
| 41 | FunctionOptimizer optimizer(RewriterConfig::DEFAULT, true); |
| 42 | |
| 43 | // Build a graph to compute y = XTimesTwo(x) |
| 44 | GrapplerItem item; |
| 45 | item.graph = test::function::GDef( |
| 46 | {NDef("x", "Placeholder", {}, {{"dtype", DT_FLOAT}}, kDevice), |
| 47 | NDef("y", "XTimesTwo", {"x"}, {{"T", DT_FLOAT}}, kDevice), |
| 48 | NDef("z", "Identity", {"y"}, {{"T", DT_FLOAT}}, kDevice)}, |
| 49 | // FunctionLib |
| 50 | { |
| 51 | test::function::XTimesTwo(), |
| 52 | }); |
| 53 | |
| 54 | GraphDef output; |
| 55 | TF_EXPECT_OK(optimizer.Optimize(nullptr, item, &output)); |
| 56 | |
| 57 | const string arg0 = "Func/y/input/_0"; |
| 58 | const string ret0 = "Func/y/output/_1"; |
| 59 | |
| 60 | const Tensor kTwo = test::AsScalar<int64>(2); |
| 61 | GraphDef expected = test::function::GDef( |
| 62 | {NDef("x", "Placeholder", {}, {{"dtype", DT_FLOAT}}), |
| 63 | NDef(arg0, "Identity", {"x"}, {{"T", DT_FLOAT}}), |
| 64 | NDef("y/two", "Const", {}, {{"dtype", DT_INT64}, {"value", kTwo}}), |
| 65 | NDef("y/scale", "Cast", {"y/two"}, |
| 66 | {{"DstT", DT_FLOAT}, {"SrcT", DT_INT64}}), |
| 67 | NDef("y/y", "Mul", {arg0, "y/scale"}, {{"T", DT_FLOAT}}), |
| 68 | NDef(ret0, "Identity", {"y/y"}, {{"T", DT_FLOAT}}), |
| 69 | NDef("z", "Identity", {ret0}, {{"T", DT_FLOAT}})}, |
| 70 | {}); |
| 71 | for (NodeDef& node : *expected.mutable_node()) node.set_device(kDevice); |
| 72 | |
| 73 | CompareGraphs(expected, output); |
| 74 | |
| 75 | Tensor pi = test::AsScalar<float>(3.14f); |
| 76 | item.fetch = {"z"}; |
| 77 | item.feed.emplace_back("x", pi); |
| 78 | auto tensors_expected = EvaluateFetchNodes(item); |
| 79 | GrapplerItem optimized = item.WithGraph(std::move(output)); |
| 80 | auto tensors = EvaluateFetchNodes(optimized); |
| 81 | test::ExpectTensorEqual<float>(tensors_expected[0], tensors[0]); |
| 82 | } |
| 83 | |
| 84 | TEST_F(FunctionOptimizerTest, InlineFunction_FixedTypeFunction) { |
| 85 | using test::function::NDef; |
nothing calls this directly
no test coverage detected