Skip to content

Commit fbcc4f7

Browse files
Create retrieval/faiss_gpu.py
1 parent 9a29b9c commit fbcc4f7

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

‎retrieval/faiss_gpu.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import faiss
2+
import numpy as np
3+
4+
5+
class FaissGPU:
6+
7+
def __init__(self, dimension):
8+
self.res = faiss.StandardGpuResources()
9+
cpu_index = faiss.IndexFlatL2(dimension)
10+
self.index = faiss.index_cpu_to_gpu(self.res, 0, cpu_index)
11+
12+
def build(self, embeddings):
13+
self.index.add(embeddings.astype(np.float32))
14+
15+
def search(self, query, top_k):
16+
return self.index.search(query.astype(np.float32), top_k)

0 commit comments

Comments
 (0)