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
16 #include "tensorflow/compiler/xla/util.h"
17
18 #include <limits>
19 #include <list>
20
21 #include "tensorflow/compiler/xla/test.h"
22 #include "tensorflow/compiler/xla/types.h"
23 #include "tensorflow/core/platform/bfloat16.h"
24
25 namespace xla {
26 namespace {
27
28 // Verifies that, even with a different number of leading spaces, the
29 // Reindent routine turns them into a uniform number of leading spaces.
30 //
31 // Also throws in some trailing whitespace on the original to show it is
32 // removed.
TEST(UtilTest,ReindentsDifferentNumberOfLeadingSpacesUniformly)33 TEST(UtilTest, ReindentsDifferentNumberOfLeadingSpacesUniformly) {
34 string original = R"( hello there
35 world)";
36 string got = Reindent(original, " ");
37 string want = R"( hello there
38 world)";
39 EXPECT_EQ(want, got);
40 }
41
TEST(UtilTest,HumanReadableNumFlopsExample)42 TEST(UtilTest, HumanReadableNumFlopsExample) {
43 ASSERT_EQ("1.00GFLOP/s", HumanReadableNumFlops(1e9, 1e9));
44 }
45
TEST(UtilTest,CommaSeparatedString)46 TEST(UtilTest, CommaSeparatedString) {
47 EXPECT_EQ(CommaSeparatedString({}), "");
48 EXPECT_EQ(CommaSeparatedString({"hello world"}), "hello world");
49 EXPECT_EQ(CommaSeparatedString({1, 57, 2}, "foo", "bar"), "foo1, 57, 2bar");
50 }
51
TEST(UtilTest,VectorString)52 TEST(UtilTest, VectorString) {
53 std::list<int64> empty_list;
54 EXPECT_EQ(VectorString(empty_list), "()");
55
56 std::vector<float> float_vector = {5.5};
57 EXPECT_EQ(VectorString(float_vector), "(5.5)");
58
59 std::set<const char*> string_set = {"a", "b"};
60 EXPECT_EQ(VectorString(string_set), "(a, b)");
61
62 EXPECT_EQ(VectorString({}), "()");
63 EXPECT_EQ(VectorString({1, 57, 2}), "(1, 57, 2)");
64 }
65
TEST(UtilTest,LogLines)66 TEST(UtilTest, LogLines) {
67 // Just make sure this code runs (not verifying the output).
68 LogLines(tensorflow::INFO, "hello\n\nworld", __FILE__, __LINE__);
69 }
70
TEST(UtilTest,CommonFactors)71 TEST(UtilTest, CommonFactors) {
72 struct {
73 std::vector<int64> a, b;
74 absl::InlinedVector<std::pair<int64, int64>, 8> expected;
75 } test_cases[] = {
76 {/*.a =*/{0}, /*.b =*/{0}, /*.expected =*/{{0, 0}, {1, 1}}},
77 {/*.a =*/{0, 1}, /*.b =*/{0, 1}, /*.expected =*/{{0, 0}, {1, 1}, {2, 2}}},
78 {/*.a =*/{}, /*.b =*/{}, /*.expected =*/{{0, 0}}},
79 {/*.a =*/{2, 5, 1, 3},
80 /*.b =*/{1, 10, 3, 1},
81 /*.expected =*/{{0, 0}, {0, 1}, {2, 2}, {3, 2}, {4, 3}, {4, 4}}},
82 {/*.a =*/{1, 1, 3},
83 /*.b =*/{1, 1, 3},
84 /*.expected =*/{{0, 0}, {1, 1}, {2, 2}, {3, 3}}},
85 // Splitting and combining dimensions.
86 {/*.a =*/{2, 6},
87 /*.b =*/{4, 3},
88 /*.expected =*/{{0, 0}, {2, 2}}},
89 {/*.a =*/{1, 2, 6},
90 /*.b =*/{4, 1, 3, 1},
91 /*.expected =*/{{0, 0}, {1, 0}, {3, 3}, {3, 4}}},
92 // Extra degenerated dimension (second and third dims in the output) forms
93 // single common factor group.
94 {/*.a =*/{1, 2, 1},
95 /*.b =*/{1, 1, 1, 2},
96 /*.expected =*/{{0, 0}, {1, 1}, {1, 2}, {1, 3}, {2, 4}, {3, 4}}}};
97 for (const auto& test_case : test_cases) {
98 EXPECT_EQ(test_case.expected, CommonFactors(test_case.a, test_case.b));
99 }
100 }
101
TEST(UtilTest,SanitizeFileName)102 TEST(UtilTest, SanitizeFileName) {
103 EXPECT_EQ(SanitizeFileName(""), "");
104 EXPECT_EQ(SanitizeFileName("abc"), "abc");
105 EXPECT_EQ(SanitizeFileName("/\\[]"), "____");
106 EXPECT_EQ(SanitizeFileName("/A\\B[C]"), "_A_B_C_");
107 }
108
TEST(UtilTest,RoundTripFpToString)109 TEST(UtilTest, RoundTripFpToString) {
110 EXPECT_EQ(RoundTripFpToString(std::numeric_limits<Eigen::half>::quiet_NaN()),
111 "nan");
112 EXPECT_EQ(RoundTripFpToString(-std::numeric_limits<Eigen::half>::quiet_NaN()),
113 "-nan");
114 EXPECT_EQ(RoundTripFpToString(
115 std::numeric_limits<tensorflow::bfloat16>::quiet_NaN()),
116 "nan");
117 EXPECT_EQ(RoundTripFpToString(
118 -std::numeric_limits<tensorflow::bfloat16>::quiet_NaN()),
119 "-nan");
120 EXPECT_EQ(RoundTripFpToString(std::numeric_limits<float>::quiet_NaN()),
121 "nan");
122 EXPECT_EQ(RoundTripFpToString(-std::numeric_limits<float>::quiet_NaN()),
123 "-nan");
124 EXPECT_EQ(RoundTripFpToString(std::numeric_limits<double>::quiet_NaN()),
125 "nan");
126 EXPECT_EQ(RoundTripFpToString(-std::numeric_limits<double>::quiet_NaN()),
127 "-nan");
128 }
129
TEST(UtilTest,SplitF64ToF32)130 TEST(UtilTest, SplitF64ToF32) {
131 // Overflowing the F32 exponent in SplitF64ToF32 should result in a pair of
132 // [∞,0].
133 EXPECT_EQ(SplitF64ToF32(std::numeric_limits<double>::max()).first,
134 std::numeric_limits<float>::infinity());
135 EXPECT_EQ(SplitF64ToF32(std::numeric_limits<double>::max()).second, 0.0f);
136 }
137
138 } // namespace
139 } // namespace xla
140