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
2 changes: 2 additions & 0 deletions faiss/gpu/GpuCloner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ faiss::Index* index_cpu_to_gpu(
int device,
const faiss::Index* index,
const GpuClonerOptions* options) {
auto index_pq = dynamic_cast<const faiss::IndexPQ*>(index);
FAISS_THROW_IF_MSG(index_pq, "This index type is not implemented on GPU.");
GpuClonerOptions defaults;
ToGpuCloner cl(provider, device, options ? *options : defaults);
return cl.clone_Index(index);
Expand Down
29 changes: 29 additions & 0 deletions faiss/gpu/test/test_index_cpu_to_gpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import numpy as np
import unittest
import faiss


class TestMoveToGpu(unittest.TestCase):
def test_index_cpu_to_gpu(self):
dimension = 128
n = 2500
db_vectors = np.random.random((n, dimension)).astype('float32')
code_size = 16
res = faiss.StandardGpuResources()
index_pq = faiss.IndexPQ(dimension, code_size, 6)
index_pq.train(db_vectors)
index_pq.add(db_vectors)
self.assertRaisesRegex(Exception, ".*not implemented.*",
faiss.index_cpu_to_gpu, res, 0, index_pq)

def test_index_cpu_to_gpu_does_not_throw_with_index_flat(self):
dimension = 128
n = 100
db_vectors = np.random.random((n, dimension)).astype('float32')
res = faiss.StandardGpuResources()
index_flat = faiss.IndexFlatL2(dimension)
index_flat.add(db_vectors)
try:
faiss.index_cpu_to_gpu(res, 0, index_flat)
except Exception:
self.fail("index_cpu_to_gpu() threw an unexpected exception.")
7 changes: 7 additions & 0 deletions faiss/impl/FaissAssert.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@
} \
} while (false)

#define FAISS_THROW_IF_MSG(X, MSG) \
do { \
if (X) { \
FAISS_THROW_FMT("Error: '%s' failed: " MSG, #X); \
} \
} while (false)

#define FAISS_THROW_IF_NOT_MSG(X, MSG) \
do { \
if (!(X)) { \
Expand Down