forked from facebookresearch/faiss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ondisk_ivf.cpp
More file actions
222 lines (168 loc) · 5.22 KB
/
Copy pathtest_ondisk_ivf.cpp
File metadata and controls
222 lines (168 loc) · 5.22 KB
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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD+Patents license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <cstdio>
#include <cstdlib>
#include <omp.h>
#include <unordered_map>
#include <pthread.h>
#include <gtest/gtest.h>
#include <faiss/OnDiskInvertedLists.h>
#include <faiss/IndexIVFFlat.h>
#include <faiss/IndexFlat.h>
#include <faiss/utils.h>
#include <faiss/index_io.h>
namespace {
struct Tempfilename {
static pthread_mutex_t mutex;
std::string filename;
Tempfilename (const char *prefix = nullptr) {
pthread_mutex_lock (&mutex);
char *cfname = tempnam (nullptr, prefix);
filename = cfname;
free(cfname);
pthread_mutex_unlock (&mutex);
}
~Tempfilename () {
if (access (filename.c_str(), F_OK)) {
unlink (filename.c_str());
}
}
const char *c_str() {
return filename.c_str();
}
};
pthread_mutex_t Tempfilename::mutex = PTHREAD_MUTEX_INITIALIZER;
} // namespace
TEST(ONDISK, make_invlists) {
int nlist = 100;
int code_size = 32;
int nadd = 1000000;
std::unordered_map<int, int> listnos;
Tempfilename filename;
faiss::OnDiskInvertedLists ivf (
nlist, code_size,
filename.c_str());
{
std::vector<uint8_t> code(32);
for (int i = 0; i < nadd; i++) {
double d = drand48();
int list_no = int(nlist * d * d); // skewed distribution
int * ar = (int*)code.data();
ar[0] = i;
ar[1] = list_no;
ivf.add_entry (list_no, i, code.data());
listnos[i] = list_no;
}
}
int ntot = 0;
for (int i = 0; i < nlist; i++) {
int size = ivf.list_size(i);
const long *ids = ivf.get_ids (i);
const uint8_t *codes = ivf.get_codes (i);
for (int j = 0; j < size; j++) {
long id = ids[j];
const int * ar = (const int*)&codes[code_size * j];
EXPECT_EQ (ar[0], id);
EXPECT_EQ (ar[1], i);
EXPECT_EQ (listnos[id], i);
ntot ++;
}
}
EXPECT_EQ (ntot, nadd);
};
TEST(ONDISK, test_add) {
int d = 8;
int nlist = 30, nq = 200, nb = 1500, k = 10;
faiss::IndexFlatL2 quantizer(d);
{
std::vector<float> x(d * nlist);
faiss::float_rand(x.data(), d * nlist, 12345);
quantizer.add(nlist, x.data());
}
std::vector<float> xb(d * nb);
faiss::float_rand(xb.data(), d * nb, 23456);
faiss::IndexIVFFlat index(&quantizer, d, nlist);
index.add(nb, xb.data());
std::vector<float> xq(d * nb);
faiss::float_rand(xq.data(), d * nq, 34567);
std::vector<float> ref_D (nq * k);
std::vector<faiss::Index::idx_t> ref_I (nq * k);
index.search (nq, xq.data(), k,
ref_D.data(), ref_I.data());
Tempfilename filename, filename2;
// test add + search
{
faiss::IndexIVFFlat index2(&quantizer, d, nlist);
faiss::OnDiskInvertedLists ivf (
index.nlist, index.code_size,
filename.c_str());
index2.replace_invlists(&ivf);
index2.add(nb, xb.data());
std::vector<float> new_D (nq * k);
std::vector<faiss::Index::idx_t> new_I (nq * k);
index2.search (nq, xq.data(), k,
new_D.data(), new_I.data());
EXPECT_EQ (ref_D, new_D);
EXPECT_EQ (ref_I, new_I);
write_index(&index2, filename2.c_str());
}
// test io
{
faiss::Index *index3 = faiss::read_index(filename2.c_str());
std::vector<float> new_D (nq * k);
std::vector<faiss::Index::idx_t> new_I (nq * k);
index3->search (nq, xq.data(), k,
new_D.data(), new_I.data());
EXPECT_EQ (ref_D, new_D);
EXPECT_EQ (ref_I, new_I);
delete index3;
}
};
// WARN this thest will run multithreaded only in opt mode
TEST(ONDISK, make_invlists_threaded) {
int nlist = 100;
int code_size = 32;
int nadd = 1000000;
int nt = 20;
Tempfilename filename;
faiss::OnDiskInvertedLists ivf (
nlist, code_size,
filename.c_str());
std::vector<int> list_nos (nadd);
for (int i = 0; i < nadd; i++) {
double d = drand48();
list_nos[i] = int(nlist * d * d); // skewed distribution
}
#pragma omp parallel
{
std::vector<uint8_t> code(32);
#pragma omp for
for (int i = 0; i < nadd; i++) {
int list_no = list_nos[i];
int * ar = (int*)code.data();
ar[0] = i;
ar[1] = list_no;
ivf.add_entry (list_no, i, code.data());
}
}
int ntot = 0;
for (int i = 0; i < nlist; i++) {
int size = ivf.list_size(i);
const long *ids = ivf.get_ids (i);
const uint8_t *codes = ivf.get_codes (i);
for (int j = 0; j < size; j++) {
long id = ids[j];
const int * ar = (const int*)&codes[code_size * j];
EXPECT_EQ (ar[0], id);
EXPECT_EQ (ar[1], i);
EXPECT_EQ (list_nos[id], i);
ntot ++;
}
}
EXPECT_EQ (ntot, nadd);
};