File: searchKnnWithFilter_test.cpp

package info (click to toggle)
hnswlib 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 628 kB
  • sloc: cpp: 4,809; python: 1,113; makefile: 32; sh: 18
file content (179 lines) | stat: -rw-r--r-- 5,473 bytes parent folder | download
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
// This is a test file for testing the filtering feature

#include "../../hnswlib/hnswlib.h"

#include <assert.h>

#include <vector>
#include <iostream>

namespace {

using idx_t = hnswlib::labeltype;

class PickDivisibleIds: public hnswlib::BaseFilterFunctor {
unsigned int divisor = 1;
 public:
    PickDivisibleIds(unsigned int divisor): divisor(divisor) {
        assert(divisor != 0);
    }
    bool operator()(idx_t label_id) {
        return label_id % divisor == 0;
    }
};

class PickNothing: public hnswlib::BaseFilterFunctor {
 public:
    bool operator()(idx_t label_id) {
        return false;
    }
};

void test_some_filtering(hnswlib::BaseFilterFunctor& filter_func, size_t div_num, size_t label_id_start) {
    int d = 4;
    idx_t n = 100;
    idx_t nq = 10;
    size_t k = 10;

    std::vector<float> data(n * d);
    std::vector<float> query(nq * d);

    std::mt19937 rng;
    rng.seed(47);
    std::uniform_real_distribution<> distrib;

    for (idx_t i = 0; i < n * d; ++i) {
        data[i] = distrib(rng);
    }
    for (idx_t i = 0; i < nq * d; ++i) {
        query[i] = distrib(rng);
    }

    hnswlib::L2Space space(d);
    hnswlib::AlgorithmInterface<float>* alg_brute  = new hnswlib::BruteforceSearch<float>(&space, 2 * n);
    hnswlib::AlgorithmInterface<float>* alg_hnsw = new hnswlib::HierarchicalNSW<float>(&space, 2 * n);

    for (size_t i = 0; i < n; ++i) {
        // `label_id_start` is used to ensure that the returned IDs are labels and not internal IDs
        alg_brute->addPoint(data.data() + d * i, label_id_start + i);
        alg_hnsw->addPoint(data.data() + d * i, label_id_start + i);
    }

    // test searchKnnCloserFirst of BruteforceSearch with filtering
    for (size_t j = 0; j < nq; ++j) {
        const void* p = query.data() + j * d;
        auto gd = alg_brute->searchKnn(p, k, &filter_func);
        auto res = alg_brute->searchKnnCloserFirst(p, k, &filter_func);
        assert(gd.size() == res.size());
        size_t t = gd.size();
        while (!gd.empty()) {
            assert(gd.top() == res[--t]);
            assert((gd.top().second % div_num) == 0);
            gd.pop();
        }
    }

    // test searchKnnCloserFirst of hnsw with filtering
    for (size_t j = 0; j < nq; ++j) {
        const void* p = query.data() + j * d;
        auto gd = alg_hnsw->searchKnn(p, k, &filter_func);
        auto res = alg_hnsw->searchKnnCloserFirst(p, k, &filter_func);
        assert(gd.size() == res.size());
        size_t t = gd.size();
        while (!gd.empty()) {
            assert(gd.top() == res[--t]);
            assert((gd.top().second % div_num) == 0);
            gd.pop();
        }
    }

    delete alg_brute;
    delete alg_hnsw;
}

void test_none_filtering(hnswlib::BaseFilterFunctor& filter_func, size_t label_id_start) {
    int d = 4;
    idx_t n = 100;
    idx_t nq = 10;
    size_t k = 10;

    std::vector<float> data(n * d);
    std::vector<float> query(nq * d);

    std::mt19937 rng;
    rng.seed(47);
    std::uniform_real_distribution<> distrib;

    for (idx_t i = 0; i < n * d; ++i) {
        data[i] = distrib(rng);
    }
    for (idx_t i = 0; i < nq * d; ++i) {
        query[i] = distrib(rng);
    }

    hnswlib::L2Space space(d);
    hnswlib::AlgorithmInterface<float>* alg_brute  = new hnswlib::BruteforceSearch<float>(&space, 2 * n);
    hnswlib::AlgorithmInterface<float>* alg_hnsw = new hnswlib::HierarchicalNSW<float>(&space, 2 * n);

    for (size_t i = 0; i < n; ++i) {
        // `label_id_start` is used to ensure that the returned IDs are labels and not internal IDs
        alg_brute->addPoint(data.data() + d * i, label_id_start + i);
        alg_hnsw->addPoint(data.data() + d * i, label_id_start + i);
    }

    // test searchKnnCloserFirst of BruteforceSearch with filtering
    for (size_t j = 0; j < nq; ++j) {
        const void* p = query.data() + j * d;
        auto gd = alg_brute->searchKnn(p, k, &filter_func);
        auto res = alg_brute->searchKnnCloserFirst(p, k, &filter_func);
        assert(gd.size() == res.size());
        assert(0 == gd.size());
    }

    // test searchKnnCloserFirst of hnsw with filtering
    for (size_t j = 0; j < nq; ++j) {
        const void* p = query.data() + j * d;
        auto gd = alg_hnsw->searchKnn(p, k, &filter_func);
        auto res = alg_hnsw->searchKnnCloserFirst(p, k, &filter_func);
        assert(gd.size() == res.size());
        assert(0 == gd.size());
    }

    delete alg_brute;
    delete alg_hnsw;
}

}  // namespace

class CustomFilterFunctor: public hnswlib::BaseFilterFunctor {
    std::unordered_set<idx_t> allowed_values;

 public:
    explicit CustomFilterFunctor(const std::unordered_set<idx_t>& values) : allowed_values(values) {}

    bool operator()(idx_t id) {
        return allowed_values.count(id) != 0;
    }
};

int main() {
    std::cout << "Testing ..." << std::endl;

    // some of the elements are filtered
    PickDivisibleIds pickIdsDivisibleByThree(3);
    test_some_filtering(pickIdsDivisibleByThree, 3, 17);
    PickDivisibleIds pickIdsDivisibleBySeven(7);
    test_some_filtering(pickIdsDivisibleBySeven, 7, 17);

    // all of the elements are filtered
    PickNothing pickNothing;
    test_none_filtering(pickNothing, 17);

    // functor style which can capture context
    CustomFilterFunctor pickIdsDivisibleByThirteen({26, 39, 52, 65});
    test_some_filtering(pickIdsDivisibleByThirteen, 13, 21);

    std::cout << "Test ok" << std::endl;

    return 0;
}