| 1 |
# 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 |
import numpy as np |
| 7 |
|
| 8 |
d = 64 # dimension |
| 9 |
nb = 100000 # database size |
| 10 |
nq = 10000 # nb of queries |
| 11 |
np.random.seed(1234) # make reproducible |
| 12 |
xb = np.random.random((nb, d)).astype('float32') |
| 13 |
xb[:, 0] += np.arange(nb) / 1000. |
| 14 |
xq = np.random.random((nq, d)).astype('float32') |
| 15 |
xq[:, 0] += np.arange(nq) / 1000. |
| 16 |
|
| 17 |
import faiss # make faiss available |
| 18 |
|
| 19 |
ngpus = faiss.get_num_gpus() |
| 20 |
|
| 21 |
print("number of GPUs:", ngpus) |
| 22 |
|
| 23 |
cpu_index = faiss.IndexFlatL2(d) |
| 24 |
|
| 25 |
gpu_index = faiss.index_cpu_to_all_gpus( # build the index |
| 26 |
cpu_index |
| 27 |
) |
| 28 |
|
| 29 |
gpu_index.add(xb) # add vectors to the index |
| 30 |
print(gpu_index.ntotal) |
| 31 |
|
| 32 |
k = 4 # we want to see 4 nearest neighbors |
| 33 |
D, I = gpu_index.search(xq, k) # actual search |
| 34 |
print(I[:5]) # neighbors of the 5 first queries |
| 35 |
print(I[-5:]) # neighbors of the 5 last queries |