1 /* Copyright 2017 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/compiler/tf2xla/sharding_util.h"
16
17 #include <functional>
18
19 #include "tensorflow/core/lib/core/status_test_util.h"
20 #include "tensorflow/core/platform/test.h"
21
22 namespace tensorflow {
23
TEST(CoreUtilTest,ParseShardingFromDevice)24 TEST(CoreUtilTest, ParseShardingFromDevice) {
25 Graph graph(OpRegistry::Global());
26
27 auto core_from_sharding =
28 [](absl::optional<xla::OpSharding> sharding) -> int64 {
29 if (sharding.has_value() &&
30 sharding.value().type() == xla::OpSharding::MAXIMAL) {
31 return sharding.value().tile_assignment_devices(0);
32 } else {
33 return -1;
34 }
35 };
36
37 auto parse_status = ParseShardingFromDevice("", 1);
38 TF_EXPECT_OK(parse_status.status());
39 EXPECT_EQ(-1, core_from_sharding(parse_status.ValueOrDie()));
40 parse_status = ParseShardingFromDevice("", 100);
41 TF_EXPECT_OK(parse_status.status());
42 EXPECT_EQ(-1, core_from_sharding(parse_status.ValueOrDie()));
43
44 parse_status = ParseShardingFromDevice("/device:A_REPLICATED_CORE:-1", 100);
45 EXPECT_FALSE(parse_status.ok());
46
47 parse_status = ParseShardingFromDevice("/device:A_REPLICATED_CORE:55", 100);
48 TF_EXPECT_OK(parse_status.status());
49 EXPECT_EQ(55, core_from_sharding(parse_status.ValueOrDie()));
50
51 parse_status = ParseShardingFromDevice("/device:A_REPLICATED_CORE:100", 100);
52 EXPECT_FALSE(parse_status.ok());
53
54 parse_status = ParseShardingFromDevice("/cpu:0", 100);
55 TF_EXPECT_OK(parse_status.status());
56 EXPECT_EQ(-1, core_from_sharding(parse_status.ValueOrDie()));
57 }
58
59 class ShardingWithMetadataTest
60 : public ::testing::TestWithParam<xla::OpSharding> {};
61
TEST_P(ShardingWithMetadataTest,GetShardingFromNode)62 TEST_P(ShardingWithMetadataTest, GetShardingFromNode) {
63 NodeDef node_def;
64 {
65 node_def.set_op("_Arg");
66 node_def.set_name("arg");
67 AttrValue xla_sharding;
68 xla_sharding.set_s("");
69 AttrValue index;
70 index.set_i(0);
71 AttrValue type;
72 type.set_type(DataType::DT_FLOAT);
73 node_def.mutable_attr()->insert(
74 {{"_XlaSharding", xla_sharding}, {"index", index}, {"T", type}});
75 }
76
77 auto check_metadata = [](const xla::OpSharding& sharding) {
78 ASSERT_EQ(sharding.metadata_size(), 1);
79 const auto& metadata = sharding.metadata(0);
80 EXPECT_EQ(metadata.op_type(), "_Arg");
81 EXPECT_EQ(metadata.op_name(), "arg");
82 };
83
84 auto test_sharding_metadata =
85 [&check_metadata](
86 const std::function<xla::StatusOr<absl::optional<xla::OpSharding>>()>&
87 fn) {
88 auto status_or_sharding = fn();
89 TF_ASSERT_OK(status_or_sharding.status());
90 ASSERT_TRUE(status_or_sharding.ValueOrDie().has_value());
91 auto& sharding = status_or_sharding.ValueOrDie();
92 ASSERT_TRUE(sharding.has_value());
93 if (sharding->type() == xla::OpSharding::TUPLE) {
94 EXPECT_TRUE(sharding->metadata().empty());
95 for (const auto& sharding_element : sharding->tuple_shardings()) {
96 check_metadata(sharding_element);
97 }
98 } else {
99 check_metadata(sharding.value());
100 }
101 };
102
103 {
104 test_sharding_metadata([&node_def]() {
105 return GetShardingFromNodeDef(node_def, /*add_metadata=*/true);
106 });
107 }
108
109 {
110 test_sharding_metadata([&node_def]() {
111 return ParseShardingFromDevice(node_def, /*num_cores_per_replica=*/1,
112 /*add_metadata=*/true);
113 });
114 }
115
116 {
117 Graph graph(OpRegistry::Global());
118 Status status;
119 Node* node = graph.AddNode(node_def, &status);
120 TF_ASSERT_OK(status);
121
122 test_sharding_metadata([node]() {
123 return ParseShardingFromDevice(*node, /*num_cores_per_replica=*/1,
124 /*add_metadata=*/true);
125 });
126 }
127 }
128
CreateTupleSharding()129 xla::OpSharding CreateTupleSharding() {
130 xla::OpSharding sharding;
131 sharding.set_type(xla::OpSharding::TUPLE);
132 sharding.add_tuple_shardings()->set_type(xla::OpSharding::REPLICATED);
133 sharding.add_tuple_shardings()->set_type(xla::OpSharding::REPLICATED);
134 return sharding;
135 }
136
137 INSTANTIATE_TEST_SUITE_P(GetShardingFromNode, ShardingWithMetadataTest,
138 ::testing::Values(xla::sharding_builder::Replicate(),
139 CreateTupleSharding()));
140
141 } // namespace tensorflow
142