pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #605: Product Quantization
6+ messages / 3 participants
[nested] [flat]

* [pgvector/pgvector] issue #605: Product Quantization
@ 2024-06-23 16:36  "aminst (@aminst)" <noreply+aminst@github.com>
  0 siblings, 0 replies; 6+ messages in thread

From: aminst (@aminst) @ 2024-06-23 16:36 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi,
Me and my friend have been reading the code for a while and we were looking for some ideas for contributing.
@ankane, you mentioned product quantization in #27. Is this still an issue? We would like to work on it if it looks like a useful feature.
I have some questions about it and would like to discuss design choices in this issue as we start implementing this feature. 
Current questions I have:
* Would it make more sense if pgvector supports a new index (e.g. IVFPQ like Faiss) to achieve product quantization or just adding a new vector type? This is much harder to do without adding a new index since we have to store the centroids for each subvector. It might make more sense to have a new index type for ivf + product quantization.
* Do you think the subvector type might help in the internal implementation? I think it can help to get a part of the vector in the process of product quantization.
* Please let me know of any specific implementations you find more performant for IVFPQ. I have provided a list of resources that I'm reading to fully understand how people have implemented it in the past.

# Some Resources
* [Faiss IVFPQ](https://github.com/facebookresearch/faiss/blob/main/faiss/IndexIVFPQ.h) implementation
* The [PQ paper](https://ieeexplore.ieee.org/document/5432202)
* A [useful blog post](https://sidshome.wordpress.com/2023/12/30/deep-dive-into-faiss-indexivfpq-for-vector-search/) and some IVFPQ optimizations
* [Billion-scale Approximate Nearest Neighbor Search](https://wangzwhu.github.io/home/file/acmmm-t-part3-ann.pdf)

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #605: Product Quantization
@ 2024-06-25 03:03  "jkatz (@jkatz)" <noreply+jkatz@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: jkatz (@jkatz) @ 2024-06-25 03:03 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

We seem to be getting good results from existing [scalar and binary quantization](https://jkatz05.com/post/postgres/pgvector-scalar-binary-quantization/) support. While I'm not opposed to adding support for PQ (it's something we've discussed in the past), it'd be good to see what improvement we can get in space/performance and its impact to recall through a POC.

My personal opinion is I'd be hesitant to add another index type, but instead use build parameters to indicate what kind of PQ to use.

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #605: Product Quantization
@ 2024-06-26 01:27  "aminst (@aminst)" <noreply+aminst@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: aminst (@aminst) @ 2024-06-26 01:27 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

That sounds good.
We will try to implement a POC and compare it with the existing scalar and binary quantization.
Then, we can check if it is significant and worthy of being added. Thanks!

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #605: Product Quantization
@ 2024-07-04 00:04  "aminst (@aminst)" <noreply+aminst@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: aminst (@aminst) @ 2024-07-04 00:04 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I read the IVF code to see how PQ can be implemented without adding a new index. I think it is possible, but I wanted to mention my ideas to ensure backward compatibility.
I'm thinking of the following changes to the current IVF implementation:

## Overall Page Structure
I don't think there is a need to change the structure of the list pages since PQ does not quantize the centroids from what I have understood from the paper and the Faiss implementation. However, the PQ implementation will need new pages to store subvector centroids. If we have M subvectors, and we want to create K centroids for each subspace, this will result in M * K centroids being stored in the new **codebook** pages.

## ivfflatbuild
There is no change for the Meta and List pages, but we should create codebook pages and run the current K-means M times for each subvector space. Please correct me if I'm wrong, but I also don't think we need to change the code to create the entry pages since the new PQ-encoded vector type will handle that. 

## ivfflatinsert
The `FindInsertPage` function will remain untouched since the ivf centroids are not quantized. We only need to encode and store the PQ-encoded version of the vector using the *codebook* pages.

## ivfscan
This can also remain unchanged since we are still doing IVF search, and the distance function should do the PQ distance functionality. There are many ways to do the distance calculation according to the Product Quantization paper itself, but I think some need more pages to precalculate distances between codebook centroids, which can be next step optimizations.

# Questions
* Will adding the new codebook pages cause any problems for the backward compatibility of the code?
* I tried to think a lot about how to implement this so there is no need for a new index type, and the complexity is manageable. Do you think the overall idea achieves this?
* I would appreciate any other comments you might have for the implementation.

Thanks.

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #605: Product Quantization
@ 2025-03-06 23:44  "ankane (@ankane)" <noreply+ankane@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: ankane (@ankane) @ 2025-03-06 23:44 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @aminst, sorry for not following up sooner.

For a proof of concept for this, I wouldn't worry about backwards compatibility (this can happen later if performance is significantly better than existing options). Another interesting approach is [RaBitQ](https://arxiv.org/abs/2405.12497), which reports to have better performance than PQ.

Closing this out since there hasn't been any movement, but still open to considering more options for quantization.

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #605: Product Quantization
@ 2025-03-06 23:44  "ankane (@ankane)" <noreply+ankane@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: ankane (@ankane) @ 2025-03-06 23:44 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2025-03-06 23:44 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-06-23 16:36 [pgvector/pgvector] issue #605: Product Quantization "aminst (@aminst)" <noreply+aminst@github.com>
2024-06-25 03:03 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-06-26 01:27 ` "aminst (@aminst)" <noreply+aminst@github.com>
2024-07-04 00:04 ` "aminst (@aminst)" <noreply+aminst@github.com>
2025-03-06 23:44 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-03-06 23:44 ` "ankane (@ankane)" <noreply+ankane@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox