faiss icon indicating copy to clipboard operation
faiss copied to clipboard

Will IVFPQR and hnsw support inner product?

Open orrorcol opened this issue 6 years ago • 13 comments

Thank you for your greate work. I find that both IVFPQR and hnsw not supporting ip and I am considering implementing them myself, is there some difficulty that make it hard to implement ip on IVFPQR?

orrorcol avatar May 27 '20 12:05 orrorcol

HNSW does support METRIC_INNER_PRODUCT. It is not particularly hard for IVFPQR it's just that we don't use IVFPQR too often.

mdouze avatar May 28 '20 22:05 mdouze

HNSW does support METRIC_INNER_PRODUCT. It is not particularly hard for IVFPQR it's just that we don't use IVFPQR too often.

Could you inform how to use METRIC_INNER_PRODUCT with HNSW? I tried faiss.index_factory( vector_size, 'HNSW32', METRIC_INNER_PRODUCT) but no luck.

JayYip avatar Jun 02 '20 12:06 JayYip

@JayYip I met the same problem, maybe index_factory shuld be updated

orrorcol avatar Jun 02 '20 13:06 orrorcol

index_factory has a bug that is fixed in the FB internal version (see #1213) In the meantime it is ok to just set metric_type on the index manually.

mdouze avatar Jun 02 '20 20:06 mdouze

Hello, @mdouze , since IVFPQ support inner product, should this line alse be removed? https://github.com/facebookresearch/faiss/blob/e8c8db85aeb72d80f4c83e8003ca778fab575092/IndexIVF.cpp#L93

orrorcol avatar Jun 16 '20 09:06 orrorcol

@uestc-lfs @mdouze any news about this line ? https://github.com/facebookresearch/faiss/blob/e8c8db85aeb72d80f4c83e8003ca778fab575092/IndexIVF.cpp#L93

Thanks for your amazing work btw :)

lhoestq avatar Jul 08 '20 16:07 lhoestq

@lhoestq https://github.com/facebookresearch/faiss/issues/1261

orrorcol avatar Jul 09 '20 06:07 orrorcol

@uestc-lfs Hi, I just wondering how to make a HNSW index with inner product metric. The code index = faiss.index_factory( vector_size, 'HNSW32', METRIC_INNER_PRODUCT)doesn't work. When I check the index.metric_type, it returns 1, which means L2 metric. but this work for IVF or IVF PQ.

KunmingS avatar May 18 '21 10:05 KunmingS

@uestc-lfs Hi, I just wondering how to make a HNSW index with inner product metric. The code index = faiss.index_factory( vector_size, 'HNSW32', METRIC_INNER_PRODUCT)doesn't work. When I check the index.metric_type, it returns 1, which means L2 metric. but this work for IVF or IVF PQ.

@Kun-Ming Use this: image

orrorcol avatar May 22 '21 03:05 orrorcol

Hi @uestc-lfs @mdouze,

I am wondering, by indicating faiss.METRIC_INNER_PRODUCT in faiss.IndexHNSWFlat, does it mean the transformation from dot-product to L2 space will be done automatically?

I have this question since I saw in the facebookresearch/DPR repo, the author additionally implemented the transformation process themselves: https://github.com/facebookresearch/DPR/blob/1ee31c6c5335f05c45138161eb5ce2c03023115d/dpr/indexer/faiss_indexers.py#L157. So maybe they did something unnecessary?

Thanks and looking forward to your reply!

kwang2049 avatar Sep 06 '21 22:09 kwang2049

@kwang2049 Since HNSW supports the inner product, I think it is unnecessary to do the transform.

orrorcol avatar Sep 10 '21 09:09 orrorcol

@uestc-lfs Hi, I just wondering how to make a HNSW index with inner product metric. The code index = faiss.index_factory( vector_size, 'HNSW32', METRIC_INNER_PRODUCT)doesn't work. When I check the index.metric_type, it returns 1, which means L2 metric. but this work for IVF or IVF PQ.

@Kun-Ming Use this: image

Works in latest version.

It does not convert the IP to a L2, it computes inner products itself.

mdouze avatar Sep 14 '21 12:09 mdouze

For other searchers, I want to share something that surprised/tricked me.

If you use metric_type outside the constructor, you will not get the behavior you expect even though metric_type will claim to be IP:

In [65]: import numpy as np
    ...: import faiss
    ...:
    ...: # create a random dataset of 100 vectors of dimension 10
    ...: d = 10
    ...: n = 100
    ...: xb = np.random.rand(n, d).astype(np.float32)
    ...:
    ...: # create an index
    ...: index = faiss.IndexHNSWFlat(d, 32)
    ...: index.metric_type = faiss.METRIC_INNER_PRODUCT
    ...:
    ...: # add the vectors to the index
    ...: index.add(xb)
    ...:
    ...: # search a new vector for the nearest neighbors using maximum inner product search
    ...: xq = np.random.rand(1, d).astype(np.float32)
    ...: k = 5  # number of nearest neighbors to search for
    ...: D, I = index.search(xq, k)
    ...:
    ...: print('Input vector:\n', xq)
    ...: print('Nearest neighbors:\n', xb[I])
    ...: print('Inner product values:\n', D)

If you use reconstruct to grab a query vector, like nineteen = index.reconstruct(19) and then search with it, you'll see 0 returned (L2 like behavior):

In [69]: nineteen = index.reconstruct(19)
In [70]: index.search(np.array([nineteen]), 2)
Out[70]: (array([[-0.       , -0.4269578]], dtype=float32), array([[19, 15]]))

Whereas if you set metric_type in the constructor like so:

In [64]: import numpy as np
    ...: import faiss
    ...:
    ...: # create a random dataset of 100 vectors of dimension 10
    ...: d = 10
    ...: n = 100
    ...: xb = np.random.rand(n, d).astype(np.float32)
    ...:
    ...: # create an index
    ...: index = faiss.IndexHNSWFlat(d, 32, faiss.METRIC_INNER_PRODUCT)
    ...:
    ...:
    ...: # add the vectors to the index
    ...: index.add(xb)
    ...:
    ...: # search a new vector for the nearest neighbors using maximum inner product search
    ...: xq = np.random.rand(1, d).astype(np.float32)
    ...: k = 5  # number of nearest neighbors to search for
    ...: D, I = index.search(xq, k)
    ...:
    ...: print('Input vector:\n', xq)
    ...: print('Nearest neighbors:\n', xb[I])
    ...: print('Inner product values:\n', D)

You'll see what you expect, a non-zero self-IP:

In [66]: index.search(np.array([nineteen]), 2)
Out[66]: (array([[2.947413 , 2.8963742]], dtype=float32), array([[43, 94]]))

mooreniemi avatar May 02 '23 16:05 mooreniemi