1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/core/data/rewrite_utils.h"
16
17 #include <memory>
18 #include <string>
19
20 #include "absl/strings/string_view.h"
21 #include "tensorflow/core/framework/function.h"
22 #include "tensorflow/core/framework/function.pb.h"
23 #include "tensorflow/core/framework/function_testlib.h"
24 #include "tensorflow/core/framework/graph.pb.h"
25 #include "tensorflow/core/framework/node_def.pb.h"
26 #include "tensorflow/core/framework/tensor_shape.h"
27 #include "tensorflow/core/framework/tensor_testutil.h"
28 #include "tensorflow/core/framework/types.pb.h"
29 #include "tensorflow/core/grappler/grappler_item.h"
30 #include "tensorflow/core/lib/gtl/array_slice.h"
31 #include "tensorflow/core/platform/statusor.h"
32 #include "tensorflow/core/platform/test.h"
33 #include "tensorflow/core/platform/types.h"
34
35 namespace tensorflow {
36 namespace data {
37 namespace {
38
39 using ::tensorflow::test::AsScalar;
40 using ::tensorflow::test::function::GDef;
41 using ::tensorflow::test::function::NDef;
42 using ::testing::ElementsAre;
43
GetMapNode(absl::string_view name,absl::string_view input_node_name,absl::string_view function_name)44 NodeDef GetMapNode(absl::string_view name, absl::string_view input_node_name,
45 absl::string_view function_name) {
46 return NDef(
47 name, /*op=*/"MapDataset", {std::string(input_node_name)},
48 {{"f", FunctionDefHelper::FunctionRef(std::string(function_name))},
49 {"Targuments", {}},
50 {"output_shapes", gtl::ArraySlice<TensorShape>{TensorShape()}},
51 {"output_types", gtl::ArraySlice<DataType>{DT_INT64}}});
52 }
53
XTimesX()54 FunctionDef XTimesX() {
55 return FunctionDefHelper::Create(
56 /*function_name=*/"XTimesX",
57 /*in_def=*/{"x: int64"},
58 /*out_def=*/{"y: int64"},
59 /*attr_def=*/{},
60 /*node_def=*/{{{"y"}, "Mul", {"x", "x"}, {{"T", DT_INT64}}}},
61 /*ret_def=*/{{"y", "y:z:0"}});
62 }
63
GetRangeSquareDatasetDef(const int64_t range)64 GraphDef GetRangeSquareDatasetDef(const int64_t range) {
65 return GDef(
66 {NDef("start", "Const", /*inputs=*/{},
67 {{"value", AsScalar<int64_t>(0)}, {"dtype", DT_INT64}}),
68 NDef("stop", "Const", /*inputs=*/{},
69 {{"value", AsScalar<int64_t>(range)}, {"dtype", DT_INT64}}),
70 NDef("step", "Const", /*inputs=*/{},
71 {{"value", AsScalar<int64_t>(1)}, {"dtype", DT_INT64}}),
72 NDef("range", "RangeDataset", /*inputs=*/{"start", "stop", "step"},
73 {{"output_shapes", gtl::ArraySlice<TensorShape>{TensorShape()}},
74 {"output_types", gtl::ArraySlice<DataType>{DT_INT64}}}),
75 GetMapNode("map", "range", "XTimesX"),
76 NDef("dataset", "_Retval", /*inputs=*/{"map"},
77 {{"T", DT_VARIANT}, {"index", 0}})},
78 {XTimesX()});
79 }
80
TEST(GraphUtilTest,GetFetchNode)81 TEST(GraphUtilTest, GetFetchNode) {
82 GraphDef graph = GetRangeSquareDatasetDef(10);
83 TF_ASSERT_OK_AND_ASSIGN(std::string dataset_node, GetDatasetNode(graph));
84 std::unique_ptr<tensorflow::grappler::GrapplerItem> grappler_item =
85 GetGrapplerItem(&graph, &dataset_node, /*add_fake_sinks=*/false);
86 EXPECT_THAT(grappler_item->fetch, ElementsAre("Sink"));
87 }
88
TEST(GraphUtilTest,GetFetchNodeDef)89 TEST(GraphUtilTest, GetFetchNodeDef) {
90 GraphDef graph = GetRangeSquareDatasetDef(10);
91 TF_ASSERT_OK_AND_ASSIGN(NodeDef dataset_nodedef, GetDatasetNodeDef(graph));
92 std::string dataset_node = dataset_nodedef.name();
93 std::unique_ptr<tensorflow::grappler::GrapplerItem> grappler_item =
94 GetGrapplerItem(&graph, &dataset_node, /*add_fake_sinks=*/false);
95 EXPECT_THAT(grappler_item->fetch, ElementsAre("Sink"));
96 }
97
98 struct SelectOptimizationsTestCase {
99 absl::flat_hash_set<string> experiments;
100 absl::flat_hash_set<tstring> optimizations_enabled;
101 absl::flat_hash_set<tstring> optimizations_disabled;
102 absl::flat_hash_set<tstring> optimizations_default;
103 std::vector<string> expected;
104 };
105
106 class SelectOptimizationsTest
107 : public ::testing::TestWithParam<SelectOptimizationsTestCase> {};
108
TEST_P(SelectOptimizationsTest,DatasetUtils)109 TEST_P(SelectOptimizationsTest, DatasetUtils) {
110 const SelectOptimizationsTestCase test_case = GetParam();
111 auto optimizations = SelectOptimizations(
112 test_case.experiments, test_case.optimizations_enabled,
113 test_case.optimizations_disabled, test_case.optimizations_default);
114 EXPECT_THAT(std::vector<string>(optimizations.begin(), optimizations.end()),
115 ::testing::UnorderedElementsAreArray(test_case.expected));
116 }
117
118 INSTANTIATE_TEST_SUITE_P(
119 Test, SelectOptimizationsTest,
120 ::testing::Values(
121 SelectOptimizationsTestCase{
122 /*experiments=*/{}, /*optimizations_enabled=*/{},
123 /*optimizations_disabled=*/{}, /*optimizations_default=*/{},
124 /*expected=*/{}},
125 SelectOptimizationsTestCase{
126 /*experiments=*/{"map_and_batch_fusion"},
127 /*optimizations_enabled=*/{"bar"},
128 /*optimizations_disabled=*/{}, /*optimizations_default=*/{"baz"},
129 /*expected=*/{"map_and_batch_fusion", "bar", "baz"}},
130 SelectOptimizationsTestCase{
131 /*experiments=*/{"this_is_not_an_optimization"},
132 /*optimizations_enabled=*/{"bar"},
133 /*optimizations_disabled=*/{}, /*optimizations_default=*/{"baz"},
134 /*expected=*/{"bar", "baz"}},
135 SelectOptimizationsTestCase{/*experiments=*/{},
136 /*optimizations_enabled=*/{"foo"},
137 /*optimizations_disabled=*/{"baz"},
138 /*optimizations_default=*/{"bar", "baz"},
139 /*expected=*/{"foo", "bar"}},
140 SelectOptimizationsTestCase{
141 /*experiments=*/{"foo"}, /*optimizations_enabled=*/{"bar"},
142 /*optimizations_disabled=*/{"foo"},
143 /*optimizations_default=*/{"baz"}, /*expected=*/{"bar", "baz"}}));
144
145 } // namespace
146 } // namespace data
147 } // namespace tensorflow
148