pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
10+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
@ 2025-03-25 16:32 "astanabe (@astanabe)" <noreply+astanabe@github.com>
0 siblings, 0 replies; 10+ messages in thread
From: astanabe (@astanabe) @ 2025-03-25 16:32 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
In my molecular sequence (i.e. DNA nucleotide or amino-acid sequences) analysis experience, (negative) inner product distance is often the best distance metric.
Recently, I faced to handle very large data contains over 5 billion vectors of 4096 dimensions, and, binary quantization is required.
For this analysis, I implemented inner product distance support for bit vectors.
inline patch 1: META.json, Makefile, Makefile.win and 15 more (+291/-11 lines) download inline-1.patch
diff --git a/META.json b/META.json
index b9a68f62a..343518d1c 100644
--- a/META.json
+++ b/META.json
@@ -2,7 +2,7 @@
"name": "vector",
"abstract": "Open-source vector similarity search for Postgres",
"description": "Supports L2 distance, inner product, and cosine distance",
- "version": "0.8.0",
+ "version": "0.8.1",
"maintainer": [
"Andrew Kane <andrew@ankane.org>"
],
@@ -20,7 +20,7 @@
"vector": {
"file": "sql/vector.sql",
"docfile": "README.md",
- "version": "0.8.0",
+ "version": "0.8.1",
"abstract": "Open-source vector similarity search for Postgres"
}
},
diff --git a/Makefile b/Makefile
index 7a4b88caf..6431ba8ac 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
EXTENSION = vector
-EXTVERSION = 0.8.0
+EXTVERSION = 0.8.1
MODULE_big = vector
DATA = $(wildcard sql/*--*--*.sql)
diff --git a/Makefile.win b/Makefile.win
index 8c62f9d5f..1fee4a683 100644
--- a/Makefile.win
+++ b/Makefile.win
@@ -1,5 +1,5 @@
EXTENSION = vector
-EXTVERSION = 0.8.0
+EXTVERSION = 0.8.1
DATA_built = sql\$(EXTENSION)--$(EXTVERSION).sql
OBJS = src\bitutils.obj src\bitvec.obj src\halfutils.obj src\halfvec.obj src\hnsw.obj src\hnswbuild.obj src\hnswinsert.obj src\hnswscan.obj src\hnswutils.obj src\hnswvacuum.obj src\ivfbuild.obj src\ivfflat.obj src\ivfinsert.obj src\ivfkmeans.obj src\ivfscan.obj src\ivfutils.obj src\ivfvacuum.obj src\sparsevec.obj src\vector.obj
diff --git a/sql/vector--0.8.0--0.8.1.sql b/sql/vector--0.8.0--0.8.1.sql
new file mode 100644
index 000000000..b75ba126f
--- /dev/null
+++ b/sql/vector--0.8.0--0.8.1.sql
@@ -0,0 +1,26 @@
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "ALTER EXTENSION vector UPDATE TO '0.8.1'" to load this file. \quit
+
+CREATE FUNCTION inner_product(bit, bit) RETURNS float8
+ AS 'MODULE_PATHNAME', 'bit_inner_product' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE FUNCTION bit_negative_inner_product(bit, bit) RETURNS float8
+ AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE OPERATOR <#> (
+ LEFTARG = bit, RIGHTARG = bit, PROCEDURE = bit_negative_inner_product,
+ COMMUTATOR = '<#>'
+);
+
+CREATE OPERATOR CLASS bit_ip_ops
+ FOR TYPE bit USING ivfflat AS
+ OPERATOR 1 <#> (bit, bit) FOR ORDER BY float_ops,
+ FUNCTION 1 bit_negative_inner_product(bit, bit),
+ FUNCTION 3 bit_negative_inner_product(bit, bit),
+ FUNCTION 5 ivfflat_bit_support(internal);
+
+CREATE OPERATOR CLASS bit_ip_ops
+ FOR TYPE bit USING hnsw AS
+ OPERATOR 1 <#> (bit, bit) FOR ORDER BY float_ops,
+ FUNCTION 1 bit_negative_inner_product(bit, bit),
+ FUNCTION 3 hnsw_bit_support(internal);
diff --git a/sql/vector.sql b/sql/vector.sql
index 7fc36712b..6199430f0 100644
--- a/sql/vector.sql
+++ b/sql/vector.sql
@@ -652,9 +652,17 @@ CREATE OPERATOR CLASS halfvec_l1_ops
CREATE FUNCTION hamming_distance(bit, bit) RETURNS float8
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+CREATE FUNCTION inner_product(bit, bit) RETURNS float8
+ AS 'MODULE_PATHNAME', 'bit_inner_product' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
CREATE FUNCTION jaccard_distance(bit, bit) RETURNS float8
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+-- bit private functions
+
+CREATE FUNCTION bit_negative_inner_product(bit, bit) RETURNS float8
+ AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
-- bit operators
CREATE OPERATOR <~> (
@@ -662,6 +670,11 @@ CREATE OPERATOR <~> (
COMMUTATOR = '<~>'
);
+CREATE OPERATOR <#> (
+ LEFTARG = bit, RIGHTARG = bit, PROCEDURE = bit_negative_inner_product,
+ COMMUTATOR = '<#>'
+);
+
CREATE OPERATOR <%> (
LEFTARG = bit, RIGHTARG = bit, PROCEDURE = jaccard_distance,
COMMUTATOR = '<%>'
@@ -676,12 +689,25 @@ CREATE OPERATOR CLASS bit_hamming_ops
FUNCTION 3 hamming_distance(bit, bit),
FUNCTION 5 ivfflat_bit_support(internal);
+CREATE OPERATOR CLASS bit_ip_ops
+ FOR TYPE bit USING ivfflat AS
+ OPERATOR 1 <#> (bit, bit) FOR ORDER BY float_ops,
+ FUNCTION 1 bit_negative_inner_product(bit, bit),
+ FUNCTION 3 bit_negative_inner_product(bit, bit),
+ FUNCTION 5 ivfflat_bit_support(internal);
+
CREATE OPERATOR CLASS bit_hamming_ops
FOR TYPE bit USING hnsw AS
OPERATOR 1 <~> (bit, bit) FOR ORDER BY float_ops,
FUNCTION 1 hamming_distance(bit, bit),
FUNCTION 3 hnsw_bit_support(internal);
+CREATE OPERATOR CLASS bit_ip_ops
+ FOR TYPE bit USING hnsw AS
+ OPERATOR 1 <#> (bit, bit) FOR ORDER BY float_ops,
+ FUNCTION 1 bit_negative_inner_product(bit, bit),
+ FUNCTION 3 hnsw_bit_support(internal);
+
CREATE OPERATOR CLASS bit_jaccard_ops
FOR TYPE bit USING hnsw AS
OPERATOR 1 <%> (bit, bit) FOR ORDER BY float_ops,
diff --git a/src/bitutils.c b/src/bitutils.c
index 578bcad1d..6a1f03a4c 100644
--- a/src/bitutils.c
+++ b/src/bitutils.c
@@ -42,6 +42,7 @@
#endif
uint64 (*BitHammingDistance) (uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 distance);
+uint64 (*BitInnerProduct) (uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 innerproduct);
double (*BitJaccardDistance) (uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 ab, uint64 aa, uint64 bb);
BIT_TARGET_CLONES static uint64
@@ -93,6 +94,55 @@ BitHammingDistanceAvx512Popcount(uint32 bytes, unsigned char *ax, unsigned char
}
#endif
+BIT_TARGET_CLONES static uint64
+BitInnerProductDefault(uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 innerproduct)
+{
+#ifdef popcount64
+ for (; bytes >= sizeof(uint64); bytes -= sizeof(uint64))
+ {
+ uint64 axs;
+ uint64 bxs;
+
+ /* Ensure aligned */
+ memcpy(&axs, ax, sizeof(uint64));
+ memcpy(&bxs, bx, sizeof(uint64));
+
+ innerproduct += popcount64(axs & bxs);
+
+ ax += sizeof(uint64);
+ bx += sizeof(uint64);
+ }
+#endif
+
+ for (uint32 i = 0; i < bytes; i++)
+ innerproduct += pg_number_of_ones[ax[i] & bx[i]];
+
+ return innerproduct;
+}
+
+#ifdef BIT_DISPATCH
+TARGET_AVX512_POPCOUNT static uint64
+BitInnerProductAvx512Popcount(uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 innerproduct)
+{
+ __m512i shared = _mm512_setzero_si512();
+
+ for (; bytes >= sizeof(__m512i); bytes -= sizeof(__m512i))
+ {
+ __m512i axs = _mm512_loadu_si512((const __m512i *) ax);
+ __m512i bxs = _mm512_loadu_si512((const __m512i *) bx);
+
+ shared = _mm512_add_epi64(shared, _mm512_popcnt_epi64(_mm512_and_si512(axs, bxs)));
+
+ ax += sizeof(__m512i);
+ bx += sizeof(__m512i);
+ }
+
+ innerproduct += _mm512_reduce_add_epi64(shared);
+
+ return BitInnerProductDefault(bytes, ax, bx, innerproduct);
+}
+#endif
+
BIT_TARGET_CLONES static double
BitJaccardDistanceDefault(uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 ab, uint64 aa, uint64 bb)
{
@@ -210,12 +260,14 @@ BitvecInit(void)
* performance
*/
BitHammingDistance = BitHammingDistanceDefault;
+ BitInnerProduct = BitInnerProductDefault;
BitJaccardDistance = BitJaccardDistanceDefault;
#ifdef BIT_DISPATCH
if (SupportsAvx512Popcount())
{
BitHammingDistance = BitHammingDistanceAvx512Popcount;
+ BitInnerProduct = BitInnerProductAvx512Popcount;
BitJaccardDistance = BitJaccardDistanceAvx512Popcount;
}
#endif
diff --git a/src/bitutils.h b/src/bitutils.h
index b4d85bb1c..f647effbd 100644
--- a/src/bitutils.h
+++ b/src/bitutils.h
@@ -9,6 +9,7 @@
#endif
extern uint64 (*BitHammingDistance) (uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 distance);
+extern uint64 (*BitInnerProduct) (uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 innerproduct);
extern double (*BitJaccardDistance) (uint32 bytes, unsigned char *ax, unsigned char *bx, uint64 ab, uint64 aa, uint64 bb);
void BitvecInit(void);
diff --git a/src/bitvec.c b/src/bitvec.c
index 094ddd282..140b01a93 100644
--- a/src/bitvec.c
+++ b/src/bitvec.c
@@ -53,6 +53,36 @@ hamming_distance(PG_FUNCTION_ARGS)
PG_RETURN_FLOAT8((double) BitHammingDistance(VARBITBYTES(a), VARBITS(a), VARBITS(b), 0));
}
+/*
+ * Get the inner product between two bit vectors
+ */
+FUNCTION_PREFIX PG_FUNCTION_INFO_V1(bit_inner_product);
+Datum
+bit_inner_product(PG_FUNCTION_ARGS)
+{
+ VarBit *a = PG_GETARG_VARBIT_P(0);
+ VarBit *b = PG_GETARG_VARBIT_P(1);
+
+ CheckDims(a, b);
+
+ PG_RETURN_FLOAT8((double) BitInnerProduct(VARBITBYTES(a), VARBITS(a), VARBITS(b), 0));
+}
+
+/*
+ * Get the negative inner product between two bit vectors
+ */
+FUNCTION_PREFIX PG_FUNCTION_INFO_V1(bit_negative_inner_product);
+Datum
+bit_negative_inner_product(PG_FUNCTION_ARGS)
+{
+ VarBit *a = PG_GETARG_VARBIT_P(0);
+ VarBit *b = PG_GETARG_VARBIT_P(1);
+
+ CheckDims(a, b);
+
+ PG_RETURN_FLOAT8((double) -((double) BitInnerProduct(VARBITBYTES(a), VARBITS(a), VARBITS(b), 0)));
+}
+
/*
* Get the Jaccard distance between two bit vectors
*/
diff --git a/test/expected/bit.out b/test/expected/bit.out
index a9ba47255..6da1e371d 100644
--- a/test/expected/bit.out
+++ b/test/expected/bit.out
@@ -62,6 +62,70 @@ SELECT hamming_distance('111', '000'::varbit(4));
SELECT hamming_distance('111', '0000'::varbit(4));
ERROR: different bit lengths 3 and 4
+SELECT inner_product(B'111', B'111');
+ inner_product
+---------------
+ 3
+(1 row)
+
+SELECT inner_product(B'111', B'110');
+ inner_product
+---------------
+ 2
+(1 row)
+
+SELECT inner_product(B'111', B'100');
+ inner_product
+---------------
+ 1
+(1 row)
+
+SELECT inner_product(B'111', B'000');
+ inner_product
+---------------
+ 0
+(1 row)
+
+SELECT inner_product(B'10101010101010101010', B'01010101010101010101');
+ inner_product
+---------------
+ 0
+(1 row)
+
+SELECT inner_product(B'101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101', B'101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101');
+ inner_product
+---------------
+ 257
+(1 row)
+
+SELECT inner_product(B'101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101', B'010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010');
+ inner_product
+---------------
+ 0
+(1 row)
+
+SELECT inner_product(B'110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011', B'100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001');
+ inner_product
+---------------
+ 2
+(1 row)
+
+SELECT inner_product(B'', B'');
+ inner_product
+---------------
+ 0
+(1 row)
+
+SELECT inner_product(B'111', B'00');
+ERROR: different bit lengths 3 and 2
+SELECT inner_product(B'111', '000'::varbit(4));
+ inner_product
+---------------
+ 0
+(1 row)
+
+SELECT inner_product(B'111', '0000'::varbit(4));
+ERROR: different bit lengths 3 and 4
SELECT jaccard_distance('1111', '1111');
jaccard_distance
------------------
diff --git a/test/expected/hnsw_bit.out b/test/expected/hnsw_bit.out
index 047954a79..1844c988a 100644
--- a/test/expected/hnsw_bit.out
+++ b/test/expected/hnsw_bit.out
@@ -19,6 +19,27 @@ SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <~> (SELECT NULL::bit)) t2;
4
(1 row)
+DROP TABLE t;
+-- inner product
+CREATE TABLE t (val bit(3));
+INSERT INTO t (val) VALUES (B'000'), (B'100'), (B'111'), (NULL);
+CREATE INDEX ON t USING hnsw (val bit_ip_ops);
+INSERT INTO t (val) VALUES (B'110');
+SELECT * FROM t ORDER BY val <#> B'111';
+ val
+-----
+ 111
+ 110
+ 100
+ 000
+(4 rows)
+
+SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <#> (SELECT NULL::bit)) t2;
+ count
+-------
+ 4
+(1 row)
+
DROP TABLE t;
-- jaccard
CREATE TABLE t (val bit(4));
diff --git a/test/expected/ivfflat_bit.out b/test/expected/ivfflat_bit.out
index ab8adfd83..7733d0509 100644
--- a/test/expected/ivfflat_bit.out
+++ b/test/expected/ivfflat_bit.out
@@ -19,6 +19,27 @@ SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <~> (SELECT NULL::bit)) t2;
4
(1 row)
+DROP TABLE t;
+-- inner product
+CREATE TABLE t (val bit(3));
+INSERT INTO t (val) VALUES (B'000'), (B'100'), (B'111'), (NULL);
+CREATE INDEX ON t USING ivfflat (val bit_ip_ops) WITH (lists = 1);
+INSERT INTO t (val) VALUES (B'110');
+SELECT * FROM t ORDER BY val <#> B'111';
+ val
+-----
+ 111
+ 110
+ 100
+ 000
+(4 rows)
+
+SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <#> (SELECT NULL::bit)) t2;
+ count
+-------
+ 4
+(1 row)
+
DROP TABLE t;
-- varbit
CREATE TABLE t (val varbit(3));
diff --git a/test/sql/bit.sql b/test/sql/bit.sql
index eee9ce60c..c1905c341 100644
--- a/test/sql/bit.sql
+++ b/test/sql/bit.sql
@@ -11,6 +11,19 @@ SELECT hamming_distance('111', '00');
SELECT hamming_distance('111', '000'::varbit(4));
SELECT hamming_distance('111', '0000'::varbit(4));
+SELECT inner_product(B'111', B'111');
+SELECT inner_product(B'111', B'110');
+SELECT inner_product(B'111', B'100');
+SELECT inner_product(B'111', B'000');
+SELECT inner_product(B'10101010101010101010', B'01010101010101010101');
+SELECT inner_product(B'101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101', B'101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101');
+SELECT inner_product(B'101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101', B'010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010');
+SELECT inner_product(B'110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011', B'100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001');
+SELECT inner_product(B'', B'');
+SELECT inner_product(B'111', B'00');
+SELECT inner_product(B'111', '000'::varbit(4));
+SELECT inner_product(B'111', '0000'::varbit(4));
+
SELECT jaccard_distance('1111', '1111');
SELECT jaccard_distance('1111', '1110');
SELECT jaccard_distance('1111', '1100');
diff --git a/test/sql/hnsw_bit.sql b/test/sql/hnsw_bit.sql
index 5a7def759..99b8d019e 100644
--- a/test/sql/hnsw_bit.sql
+++ b/test/sql/hnsw_bit.sql
@@ -13,6 +13,19 @@ SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <~> (SELECT NULL::bit)) t2;
DROP TABLE t;
+-- inner product
+
+CREATE TABLE t (val bit(3));
+INSERT INTO t (val) VALUES (B'000'), (B'100'), (B'111'), (NULL);
+CREATE INDEX ON t USING hnsw (val bit_ip_ops);
+
+INSERT INTO t (val) VALUES (B'110');
+
+SELECT * FROM t ORDER BY val <#> B'111';
+SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <#> (SELECT NULL::bit)) t2;
+
+DROP TABLE t;
+
-- jaccard
CREATE TABLE t (val bit(4));
diff --git a/test/sql/ivfflat_bit.sql b/test/sql/ivfflat_bit.sql
index e0f9e2519..63b8557af 100644
--- a/test/sql/ivfflat_bit.sql
+++ b/test/sql/ivfflat_bit.sql
@@ -13,6 +13,19 @@ SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <~> (SELECT NULL::bit)) t2;
DROP TABLE t;
+-- inner product
+
+CREATE TABLE t (val bit(3));
+INSERT INTO t (val) VALUES (B'000'), (B'100'), (B'111'), (NULL);
+CREATE INDEX ON t USING ivfflat (val bit_ip_ops) WITH (lists = 1);
+
+INSERT INTO t (val) VALUES (B'110');
+
+SELECT * FROM t ORDER BY val <#> B'111';
+SELECT COUNT(*) FROM (SELECT * FROM t ORDER BY val <#> (SELECT NULL::bit)) t2;
+
+DROP TABLE t;
+
-- varbit
CREATE TABLE t (val varbit(3));
diff --git a/test/t/020_hnsw_bit_build_recall.pl b/test/t/020_hnsw_bit_build_recall.pl
index 8772ed537..499bc019c 100644
--- a/test/t/020_hnsw_bit_build_recall.pl
+++ b/test/t/020_hnsw_bit_build_recall.pl
@@ -70,8 +70,8 @@ sub test_recall
}
# Check each index type
-my @operators = ("<~>", "<\%>");
-my @opclasses = ("bit_hamming_ops", "bit_jaccard_ops");
+my @operators = ("<~>", "<#>", "<\%>");
+my @opclasses = ("bit_hamming_ops", "bit_ip_ops", "bit_jaccard_ops");
for my $i (0 .. $#operators)
{
diff --git a/test/t/021_hnsw_bit_insert_recall.pl b/test/t/021_hnsw_bit_insert_recall.pl
index d9bd2e113..749d73394 100644
--- a/test/t/021_hnsw_bit_insert_recall.pl
+++ b/test/t/021_hnsw_bit_insert_recall.pl
@@ -67,8 +67,8 @@ sub test_recall
}
# Check each index type
-my @operators = ("<~>", "<\%>");
-my @opclasses = ("bit_hamming_ops", "bit_jaccard_ops");
+my @operators = ("<~>", "<#>", "<\%>");
+my @opclasses = ("bit_hamming_ops", "bit_ip_ops", "bit_jaccard_ops");
for my $i (0 .. $#operators)
{
diff --git a/test/t/035_ivfflat_bit_build_recall.pl b/test/t/035_ivfflat_bit_build_recall.pl
index d58d597f0..f6fb3cc8f 100644
--- a/test/t/035_ivfflat_bit_build_recall.pl
+++ b/test/t/035_ivfflat_bit_build_recall.pl
@@ -70,8 +70,8 @@ sub test_recall
}
# Check each index type
-my @operators = ("<~>");
-my @opclasses = ("bit_hamming_ops");
+my @operators = ("<~>", "<#>");
+my @opclasses = ("bit_hamming_ops", "bit_ip_ops");
for my $i (0 .. $#operators)
{
diff --git a/vector.control b/vector.control
index 7bfc0f1fa..2ad02286a 100644
--- a/vector.control
+++ b/vector.control
@@ -1,4 +1,4 @@
comment = 'vector data type and ivfflat and hnsw access methods'
-default_version = '0.8.0'
+default_version = '0.8.1'
module_pathname = '$libdir/vector'
relocatable = true
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
@ 2025-03-25 17:39 ` "ankane (@ankane)" <noreply+ankane@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: ankane (@ankane) @ 2025-03-25 17:39 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @astanabe, thanks for the PR. However, I'd expect Hamming distance to provide better recall for this (which is already available).
A few questions:
1. How are you generating the vectors?
2. What is the recall when using exact search on the binary quantized vectors with Hamming distance and inner product (compared to the non-quantized vectors)?
3. Are there any papers that discuss this approach?
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
@ 2025-03-25 19:32 ` "astanabe (@astanabe)" <noreply+astanabe@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: astanabe (@astanabe) @ 2025-03-25 19:32 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Consider a case where the following DNA base sequence is stored in a database:
```
DNA1 CGTATACGTATAG
```
We can create a vector by extracting 6-character-long words, shifting one character at a time, and counting their frequency.
This is similar to an n-gram indexing in full-text search.
In molecular biology, this is called a k-mer, but it is exactly the same as n-gram in full-text search.
DNA1 would look like this:
```
CGTATA 2
GTATAC 1
TATACG 1
ATACGT 1
TACGTA 1
ACGTAT 1
GTATAG 1
```
DNA consists of only four types of characters: A, C, G, and T.
Therefore, when the word length is 6 characters, there are 4096 possible words, making the vector 4096-dimensional.
Now, consider using the following as a query for searching:
```
Query CGTATAC
```
This can be broken down as follows:
```
CGTATA 1
GTATAC 1
```
In this scenario, the Hamming distance between DNA1 and the Query is 5, and the negative inner product is -2.
Furthermore, consider that the database also contains the following sequence:
```
DNA2 AGTATAC
```
For DNA2 and the Query, the Hamming distance is 1, and the negative inner product is -1.
In this search, we want to retrieve DNA1, but because DNA2 is closer when using the Hamming distance, we would need to increase the LIMIT.
In real-world molecular biology research, the database often contains a mixture of very long and short nucleotide sequences, and they are searched with short query sequences.
In this situation, when using the Hamming distance, shorter sequences that are slightly less similar can appear as closer neighbors than longer sequences that are more similar.
Jaccard distance improves upon the Hamming distance.
Sorensen distance improves upon Jaccard distance.
Simpson distance improves upon Sorensen distance.
However, the negative inner product surpasses all of these in terms of accuracy, and it is the fastest to compute in this case.
The same thing should happen when searching for short keywords or phrases in a large number of long natural language texts.
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
@ 2025-03-25 20:11 ` "ankane (@ankane)" <noreply+ankane@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: ankane (@ankane) @ 2025-03-25 20:11 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks, will spend some time getting caught up on ANN research for k-mers.
MinHash + Jaccard distance sounds like [one approach](https://academic.oup.com/nar/article/52/16/e74/7714450).
Also, would be good to see how recall compares between different distance functions / approaches on your dataset or (ideally) an open dataset.
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
@ 2025-03-25 21:10 ` "astanabe (@astanabe)" <noreply+astanabe@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: astanabe (@astanabe) @ 2025-03-25 21:10 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
GSearch and its predecessors primarily focus on searching a database that contains numerous genome (i.e. very long) sequences using genome sequences as queries, and on clustering and classifying genome sequences.
Therefore, they do not give much consideration to searching a database that contains a mixture of sequences of various lengths using shorter sequences as queries - this is the issue I am currently dealing with.
I am trying to examine recall using publicly available data, but because the database is extremely huge, it looks like it will take a considerable amount of time.
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
@ 2025-03-25 21:28 ` "ankane (@ankane)" <noreply+ankane@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: ankane (@ankane) @ 2025-03-25 21:28 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks, are there any papers that describe the approach you're taking?
Would also be good to compare with inner product and `sparsevec` (this would provide better recall than the binary approach, but likely has much worse performance).
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
@ 2025-03-25 21:49 ` "astanabe (@astanabe)" <noreply+astanabe@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: astanabe (@astanabe) @ 2025-03-25 21:49 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
This probably a new idea and there is no paper about this.
Because `sparsevec` is limited to 1000 non-zero elements, it is unable to store the data.
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
@ 2025-03-25 23:00 ` "ankane (@ankane)" <noreply+ankane@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: ankane (@ankane) @ 2025-03-25 23:00 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
> This probably a new idea and there is no paper about this.
It's probably not a good fit for pgvector if that's the case, but feel free to share when your work is public.
Unofficially, you can probably add it to an existing installation like in #234.
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
@ 2025-03-25 23:00 ` "ankane (@ankane)" <noreply+ankane@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: ankane (@ankane) @ 2025-03-25 23:00 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 10+ messages in thread
* Re: [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
@ 2025-03-28 21:42 ` "ankane (@ankane)" <noreply+ankane@github.com>
8 siblings, 0 replies; 10+ messages in thread
From: ankane (@ankane) @ 2025-03-28 21:42 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Just to follow up, here's how you could add it unofficially to a database:
```sql
CREATE FUNCTION bit_negative_inner_product(bit, bit) RETURNS float8
AS 'BEGIN RETURN -bit_count($1 & $2); END;'
LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
CREATE OPERATOR <#> (
LEFTARG = bit, RIGHTARG = bit, PROCEDURE = bit_negative_inner_product,
COMMUTATOR = '<#>'
);
CREATE OPERATOR CLASS bit_ip_ops
FOR TYPE bit USING hnsw AS
OPERATOR 1 <#> (bit, bit) FOR ORDER BY float_ops,
FUNCTION 1 bit_negative_inner_product(bit, bit),
FUNCTION 3 hnsw_bit_support(internal);
```
^ permalink raw [nested|flat] 10+ messages in thread
end of thread, other threads:[~2025-03-28 21:42 UTC | newest]
Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-03-25 16:32 [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors "astanabe (@astanabe)" <noreply+astanabe@github.com>
2025-03-25 17:39 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-03-25 19:32 ` "astanabe (@astanabe)" <noreply+astanabe@github.com>
2025-03-25 20:11 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-03-25 21:10 ` "astanabe (@astanabe)" <noreply+astanabe@github.com>
2025-03-25 21:28 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-03-25 21:49 ` "astanabe (@astanabe)" <noreply+astanabe@github.com>
2025-03-25 23:00 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-03-25 23:00 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-03-28 21:42 ` "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