[WIP] Update tutorial examples (#342)

* Remove transitional print import.

* Add example for multiple GPUs in python.

* Add example on GPU in C++.

* Add example on multiple GPUs in C++.

* Add IVFFlat example on GPU in python.

* Add Makefile for C++ tutorial examples.

* Add IVF index on GPU example in C++.
This commit is contained in:
Lucas Hosseini
2018-02-20 15:53:56 +01:00
committed by GitHub
parent d970473dd8
commit 2c9aea24e7
8 changed files with 311 additions and 11 deletions
-1
View File
@@ -4,7 +4,6 @@
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import print_function
import numpy as np
d = 64 # dimension
-1
View File
@@ -4,7 +4,6 @@
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import print_function
import numpy as np
d = 64 # dimension
-1
View File
@@ -4,7 +4,6 @@
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import print_function
import numpy as np
d = 64 # dimension
+33 -8
View File
@@ -4,7 +4,6 @@
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import print_function
import numpy as np
d = 64 # dimension
@@ -16,20 +15,46 @@ xb[:, 0] += np.arange(nb) / 1000.
xq = np.random.random((nq, d)).astype('float32')
xq[:, 0] += np.arange(nq) / 1000.
import faiss # make faiss available
import faiss # make faiss available
print("number of GPUs:", faiss.get_num_gpus())
index = faiss.IndexFlatL2(d) # build the index
res = faiss.StandardGpuResources() # use a single GPU
res = faiss.StandardGpuResources()
## Using a flat index
index = faiss.index_cpu_to_gpu(res, 0, index)
index_flat = faiss.IndexFlatL2(d) # build a flat (CPU) index
index.add(xb) # add vectors to the index
print(index.ntotal)
# make it a flat GPU index
gpu_index_flat = faiss.index_cpu_to_gpu(res, 0, index_flat)
gpu_index_flat.add(xb) # add vectors to the index
print(gpu_index_flat.ntotal)
k = 4 # we want to see 4 nearest neighbors
D, I = index.search(xq, k) # actual search
D, I = gpu_index_flat.search(xq, k) # actual search
print(I[:5]) # neighbors of the 5 first queries
print(I[-5:]) # neighbors of the 5 last queries
## Using an IVF index
nlist = 100
quantizer = faiss.IndexFlatL2(d) # the other index
index_ivf = faiss.IndexIVFFlat(quantizer, d, nlist, faiss.METRIC_L2)
# here we specify METRIC_L2, by default it performs inner-product search
# make it an IVF GPU index
gpu_index_ivf = faiss.index_cpu_to_gpu(res, 0, index_ivf)
assert not gpu_index_ivf.is_trained
gpu_index_ivf.train(xb) # add vectors to the index
assert gpu_index_ivf.is_trained
gpu_index_ivf.add(xb) # add vectors to the index
print(gpu_index_ivf.ntotal)
k = 4 # we want to see 4 nearest neighbors
D, I = gpu_index_ivf.search(xq, k) # actual search
print(I[:5]) # neighbors of the 5 first queries
print(I[-5:]) # neighbors of the 5 last queries
+34
View File
@@ -0,0 +1,34 @@
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.
import numpy as np
d = 64 # dimension
nb = 100000 # database size
nq = 10000 # nb of queries
np.random.seed(1234) # make reproducible
xb = np.random.random((nb, d)).astype('float32')
xb[:, 0] += np.arange(nb) / 1000.
xq = np.random.random((nq, d)).astype('float32')
xq[:, 0] += np.arange(nq) / 1000.
import faiss # make faiss available
ngpus = faiss.get_num_gpus()
print("number of GPUs:", ngpus)
index = faiss.index_cpu_to_all_gpus( # build the index
faiss.IndexFlatL2(d)
)
index.add(xb) # add vectors to the index
print(index.ntotal)
k = 4 # we want to see 4 nearest neighbors
D, I = index.search(xq, k) # actual search
print(I[:5]) # neighbors of the 5 first queries
print(I[-5:]) # neighbors of the 5 last queries