/root/doris/contrib/faiss/faiss/IndexBinary.cpp
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 | | #include <faiss/IndexBinary.h> |
11 | | #include <faiss/impl/FaissAssert.h> |
12 | | |
13 | | #include <cinttypes> |
14 | | #include <cstring> |
15 | | |
16 | | namespace faiss { |
17 | | |
18 | | IndexBinary::IndexBinary(idx_t d, MetricType metric) |
19 | 0 | : d(d), code_size(d / 8), metric_type(metric) { |
20 | 0 | FAISS_THROW_IF_NOT(d % 8 == 0); |
21 | 0 | } |
22 | | |
23 | 0 | IndexBinary::~IndexBinary() = default; |
24 | | |
25 | 0 | void IndexBinary::train(idx_t, const uint8_t*) { |
26 | | // Does nothing by default. |
27 | 0 | } |
28 | | |
29 | | void IndexBinary::range_search( |
30 | | idx_t, |
31 | | const uint8_t*, |
32 | | int, |
33 | | RangeSearchResult*, |
34 | 0 | const SearchParameters*) const { |
35 | 0 | FAISS_THROW_MSG("range search not implemented"); |
36 | 0 | } |
37 | | |
38 | | void IndexBinary::assign(idx_t n, const uint8_t* x, idx_t* labels, idx_t k) |
39 | 0 | const { |
40 | 0 | std::vector<int> distances(n * k); |
41 | 0 | search(n, x, k, distances.data(), labels); |
42 | 0 | } |
43 | | |
44 | 0 | void IndexBinary::add_with_ids(idx_t, const uint8_t*, const idx_t*) { |
45 | 0 | FAISS_THROW_MSG("add_with_ids not implemented for this type of index"); |
46 | 0 | } |
47 | | |
48 | 0 | size_t IndexBinary::remove_ids(const IDSelector&) { |
49 | 0 | FAISS_THROW_MSG("remove_ids not implemented for this type of index"); |
50 | 0 | return 0; |
51 | 0 | } |
52 | | |
53 | 0 | void IndexBinary::reconstruct(idx_t, uint8_t*) const { |
54 | 0 | FAISS_THROW_MSG("reconstruct not implemented for this type of index"); |
55 | 0 | } |
56 | | |
57 | 0 | void IndexBinary::reconstruct_n(idx_t i0, idx_t ni, uint8_t* recons) const { |
58 | 0 | for (idx_t i = 0; i < ni; i++) { |
59 | 0 | reconstruct(i0 + i, recons + i * code_size); |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | | void IndexBinary::search_and_reconstruct( |
64 | | idx_t n, |
65 | | const uint8_t* x, |
66 | | idx_t k, |
67 | | int32_t* distances, |
68 | | idx_t* labels, |
69 | | uint8_t* recons, |
70 | 0 | const SearchParameters* params) const { |
71 | 0 | FAISS_THROW_IF_NOT(k > 0); |
72 | | |
73 | 0 | search(n, x, k, distances, labels, params); |
74 | 0 | for (idx_t i = 0; i < n; ++i) { |
75 | 0 | for (idx_t j = 0; j < k; ++j) { |
76 | 0 | idx_t ij = i * k + j; |
77 | 0 | idx_t key = labels[ij]; |
78 | 0 | uint8_t* reconstructed = recons + ij * code_size; |
79 | 0 | if (key < 0) { |
80 | | // Fill with NaNs |
81 | 0 | memset(reconstructed, -1, code_size); |
82 | 0 | } else { |
83 | 0 | reconstruct(key, reconstructed); |
84 | 0 | } |
85 | 0 | } |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | 0 | void IndexBinary::display() const { |
90 | 0 | printf("Index: %s -> %" PRId64 " elements\n", |
91 | 0 | typeid(*this).name(), |
92 | 0 | ntotal); |
93 | 0 | } |
94 | | |
95 | | void IndexBinary::merge_from( |
96 | | IndexBinary& /* otherIndex */, |
97 | 0 | idx_t /* add_id */) { |
98 | 0 | FAISS_THROW_MSG("merge_from() not implemented"); |
99 | 0 | } |
100 | | |
101 | | void IndexBinary::check_compatible_for_merge( |
102 | 0 | const IndexBinary& /* otherIndex */) const { |
103 | 0 | FAISS_THROW_MSG("check_compatible_for_merge() not implemented"); |
104 | 0 | } |
105 | | |
106 | 0 | size_t IndexBinary::sa_code_size() const { |
107 | 0 | return code_size; |
108 | 0 | } |
109 | | |
110 | | void IndexBinary::add_sa_codes( |
111 | | idx_t n, |
112 | | const uint8_t* codes, |
113 | 0 | const idx_t* xids) { |
114 | 0 | add_with_ids(n, codes, xids); |
115 | 0 | } |
116 | | |
117 | | } // namespace faiss |