facebookresearch / faiss

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

IVFFlat is slower than FlatIP on cpu #3151

Closed devilztt closed 4 months ago

devilztt commented 11 months ago

Summary

faiss.IndexIVFFlat is slower than faiss.IndexFlatIP, I dont know why , the numpy installed like "pip install intel-numpy" faiss installed like "pip install faiss-cpu", whatever windows or linux , always slow

Running on:

Interface:

import faiss
import numpy as np
import time

feat = np.random.rand(200000,256).astype(np.float32)
faiss.normalize_L2(feat)

index = faiss.IndexFlatIP(256)

t1 = time.time()
index.add(feat)

D, I = index.search(feat, 80)
I = I[5]
I.sort()
print(I)
print(time.time() - t1)

nlist = 100
k = 80
quantizer = faiss.IndexFlatIP(256)  # the other index
index = faiss.IndexIVFFlat(quantizer, 256, nlist, faiss.METRIC_INNER_PRODUCT)
# here we specify METRIC_L2, by default it performs inner-product search
t1 = time.time()
assert not index.is_trained
index.train(feat)
assert index.is_trained

t2 = time.time()
print("train time", t2 - t1)
index.add(feat)                  # add may be a bit slower as well
print("add time", time.time() - t2)
index.nprobe = 10
D, I2 = index.search(feat, k)     # actual search
I2 = I2[5]
I2.sort()
print(I2)                  # neighbors of the 5 last queries
print(time.time() - t2)

the result like this [ 5 1894 2280 5111 10577 11089 14732 18807 18967 21015 23812 24122 28390 34554 35481 35825 36582 36676 38201 41180 43978 45719 52503 53086 60008 64889 66976 70646 72942 73486 74631 75932 79380 79633 80082 86244 90109 93231 94581 94660 100069 101279 106608 108983 109923 110811 111534 114150 115507 120026 121706 121856 122207 128322 130886 132046 132558 132770 135639 136214 138723 139387 140430 143404 143824 147467 150482 154372 159113 161653 162230 163741 164281 171253 175471 181143 188320 190826 196939 198481] 68.06165814399719 train time 0.2234022617340088 add time 0.1904909610748291 [ 5 649 2003 2592 5275 5722 6303 8940 14659 14732 16775 17021 18792 18967 21015 23812 26365 27189 27495 28345 28839 30134 31050 34804 37239 37717 38332 41180 47334 50602 51536 52664 53286 55272 58175 58839 61951 68532 70991 74631 75405 75932 83016 88473 91417 92814 97242 100885 110811 111843 115925 116086 117007 120136 121706 125411 125960 127650 139682 140102 147988 154360 159113 160612 163741 164281 165284 181111 181976 186438 188130 188616 190336 191513 192460 192993 193155 193458 197498 199241] 222.23961925506592

EFLql commented 11 months ago

I also encountered the same question, please take a look of it, thanks. @mdouze @devilztt @tomdyson @ot @lsb @benfred

mdouze commented 11 months ago

things to look at:

devilztt commented 11 months ago

Reduce nprobe can make it quick a little, but still slow, if set nprobe = 1, the acc is too low. Why is setting it to 10 so slow, my nlist = 100 . The data is random, but even when I use real data, the results are similar. I think it has nothing to do with the proportion of the data. And I print index.invlists.imbalance_factor() = 1.06509302. I have tried using Conda to install it, but it is also the same problem, that is, using C++as an example, compiling with the addition of the mkl library is the same I want to know if you have tried this before. Currently, as long as I use a CPU platform, the results are the same. As long as I use a math library, IVFFlat is much slower than FlatL2.

  1. Reduce nprobe can make it quick a little, but still slow, if set nprobe = 1, the acc is too low. Why is setting it to 10 so slow, my nlist = 100 .
  2. The data is random, but even when I use real data, the results are similar. I think it has nothing to do with the proportion of the data. And I print index.invlists.imbalance_factor() = 1.06509302.
  3. I have tried using Conda to install it, but it is also the same problem, that is, using C++as an example, compiling with the addition of the mkl library is the same

I want to know if you have tried this before. Currently, as long as I use a CPU platform, the results are the same. As long as I use a math library, IVFFlat is much slower than FlatL2.