1 /* Copyright 2018 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
16 #include "tensorflow/compiler/xla/layout.h"
17
18 #include "absl/strings/str_cat.h"
19 #include "absl/strings/str_join.h"
20 #include "tensorflow/compiler/xla/layout_util.h"
21 #include "tensorflow/compiler/xla/shape_util.h"
22 #include "tensorflow/compiler/xla/status_macros.h"
23 #include "tensorflow/compiler/xla/test.h"
24 #include "tensorflow/compiler/xla/test_helpers.h"
25 #include "tensorflow/compiler/xla/types.h"
26 #include "tensorflow/compiler/xla/util.h"
27 #include "tensorflow/compiler/xla/xla_data.pb.h"
28
29 namespace xla {
30 namespace {
31
32 class LayoutTest : public ::testing::Test {};
33
TEST_F(LayoutTest,ToString)34 TEST_F(LayoutTest, ToString) {
35 EXPECT_EQ(Layout().ToString(), "{}");
36 EXPECT_EQ(Layout({4, 5, 6}).ToString(), "{4,5,6}");
37 EXPECT_EQ(Layout({4, 5, 6}).ToString(), "{4,5,6}");
38 EXPECT_EQ(
39 Layout({3, 2, 1, 0}, {}, {Tile({42, 123}), Tile({4, 5})}).ToString(),
40 "{3,2,1,0:T(42,123)(4,5)}");
41 EXPECT_EQ(Layout({1, 0}, {}, {Tile({2, 55})})
42 .set_element_size_in_bits(42)
43 .ToString(),
44 "{1,0:T(2,55)E(42)}");
45 EXPECT_EQ(Layout({3, 2, 1, 0}, {}, {Tile({42, 123}), Tile({4, 5})})
46 .set_memory_space(3)
47 .ToString(),
48 "{3,2,1,0:T(42,123)(4,5)S(3)}");
49 EXPECT_EQ(Layout({1, 0}, {}, {Tile({-2, 55})})
50 .set_element_size_in_bits(42)
51 .ToString(),
52 "{1,0:T(Invalid value -2,55)E(42)}");
53 }
54
TEST_F(LayoutTest,StreamOut)55 TEST_F(LayoutTest, StreamOut) {
56 {
57 std::ostringstream oss;
58 oss << Tile({7, 8});
59 EXPECT_EQ(oss.str(), "(7,8)");
60 }
61
62 {
63 std::ostringstream oss;
64 oss << Layout({0, 1, 2});
65 EXPECT_EQ(oss.str(), "{0,1,2}");
66 }
67 }
68
TEST_F(LayoutTest,Equality)69 TEST_F(LayoutTest, Equality) {
70 EXPECT_EQ(Layout(), Layout());
71 const std::vector<int64_t> empty_dims;
72 EXPECT_EQ(Layout(empty_dims), Layout(empty_dims));
73 EXPECT_EQ(Layout(), Layout(empty_dims));
74 EXPECT_EQ(Layout({0, 1, 2, 3}), Layout({0, 1, 2, 3}));
75 EXPECT_NE(Layout({0, 1, 2, 3}), Layout({0, 1, 2}));
76 EXPECT_EQ(Layout({0, 1, 2}, {}, {Tile({42, 44})}),
77 Layout({0, 1, 2}, {}, {Tile({42, 44})}));
78 EXPECT_NE(Layout({0, 1, 2}, {}, {Tile({42, 44})}),
79 Layout({0, 1, 2}, {}, {Tile({42, 45})}));
80 EXPECT_NE(Layout({0, 1, 2}, {}, {Tile({42, 44})}), Layout({0, 1, 2, 3}));
81 EXPECT_EQ(Layout({0, 1, 2}).set_element_size_in_bits(33),
82 Layout({0, 1, 2}).set_element_size_in_bits(33));
83 EXPECT_NE(Layout({0, 1, 2}).set_element_size_in_bits(33),
84 Layout({0, 1, 2}).set_element_size_in_bits(7));
85 EXPECT_EQ(Layout({0, 1, 2}).set_memory_space(3),
86 Layout({0, 1, 2}).set_memory_space(3));
87 EXPECT_NE(Layout({0, 1, 2}).set_memory_space(1),
88 Layout({0, 1, 2}).set_memory_space(3));
89 EXPECT_FALSE(Layout::Equal()(Layout({0, 1, 2}, {}, {Tile({42, 44})}),
90 Layout({0, 1, 2})));
91 EXPECT_TRUE(Layout::Equal().IgnoreTiles()(
92 Layout({0, 1, 2}, {}, {Tile({42, 44})}), Layout({0, 1, 2})));
93 EXPECT_FALSE(Layout::Equal()(Layout({0, 1, 2}, {}, {}, 32),
94 Layout({0, 1, 2}, {}, {}, 1)));
95 EXPECT_TRUE(Layout::Equal().IgnoreElementSize()(
96 Layout({0, 1, 2}, {}, {}, 32), Layout({0, 1, 2}, {}, {}, 1)));
97 EXPECT_TRUE(Layout::Equal().IgnoreMemorySpace()(
98 Layout({0, 1, 2}).set_memory_space(1),
99 Layout({0, 1, 2}).set_memory_space(3)));
100 }
101
TEST_F(LayoutTest,LayoutToFromProto)102 TEST_F(LayoutTest, LayoutToFromProto) {
103 // Round-trips a Layout through proto de/serialization.
104 auto expect_unchanged = [](const Layout& layout) {
105 EXPECT_EQ(layout, Layout::CreateFromProto(layout.ToProto()));
106 };
107
108 expect_unchanged(Layout());
109 expect_unchanged(Layout({1, 3, 2, 0}));
110 expect_unchanged(Layout({0, 1}).set_element_size_in_bits(42));
111 expect_unchanged(Layout({3, 2, 1, 0}, {}, {Tile({42, 123}), Tile({4, 5})}));
112 }
113
114 } // namespace
115 } // namespace xla
116