TestGpuSelect.cu.prehip 6.48 KB
Newer Older
huchen's avatar
huchen committed
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
/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#include <faiss/gpu/StandardGpuResources.h>
#include <faiss/gpu/test/TestUtils.h>
#include <faiss/gpu/utils/DeviceUtils.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <faiss/gpu/utils/BlockSelectKernel.cuh>
#include <faiss/gpu/utils/DeviceDefs.cuh>
#include <faiss/gpu/utils/DeviceTensor.cuh>
#include <faiss/gpu/utils/HostTensor.cuh>
#include <faiss/gpu/utils/WarpSelectKernel.cuh>
#include <sstream>
#include <unordered_map>
#include <vector>

void testForSize(int rows, int cols, int k, bool dir, bool warp) {
    using namespace faiss::gpu;

    StandardGpuResources res;

    std::vector<float> v = randVecs(rows, cols);
    HostTensor<float, 2, true> hostVal({rows, cols});

    for (int r = 0; r < rows; ++r) {
        for (int c = 0; c < cols; ++c) {
            hostVal[r][c] = v[r * cols + c];
        }
    }

    // row -> (val -> idx)
    std::unordered_map<int, std::vector<std::pair<int, float>>>
            hostOutValAndInd;
    for (int r = 0; r < rows; ++r) {
        std::vector<std::pair<int, float>> closest;

        for (int c = 0; c < cols; ++c) {
            closest.emplace_back(c, (float)hostVal[r][c]);
        }

        auto dirFalseFn = [](std::pair<int, float>& a,
                             std::pair<int, float>& b) {
            return a.second < b.second;
        };
        auto dirTrueFn = [](std::pair<int, float>& a,
                            std::pair<int, float>& b) {
            return a.second > b.second;
        };

        std::sort(closest.begin(), closest.end(), dir ? dirTrueFn : dirFalseFn);
        hostOutValAndInd.emplace(r, closest);
    }

    // Select top-k on GPU
    DeviceTensor<float, 2, true> gpuVal(
            res.getResources().get(),
            makeDevAlloc(AllocType::Other, 0),
            hostVal);
    DeviceTensor<float, 2, true> gpuOutVal(
            res.getResources().get(),
            makeDevAlloc(AllocType::Other, 0),
            {rows, k});
    DeviceTensor<int, 2, true> gpuOutInd(
            res.getResources().get(),
            makeDevAlloc(AllocType::Other, 0),
            {rows, k});

    if (warp) {
        runWarpSelect(gpuVal, gpuOutVal, gpuOutInd, dir, k, 0);
    } else {
        runBlockSelect(gpuVal, gpuOutVal, gpuOutInd, dir, k, 0);
    }

    // Copy back to CPU
    HostTensor<float, 2, true> outVal(gpuOutVal, 0);
    HostTensor<int, 2, true> outInd(gpuOutInd, 0);

    for (int r = 0; r < rows; ++r) {
        std::unordered_map<int, int> seenIndices;

        for (int i = 0; i < k; ++i) {
            float gpuV = outVal[r][i];
            float cpuV = hostOutValAndInd[r][i].second;

            EXPECT_EQ(gpuV, cpuV)
                    << "rows " << rows << " cols " << cols << " k " << k
                    << " dir " << dir << " row " << r << " ind " << i;

            // If there are identical elements in a row that should be
            // within the top-k, then it is possible that the index can
            // differ, because the order in which the GPU will see the
            // equivalent values is different than the CPU (and will remain
            // unspecified, since this is affected by the choice of
            // k-selection algorithm that we use)
            int gpuInd = outInd[r][i];
            int cpuInd = hostOutValAndInd[r][i].first;

            // We should never see duplicate indices, however
            auto itSeenIndex = seenIndices.find(gpuInd);

            EXPECT_EQ(itSeenIndex, seenIndices.end())
                    << "Row " << r << " user index " << gpuInd
                    << " was seen at both " << itSeenIndex->second << " and "
                    << i;

            seenIndices[gpuInd] = i;

            if (gpuInd != cpuInd) {
                // Gather the values from the original data via index; the
                // values should be the same
                float gpuGatherV = hostVal[r][gpuInd];
                float cpuGatherV = hostVal[r][cpuInd];

                EXPECT_EQ(gpuGatherV, cpuGatherV)
                        << "rows " << rows << " cols " << cols << " k " << k
                        << " dir " << dir << " row " << r << " ind " << i
                        << " source ind " << gpuInd << " " << cpuInd;
            }
        }
    }
}

// General test
TEST(TestGpuSelect, test) {
    for (int i = 0; i < 10; ++i) {
        int rows = faiss::gpu::randVal(10, 100);
        int cols = faiss::gpu::randVal(1, 30000);
        int k = std::min(cols, faiss::gpu::randVal(1, GPU_MAX_SELECTION_K));
        bool dir = faiss::gpu::randBool();

        testForSize(rows, cols, k, dir, false);
    }
}

// Test for k = 1
TEST(TestGpuSelect, test1) {
    for (int i = 0; i < 5; ++i) {
        int rows = faiss::gpu::randVal(10, 100);
        int cols = faiss::gpu::randVal(1, 30000);
        bool dir = faiss::gpu::randBool();

        testForSize(rows, cols, 1, dir, false);
    }
}

// Test for where k = #cols exactly (we are returning all the values,
// just sorted)
TEST(TestGpuSelect, testExact) {
    for (int i = 0; i < 5; ++i) {
        int rows = faiss::gpu::randVal(10, 100);
        int cols = faiss::gpu::randVal(1, GPU_MAX_SELECTION_K);
        bool dir = faiss::gpu::randBool();

        testForSize(rows, cols, cols, dir, false);
    }
}

// General test
TEST(TestGpuSelect, testWarp) {
    for (int i = 0; i < 10; ++i) {
        int rows = faiss::gpu::randVal(10, 100);
        int cols = faiss::gpu::randVal(1, 30000);
        int k = std::min(cols, faiss::gpu::randVal(1, GPU_MAX_SELECTION_K));
        bool dir = faiss::gpu::randBool();

        testForSize(rows, cols, k, dir, true);
    }
}

// Test for k = 1
TEST(TestGpuSelect, test1Warp) {
    for (int i = 0; i < 5; ++i) {
        int rows = faiss::gpu::randVal(10, 100);
        int cols = faiss::gpu::randVal(1, 30000);
        bool dir = faiss::gpu::randBool();

        testForSize(rows, cols, 1, dir, true);
    }
}

// Test for where k = #cols exactly (we are returning all the values,
// just sorted)
TEST(TestGpuSelect, testExactWarp) {
    for (int i = 0; i < 5; ++i) {
        int rows = faiss::gpu::randVal(10, 100);
        int cols = faiss::gpu::randVal(1, GPU_MAX_SELECTION_K);
        bool dir = faiss::gpu::randBool();

        testForSize(rows, cols, cols, dir, true);
    }
}

int main(int argc, char** argv) {
    testing::InitGoogleTest(&argc, argv);

    // just run with a fixed test seed
    faiss::gpu::setTestSeed(100);

    return RUN_ALL_TESTS();
}