pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
13+ messages / 3 participants
[nested] [flat]

* [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-02-03 10:30 "mikejuliet13 (@mikejuliet13)" <noreply+mikejuliet13@github.com>
  0 siblings, 0 replies; 13+ messages in thread

From: mikejuliet13 (@mikejuliet13) @ 2025-02-03 10:30 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

### Summary

This PR optimises vector distance functions in PGVector, specifically:
- Cosine Similarity
- Inner Product
- L2 Squared Distance

By leveraging SIMD (Single Instruction, Multiple Data), these functions have been vectorizsd, reducing computation time and improving performance.

### Implementation Details:

- Used Altivec/VSX for IBM Power architecture.
- Applied vector-based optimizations to process multiple elements simultaneously.
- Reduced redundant computations, leading to overall efficiency gains.

### Testing & Benchmarking

Functionality Testing:
Verified correctness on both Power and x86 architectures.
Performance Testing:
Benchmarked on IBM Power using ANN-Benchmark, showing significant improvements in execution time.

### Impact
This optimisation enhances the efficiency of PGVector’s vector search operations, making it faster and more scalable for workloads requiring high-performance similarity searches.

inline patch 1: src/vector.c (+208/-0 lines) download inline-1.patchdiff --git a/src/vector.c b/src/vector.c index a5b2aac36..b8b222916 100644 --- a/src/vector.c +++ b/src/vector.c @@ -35,6 +35,11 @@ #define VECTOR_TARGET_CLONES #endif +#if defined(__powerpc__) +#include <altivec.h> /* Required for the Power GCC built-ins */ +#define FLOAT_VEC_SIZE 4 +#endif + PG_MODULE_MAGIC; /* @@ -542,6 +547,67 @@ halfvec_to_vector(PG_FUNCTION_ARGS) PG_RETURN_POINTER(result); } +#ifdef __powerpc__ + +VECTOR_TARGET_CLONES static float +VectorL2SquaredDistance(int dim, float *ax, float *bx) +{ + /* Vector implmentaion uses vector size of FLOAT_VEC_SIZE. If the input + array size is not a power of FLOAT_VEC_SIZE, do the remaining elements + in scalar mode. */ + + size_t i; + float res = 0; + size_t base; + + vector float vres1 = {0, 0, 0, 0}; + vector float vres2 = {0, 0, 0, 0}; + vector float vres3 = {0, 0, 0, 0}; + vector float vres4 = {0, 0, 0, 0}; + + base = (dim / 16) * 16; // Process 16 elements per loop + + for (size_t i = 0; i < base; i += 16) { + vector float vx1 = vec_xl(0, &ax[i]); + vector float vy1 = vec_xl(0, &bx[i]); + vector float vx2 = vec_xl(0, &ax[i + 4]); + vector float vy2 = vec_xl(0, &bx[i + 4]); + vector float vx3 = vec_xl(0, &ax[i + 8]); + vector float vy3 = vec_xl(0, &bx[i + 8]); + vector float vx4 = vec_xl(0, &ax[i + 12]); + vector float vy4 = vec_xl(0, &bx[i + 12]); + + vector float vdiff1 = vec_sub(vx1, vy1); + vector float vdiff2 = vec_sub(vx2, vy2); + vector float vdiff3 = vec_sub(vx3, vy3); + vector float vdiff4 = vec_sub(vx4, vy4); + + vres1 = vec_madd(vdiff1, vdiff1, vres1); + vres2 = vec_madd(vdiff2, vdiff2, vres2); + vres3 = vec_madd(vdiff3, vdiff3, vres3); + vres4 = vec_madd(vdiff4, vdiff4, vres4); + } + + // Sum up partial results + vres1 = vec_add(vres1, vres2); + vres3 = vec_add(vres3, vres4); + vres1 = vec_add(vres1, vres3); + + // Reduce vector sum to scalar + res = vec_extract(vres1, 0) + vec_extract(vres1, 1) + + vec_extract(vres1, 2) + vec_extract(vres1, 3); + + // Compute remaining elements (if any) + for (size_t i = base; i < dim; i++) { + float diff = ax[i] - bx[i]; + res += diff * diff; + } + + return res; +} + +#else + VECTOR_TARGET_CLONES static float VectorL2SquaredDistance(int dim, float *ax, float *bx) { @@ -558,6 +624,8 @@ VectorL2SquaredDistance(int dim, float *ax, float *bx) return distance; } +#endif + /* * Get the L2 distance between vectors */ @@ -589,6 +657,57 @@ vector_l2_squared_distance(PG_FUNCTION_ARGS) PG_RETURN_FLOAT8((double) VectorL2SquaredDistance(a->dim, a->x, b->x)); } +#ifdef __powerpc__ + +VECTOR_TARGET_CLONES static float +VectorInnerProduct(int dim, float *ax, float *bx) +{ + size_t i; + float res = 0; + size_t base; + + vector float vres1 = {0, 0, 0, 0}; + vector float vres2 = {0, 0, 0, 0}; + vector float vres3 = {0, 0, 0, 0}; + vector float vres4 = {0, 0, 0, 0}; + + base = (dim / 16) * 16; + + for (size_t i = 0; i < base; i += 16) { + vector float vx1 = vec_xl(0, &ax[i]); + vector float vy1 = vec_xl(0, &bx[i]); + vector float vx2 = vec_xl(0, &ax[i + 4]); + vector float vy2 = vec_xl(0, &bx[i + 4]); + vector float vx3 = vec_xl(0, &ax[i + 8]); + vector float vy3 = vec_xl(0, &bx[i + 8]); + vector float vx4 = vec_xl(0, &ax[i + 12]); + vector float vy4 = vec_xl(0, &bx[i + 12]); + + vres1 = vec_madd(vx1, vy1, vres1); + vres2 = vec_madd(vx2, vy2, vres2); + vres3 = vec_madd(vx3, vy3, vres3); + vres4 = vec_madd(vx4, vy4, vres4); + } + + // Sum all vector accumulators + vres1 = vec_add(vres1, vres2); + vres3 = vec_add(vres3, vres4); + vres1 = vec_add(vres1, vres3); + + // Reduce vector sum to scalar + res = vec_extract(vres1, 0) + vec_extract(vres1, 1) + + vec_extract(vres1, 2) + vec_extract(vres1, 3); + + // Handle remaining elements (if dim is not a multiple of 16) + for (size_t i = base; i < dim; i++) { + res += ax[i] * bx[i]; + } + + return res; +} + +#else + VECTOR_TARGET_CLONES static float VectorInnerProduct(int dim, float *ax, float *bx) { @@ -601,6 +720,8 @@ VectorInnerProduct(int dim, float *ax, float *bx) return distance; } +#endif + /* * Get the inner product of two vectors */ @@ -631,6 +752,91 @@ vector_negative_inner_product(PG_FUNCTION_ARGS) PG_RETURN_FLOAT8((double) -VectorInnerProduct(a->dim, a->x, b->x)); } +#ifdef __powerpc__ + +VECTOR_TARGET_CLONES static double +VectorCosineSimilarity(int dim, float *ax, float *bx) +{ + size_t i; + float res = 0; + size_t base; + + vector float vdot1 = {0, 0, 0, 0}; + vector float vdot2 = {0, 0, 0, 0}; + vector float vdot3 = {0, 0, 0, 0}; + vector float vdot4 = {0, 0, 0, 0}; + + vector float vmag_x1 = {0, 0, 0, 0}; + vector float vmag_x2 = {0, 0, 0, 0}; + vector float vmag_x3 = {0, 0, 0, 0}; + vector float vmag_x4 = {0, 0, 0, 0}; + + vector float vmag_y1 = {0, 0, 0, 0}; + vector float vmag_y2 = {0, 0, 0, 0}; + vector float vmag_y3 = {0, 0, 0, 0}; + vector float vmag_y4 = {0, 0, 0, 0}; + + base = (dim / 16) * 16; // Process 16 elements per iteration + + for (size_t i = 0; i < base; i += 16) { + vector float vx1 = vec_xl(0, &ax[i]); + vector float vy1 = vec_xl(0, &bx[i]); + vector float vx2 = vec_xl(0, &ax[i + 4]); + vector float vy2 = vec_xl(0, &bx[i + 4]); + vector float vx3 = vec_xl(0, &ax[i + 8]); + vector float vy3 = vec_xl(0, &bx[i + 8]); + vector float vx4 = vec_xl(0, &ax[i + 12]); + vector float vy4 = vec_xl(0, &bx[i + 12]); + + vdot1 = vec_madd(vx1, vy1, vdot1); + vdot2 = vec_madd(vx2, vy2, vdot2); + vdot3 = vec_madd(vx3, vy3, vdot3); + vdot4 = vec_madd(vx4, vy4, vdot4); + + vmag_x1 = vec_madd(vx1, vx1, vmag_x1); + vmag_x2 = vec_madd(vx2, vx2, vmag_x2); + vmag_x3 = vec_madd(vx3, vx3, vmag_x3); + vmag_x4 = vec_madd(vx4, vx4, vmag_x4); + + vmag_y1 = vec_madd(vy1, vy1, vmag_y1); + vmag_y2 = vec_madd(vy2, vy2, vmag_y2); + vmag_y3 = vec_madd(vy3, vy3, vmag_y3); + vmag_y4 = vec_madd(vy4, vy4, vmag_y4); + } + + // Sum vector accumulators + vdot1 = vec_add(vdot1, vdot2); + vdot3 = vec_add(vdot3, vdot4); + vdot1 = vec_add(vdot1, vdot3); + + vmag_x1 = vec_add(vmag_x1, vmag_x2); + vmag_x3 = vec_add(vmag_x3, vmag_x4); + vmag_x1 = vec_add(vmag_x1, vmag_x3); + + vmag_y1 = vec_add(vmag_y1, vmag_y2); + vmag_y3 = vec_add(vmag_y3, vmag_y4); + vmag_y1 = vec_add(vmag_y1, vmag_y3); + + // Reduce vector sum to scalar + float dotpdt = vec_extract(vdot1, 0) + vec_extract(vdot1, 1) + + vec_extract(vdot1, 2) + vec_extract(vdot1, 3); + float mag_vx = vec_extract(vmag_x1, 0) + vec_extract(vmag_x1, 1) + + vec_extract(vmag_x1, 2) + vec_extract(vmag_x1, 3); + float mag_vy = vec_extract(vmag_y1, 0) + vec_extract(vmag_y1, 1) + + vec_extract(vmag_y1, 2) + vec_extract(vmag_y1, 3); + + // Process remaining elements (if any) + for (size_t i = base; i < dim; i++) { + dotpdt += ax[i] * bx[i]; + mag_vx += ax[i] * ax[i]; + mag_vy += bx[i] * bx[i]; + } + + return dotpdt / (sqrt(mag_vx) * sqrt(mag_vy)); +} + +#else + VECTOR_TARGET_CLONES static double VectorCosineSimilarity(int dim, float *ax, float *bx) { @@ -650,6 +856,8 @@ VectorCosineSimilarity(int dim, float *ax, float *bx) return (double) similarity / sqrt((double) norma * (double) normb); } +#endif + /* * Get the cosine distance between two vectors */
^ permalink raw [nested|flat] 13+ messages in thread

* Re: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-02-03 10:53 ` "mikejuliet13 (@mikejuliet13)" <noreply+mikejuliet13@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: mikejuliet13 (@mikejuliet13) @ 2025-02-03 10:53 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@mikejuliet13 renamed this.

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

* Re: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-02-03 14:33 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: jkatz (@jkatz) @ 2025-02-03 14:33 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

> Benchmarked on IBM Power using ANN-Benchmark, showing significant improvements in execution time.

Can you show the output of these tests, with the before and after?

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

* Re: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-02-04 13:54 ` "mikejuliet13 (@mikejuliet13)" <noreply+mikejuliet13@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: mikejuliet13 (@mikejuliet13) @ 2025-02-04 13:54 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

> > Benchmarked on IBM Power using ANN-Benchmark, showing significant improvements in execution time.
> 
> Can you show the output of these tests, with the before and after?

The data collected for before and after changes for Angular metric for m=24 is:
[Angular metric data for m=24.xlsx](https://github.com/user-attachments/files/18658028/Angular.metric.data.for.m.24.xlsx)

The data collected for before and after changes for Eucledian metric for m=24 is:
[Eucledian metric data for m=24.xlsx](https://github.com/user-attachments/files/18658035/Eucledian.metric.data.for.m.24.xlsx)


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

* Re: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-02-04 14:22 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: jkatz (@jkatz) @ 2025-02-04 14:22 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@mikejuliet13 Which dataset was this test run on? Was there any impact on recall?  What hardware was this on? What were the PostgreSQL settings used? Did you test this with any concurrency What were the results from other datasets?

Here's [an example](https://jkatz05.com/post/postgres/pgvector-performance-150x-speedup/) for how to report speedup results.

Also, is there a reason that POWER requires the explicit SIMD instructions that aren't available from the autovectorization gcc flags?

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

* Re: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-02-14 00:03 ` "ankane (@ankane)" <noreply+ankane@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: ankane (@ankane) @ 2025-02-14 00:03 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @mikejuliet13, thanks for the PR. As @jkatz mentioned, I think it'd be better to try [auto-vectorization](https://gcc.gnu.org/projects/tree-ssa/vectorization.html#using):

```sh
make OPTFLAGS="-maltivec"
```

If that works, we could probably add it [to the Makefile](https://github.com/pgvector/pgvector/blob/v0.8.0/Makefile#L27).

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

* Re: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-02-14 00:11 ` "ankane (@ankane)" <noreply+ankane@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: ankane (@ankane) @ 2025-02-14 00:11 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Although looking at #117, this may not be needed.

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

* Re: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-02-17 08:51 ` "mikejuliet13 (@mikejuliet13)" <noreply+mikejuliet13@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: mikejuliet13 (@mikejuliet13) @ 2025-02-17 08:51 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks @jkatz and @ankane for your comments and inputs. I am verifying the changes with different datasets and differences between Auto vectorised to custom optimised code.  Will comeback with observations.

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

* Re: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-04-10 20:21 ` "ankane (@ankane)" <noreply+ankane@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: ankane (@ankane) @ 2025-04-10 20:21 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

If there is a difference between this approach and auto-vectorization, I think it'd be better to submit the improvements to the compiler. Closing this out, but feel free to share any numbers if you have them.

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

* Re: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-04-10 20:21 ` "ankane (@ankane)" <noreply+ankane@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: ankane (@ankane) @ 2025-04-10 20:21 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

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

* Re: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-04-29 12:39 ` "mikejuliet13 (@mikejuliet13)" <noreply+mikejuliet13@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: mikejuliet13 (@mikejuliet13) @ 2025-04-29 12:39 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @ankane  ,
Thanks for the follow-up.

My changes leverage Power-specific SIMD intrinsics (Altivec/VSX) along with manual loop unrolling to better utilize the architecture’s capabilities. While the -O3 flag in the original code enables auto-vectorization, it does not perform loop unrolling. This leads to a performance bottleneck—specifically around the xvmaddmsp instruction, which accounts for ~54% of the hotspots in perf, as operations repeatedly funnel through multiply-adds on the same register.

There is clear scope for performance improvement on Power by applying loop unrolling, either manually or via the -funroll-loops compiler flag.

Key Performance Observations (Power10):
Original code + -funroll-loops performs better than without it:
→ QPS improvement: 6–12%
→ Indexing time reduction: 10–20%
(observed primarily on higher-dimensional datasets like fashion-mnist-784-euclidean and gist-960-euclidean)

For lower-dimensional datasets like nytimes-256-angular and glove-100-angular, the performance difference is negligible for both QPS and indexing time.

Optimized code (manual VSX + manual loop unrolling) outperforms even the loop-unrolled original:
→ QPS improvement: 4–10%
→ Indexing time reduction: 10–30%
(notable gains on higher-dimensional datasets and even nytimes-256-angular)

For glove-100-angular, the difference remains minimal.

I tested across the following datasets in both batch and non-batch modes:
fashion-mnist-784-euclidean, gist-960-euclidean, nytimes-256-angular, and glove-100-angular.

Additional Insights:
Higher dimensionality correlates with greater performance benefits from the optimized code.

Larger m and ef_search values also amplify the performance gains in favor of the optimized version.

While auto-vectorization is certainly beneficial, these findings suggest that manual control—particularly with loop unrolling—still delivers tangible performance improvements on Power architecture.

I'm happy to share the full perf data or benchmarking logs if needed.

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

* Re: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-05-02 20:21 ` "ankane (@ankane)" <noreply+ankane@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: ankane (@ankane) @ 2025-05-02 20:21 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks for sharing. Auto-vectorization typically performs loop interleaving, so I'd submit a patch to the compiler if that's not the case with Power.

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

* Re: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
@ 2025-05-02 20:36 ` "ankane (@ankane)" <noreply+ankane@github.com>
  11 siblings, 0 replies; 13+ messages in thread

From: ankane (@ankane) @ 2025-05-02 20:36 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

It looks like it is interleaved with GCC 13+ for `power64le` (and does not require `-maltivec`).

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


end of thread, other threads:[~2025-05-02 20:36 UTC | newest]

Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-02-03 10:30 [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture "mikejuliet13 (@mikejuliet13)" <noreply+mikejuliet13@github.com>
2025-02-03 10:53 ` "mikejuliet13 (@mikejuliet13)" <noreply+mikejuliet13@github.com>
2025-02-03 14:33 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2025-02-04 13:54 ` "mikejuliet13 (@mikejuliet13)" <noreply+mikejuliet13@github.com>
2025-02-04 14:22 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2025-02-14 00:03 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-02-14 00:11 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-02-17 08:51 ` "mikejuliet13 (@mikejuliet13)" <noreply+mikejuliet13@github.com>
2025-04-10 20:21 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-04-10 20:21 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-04-29 12:39 ` "mikejuliet13 (@mikejuliet13)" <noreply+mikejuliet13@github.com>
2025-05-02 20:21 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-05-02 20:36 ` "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