/root/doris/contrib/faiss/faiss/IndexBinaryHash.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | // -*- c++ -*- |
9 | | |
10 | | #ifndef FAISS_BINARY_HASH_H |
11 | | #define FAISS_BINARY_HASH_H |
12 | | |
13 | | #include <unordered_map> |
14 | | #include <vector> |
15 | | |
16 | | #include <faiss/IndexBinary.h> |
17 | | #include <faiss/IndexBinaryFlat.h> |
18 | | #include <faiss/impl/platform_macros.h> |
19 | | #include <faiss/utils/Heap.h> |
20 | | |
21 | | namespace faiss { |
22 | | |
23 | | struct RangeSearchResult; |
24 | | |
25 | | /** just uses the b first bits as a hash value */ |
26 | | struct IndexBinaryHash : IndexBinary { |
27 | | struct InvertedList { |
28 | | std::vector<idx_t> ids; |
29 | | std::vector<uint8_t> vecs; |
30 | | |
31 | | void add(idx_t id, size_t code_size, const uint8_t* code); |
32 | | }; |
33 | | |
34 | | using InvertedListMap = std::unordered_map<idx_t, InvertedList>; |
35 | | InvertedListMap invlists; |
36 | | |
37 | | int b, nflip; |
38 | | |
39 | | IndexBinaryHash(int d, int b); |
40 | | |
41 | | IndexBinaryHash(); |
42 | | |
43 | | void reset() override; |
44 | | |
45 | | void add(idx_t n, const uint8_t* x) override; |
46 | | |
47 | | void add_with_ids(idx_t n, const uint8_t* x, const idx_t* xids) override; |
48 | | |
49 | | void range_search( |
50 | | idx_t n, |
51 | | const uint8_t* x, |
52 | | int radius, |
53 | | RangeSearchResult* result, |
54 | | const SearchParameters* params = nullptr) const override; |
55 | | |
56 | | void search( |
57 | | idx_t n, |
58 | | const uint8_t* x, |
59 | | idx_t k, |
60 | | int32_t* distances, |
61 | | idx_t* labels, |
62 | | const SearchParameters* params = nullptr) const override; |
63 | | |
64 | | void display() const; |
65 | | size_t hashtable_size() const; |
66 | | }; |
67 | | |
68 | | struct IndexBinaryHashStats { |
69 | | size_t nq; // nb of queries run |
70 | | size_t n0; // nb of empty lists |
71 | | size_t nlist; // nb of non-empty inverted lists scanned |
72 | | size_t ndis; // nb of distancs computed |
73 | | |
74 | 6 | IndexBinaryHashStats() { |
75 | 6 | reset(); |
76 | 6 | } |
77 | | void reset(); |
78 | | }; |
79 | | |
80 | | FAISS_API extern IndexBinaryHashStats indexBinaryHash_stats; |
81 | | |
82 | | /** just uses the b first bits as a hash value */ |
83 | | struct IndexBinaryMultiHash : IndexBinary { |
84 | | // where the vectors are actually stored |
85 | | IndexBinaryFlat* storage; |
86 | | bool own_fields; |
87 | | |
88 | | // maps hash values to the ids that hash to them |
89 | | using Map = std::unordered_map<idx_t, std::vector<idx_t>>; |
90 | | |
91 | | // the different hashes, size nhash |
92 | | std::vector<Map> maps; |
93 | | |
94 | | int nhash; ///< nb of hash maps |
95 | | int b; ///< nb bits per hash map |
96 | | int nflip; ///< nb bit flips to use at search time |
97 | | |
98 | | IndexBinaryMultiHash(int d, int nhash, int b); |
99 | | |
100 | | IndexBinaryMultiHash(); |
101 | | |
102 | | ~IndexBinaryMultiHash(); |
103 | | |
104 | | void reset() override; |
105 | | |
106 | | void add(idx_t n, const uint8_t* x) override; |
107 | | |
108 | | void range_search( |
109 | | idx_t n, |
110 | | const uint8_t* x, |
111 | | int radius, |
112 | | RangeSearchResult* result, |
113 | | const SearchParameters* params = nullptr) const override; |
114 | | |
115 | | void search( |
116 | | idx_t n, |
117 | | const uint8_t* x, |
118 | | idx_t k, |
119 | | int32_t* distances, |
120 | | idx_t* labels, |
121 | | const SearchParameters* params = nullptr) const override; |
122 | | |
123 | | size_t hashtable_size() const; |
124 | | }; |
125 | | |
126 | | } // namespace faiss |
127 | | |
128 | | #endif |