Faiss GPU#

In the last tutorial, we went through the basics of indexing using faiss-cpu. While for the use cases in research and industry. The size of dataset for indexing will be extremely large, the frequency of searching might also be very high. In this tutorial we’ll see how to combine Faiss and GPU almost seamlessly.

1. Installation#

Faiss maintain the latest updates on conda. And its gpu version only supports Linux x86_64

create a conda virtual environment and run:

conda install -c pytorch -c nvidia faiss-gpu=1.8.0

make sure you select that conda env as the kernel for this notebook. After installation, restart the kernal.

If your system does not satisfy the requirement, install faiss-cpu and just skip the steps with gpu related codes.

2. Data Preparation#

First let’s create two datasets with “fake embeddings” of corpus and queries:

import faiss
import numpy as np

dim = 768
corpus_size = 1000
# np.random.seed(111)

corpus = np.random.random((corpus_size, dim)).astype('float32')

3. Create Index on CPU#

Option 1:#

Faiss provides a great amount of choices of indexes by initializing directly:

# first build a flat index (on CPU)
index = faiss.IndexFlatIP(dim)

Option 2:#

Besides the basic index class, we can also use the index_factory function to produce composite Faiss index.

index = faiss.index_factory(dim, "Flat", faiss.METRIC_L2)

5. Results#

All the three approaches should lead to identical result. Now let’s do a quick sanity check:

# The nearest neighbor of each vector in the corpus is itself
assert np.all(corpus[:] == corpus[I[:, 0]])

And the corresponding distance should be 0.

print(D[:3])
[[  0.       111.30057  113.2251   113.342316]
 [  0.       111.158875 111.742325 112.09038 ]
 [  0.       116.44429  116.849915 117.30502 ]]