Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions contrib/exhaustive_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

LOG = logging.getLogger(__name__)

def knn_ground_truth(xq, db_iterator, k, metric_type=faiss.METRIC_L2):
def knn_ground_truth(xq, db_iterator, k, metric_type=faiss.METRIC_L2, shard=False, ngpu=-1):
"""Computes the exact KNN search results for a dataset that possibly
does not fit in RAM but for which we have an iterator that
returns it block by block.
Expand All @@ -23,9 +23,14 @@ def knn_ground_truth(xq, db_iterator, k, metric_type=faiss.METRIC_L2):
rh = faiss.ResultHeap(nq, k, keep_max=keep_max)

index = faiss.IndexFlat(d, metric_type)
if faiss.get_num_gpus():
LOG.info('running on %d GPUs' % faiss.get_num_gpus())
index = faiss.index_cpu_to_all_gpus(index)
if ngpu == -1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand it correctly, you are trying to find a method to enable gpu based some parameter. I'd suggest that you could use a bool because right now, we either not use gpu or we use all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review.

Since the other functions range_search_max_results and range_ground_truth control the number of GPUs with the argument ngpu argument, I thought it would be good to add ngpu for API uniformity.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then probably you need to pass the ngpu as part of parameters in the index_cpu_to_all_gpus, like

index_gpu = faiss.index_cpu_to_all_gpus(index, co=co, ngpu=ngpu)

index_gpu = faiss.index_cpu_to_all_gpus(index, co=co, ngpu=ngpu)

It will use ngpu to control the number of GPUs

ngpu = faiss.get_num_gpus()

if ngpu:
LOG.info('running on %d GPUs' % ngpu)
co = faiss.GpuMultipleClonerOptions()
co.shard = shard
index = faiss.index_cpu_to_all_gpus(index, co=co, ngpu=ngpu)

# compute ground-truth by blocks, and add to heaps
i0 = 0
Expand Down
2 changes: 1 addition & 1 deletion tests/test_contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def matrix_iterator(xb, bs):
yield xb[i0:i0 + bs]

Dnew, Inew = knn_ground_truth(
xq, matrix_iterator(xb, 1000), 10, metric)
xq, matrix_iterator(xb, 1000), 10, metric, ngpu=0)

np.testing.assert_array_equal(Iref, Inew)
# decimal = 4 required when run on GPU
Expand Down
Loading