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
|
/*
* Copyright (c) Meta Platforms, Inc. and 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/IndexReplicas.h>
#include <faiss/IndexShards.h>
#include <faiss/impl/ThreadedIndex.h>
#include <gtest/gtest.h>
#include <chrono>
#include <memory>
#include <thread>
#include <vector>
namespace {
struct TestException : public std::exception {};
using idx_t = faiss::idx_t;
struct MockIndex : public faiss::Index {
explicit MockIndex(idx_t d) : faiss::Index(d) {
resetMock();
}
void resetMock() {
flag = false;
nCalled = 0;
xCalled = nullptr;
kCalled = 0;
distancesCalled = nullptr;
labelsCalled = nullptr;
}
void add(idx_t n, const float* x) override {
nCalled = n;
xCalled = x;
}
void search(
idx_t n,
const float* x,
idx_t k,
float* distances,
idx_t* labels,
const faiss::SearchParameters* params) const override {
FAISS_THROW_IF_NOT(!params);
nCalled = n;
xCalled = x;
kCalled = k;
distancesCalled = distances;
labelsCalled = labels;
}
void reset() override {}
bool flag;
mutable idx_t nCalled;
mutable const float* xCalled;
mutable idx_t kCalled;
mutable float* distancesCalled;
mutable idx_t* labelsCalled;
};
template <typename IndexT>
struct MockThreadedIndex : public faiss::ThreadedIndex<IndexT> {
using idx_t = faiss::idx_t;
explicit MockThreadedIndex(bool threaded)
: faiss::ThreadedIndex<IndexT>(threaded) {}
void add(idx_t, const float*) override {}
void search(
idx_t,
const float*,
idx_t,
float*,
idx_t*,
const faiss::SearchParameters*) const override {}
void reset() override {}
};
} // namespace
TEST(ThreadedIndex, SingleException) {
std::vector<std::unique_ptr<MockIndex>> idxs;
for (int i = 0; i < 3; ++i) {
idxs.emplace_back(new MockIndex(1));
}
auto fn = [](int i, MockIndex* index) {
if (i == 1) {
throw TestException();
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(i * 250));
index->flag = true;
}
};
// Try with threading and without
for (bool threaded : {true, false}) {
// clear flags
for (auto& idx : idxs) {
idx->resetMock();
}
MockThreadedIndex<MockIndex> ti(threaded);
for (auto& idx : idxs) {
ti.addIndex(idx.get());
}
// The second index should throw
EXPECT_THROW(ti.runOnIndex(fn), TestException);
// Index 0 and 2 should have processed
EXPECT_TRUE(idxs[0]->flag);
EXPECT_TRUE(idxs[2]->flag);
}
}
TEST(ThreadedIndex, MultipleException) {
std::vector<std::unique_ptr<MockIndex>> idxs;
for (int i = 0; i < 3; ++i) {
idxs.emplace_back(new MockIndex(1));
}
auto fn = [](int i, MockIndex* index) {
if (i < 2) {
throw TestException();
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(i * 250));
index->flag = true;
}
};
// Try with threading and without
for (bool threaded : {true, false}) {
// clear flags
for (auto& idx : idxs) {
idx->resetMock();
}
MockThreadedIndex<MockIndex> ti(threaded);
for (auto& idx : idxs) {
ti.addIndex(idx.get());
}
// Multiple indices threw an exception that was aggregated into a
// FaissException
EXPECT_THROW(ti.runOnIndex(fn), faiss::FaissException);
// Index 2 should have processed
EXPECT_TRUE(idxs[2]->flag);
}
}
TEST(ThreadedIndex, TestReplica) {
int numReplicas = 5;
int n = 10 * numReplicas;
int d = 3;
int k = 6;
// Try with threading and without
for ([[maybe_unused]] const bool threaded : {true, false}) {
std::vector<std::unique_ptr<MockIndex>> idxs;
faiss::IndexReplicas replica(d);
for (int i = 0; i < numReplicas; ++i) {
idxs.emplace_back(new MockIndex(d));
replica.addIndex(idxs.back().get());
}
std::vector<float> x(n * d);
std::vector<float> distances(n * k);
std::vector<faiss::idx_t> labels(n * k);
replica.add(n, x.data());
for (int i = 0; i < idxs.size(); ++i) {
EXPECT_EQ(idxs[i]->nCalled, n);
EXPECT_EQ(idxs[i]->xCalled, x.data());
}
for (auto& idx : idxs) {
idx->resetMock();
}
replica.search(n, x.data(), k, distances.data(), labels.data());
for (int i = 0; i < idxs.size(); ++i) {
auto perReplica = n / idxs.size();
EXPECT_EQ(idxs[i]->nCalled, perReplica);
EXPECT_EQ(idxs[i]->xCalled, x.data() + i * perReplica * d);
EXPECT_EQ(idxs[i]->kCalled, k);
EXPECT_EQ(
idxs[i]->distancesCalled,
distances.data() + (i * perReplica) * k);
EXPECT_EQ(
idxs[i]->labelsCalled,
labels.data() + (i * perReplica) * k);
}
}
}
TEST(ThreadedIndex, TestShards) {
int numShards = 7;
int d = 3;
int n = 10 * numShards;
int k = 6;
// Try with threading and without
for (bool threaded : {true, false}) {
std::vector<std::unique_ptr<MockIndex>> idxs;
faiss::IndexShards shards(d, threaded);
for (int i = 0; i < numShards; ++i) {
idxs.emplace_back(new MockIndex(d));
shards.addIndex(idxs.back().get());
}
std::vector<float> x(n * d);
std::vector<float> distances(n * k);
std::vector<faiss::idx_t> labels(n * k);
shards.add(n, x.data());
for (int i = 0; i < idxs.size(); ++i) {
auto perShard = n / idxs.size();
EXPECT_EQ(idxs[i]->nCalled, perShard);
EXPECT_EQ(idxs[i]->xCalled, x.data() + i * perShard * d);
}
for (auto& idx : idxs) {
idx->resetMock();
}
shards.search(n, x.data(), k, distances.data(), labels.data());
for (int i = 0; i < idxs.size(); ++i) {
EXPECT_EQ(idxs[i]->nCalled, n);
EXPECT_EQ(idxs[i]->xCalled, x.data());
EXPECT_EQ(idxs[i]->kCalled, k);
// There is a temporary buffer used for shards
EXPECT_EQ(
idxs[i]->distancesCalled,
idxs[0]->distancesCalled + i * k * n);
EXPECT_EQ(idxs[i]->labelsCalled, idxs[0]->labelsCalled + i * k * n);
}
}
}
|