Skip to content

Support for Binary Quantization #395

Closed

Description

@nileshtrivedi

Would love to see this capability in pgvector: https://qdrant.tech/articles/binary-quantization/

Essentially, BQ converts any vector embedding of floating point numbers into a vector of binary or boolean values.

In exchange for reducing our 32 bit embeddings to 1 bit embeddings we can see up to a 40x retrieval speed up gain!

One of the reasons vector search still works with such a high compression rate is that these large vectors are over-parameterized for retrieval. This is because they are designed for ranking, clustering, and similar use cases, which typically need more information encoded in the vector.

Activity

  1. jkatz commented on Jan 4, 2024

    @jkatz
    Contributor

    This is just another form of product quantization -- it could be interesting, but requires testing. The biggest tradeoff (as mentioned in the article) is the impact to recall, which requires searching over a larger space to get a better representative set of the approximated results. There's also testing to see how this would work given how PostgreSQL stores data on disk, as the benefit would be towards minimizing the number of pages loaded into memory during an index scan, which is not necessarily correlated to vector size.

  2. ankane commented on Jan 8, 2024

    @ankane
    Member

    Hey @nileshtrivedi, thanks for sharing! I'm not sure it's common enough to include at this point, but seems like it'd be simpler to implement and use than scalar or product quantization, so may try it out in a branch.

  3. aamir-s18 commented on Jan 27, 2024

    @aamir-s18

    @ankane I think this will be become really relevant in the future. Cohere is providing binary embeddings out of the box. We are working on a novel form of binary quantization. I think this could work well with #429.

  4. ankane commented on Mar 27, 2024

    @ankane
    Member

    Support is now available in the bitvector branch for anyone who wants to try it (in a non-production environment).

    CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
    INSERT INTO items (embedding) VALUES ('[1,-2,3]'), ('[-4,5,-6]');
    CREATE INDEX ON items USING hnsw ((quantize_binary(embedding)::bit(3)) bit_hamming_ops);
    
    -- no re-ranking
    SELECT id FROM items ORDER BY quantize_binary(embedding)::bit(3) <~> quantize_binary('[1,-2,3]') LIMIT 5;
    
    -- re-ranking
    SELECT id FROM (
        SELECT * FROM items ORDER BY quantize_binary(embedding)::bit(3) <~> quantize_binary('[1,-2,3]') LIMIT 20
    ) ORDER BY embedding <=> '[1,-2,3]' LIMIT 5;
  5. ankane commented on Mar 27, 2024

    @ankane
    Member

    You can also store binary embeddings directly.

    CREATE TABLE items (id bigserial PRIMARY KEY, embedding bit(3));
    INSERT INTO items (embedding) VALUES (B'000'), (B'111');
    CREATE INDEX ON items USING hnsw (embedding bit_hamming_ops);
    SELECT * FROM items ORDER BY embedding <~> B'101' LIMIT 5;
  6. ankane commented on Apr 2, 2024

    @ankane
    Member

    Support for binary quantization been merged (94a444f) and will be included in 0.7.0 (#508). Thanks for the suggestion @nileshtrivedi!

  7. nileshtrivedi commented on Apr 4, 2024

    @nileshtrivedi
    Author

    @ankane Thanks! This looks great. 👏

    With the vector type (instead of say vector(3)), I am able to store vectors of different dimensions in the same column.
    image

    To get the same flexibility, can/should I store binary embeddings in a column of type bytea?

  8. ankane commented on Apr 4, 2024

    @ankane
    Member

    You can use bit varying / varbit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions