Faiss: Example of querying faiss in Python with multiprocessing?

Created on 21 Aug 2019  ·  6Comments  ·  Source: facebookresearch/faiss

Can anyone help provide an example of how to use Faiss with python multiprocessing? Currently I can only load faiss index in each individual process, and in each process the index is loaded into its own memory (leading to large memory consumption).

Any build-in way I can load the index once and query from multiple processes in parallel?

question

All 6 comments

You can use the Python multiprocessing.dummy module, see eg.

from multiprocessing.dummy import Pool as ThreadPool

index = ...

def do_search(i): 
    return index.search(x[i:i+1])

pool = ThreadPool(10)
results = pool.map(do_search, range(10))

@mdouze Thanks for your answer! I am not very familiar with Python's multi-threading. I know python has a global interpreter lock. Will about code make use more than 1 CPU? Thanks again!

Faiss (like numpy) releases the GIL so this should not be a problem.

AFAIK once the index is loaded, the search utilize all of my CPUs.

no activity, closing.

Faiss (like numpy) releases the GIL so this should not be a problem.

why not use multiprocess.Pool to concurrent?
Does the process pool thread pool make any difference to the use of faiss?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brunodoamaral picture brunodoamaral  ·  3Comments

lukedeo picture lukedeo  ·  3Comments

hipitt picture hipitt  ·  3Comments

hashyong picture hashyong  ·  3Comments

daniellevy picture daniellevy  ·  3Comments