Faiss: save index error??

Created on 11 May 2020  Â·  3Comments  Â·  Source: facebookresearch/faiss

OS: -- Ubuntu1804 --

Faiss version: 1.6.3
Faiss compilation options: -- pip install faiss-gpu --

Running on:GPU
Interface: Python

I refer the script and will use in the script

save method refer the web

datasets:

d = 64                           # dimension
nb = 10000000                      # database size
nq = 10000                       # nb of queries
np.random.seed(1234)             # make reproducible
xb = np.random.random((nb, d)).astype('float32')
xb[:, 0] += np.arange(nb) / 1000.
xq = np.random.random((nq, d)).astype('float32')
xq[:, 0] += np.arange(nq) / 1000.

code 1

res = faiss.StandardGpuResources()  # use a single GPU
nlist = 1000#
quantizer = faiss.IndexFlatL2(d)  # the other index
index_ivf = faiss.IndexIVFFlat(quantizer, d, nlist, faiss.METRIC_L2)
# here we specify METRIC_L2, by default it performs inner-product search

# make it an IVF GPU index
gpu_index_ivf = faiss.index_cpu_to_gpu(res, 0, index_ivf)

assert not gpu_index_ivf.is_trained
gpu_index_ivf.train(xb)        # add vectors to the index
assert gpu_index_ivf.is_trained
gpu_index_ivf.add(xb)          # add vectors to the index
k = 100                          # we want to see 4 nearest neighbors
D, I = gpu_index_ivf.search(xq, k)  # actual search

#save index and load index
writer = faiss.VectorIOWriter()
faiss.write_index(gpu_index_ivf,writer.data)
index5=faiss.vector_to_array(writer.data)

error 1

Traceback (most recent call last):
  File "faiss_org_.py", line 139, in <module>
    faiss.write_index(gpu_index_ivf,writer.data)
  File "/home/xu1/anaconda3/lib/python3.7/site-packages/faiss/swigfaiss.py", line 4730, in write_index
    return _swigfaiss.write_index(*args)
TypeError: Wrong number or type of arguments for overloaded function 'write_index'.
  Possible C/C++ prototypes are:
    faiss::write_index(faiss::Index const *,char const *)
    faiss::write_index(faiss::Index const *,FILE *)
    faiss::write_index(faiss::Index const *,faiss::IOWriter *)

code 2

ngpus = faiss.get_num_gpus()
cpu_index = faiss.IndexFlatL2(d)
gpu_index = faiss.index_cpu_to_all_gpus(  # build the index
    cpu_index
)
gpu_index.add(xb)              # add vectors to the index
print(gpu_index.ntotal)
k = 100                         # we want to see 4 nearest neighbors
D, I = gpu_index.search(xq, k) # actual search
writer = faiss.VectorIOWriter()
faiss.write_index(gpu_index,writer.data)

and the error is same as error 1

could you please help me ?
thx

Most helpful comment

@mdouze
how to move CPU??
thx

All 3 comments

GPU indexes cannot be written. You have to move them to CPU first.

@mdouze
how to move CPU??
thx

faiss.write_index(faiss.index_gpu_to_cpu(gpu_index), writer.data)

@mdouze
how to move CPU??
thx

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ilyakhov picture ilyakhov  Â·  3Comments

hashyong picture hashyong  Â·  3Comments

linghuang picture linghuang  Â·  3Comments

cherryPotter picture cherryPotter  Â·  3Comments

zoe-cheung picture zoe-cheung  Â·  3Comments