This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
TopKTest.cpp
391 lines (311 loc) · 9.96 KB
/
TopKTest.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright 2004-present Facebook. All Rights Reserved.
#include "cuda/TopKTestBindings.cuh"
#include <algorithm>
#include <cuda_runtime.h>
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <random>
#include <unordered_set>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);
auto ret = RUN_ALL_TESTS();
cudaDeviceReset(); // to push stats cleanly
return ret;
}
namespace facebook { namespace cuda {
namespace {
// Add in +/- inf, +/- 0, denorms
void addSpecialFloats(vector<float>& vals) {
// Add in +/- infinity, with duplicates
vals.push_back(numeric_limits<float>::infinity());
vals.push_back(numeric_limits<float>::infinity());
vals.push_back(-numeric_limits<float>::infinity());
vals.push_back(-numeric_limits<float>::infinity());
// Add in +/- zero, with duplicates
vals.push_back(0.0f);
vals.push_back(0.0f);
vals.push_back(-0.0f);
vals.push_back(-0.0f);
// Add in some denorm floats, with duplicates
vals.push_back(numeric_limits<float>::denorm_min() * 4.0f);
vals.push_back(numeric_limits<float>::denorm_min());
vals.push_back(numeric_limits<float>::denorm_min());
vals.push_back(-numeric_limits<float>::denorm_min());
vals.push_back(-numeric_limits<float>::denorm_min());
vals.push_back(-numeric_limits<float>::denorm_min() * 4.0f);
}
struct ValueOrder {
bool operator()(const pair<float, int>& lhs,
const pair<float, int>& rhs) {
return (lhs.first > rhs.first) ||
((lhs.first == rhs.first) && (lhs.second > rhs.second));
}
};
void
testSorting(const vector<float>& shuffled,
const vector<float>& sorted,
int k) {
auto outIndex = findTopKElementsAndIndicesIndexOrder(shuffled, k);
// Verify that outIndex is in index order, and are unique
for (int j = 0; j < outIndex.size() - 1; ++j) {
ASSERT_LT(outIndex[j].second, outIndex[j + 1].second);
}
// Test that outIndex indexes the same element
for (int j = 0; j < outIndex.size(); ++j) {
int idx = outIndex[j].second;
ASSERT_GE(idx, 0);
ASSERT_LT(idx, shuffled.size());
ASSERT_EQ(outIndex[j].first, shuffled[idx]);
}
// Sorting outIndex by value order should result in something
// comparable with `sorted`
sort(outIndex.begin(), outIndex.end(), ValueOrder());
for (int j = 0; j < outIndex.size(); ++j) {
ASSERT_EQ(sorted[j], outIndex[j].first);
int idx = outIndex[j].second;
ASSERT_GE(idx, 0);
ASSERT_LT(idx, shuffled.size());
ASSERT_EQ(outIndex[j].first, shuffled[idx]);
}
if (k <= 128) {
// Sorting by value is implemented too; test that
auto outValue = findTopKElementsAndIndicesValueOrder(shuffled, k);
ASSERT_EQ(outValue.size(), outIndex.size());
// Test sorted index
for (int j = 0; j < outValue.size(); ++j) {
ASSERT_EQ(sorted[j], outValue[j].first);
int idx = outValue[j].second;
ASSERT_GE(idx, 0);
ASSERT_LT(idx, shuffled.size());
ASSERT_EQ(outValue[j].first, shuffled[idx]);
}
// Test for uniqueness of indices
unordered_set<int> indices;
for (const auto p : outValue) {
ASSERT_FALSE(indices.count(p.second));
indices.emplace(p.second);
}
}
}
} // namespace
TEST(TopK, basicSmall) {
vector<float> vals = {2.0f, 3.0f, 5.0f, 11.0f};
shuffle(vals.begin(), vals.end(), random_device());
EXPECT_EQ(11.0f, findTopKthElement(vals, 1));
EXPECT_EQ(5.0f, findTopKthElement(vals, 2));
EXPECT_EQ(3.0f, findTopKthElement(vals, 3));
EXPECT_EQ(2.0f, findTopKthElement(vals, 4));
}
TEST(TopK, basicLarge) {
vector<float> vals;
for (int i = 0; i <= 50; ++i) {
vals.push_back((float) i * 2);
}
shuffle(vals.begin(), vals.end(), random_device());
for (int i = 0; i <= 50; ++i) {
EXPECT_EQ((50 - i) * 2,
findTopKthElement(vals, i + 1));
}
}
TEST(TopK, negativeSmall) {
vector<float> vals = {1.0f, -2.0f, -3.0f, -5.0f, -11.0f};
shuffle(vals.begin(), vals.end(), random_device());
EXPECT_EQ(1.0f, findTopKthElement(vals, 1));
EXPECT_EQ(-2.0f, findTopKthElement(vals, 2));
EXPECT_EQ(-3.0f, findTopKthElement(vals, 3));
EXPECT_EQ(-5.0f, findTopKthElement(vals, 4));
EXPECT_EQ(-11.0f, findTopKthElement(vals, 5));
}
TEST(TopK, weird) {
// As long as it is not a NaN, we should deal with it properly.
vector<float> vals = {
numeric_limits<float>::infinity(),
0.0f,
numeric_limits<float>::denorm_min(),
-0.0f,
-numeric_limits<float>::infinity(),
-numeric_limits<float>::denorm_min(),
4.0f * numeric_limits<float>::denorm_min()
};
shuffle(vals.begin(), vals.end(), random_device());
EXPECT_EQ(numeric_limits<float>::infinity(),
findTopKthElement(vals, 1));
EXPECT_EQ(4.0f * numeric_limits<float>::denorm_min(),
findTopKthElement(vals, 2));
EXPECT_EQ(numeric_limits<float>::denorm_min(),
findTopKthElement(vals, 3));
EXPECT_EQ(0.0f,
findTopKthElement(vals, 4));
EXPECT_EQ(-0.0f,
findTopKthElement(vals, 5));
EXPECT_EQ(-numeric_limits<float>::denorm_min(),
findTopKthElement(vals, 6));
EXPECT_EQ(-numeric_limits<float>::infinity(),
findTopKthElement(vals, 7));
}
TEST(TopK, nonUnique) {
vector<float> vals;
for (int i = 0; i < 16; ++i) {
vals.push_back(1.0f);
}
for (int i = 16; i < 32; ++i) {
vals.push_back(2.0f);
}
EXPECT_EQ(2.0f, findTopKthElement(vals, 1));
EXPECT_EQ(2.0f, findTopKthElement(vals, 2));
EXPECT_EQ(2.0f, findTopKthElement(vals, 16));
EXPECT_EQ(1.0f, findTopKthElement(vals, 17));
EXPECT_EQ(1.0f, findTopKthElement(vals, 32));
}
TEST(TopK, random) {
random_device dev;
mt19937 gen(dev());
normal_distribution<float> dist(0, 1e6f);
uniform_real_distribution<float> smallDist(-1e-10f, 1e-10f);
vector<float> vals;
int dupsRemaining = 50;
for (int i = 0; i < 1000; ++i) {
const auto val = dist(gen);
vals.push_back(val);
const auto smallVal = smallDist(gen);
vals.push_back(smallVal);
// Also add in some duplicate entries
if (dupsRemaining-- > 0) {
vals.push_back(val);
vals.push_back(smallVal);
}
}
addSpecialFloats(vals);
auto sorted = vals;
sort(sorted.begin(), sorted.end(), std::greater<float>());
for (int i = 0; i < vals.size(); ++i) {
ASSERT_EQ(sorted[i], findTopKthElement(vals, i + 1));
}
}
TEST(TopK, topKSortedWeird) {
// As long as it is not a NaN, we should deal with it properly.
vector<float> vals;
addSpecialFloats(vals);
auto sorted = vals;
sort(sorted.begin(), sorted.end(), std::greater<float>());
for (int i = 0; i < vals.size(); ++i) {
shuffle(vals.begin(), vals.end(), random_device());
auto out = findTopKElements(vals, i + 1);
for (int j = 0; j < out.size(); ++j) {
ASSERT_EQ(sorted[j], out[j]);
}
}
}
TEST(TopK, topKSortedSmall) {
vector<float> vals;
// A vector smaller than the warp size
for (int i = 0; i <= 10; ++i) {
vals.push_back((float) i);
}
// Add one duplicate value too
vals.push_back(5.0f);
auto sorted = vals;
sort(sorted.begin(), sorted.end(), std::greater<float>());
for (int i = 0; i < vals.size(); ++i) {
shuffle(vals.begin(), vals.end(), random_device());
auto out = findTopKElements(vals, i + 1);
for (int j = 0; j < out.size(); ++j) {
ASSERT_EQ(sorted[j], out[j]);
}
}
}
TEST(TopK, topKSortedInWarp) {
for (int len = 0; len < 3 * 32; ++len) {
vector<float> vals;
for (int i = 0; i < len; ++i) {
vals.push_back((float) i);
}
auto sorted = vals;
sort(sorted.begin(), sorted.end(), std::greater<float>());
// Test top-k for all sizes up to `len`
for (int k = 1; k <= len; ++k) {
shuffle(vals.begin(), vals.end(), random_device());
auto out = findTopKElements(vals, k);
for (int j = 0; j < out.size(); ++j) {
ASSERT_EQ(sorted[j], out[j]);
}
}
}
}
TEST(TopK, topKSortedLarge) {
vector<float> vals;
// A vector larger than the warp size
for (int i = 0; i <= 1000; ++i) {
vals.push_back((float) i);
}
// Add a top duplicated element too
vals.push_back(998.0f);
auto sorted = vals;
sort(sorted.begin(), sorted.end(), std::greater<float>());
// Algorithm only deals with k <= 32 at the moment
for (int i = 0; i < 32; ++i) {
shuffle(vals.begin(), vals.end(), random_device());
auto out = findTopKElements(vals, i + 1);
for (int j = 0; j < out.size(); ++j) {
ASSERT_EQ(sorted[j], out[j]);
}
}
}
TEST(TopK, sortedIndicesSmall) {
vector<float> vals;
for (int i = 0; i <= 10; ++i) {
vals.push_back((float) i);
}
// Add one duplicate normal value too
vals.push_back(5.0f);
addSpecialFloats(vals);
auto sorted = vals;
sort(sorted.begin(), sorted.end(), std::greater<float>());
// Test top-k for all sizes
for (int k = 1; k <= vals.size(); ++k) {
shuffle(vals.begin(), vals.end(), random_device());
testSorting(vals, sorted, k);
}
}
TEST(TopK, sortedIndicesInWarp) {
for (int len = 1; len <= 3 * 32; ++len) {
vector<float> vals;
for (int i = 0; i < len; ++i) {
vals.push_back((float) i);
}
auto sorted = vals;
sort(sorted.begin(), sorted.end(), std::greater<float>());
// Test top-k for all sizes
for (int k = 1; k <= vals.size(); ++k) {
shuffle(vals.begin(), vals.end(), random_device());
testSorting(vals, sorted, k);
}
}
}
TEST(TopK, sortedIndicesLarge) {
random_device dev;
mt19937 gen(dev());
normal_distribution<float> dist(0, 1e6f);
vector<float> vals;
int dupsRemaining = 200;
// A vector larger than the warp size
for (int i = 0; i <= 500; ++i) {
const auto val = dist(gen);
vals.push_back(val);
if (dupsRemaining-- > 0) {
vals.push_back(val);
}
}
addSpecialFloats(vals);
auto sorted = vals;
sort(sorted.begin(), sorted.end(), std::greater<float>());
// Test top-k for all sizes
for (int k = 1; k <= vals.size(); ++k) {
shuffle(vals.begin(), vals.end(), random_device());
testSorting(vals, sorted, k);
}
}
} }