/root/doris/contrib/faiss/faiss/IndexIVFFlat.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_INDEX_IVF_FLAT_H |
11 | | #define FAISS_INDEX_IVF_FLAT_H |
12 | | |
13 | | #include <stdint.h> |
14 | | #include <unordered_map> |
15 | | |
16 | | #include <faiss/IndexIVF.h> |
17 | | |
18 | | namespace faiss { |
19 | | |
20 | | /** Inverted file with stored vectors. Here the inverted file |
21 | | * pre-selects the vectors to be searched, but they are not otherwise |
22 | | * encoded, the code array just contains the raw float entries. |
23 | | */ |
24 | | struct IndexIVFFlat : IndexIVF { |
25 | | IndexIVFFlat( |
26 | | Index* quantizer, |
27 | | size_t d, |
28 | | size_t nlist_, |
29 | | MetricType = METRIC_L2); |
30 | | |
31 | | void add_core( |
32 | | idx_t n, |
33 | | const float* x, |
34 | | const idx_t* xids, |
35 | | const idx_t* precomputed_idx, |
36 | | void* inverted_list_context = nullptr) override; |
37 | | |
38 | | void encode_vectors( |
39 | | idx_t n, |
40 | | const float* x, |
41 | | const idx_t* list_nos, |
42 | | uint8_t* codes, |
43 | | bool include_listnos = false) const override; |
44 | | |
45 | | InvertedListScanner* get_InvertedListScanner( |
46 | | bool store_pairs, |
47 | | const IDSelector* sel, |
48 | | const IVFSearchParameters* params) const override; |
49 | | |
50 | | void reconstruct_from_offset(int64_t list_no, int64_t offset, float* recons) |
51 | | const override; |
52 | | |
53 | | void sa_decode(idx_t n, const uint8_t* bytes, float* x) const override; |
54 | | |
55 | | IndexIVFFlat(); |
56 | | }; |
57 | | |
58 | | struct IndexIVFFlatDedup : IndexIVFFlat { |
59 | | /** Maps ids stored in the index to the ids of vectors that are |
60 | | * the same. When a vector is unique, it does not appear in the |
61 | | * instances map */ |
62 | | std::unordered_multimap<idx_t, idx_t> instances; |
63 | | |
64 | | IndexIVFFlatDedup( |
65 | | Index* quantizer, |
66 | | size_t d, |
67 | | size_t nlist_, |
68 | | MetricType = METRIC_L2); |
69 | | |
70 | | /// also dedups the training set |
71 | | void train(idx_t n, const float* x) override; |
72 | | |
73 | | /// implemented for all IndexIVF* classes |
74 | | void add_with_ids(idx_t n, const float* x, const idx_t* xids) override; |
75 | | |
76 | | void search_preassigned( |
77 | | idx_t n, |
78 | | const float* x, |
79 | | idx_t k, |
80 | | const idx_t* assign, |
81 | | const float* centroid_dis, |
82 | | float* distances, |
83 | | idx_t* labels, |
84 | | bool store_pairs, |
85 | | const IVFSearchParameters* params = nullptr, |
86 | | IndexIVFStats* stats = nullptr) const override; |
87 | | |
88 | | size_t remove_ids(const IDSelector& sel) override; |
89 | | |
90 | | /// not implemented |
91 | | void range_search( |
92 | | idx_t n, |
93 | | const float* x, |
94 | | float radius, |
95 | | RangeSearchResult* result, |
96 | | const SearchParameters* params = nullptr) const override; |
97 | | |
98 | | /// not implemented |
99 | | void update_vectors(int nv, const idx_t* idx, const float* v) override; |
100 | | |
101 | | /// not implemented |
102 | | void reconstruct_from_offset(int64_t list_no, int64_t offset, float* recons) |
103 | | const override; |
104 | | |
105 | 0 | IndexIVFFlatDedup() {} |
106 | | }; |
107 | | |
108 | | } // namespace faiss |
109 | | |
110 | | #endif |