aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/tests/ann/remove-bm.cpp
blob: bb06779477f76cff08f54b8710fa1b0de17c35bb (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/util/priority_queue.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <chrono>

#define NUM_DIMS 960
#define NUM_DOCS 250000
#define NUM_DOCS_REMOVE 50000
#define EFFECTIVE_DOCS (NUM_DOCS - NUM_DOCS_REMOVE)
#define NUM_REACH 10000
#define NUM_Q 1000

#include "doc_vector_access.h"
#include "nns.h"
#include "for-sift-hit.h"
#include "for-sift-top-k.h"
#include "time-util.h"
#include "point-vector.h"
#include "read-vecs.h"
#include "bruteforce-nns.h"

using NNS_API = NNS<float>;

#if 1
TEST("require that HNSW via NNS api remove all works") {
    DocVectorAdapter adapter;
    std::unique_ptr<NNS_API> nns = make_hnsw_nns(NUM_DIMS, adapter);
    fprintf(stderr, "adding and removing all docs forward...\n");
    for (uint32_t i = 0; i < 1000; ++i) {
        nns->addDoc(i);
    }
    for (uint32_t i = 0; i < 1000; ++i) {
        nns->removeDoc(i);
    }
    fprintf(stderr, "adding and removing all docs reverse...\n");
    for (uint32_t i = 1000; i < 2000; ++i) {
        nns->addDoc(i);
    }
    for (uint32_t i = 2000; i-- > 1000; ) {
        nns->removeDoc(i);
    }
}
#endif

TEST("require that brute force works") {
    TimePoint bef = std::chrono::steady_clock::now();
    fprintf(stderr, "generating %u brute force results\n", NUM_Q);
    bruteforceResults.reserve(NUM_Q);
    for (uint32_t cnt = 0; cnt < NUM_Q; ++cnt) {
        const PointVector &query = generatedQueries[cnt];
        bruteforceResults.emplace_back(bruteforce_nns(query));
    }
    TimePoint aft = std::chrono::steady_clock::now();
    fprintf(stderr, "timing for brute force: %.3f ms = %.3f ms per query\n",
            to_ms(aft - bef), to_ms(aft - bef)/NUM_Q);
    for (int cnt = 0; cnt < NUM_Q; cnt = (cnt+1)*2) {
        verifyBF(cnt);
    }
}

#include "find-with-nns.h"
#include "verify-top-k.h"

void timing_nns(const char *name, NNS_API &nns, std::vector<uint32_t> sk_list) {
    for (uint32_t search_k : sk_list) {
        TimePoint bef = std::chrono::steady_clock::now();
        for (int cnt = 0; cnt < NUM_Q; ++cnt) {
            find_with_nns(search_k, nns, cnt);
        }
        TimePoint aft = std::chrono::steady_clock::now();
        fprintf(stderr, "timing for %s search_k=%u: %.3f ms = %.3f ms/q\n",
                name, search_k, to_ms(aft - bef), to_ms(aft - bef)/NUM_Q);
    }
}

#include "quality-nns.h"

template <typename FUNC>
void bm_nns_simple(const char *name, FUNC creator, std::vector<uint32_t> sk_list) {
    std::unique_ptr<NNS_API> nnsp = creator();
    NNS_API &nns = *nnsp;
    fprintf(stderr, "trying %s indexing...\n", name);
    TimePoint bef = std::chrono::steady_clock::now();
    for (uint32_t i = 0; i < EFFECTIVE_DOCS; ++i) {
        nns.addDoc(i);
    }
    TimePoint aft = std::chrono::steady_clock::now();
    fprintf(stderr, "build %s index with %u docs: %.3f ms\n", name, EFFECTIVE_DOCS, to_ms(aft - bef));
    timing_nns(name, nns, sk_list);
    fprintf(stderr, "Quality for %s [A] clean build with %u documents:\n", name, EFFECTIVE_DOCS);
    quality_nns(nns, sk_list);
    bef = std::chrono::steady_clock::now();
    for (uint32_t i = 0; i < NUM_DOCS_REMOVE; ++i) {
        nns.addDoc(EFFECTIVE_DOCS + i);
    }
    for (uint32_t i = 0; i < NUM_DOCS_REMOVE; ++i) {
        nns.removeDoc(EFFECTIVE_DOCS + i);
    }
    aft = std::chrono::steady_clock::now();
    fprintf(stderr, "build %s index add then remove %u docs: %.3f ms\n",
            name, NUM_DOCS_REMOVE, to_ms(aft - bef));
    timing_nns(name, nns, sk_list);
    fprintf(stderr, "Quality for %s [B] remove-damaged build with %u documents:\n", name, EFFECTIVE_DOCS);
    quality_nns(nns, sk_list);
}

template <typename FUNC>
void bm_nns_remove_old(const char *name, FUNC creator, std::vector<uint32_t> sk_list) {
    std::unique_ptr<NNS_API> nnsp = creator();
    NNS_API &nns = *nnsp;
    TimePoint bef = std::chrono::steady_clock::now();
    for (uint32_t i = 0; i < NUM_DOCS_REMOVE; ++i) {
        nns.addDoc(EFFECTIVE_DOCS + i);
    }
    for (uint32_t i = 0; i < EFFECTIVE_DOCS; ++i) {
        nns.addDoc(i);
    }
    for (uint32_t i = 0; i < NUM_DOCS_REMOVE; ++i) {
        nns.removeDoc(EFFECTIVE_DOCS + i);
    }
    TimePoint aft = std::chrono::steady_clock::now();
    fprintf(stderr, "build %s index with %u docs: %.3f ms\n", name, EFFECTIVE_DOCS, to_ms(aft - bef));
    timing_nns(name, nns, sk_list);
    fprintf(stderr, "Quality for %s [C] remove-oldest build with %u documents:\n", name, EFFECTIVE_DOCS);
    quality_nns(nns, sk_list);
}

template <typename FUNC>
void bm_nns_interleave(const char *name, FUNC creator, std::vector<uint32_t> sk_list) {
    std::unique_ptr<NNS_API> nnsp = creator();
    NNS_API &nns = *nnsp;
    TimePoint bef = std::chrono::steady_clock::now();
    for (uint32_t i = 0; i < NUM_DOCS_REMOVE; ++i) {
        nns.addDoc(EFFECTIVE_DOCS + i);
    }
    for (uint32_t i = 0; i < EFFECTIVE_DOCS - NUM_DOCS_REMOVE; ++i) {
        nns.addDoc(i);
    }
    for (uint32_t i = 0; i < NUM_DOCS_REMOVE; ++i) {
        nns.removeDoc(EFFECTIVE_DOCS + i);
        nns.addDoc(EFFECTIVE_DOCS - NUM_DOCS_REMOVE + i);
    }
    TimePoint aft = std::chrono::steady_clock::now();
    fprintf(stderr, "build %s index with %u docs: %.3f ms\n", name, EFFECTIVE_DOCS, to_ms(aft - bef));
    timing_nns(name, nns, sk_list);
    fprintf(stderr, "Quality for %s [D] realistic build with %u documents:\n", name, EFFECTIVE_DOCS);
    quality_nns(nns, sk_list);
}

template <typename FUNC>
void bm_nns_remove_old_add_new(const char *name, FUNC creator, std::vector<uint32_t> sk_list) {
    std::unique_ptr<NNS_API> nnsp = creator();
    NNS_API &nns = *nnsp;
    TimePoint bef = std::chrono::steady_clock::now();
    for (uint32_t i = 0; i < NUM_DOCS_REMOVE; ++i) {
        nns.addDoc(EFFECTIVE_DOCS + i);
    }
    for (uint32_t i = 0; i < EFFECTIVE_DOCS - NUM_DOCS_REMOVE; ++i) {
        nns.addDoc(i);
    }
    for (uint32_t i = 0; i < NUM_DOCS_REMOVE; ++i) {
        nns.removeDoc(EFFECTIVE_DOCS + i);
    }
    for (uint32_t i = 0; i < NUM_DOCS_REMOVE; ++i) {
        nns.addDoc(EFFECTIVE_DOCS - NUM_DOCS_REMOVE + i);
    }
    TimePoint aft = std::chrono::steady_clock::now();
    fprintf(stderr, "build %s index with %u docs: %.3f ms\n", name, EFFECTIVE_DOCS, to_ms(aft - bef));
    timing_nns(name, nns, sk_list);
    fprintf(stderr, "Quality for %s [E] remove old, add new build with %u documents:\n", name, EFFECTIVE_DOCS);
    quality_nns(nns, sk_list);
}

template <typename FUNC>
void benchmark_nns(const char *name, FUNC creator, std::vector<uint32_t> sk_list) {
    bm_nns_simple(name, creator, sk_list);
    bm_nns_remove_old(name, creator, sk_list);
    bm_nns_interleave(name, creator, sk_list);
    bm_nns_remove_old_add_new(name, creator, sk_list);
}

#if 0
TEST("require that Locality Sensitive Hashing mostly works") {
    DocVectorAdapter adapter;
    auto creator = [&adapter]() { return make_rplsh_nns(NUM_DIMS, adapter); };
    benchmark_nns("RPLSH", creator, { 200, 1000 });
}
#endif

#if 0
TEST("require that Annoy via NNS api mostly works") {
    DocVectorAdapter adapter;
    auto creator = [&adapter]() { return make_annoy_nns(NUM_DIMS, adapter); };
    benchmark_nns("Annoy", creator, { 8000, 10000 });
}
#endif

#if 1
TEST("require that HNSW via NNS api mostly works") {
    DocVectorAdapter adapter;
    auto creator = [&adapter]() { return make_hnsw_nns(NUM_DIMS, adapter); };
    benchmark_nns("HNSW-like", creator, { 100, 150, 200 });
}
#endif

#if 0
TEST("require that HNSW wrapped api mostly works") {
    DocVectorAdapter adapter;
    auto creator = [&adapter]() { return make_hnsw_wrap(NUM_DIMS, adapter); };
    benchmark_nns("HNSW-wrap", creator, { 100, 150, 200 });
}
#endif

/**
 * Before running the benchmark the ANN_GIST1M data set must be downloaded and extracted:
 *   wget ftp://ftp.irisa.fr/local/texmex/corpus/gist.tar.gz
 *   tar -xf gist.tar.gz
 *
 * The benchmark program will load the data set from $HOME/gist if no directory is specified.
 *
 * More information about the dataset is found here: http://corpus-texmex.irisa.fr/.
 */
int main(int argc, char **argv) {
    TEST_MASTER.init(__FILE__);
    std::string data_set = "gist";
    std::string data_dir = ".";
    if (argc > 2) {
        data_set = argv[1];
        data_dir = argv[2];
    } else if (argc > 1) {
        data_dir = argv[1];
    } else {
        char *home = getenv("HOME");
        if (home) {
            data_dir = home;
            data_dir += "/" + data_set;
        }
    }
    read_data(data_dir, data_set);
    TEST_RUN_ALL();
    return (TEST_MASTER.fini() ? 0 : 1);
}