Will IVFPQR and hnsw support inner product?
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?
HNSW does support METRIC_INNER_PRODUCT. It is not particularly hard for IVFPQR it's just that we don't use IVFPQR too often.
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 I met the same problem, maybe index_factory shuld be updated
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.
Hello, @mdouze , since IVFPQ support inner product, should this line alse be removed? https://github.com/facebookresearch/faiss/blob/e8c8db85aeb72d80f4c83e8003ca778fab575092/IndexIVF.cpp#L93
@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 https://github.com/facebookresearch/faiss/issues/1261
@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.
@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 theindex.metric_type, it returns 1, which means L2 metric. but this work for IVF or IVF PQ.
@Kun-Ming Use this:

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 Since HNSW supports the inner product, I think it is unnecessary to do the transform.
@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 theindex.metric_type, it returns 1, which means L2 metric. but this work for IVF or IVF PQ.@Kun-Ming Use this:
Works in latest version.
It does not convert the IP to a L2, it computes inner products itself.
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]]))