pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter
11+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter
@ 2025-06-17 15:40 "binarycleric (@binarycleric)" <noreply+binarycleric@github.com>
  0 siblings, 0 replies; 11+ messages in thread

From: binarycleric (@binarycleric) @ 2025-06-17 15:40 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

The functions `VectorSumCenter` and `HalfvecSumCenter` were not being vectorized by the compiler. A few slight changes will allow these optimizations to take place and get a performance boost by utilizing SIMD instructions.

This optimization helps improve performance of vector operations in IVF index building and updating.

Verified that the loops are being vectorized by checking `PG_CFLAGS += -Rpass=loop-vectorize -Rpass-analysis=loop-vectorize`.

### Before

```
src/ivfutils.c:262:2: remark: vectorized loop (vectorization width: 4, interleaved count: 4) [-Rpass=loop-vectorize]
  262 |         for (int k = 0; k < dimensions; k++)
      |         ^
src/ivfutils.c:299:2: remark: loop not vectorized: could not determine number of loop iterations [-Rpass-analysis=loop-vectorize]
  299 |         for (int k = 0; k < vec->dim; k++)
      |         ^
src/ivfutils.c:274:2: remark: vectorized loop (vectorization width: 8, interleaved count: 4) [-Rpass=loop-vectorize]
  274 |         for (int k = 0; k < dimensions; k++)
      |         ^
src/ivfutils.c:308:2: remark: loop not vectorized: could not determine number of loop iterations [-Rpass-analysis=loop-vectorize]
  308 |         for (int k = 0; k < vec->dim; k++)
      |         ^
src/ivfutils.c:287:2: remark: vectorized loop (vectorization width: 16, interleaved count: 4) [-Rpass=loop-vectorize]
  287 |         for (uint32 k = 0; k < VARBITBYTES(vec); k++)
      |         ^
src/ivfutils.c:291:3: remark: loop not vectorized: cannot identify array bounds [-Rpass-analysis=loop-vectorize]
  291 |                 nx[k / 8] |= (x[k] > 0.5 ? 1 : 0) << (7 - (k % 8));
      |                 ^
src/ivfutils.c:318:22: remark: loop not vectorized: cannot identify array bounds [-Rpass-analysis=loop-vectorize]
  318 |                 x[k] += (float) (((VARBITS(vec)[k / 8]) >> (7 - (k % 8))) & 0x01);
      |                                    ^
src/ivfutils.c:317:2: remark: loop not vectorized: could not determine number of loop iterations [-Rpass-analysis=loop-vectorize]
  317 |         for (int k = 0; k < VARBITLEN(vec); k++)
      |   
```

### After

```
src/ivfutils.c:262:2: remark: vectorized loop (vectorization width: 4, interleaved count: 4) [-Rpass=loop-vectorize]
  262 |         for (int k = 0; k < dimensions; k++)
      |         ^
src/ivfutils.c:301:2: remark: vectorized loop (vectorization width: 4, interleaved count: 4) [-Rpass=loop-vectorize]
  301 |         for (int k = 0; k < dim; k++)
      |         ^
src/ivfutils.c:274:2: remark: vectorized loop (vectorization width: 8, interleaved count: 4) [-Rpass=loop-vectorize]
  274 |         for (int k = 0; k < dimensions; k++)
      |         ^
src/ivfutils.c:312:2: remark: vectorized loop (vectorization width: 8, interleaved count: 4) [-Rpass=loop-vectorize]
  312 |         for (int k = 0; k < dim; k++)
      |         ^
In file included from src/ivfutils.c:7:
src/halfutils.h:68:9: remark: floating point conversion changes vector width. Mixed floating point precision requires an up/down cast that will negatively impact performance. [-Rpass-analysis=loop-vectorize]
   68 |         return (float) num;
      |                ^
src/ivfutils.c:287:2: remark: vectorized loop (vectorization width: 16, interleaved count: 4) [-Rpass=loop-vectorize]
  287 |         for (uint32 k = 0; k < VARBITBYTES(vec); k++)
      |         ^
src/ivfutils.c:291:3: remark: loop not vectorized: cannot identify array bounds [-Rpass-analysis=loop-vectorize]
  291 |                 nx[k / 8] |= (x[k] > 0.5 ? 1 : 0) << (7 - (k % 8));
      |                 ^
src/ivfutils.c:322:22: remark: loop not vectorized: cannot identify array bounds [-Rpass-analysis=loop-vectorize]
  322 |                 x[k] += (float) (((VARBITS(vec)[k / 8]) >> (7 - (k % 8))) & 0x01);
      |                                    ^
src/ivfutils.c:321:2: remark: loop not vectorized: could not determine number of loop iterations [-Rpass-analysis=loop-vectorize]
  321 |         for (int k = 0; k < VARBITLEN(vec); k++)
      |         ^
```

inline patch 1: src/ivfutils.c (+6/-2 lines) download inline-1.patchdiff --git a/src/ivfutils.c b/src/ivfutils.c index da241ee0a..044fbabf6 100644 --- a/src/ivfutils.c +++ b/src/ivfutils.c @@ -295,8 +295,10 @@ static void VectorSumCenter(Pointer v, float *x) { Vector *vec = (Vector *) v; + int dim = vec->dim; - for (int k = 0; k < vec->dim; k++) + /* Auto-vectorized */ + for (int k = 0; k < dim; k++) x[k] += vec->x[k]; } @@ -304,8 +306,10 @@ static void HalfvecSumCenter(Pointer v, float *x) { HalfVector *vec = (HalfVector *) v; + int dim = vec->dim; - for (int k = 0; k < vec->dim; k++) + /* Auto-vectorized on aarch64 */ + for (int k = 0; k < dim; k++) x[k] += HalfToFloat4(vec->x[k]); }
^ permalink raw [nested|flat] 11+ messages in thread

* Re: [pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter
@ 2025-06-17 16:02 ` "ankane (@ankane)" <noreply+ankane@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: ankane (@ankane) @ 2025-06-17 16:02 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @binarycleric, thanks for the PR. Can you share data on how this impacts the k-means stage of the index build time?

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

* Re: [pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter
@ 2025-06-17 16:17 ` "binarycleric (@binarycleric)" <noreply+binarycleric@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: binarycleric (@binarycleric) @ 2025-06-17 16:17 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane Sure thing. I was just about to publish a test when I saw your comment. This this commit we're seeing a significant drop in `k-means` computation time.

## Given the following SQL script:

```sql
CREATE EXTENSION IF NOT EXISTS vector;

CREATE OR REPLACE FUNCTION generate_random_floats(size integer)
RETURNS float[] AS $$
BEGIN
    RETURN (
        SELECT array_agg(random() * 2 - 1)
        FROM generate_series(1, size)
    );
END;
$$ LANGUAGE plpgsql;

CREATE TABLE t (val vector(1536));

INSERT INTO t (val)
SELECT generate_random_floats(1536)
FROM generate_series(1, 10000);

CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 1);

CREATE TABLE t2 (val halfvec(1536));

INSERT INTO t2 (val)
SELECT generate_random_floats(1536)
FROM generate_series(1, 10000);

CREATE INDEX ON t2 USING ivfflat (val halfvec_l2_ops) WITH (lists = 1);

DROP FUNCTION generate_random_floats(integer);
DROP TABLE t;
DROP TABLE t2;
```

## Without autovectorization in Sums.

```
psql:ivfflat_bench.sql:1: NOTICE:  extension "vector" already exists, skipping
CREATE EXTENSION
CREATE FUNCTION
CREATE TABLE
INSERT 0 10000
psql:ivfflat_bench.sql:23: INFO:  k-means: 9.446 ms
psql:ivfflat_bench.sql:23: INFO:  assign tuples: 17.452 ms
psql:ivfflat_bench.sql:23: INFO:  sort tuples: 0.010 ms
psql:ivfflat_bench.sql:23: INFO:  load tuples: 330.834 ms
CREATE INDEX
CREATE TABLE
INSERT 0 10000
psql:ivfflat_bench.sql:32: INFO:  k-means: 9.079 ms
psql:ivfflat_bench.sql:32: INFO:  assign tuples: 11.688 ms
psql:ivfflat_bench.sql:32: INFO:  sort tuples: 0.010 ms
psql:ivfflat_bench.sql:32: INFO:  load tuples: 177.941 ms
CREATE INDEX
DROP FUNCTION
DROP TABLE
DROP TABLE
```

## With autovectorization in Sums.

```
psql:ivfflat_bench.sql:1: NOTICE:  extension "vector" already exists, skipping
CREATE EXTENSION
CREATE FUNCTION
CREATE TABLE
INSERT 0 10000
psql:ivfflat_bench.sql:23: INFO:  k-means: 2.859 ms
psql:ivfflat_bench.sql:23: INFO:  assign tuples: 18.163 ms
psql:ivfflat_bench.sql:23: INFO:  sort tuples: 0.010 ms
psql:ivfflat_bench.sql:23: INFO:  load tuples: 327.269 ms
CREATE INDEX
CREATE TABLE
INSERT 0 10000
psql:ivfflat_bench.sql:32: INFO:  k-means: 2.420 ms
psql:ivfflat_bench.sql:32: INFO:  assign tuples: 12.136 ms
psql:ivfflat_bench.sql:32: INFO:  sort tuples: 0.012 ms
psql:ivfflat_bench.sql:32: INFO:  load tuples: 150.017 ms
CREATE INDEX
DROP FUNCTION
DROP TABLE
DROP TABLE
```

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

* Re: [pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter
@ 2025-06-17 17:49 ` "binarycleric (@binarycleric)" <noreply+binarycleric@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: binarycleric (@binarycleric) @ 2025-06-17 17:49 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I see that my change introduced the following compiler warning.

```
In file included from src/ivfutils.c:7:
src/halfutils.h:68:9: remark: floating point conversion changes vector width. Mixed floating point precision requires an up/down cast that will negatively impact performance. [-Rpass-analysis=loop-vectorize]
   68 |         return (float) num;
      |                ^
```

I'm looking into seeing if there is anything I can do to address this. Looks like any kind of change to use native ARM functions breaks vectorization. Despite the warning this commit does seem to improve performance (at least on ARM, which is what I have available to test).

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

* Re: [pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter
@ 2025-06-18 19:26 ` "ankane (@ankane)" <noreply+ankane@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: ankane (@ankane) @ 2025-06-18 19:26 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

(on src/ivfutils.c)

`const` isn't needed here or below, so let's remove it.

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

* Re: [pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter
@ 2025-06-18 19:28 ` "ankane (@ankane)" <noreply+ankane@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: ankane (@ankane) @ 2025-06-18 19:28 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

(on src/ivfutils.c)

Let's change this to `Auto-vectorized on aarch64` (since it isn't on x86-64)

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

* Re: [pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter
@ 2025-06-18 19:29 ` "ankane (@ankane)" <noreply+ankane@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: ankane (@ankane) @ 2025-06-18 19:29 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Some results from my testing for the k-means stage (with 1,000 lists):

Platform | Dataset | Type | Before (sec) | After (sec)
--- | --- | --- | --- | ---
Linux x86-64 | SIFT | vector | 7.3 | 7.2
Mac arm64 | SIFT | vector | 3.52 | 3.48
Mac arm64 | SIFT | halfvec | 4.33 | 4.25
Linux x86-64 | GIST | vector | 36.0 | 34.8
Mac arm64 | GIST | vector | 14.6 | 14.1
Mac arm64 | GIST | halfvec | 15.2 | 14.5

It's not a big difference (especially when you factor in the rest of the index build time), but seems fine to include. Added a few comments inline.

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

* Re: [pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter
@ 2025-06-18 21:21 ` "binarycleric (@binarycleric)" <noreply+binarycleric@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: binarycleric (@binarycleric) @ 2025-06-18 21:21 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Updated to remove `const`.

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

* Re: [pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter
@ 2025-06-18 23:09 ` "ankane (@ankane)" <noreply+ankane@github.com>
  9 siblings, 0 replies; 11+ messages in thread

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

@ankane merged this (commit fe697e878808).

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

* Re: [pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter
@ 2025-06-18 23:09 ` "ankane (@ankane)" <noreply+ankane@github.com>
  9 siblings, 0 replies; 11+ messages in thread

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

@ankane closed this.

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

* Re: [pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter
@ 2025-06-18 23:10 ` "ankane (@ankane)" <noreply+ankane@github.com>
  9 siblings, 0 replies; 11+ messages in thread

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

Great, thanks!

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


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

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-06-17 15:40 [pgvector/pgvector] PR #860: vectorize: optimize VectorSumCenter and HalfvecSumCenter "binarycleric (@binarycleric)" <noreply+binarycleric@github.com>
2025-06-17 16:02 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-06-17 16:17 ` "binarycleric (@binarycleric)" <noreply+binarycleric@github.com>
2025-06-17 17:49 ` "binarycleric (@binarycleric)" <noreply+binarycleric@github.com>
2025-06-18 19:26 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-06-18 19:28 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-06-18 19:29 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-06-18 21:21 ` "binarycleric (@binarycleric)" <noreply+binarycleric@github.com>
2025-06-18 23:09 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-06-18 23:09 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-06-18 23:10 ` "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