forked from facebookresearch/faiss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_transfer_invlists.cpp
148 lines (115 loc) · 4.21 KB
/
test_transfer_invlists.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
/**
* 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 <cstdio>
#include <cstdlib>
#include <memory>
#include <gtest/gtest.h>
#include <faiss/AutoTune.h>
#include <faiss/IVFlib.h>
#include <faiss/IndexIVFFlat.h>
#include <faiss/VectorTransform.h>
#include <faiss/clone_index.h>
#include <faiss/impl/io.h>
#include <faiss/index_factory.h>
#include <faiss/index_io.h>
#include <faiss/utils/random.h>
namespace {
// parameters to use for the test
int d = 64;
size_t nb = 1000;
size_t nq = 100;
size_t nt = 500;
int k = 10;
int nlist = 40;
using namespace faiss;
typedef faiss::idx_t idx_t;
std::vector<float> get_data(size_t nb, int seed) {
std::vector<float> x(nb * d);
float_randn(x.data(), nb * d, seed);
return x;
}
void test_index_type(const char* factory_string) {
// transfer inverted lists in nslice slices
int nslice = 3;
/****************************************************************
* trained reference index
****************************************************************/
std::unique_ptr<Index> trained(index_factory(d, factory_string));
{
auto xt = get_data(nt, 123);
trained->train(nt, xt.data());
}
// sample nq query vectors to check if results are the same
auto xq = get_data(nq, 818);
/****************************************************************
* source index
***************************************************************/
std::unique_ptr<Index> src_index(clone_index(trained.get()));
{ // add some data to source index
auto xb = get_data(nb, 245);
src_index->add(nb, xb.data());
}
ParameterSpace().set_index_parameter(src_index.get(), "nprobe", 4);
// remember reference search result on source index
std::vector<idx_t> Iref(nq * k);
std::vector<float> Dref(nq * k);
src_index->search(nq, xq.data(), k, Dref.data(), Iref.data());
/****************************************************************
* destination index -- should be replaced by source index
***************************************************************/
std::unique_ptr<Index> dst_index(clone_index(trained.get()));
{ // initial state: filled in with some garbage
int nb2 = nb + 10;
auto xb = get_data(nb2, 366);
dst_index->add(nb2, xb.data());
}
std::vector<idx_t> Inew(nq * k);
std::vector<float> Dnew(nq * k);
ParameterSpace().set_index_parameter(dst_index.get(), "nprobe", 4);
// transfer from source to destination in nslice slices
for (int sl = 0; sl < nslice; sl++) {
// so far, the indexes are different
dst_index->search(nq, xq.data(), k, Dnew.data(), Inew.data());
EXPECT_TRUE(Iref != Inew);
EXPECT_TRUE(Dref != Dnew);
// range of inverted list indices to transfer
long i0 = sl * nlist / nslice;
long i1 = (sl + 1) * nlist / nslice;
std::vector<uint8_t> data_to_transfer;
{
std::unique_ptr<ArrayInvertedLists> il(
ivflib::get_invlist_range(src_index.get(), i0, i1));
// serialize inverted lists
VectorIOWriter wr;
write_InvertedLists(il.get(), &wr);
data_to_transfer.swap(wr.data);
}
// transfer data here from source machine to dest machine
{
VectorIOReader reader;
reader.data.swap(data_to_transfer);
// deserialize inverted lists
std::unique_ptr<ArrayInvertedLists> il(
dynamic_cast<ArrayInvertedLists*>(
read_InvertedLists(&reader)));
// swap inverted lists. Block searches here!
{ ivflib::set_invlist_range(dst_index.get(), i0, i1, il.get()); }
}
}
EXPECT_EQ(dst_index->ntotal, src_index->ntotal);
// now, the indexes are the same
dst_index->search(nq, xq.data(), k, Dnew.data(), Inew.data());
EXPECT_TRUE(Iref == Inew);
EXPECT_TRUE(Dref == Dnew);
}
} // namespace
TEST(TRANS, IVFFlat) {
test_index_type("IVF40,Flat");
}
TEST(TRANS, IVFFlatPreproc) {
test_index_type("PCAR32,IVF40,Flat");
}