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