Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test to cover GPU #4130

Closed
wants to merge 2 commits into from
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:
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
10 changes: 8 additions & 2 deletions tests/test_contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

class TestComputeGT(unittest.TestCase):

def do_test_compute_GT(self, metric=faiss.METRIC_L2):
def do_test_compute_GT(self, metric=faiss.METRIC_L2, ngpu=0):
d = 64
xt, xb, xq = get_dataset_2(d, 0, 10000, 100)

Expand All @@ -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=ngpu)

np.testing.assert_array_equal(Iref, Inew)
# decimal = 4 required when run on GPU
Expand All @@ -62,6 +62,12 @@ def test_compute_GT(self):
def test_compute_GT_ip(self):
self.do_test_compute_GT(faiss.METRIC_INNER_PRODUCT)

def test_compute_GT_gpu(self):
self.do_test_compute_GT(ngpu=-1)

def test_compute_GT_ip_gpu(self):
self.do_test_compute_GT(faiss.METRIC_INNER_PRODUCT, ngpu=-1)


class TestDatasets(unittest.TestCase):
"""here we test only the synthetic dataset. Datasets that require
Expand Down
Loading