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/client/lib/sorting.h"
17
18 #include <limits>
19
20 #include "tensorflow/compiler/xla/client/xla_builder.h"
21 #include "tensorflow/compiler/xla/test.h"
22 #include "tensorflow/compiler/xla/tests/client_library_test_base.h"
23 #include "tensorflow/compiler/xla/tests/test_macros.h"
24 #include "tensorflow/compiler/xla/types.h"
25
26 namespace xla {
27 namespace {
28
29 using SortingTest = ClientLibraryTestBase;
30
XLA_TEST_F(SortingTest,TopK3From8Values)31 XLA_TEST_F(SortingTest, TopK3From8Values) {
32 XlaBuilder builder(TestName());
33 auto x =
34 ConstantR1<float>(&builder, {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0});
35 xla::GetTupleElement(xla::TopK(x, 3), 0);
36 ComputeAndCompareR1<float>(&builder, {7.0, 6.0, 5.0}, {});
37 }
38
XLA_TEST_F(SortingTest,TopK3From8Indices)39 XLA_TEST_F(SortingTest, TopK3From8Indices) {
40 XlaBuilder builder(TestName());
41 auto x_rev =
42 ConstantR1<float>(&builder, {7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0});
43 xla::GetTupleElement(xla::TopK(x_rev, 3), 1);
44 ComputeAndCompareR1<int>(&builder, {0, 1, 2}, {});
45 }
46
47 // TODO(b/119930279): enable this test.
XLA_TEST_F(SortingTest,DISABLED_TopKFullSortMinInt)48 XLA_TEST_F(SortingTest, DISABLED_TopKFullSortMinInt) {
49 XlaBuilder builder(TestName());
50 auto x_rev = ConstantR1<int>(&builder, {std::numeric_limits<int>::min(),
51 std::numeric_limits<int>::min() + 1,
52 std::numeric_limits<int>::max()});
53 xla::GetTupleElement(xla::TopK(x_rev, 3), 1);
54 ComputeAndCompareR1<int>(&builder, {2, 1, 0}, {});
55 }
56
XLA_TEST_F(SortingTest,NOT_TopKFullSortMinInt)57 XLA_TEST_F(SortingTest, NOT_TopKFullSortMinInt) {
58 XlaBuilder builder(TestName());
59 auto x_rev = ConstantR1<int>(&builder, {std::numeric_limits<int>::min(),
60 std::numeric_limits<int>::min() + 1,
61 std::numeric_limits<int>::max()});
62 xla::GetTupleElement(xla::TopK(x_rev, 3), 1);
63 // TopK currently negates the keys, which doesn't work correctly for
64 // std::numeric_limits<int>::min(). Therefore, it will sort this key to the
65 // front instead of to the back.
66 ComputeAndCompareR1<int>(&builder, {0, 2, 1}, {});
67 }
68
XLA_TEST_F(SortingTest,TopKFullSort)69 XLA_TEST_F(SortingTest, TopKFullSort) {
70 XlaBuilder builder(TestName());
71 const int kSize = 16;
72 std::mt19937 eng;
73 std::uniform_real_distribution<float> u_dist(0.0, 100.0);
74 auto gen = std::bind(u_dist, eng);
75 std::vector<float> inputs(kSize);
76 std::generate(inputs.begin(), inputs.end(), gen);
77 auto x = ConstantR1<float>(&builder, inputs);
78 xla::GetTupleElement(xla::TopK(x, kSize), 0);
79
80 absl::c_sort(inputs, std::greater<float>());
81 ComputeAndCompareR1<float>(&builder, inputs, {});
82 }
83
XLA_TEST_F(SortingTest,TopKFullSortWithDuplicates)84 XLA_TEST_F(SortingTest, TopKFullSortWithDuplicates) {
85 XlaBuilder builder(TestName());
86 XlaOp a;
87 auto a_data = CreateR1Parameter<int>({1, 1, 2, 2, 1}, 0, "a", &builder, &a);
88 xla::GetTupleElement(xla::TopK(a, 5), 1);
89 ComputeAndCompareR1<int>(&builder, {2, 3, 0, 1, 4}, {a_data.get()});
90 }
91
92 } // namespace
93 } // namespace xla
94