/root/doris/contrib/faiss/faiss/IndexLSH.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 INDEX_LSH_H |
11 | | #define INDEX_LSH_H |
12 | | |
13 | | #include <vector> |
14 | | |
15 | | #include <faiss/IndexFlatCodes.h> |
16 | | #include <faiss/VectorTransform.h> |
17 | | |
18 | | namespace faiss { |
19 | | |
20 | | /** The sign of each vector component is put in a binary signature */ |
21 | | struct IndexLSH : IndexFlatCodes { |
22 | | int nbits; ///< nb of bits per vector |
23 | | bool rotate_data; ///< whether to apply a random rotation to input |
24 | | bool train_thresholds; ///< whether we train thresholds or use 0 |
25 | | |
26 | | RandomRotationMatrix rrot; ///< optional random rotation |
27 | | |
28 | | std::vector<float> thresholds; ///< thresholds to compare with |
29 | | |
30 | | IndexLSH( |
31 | | idx_t d, |
32 | | int nbits, |
33 | | bool rotate_data = true, |
34 | | bool train_thresholds = false); |
35 | | |
36 | | /** Preprocesses and resizes the input to the size required to |
37 | | * binarize the data |
38 | | * |
39 | | * @param x input vectors, size n * d |
40 | | * @return output vectors, size n * bits. May be the same pointer |
41 | | * as x, otherwise it should be deleted by the caller |
42 | | */ |
43 | | const float* apply_preprocess(idx_t n, const float* x) const; |
44 | | |
45 | | void train(idx_t n, const float* x) override; |
46 | | |
47 | | void search( |
48 | | idx_t n, |
49 | | const float* x, |
50 | | idx_t k, |
51 | | float* distances, |
52 | | idx_t* labels, |
53 | | const SearchParameters* params = nullptr) const override; |
54 | | |
55 | | /// transfer the thresholds to a pre-processing stage (and unset |
56 | | /// train_thresholds) |
57 | | void transfer_thresholds(LinearTransform* vt); |
58 | | |
59 | 0 | ~IndexLSH() override {} |
60 | | |
61 | | IndexLSH(); |
62 | | |
63 | | /* standalone codec interface. |
64 | | * |
65 | | * The vectors are decoded to +/- 1 (not 0, 1) */ |
66 | | void sa_encode(idx_t n, const float* x, uint8_t* bytes) const override; |
67 | | |
68 | | void sa_decode(idx_t n, const uint8_t* bytes, float* x) const override; |
69 | | }; |
70 | | |
71 | | } // namespace faiss |
72 | | |
73 | | #endif |