facebookresearch / faiss

A library for efficient similarity search and clustering of dense vectors.
https://faiss.ai
MIT License
30.94k stars 3.61k forks source link

Move ParameterSpace params to GPU together with index? #3270

Open mlomeli1 opened 7 months ago

mlomeli1 commented 7 months ago

Summary

I would like to either enable moving the parameters, defined with ParameterSpace for a cpu index, to the GPU if the index is moved to the GPU or have a warning that flags that I need to reset the parameters once I move my index to GPU.

Platform

OS: Ubuntu 20.04.6

Faiss version:1.7.4

Installed from: conda

Running on:

Interface:

Reproduction instructions

An example is provided where I set the verbose mode to true using ParameterSpace() but I can't see the expected logs once I move my index to the gpu.

import faiss
import numpy as np
import math
dimension = 128                          
n = 1000000                
db_vectors = np.random.random((n, dimension)).astype('float32') 
k = 3 
dimension=128
query_vectors = np.random.random((n, dimension)).astype('float32')
code_size=16
nlist=math.floor(math.sqrt(n))
res = faiss.StandardGpuResources()
#define the ivfpq index in cpu
quantizer = faiss.IndexFlatL2(dimension)  # coarse quantizer
#define the inverted index 
index_ivfpq = faiss.IndexIVFPQ(quantizer, dimension, nlist, code_size, 8)
params = faiss.ParameterSpace().set_index_parameter(index_ivfpq, "verbose", 1)
#params.initialize(index_ivfpq)
index_ivfpq.train(db_vectors) 
gpu_index_ivfpq = faiss.index_cpu_to_gpu(res, 0, index_ivfpq)
gpu_index_ivfpq.add(db_vectors) 

Logs:

Training level-1 quantizer
Training level-1 quantizer on 1000000 vectors in 128D
Training IVF residual
  Input training set too big (max size is 65536), sampling 65536 / 1000000 vectors
precomputing IVFPQ tables type 1

Expected logs:

Training level-1 quantizer
Training level-1 quantizer on 1000000 vectors in 128D
Training IVF residual
  Input training set too big (max size is 65536), sampling 65536 / 1000000 vectors
precomputing IVFPQ tables type 1
IndexIVFPQ::add_core_o: adding 0:32768 / 1000000
 add_core times: 0.000 10.972 4.036 
IndexIVFPQ::add_core_o: adding 32768:65536 / 1000000
 add_core times: 0.000 10.282 2.768 
IndexIVFPQ::add_core_o: adding 65536:98304 / 1000000
 add_core times: 0.000 10.315 1.990 
IndexIVFPQ::add_core_o: adding 98304:131072 / 1000000
 add_core times: 0.000 10.265 2.927 
IndexIVFPQ::add_core_o: adding 131072:163840 / 1000000
 add_core times: 0.000 10.234 2.146 
IndexIVFPQ::add_core_o: adding 163840:196608 / 1000000
 add_core times: 0.000 10.244 1.604 
IndexIVFPQ::add_core_o: adding 196608:229376 / 1000000
 add_core times: 0.000 10.307 1.813 
IndexIVFPQ::add_core_o: adding 229376:262144 / 1000000
 add_core times: 0.000 10.226 3.425 
IndexIVFPQ::add_core_o: adding 262144:294912 / 1000000
 add_core times: 0.000 10.271 2.303 
IndexIVFPQ::add_core_o: adding 294912:327680 / 1000000
 add_core times: 0.000 10.288 1.718 
IndexIVFPQ::add_core_o: adding 327680:360448 / 1000000
 add_core times: 0.000 10.336 1.774 
IndexIVFPQ::add_core_o: adding 360448:393216 / 1000000
 add_core times: 0.000 10.261 1.791 
IndexIVFPQ::add_core_o: adding 393216:425984 / 1000000
 add_core times: 0.000 10.275 1.840 
IndexIVFPQ::add_core_o: adding 425984:458752 / 1000000
 add_core times: 0.001 10.319 1.974 
IndexIVFPQ::add_core_o: adding 458752:491520 / 1000000
 add_core times: 0.000 10.685 2.827 
IndexIVFPQ::add_core_o: adding 491520:524288 / 1000000
 add_core times: 0.000 11.302 3.869 
IndexIVFPQ::add_core_o: adding 524288:557056 / 1000000
 add_core times: 0.000 10.720 2.756 
IndexIVFPQ::add_core_o: adding 557056:589824 / 1000000
 add_core times: 0.001 10.776 2.181 
IndexIVFPQ::add_core_o: adding 589824:622592 / 1000000
 add_core times: 0.000 10.457 2.213 
IndexIVFPQ::add_core_o: adding 622592:655360 / 1000000
 add_core times: 0.000 10.604 2.088 
IndexIVFPQ::add_core_o: adding 655360:688128 / 1000000
 add_core times: 0.000 11.342 2.086 
IndexIVFPQ::add_core_o: adding 688128:720896 / 1000000
 add_core times: 0.000 10.585 2.070 
IndexIVFPQ::add_core_o: adding 720896:753664 / 1000000
 add_core times: 0.000 10.772 2.052 
IndexIVFPQ::add_core_o: adding 753664:786432 / 1000000
 add_core times: 0.000 10.778 2.030 
IndexIVFPQ::add_core_o: adding 786432:819200 / 1000000
 add_core times: 0.000 10.539 2.031 
IndexIVFPQ::add_core_o: adding 819200:851968 / 1000000
 add_core times: 0.001 10.674 2.029 
IndexIVFPQ::add_core_o: adding 851968:884736 / 1000000
 add_core times: 0.000 10.457 2.050 
IndexIVFPQ::add_core_o: adding 884736:917504 / 1000000
 add_core times: 0.000 10.485 2.264 
IndexIVFPQ::add_core_o: adding 917504:950272 / 1000000
 add_core times: 0.000 10.657 3.549 
IndexIVFPQ::add_core_o: adding 950272:983040 / 1000000
 add_core times: 0.000 10.528 4.910 
IndexIVFPQ::add_core_o: adding 983040:1000000 / 1000000
 add_core times: 0.000 8.165 2.784 
mdouze commented 7 months ago

I think this is one-off for verbose. Normally as many parameters as possible should be moved to GPU.