pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feedFrom: mikejuliet13 (@mikejuliet13) <noreply+mikejuliet13@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] PR #772: Optimise vector distance functions for IBM Power Architecture
Date: Mon, 03 Feb 2025 10:30:09 +0000
Message-ID: <gh-pgvector-pgvector-772@github.com> (raw)
### 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.patch
diff --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
*/
view thread (13+ messages) latest in thread
Message-ID: <gh-pgvector-pgvector-772@github.com>
Permalink: ../gh-pgvector-pgvector-772@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