| 1 |
# Portions Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# |
| 3 |
# This source code is licensed under the MIT license found in the |
| 4 |
# LICENSE file in the root directory of this source tree. |
| 5 |
|
| 6 |
# Portions Copyright 2025 Intel Corporation |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
import numpy as np |
| 22 |
import faiss # make faiss available |
| 23 |
|
| 24 |
d = 64 # dimension |
| 25 |
nb = 100000 # database size |
| 26 |
nq = 10000 # nb of queries |
| 27 |
np.random.seed(1234) # make reproducible |
| 28 |
xb = np.random.random((nb, d)).astype('float32') |
| 29 |
xb[:, 0] += np.arange(nb) / 1000. |
| 30 |
xq = np.random.random((nq, d)).astype('float32') |
| 31 |
xq[:, 0] += np.arange(nq) / 1000. |
| 32 |
|
| 33 |
index = faiss.IndexSVSVamana(d, 64) # build the index (DynamicVamana, float32) |
| 34 |
|
| 35 |
print(index.is_trained) |
| 36 |
index.add(xb) # add vectors to the index |
| 37 |
print(index.ntotal) |
| 38 |
|
| 39 |
k = 4 # we want to see 4 nearest neighbors |
| 40 |
|
| 41 |
print(f"{k} nearest neighbors of the first 5 vectors") |
| 42 |
D, I = index.search(xb[:5], k) # sanity check |
| 43 |
print(I) |
| 44 |
print(D) |
| 45 |
D, I = index.search(xq, k) # actual search |
| 46 |
print(f"{k} nearest neighbors of the 5 first query vectors") |
| 47 |
print(I[:5]) # neighbors of the 5 first queries |
| 48 |
print(f"{k} nearest neighbors of the 5 last query vectors") |
| 49 |
print(I[-5:]) # neighbors of the 5 last queries |
| 50 |
|
| 51 |
faiss.write_index(index, "index.faiss") |
| 52 |
reloaded = faiss.read_index("index.faiss") |
| 53 |
|
| 54 |
D, I = reloaded.search(xq, k) # search with the reloaded |
| 55 |
print(f"{k} nearest neighbors of the 5 first query vectors (after reloading)") |
| 56 |
print(I[:5]) # neighbors of the 5 first queries |
| 57 |
print(f"{k} nearest neighbors of the 5 last query vectors (after reloading)") |
| 58 |
print(I[-5:]) # neighbors of the 5 last queries |
| 59 |
|
| 60 |
flat_idx_fac = faiss.index_factory(d, 'SVSFlat', faiss.METRIC_L2) # example of using factory for SVS Flat |
| 61 |
flat_idx_fac.add(xb) |
| 62 |
flat_idx_fac.search(xq, k) |
| 63 |
|
| 64 |
uncompressed_idx_fac = faiss.index_factory(d, 'SVSVamana64', faiss.METRIC_L2) # example of using factory for SVS Vamana uncompressed |
| 65 |
uncompressed_idx_fac.add(xb) |
| 66 |
uncompressed_idx_fac.search(xq, k) |
| 67 |
|
| 68 |
lvq_idx = faiss.IndexSVSVamanaLVQ(d, 32, faiss.METRIC_L2, faiss.SVS_LVQ4x8) # example of using SVS Vamana LVQ |
| 69 |
lvq_idx_fac_2 = faiss.index_factory(d, 'SVSVamana32,LVQ4x4', faiss.METRIC_L2) # example of using factory for SVS Vamana LVQ |
| 70 |
lvq_idx_fac_2.add(xb) |
| 71 |
lvq_idx_fac_2.search(xq, k) |
| 72 |
|
| 73 |
|
| 74 |
leanvec_idx = faiss.IndexSVSVamanaLeanVec(d, 32, faiss.METRIC_L2, 0, faiss.SVS_LeanVec4x4) # example of using SVS Vamana LeanVec |
| 75 |
leanvec_idx_fac = faiss.index_factory(d, 'SVSVamana32,LeanVec4x4', faiss.METRIC_L2) # example of using factory for SVS Vamana LeanVec |
| 76 |
leanvec_idx_fac.train(xb) |
| 77 |
leanvec_idx_fac.add(xb) |
| 78 |
leanvec_idx_fac.search(xq, k) |
| 79 |
|
| 80 |
leanvec_idx_fac2 = faiss.index_factory(d, 'SVSVamana64,LeanVec4x4_16', faiss.METRIC_L2) # example of using factory for SVS Vamana LeanVec. leanvec_dims is 16 |
| 81 |
leanvec_idx_fac2.train(xb) |
| 82 |
leanvec_idx_fac2.add(xb) |
| 83 |
leanvec_idx_fac2.search(xq, k) |