Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inquiry related to DistanceComputer #3415

Open
anupsingh15 opened this issue May 7, 2024 · 2 comments
Open

inquiry related to DistanceComputer #3415

anupsingh15 opened this issue May 7, 2024 · 2 comments
Labels

Comments

@anupsingh15
Copy link

anupsingh15 commented May 7, 2024

Is there a way to compute the asymmetric distance of a query with respect to encoded (using PQ) database items? I figured out that I needed to use the get_FlatCodesDistanceComputer method of the index. Hereunder is my attempt:

x = np.random.randn(10000, 128)
x_norm = x/np.linalg.norm(x, axis=-1).reshape(-1,1)
index_pq = faiss.IndexPQ(128,8,7)
index_pq.train(x_norm)
index_pq.add(x_norm)
codes = index_pq.sa_encode(x_norm)

dist_comp = index_pq.get_FlatCodesDistanceComputer()
dist_comp.set_query = x_norm[0]
dist_comp.distance_to_code(codes[:5]) # to find distance wrt first 5 encoded database items

However, I get the following error:

TypeError: in method 'FlatCodesDistanceComputer_distance_to_code', argument 2 of type 'uint8_t const *'

Could you please let me know what I am missing?

Thanks.

@mdouze
Copy link
Contributor

mdouze commented May 10, 2024

This is because the distance_to_code function does not have a simplified wrapping layer.
It can still be called using swig_ptr:

dist_comp = index_pq.get_FlatCodesDistanceComputer()
dist_comp.set_query(faiss.swig_ptr(x_norm[0]))
dist_comp.distance_to_code(faiss.swig_ptr(codes[:5])) 

This computes one distance at a time (because it's a low-level function).

@anupsingh15
Copy link
Author

anupsingh15 commented May 11, 2024

Thanks for your reply.

I get the following error when executing: dist_comp.set_query(faiss.swig_ptr(x_norm[0]))

TypeError: in method 'DistanceComputer_set_query', argument 2 of type 'float const *'

I also tried the following, which does not give an error, but unsure why does it output only 0. I expected it to output 5 distances since I compare a query with 5 different encodings :

dist_comp = index_pq.get_FlatCodesDistanceComputer()
dist_comp.set_query = faiss.swig_ptr(x_norm[0])
dist_comp.distance_to_code(faiss.swig_ptr(codes[:5])) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants