pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] PR #536: SVE vector optimization for halfvectors dot product calculation
5+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] PR #536: SVE vector optimization for halfvectors dot product calculation
@ 2024-04-30 12:41 "pashkinelfe (@pashkinelfe)" <noreply+pashkinelfe@github.com>
0 siblings, 0 replies; 5+ messages in thread
From: pashkinelfe (@pashkinelfe) @ 2024-04-30 12:41 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi, @ankane and @jkatz !
I optimized the dot product calculation for half vector using SVE extension that is present on the many machines suitable for vector search on ARM architecture.
1. Testing HNSW build time (same test as https://github.com/pgvector/pgvector/issues/326#issuecomment-2069362693 and https://github.com/pgvector/pgvector/issues/409#issuecomment-1895470718
<img width="732" alt="image" src="https://github.com/pgvector/pgvector/assets/63344111/ff88539e-baa3-43ff-aa71-30cdb083a85d";
Results show that on the same machine (Graviton3):
- index build time for half vectors using default inner product function is better than for float32 vectors only at high number of cores. Possibly due to less locks/memory accesses/disk IO
- index build time for SVE inner product function for halfvectore is better than with default inner product function for halfvectors at any number of cores. Even at serial build.
2. I tried to add SVE optimization for converting float32<->float16 but found no performance gain in
`insert into emb_f16_3 (vector) select vector::halfvec(1536) from emb;`
Possibly because using SVE intrinsic makes sense when we deal with many float16 number altogether as it's in dot product calculation. So it's not in the patch.
I suggest that if the performance gain for (1) is good enough we could try to add some architecture checks into the patch. I'm not convinced that the way I did is optimal one. So if anyone more experiences in these checks add some improvements, I appreciate it very much.
And also maybe it's worth adding SVE optimized cosine and l2 distance and testing these cases separately.
inline patch 1: src/halfutils.c, src/halfvec.h (+33/-3 lines) download inline-1.patch
diff --git a/src/halfutils.c b/src/halfutils.c
index 6f499dd74..ec291c8cd 100644
--- a/src/halfutils.c
+++ b/src/halfutils.c
@@ -15,7 +15,7 @@
#ifdef _MSC_VER
#define TARGET_F16C
#else
-#define TARGET_F16C __attribute__((target("avx,f16c,fma")))
+#define TARGET_F16C __attribute__((target("avx,f16c,fma,sve,fp16")))
#endif
#endif
@@ -76,6 +76,25 @@ HalfvecL2SquaredDistanceF16c(int dim, half * ax, half * bx)
}
#endif
+#ifdef ARM_SVE
+static float
+HalfvecInnerProductSVE(int dim, half * ax, half * bx)
+{
+ svfloat16_t dist = svdupq_n_f16(0, 0, 0, 0, 0, 0, 0, 0);
+
+ for (int i = 0; i < dim; i += svcnth())
+ {
+ svbool_t layout = svwhilelt_b16((unsigned int)i, (unsigned int)dim);
+ svfloat16_t vax = svld1_f16(layout, ax + i);
+ svfloat16_t vbx = svld1_f16(layout, bx + i);
+
+ dist = svmla_f16_m(layout, dist, vax, vbx);
+ }
+
+ return svaddv_f16(svptrue_b16(), dist);
+}
+#endif
+
static float
HalfvecInnerProductDefault(int dim, half * ax, half * bx)
{
@@ -285,7 +304,10 @@ HalfvecInit(void)
HalfvecCosineSimilarity = HalfvecCosineSimilarityDefault;
HalfvecL1Distance = HalfvecL1DistanceDefault;
-#ifdef HALFVEC_DISPATCH
+#ifdef ARM_SVE
+ HalfvecInnerProduct = HalfvecInnerProductSVE;
+
+#elif defined(HALFVEC_DISPATCH)
if (SupportsCpuFeature(CPU_FEATURE_AVX | CPU_FEATURE_F16C | CPU_FEATURE_FMA))
{
HalfvecL2SquaredDistance = HalfvecL2SquaredDistanceF16c;
diff --git a/src/halfvec.h b/src/halfvec.h
index d5c56d38d..bf05047c4 100644
--- a/src/halfvec.h
+++ b/src/halfvec.h
@@ -4,6 +4,7 @@
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include <float.h>
+#include <arm_sve.h>
/* We use two types of dispatching: intrinsics and target_clones */
/* TODO Move to better place */
@@ -23,6 +24,10 @@
#define USE_TARGET_CLONES
#endif
+#if defined(__ARM_FEATURE_SVE) || defined(__ARM_FEATURE_SVE2)
+#define ARM_SVE
+#endif
+
#if defined(USE_DISPATCH)
#define HALFVEC_DISPATCH
#endif
@@ -34,7 +39,10 @@
#define FLT16_SUPPORT
#endif
-#ifdef FLT16_SUPPORT
+#if defined(ARM_SVE)
+#define half __fp16
+#define HALF_MAX FLT16_MAX
+#elif defined FLT16_SUPPORT
#define half _Float16
#define HALF_MAX FLT16_MAX
#else
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] PR #536: SVE vector optimization for halfvectors dot product calculation
@ 2024-09-24 20:11 ` "ankane (@ankane)" <noreply+ankane@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: ankane (@ankane) @ 2024-09-24 20:11 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @pashkinelfe, thanks for the PR, and sorry for the delay. The build improvement looks nice. My main concern (besides maintenance of more distance functions) is accumulation happens at half precision, which will produce different results than the current code. I'd be curious to see how performance looks if it's changed to single precision.
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] PR #536: SVE vector optimization for halfvectors dot product calculation
@ 2024-09-26 13:07 ` "pashkinelfe (@pashkinelfe)" <noreply+pashkinelfe@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: pashkinelfe (@pashkinelfe) @ 2024-09-26 13:07 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane thanks! I'll try to ann-benchmark index quality soon
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] PR #536: SVE vector optimization for halfvectors dot product calculation
@ 2025-04-10 20:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: ankane (@ankane) @ 2025-04-10 20:25 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Cleaning up stale PRs (but happy to revisit if there's a way to significantly improve performance without changing the accumulation precision).
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] PR #536: SVE vector optimization for halfvectors dot product calculation
@ 2025-04-10 20:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: ankane (@ankane) @ 2025-04-10 20:25 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 5+ messages in thread
end of thread, other threads:[~2025-04-10 20:25 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-04-30 12:41 [pgvector/pgvector] PR #536: SVE vector optimization for halfvectors dot product calculation "pashkinelfe (@pashkinelfe)" <noreply+pashkinelfe@github.com>
2024-09-24 20:11 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-09-26 13:07 ` "pashkinelfe (@pashkinelfe)" <noreply+pashkinelfe@github.com>
2025-04-10 20:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-04-10 20:25 ` "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