pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] PR #639: Combine overflow checks and computation steps.
3+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] PR #639: Combine overflow checks and computation steps.
@ 2024-08-08 03:08 "higuoxing (@higuoxing)" <noreply+higuoxing@github.com>
0 siblings, 0 replies; 3+ messages in thread
From: higuoxing (@higuoxing) @ 2024-08-08 03:08 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Some of the overflow checks can be combined with the computation step and can save some loops.
inline patch 1: src/halfvec.c, src/vector.c (+13/-30 lines) download inline-1.patch
diff --git a/src/halfvec.c b/src/halfvec.c
index 2cb1127ff..be006e5cf 100644
--- a/src/halfvec.c
+++ b/src/halfvec.c
@@ -753,11 +753,10 @@ halfvec_l2_normalize(PG_FUNCTION_ARGS)
if (norm > 0)
{
for (int i = 0; i < a->dim; i++)
+ {
rx[i] = Float4ToHalfUnchecked(HalfToFloat4(ax[i]) / norm);
- /* Check for overflow */
- for (int i = 0; i < a->dim; i++)
- {
+ /* Check for overflow */
if (HalfIsInf(rx[i]))
float_overflow_error();
}
@@ -793,11 +792,7 @@ halfvec_add(PG_FUNCTION_ARGS)
#else
rx[i] = Float4ToHalfUnchecked(HalfToFloat4(ax[i]) + HalfToFloat4(bx[i]));
#endif
- }
-
- /* Check for overflow */
- for (int i = 0, imax = a->dim; i < imax; i++)
- {
+ /* Check for overflow */
if (HalfIsInf(rx[i]))
float_overflow_error();
}
@@ -832,11 +827,7 @@ halfvec_sub(PG_FUNCTION_ARGS)
#else
rx[i] = Float4ToHalfUnchecked(HalfToFloat4(ax[i]) - HalfToFloat4(bx[i]));
#endif
- }
-
- /* Check for overflow */
- for (int i = 0, imax = a->dim; i < imax; i++)
- {
+ /* Check for overflow */
if (HalfIsInf(rx[i]))
float_overflow_error();
}
@@ -871,11 +862,7 @@ halfvec_mul(PG_FUNCTION_ARGS)
#else
rx[i] = Float4ToHalfUnchecked(HalfToFloat4(ax[i]) * HalfToFloat4(bx[i]));
#endif
- }
-
- /* Check for overflow and underflow */
- for (int i = 0, imax = a->dim; i < imax; i++)
- {
+ /* Check for overflow and underflow */
if (HalfIsInf(rx[i]))
float_overflow_error();
diff --git a/src/vector.c b/src/vector.c
index a0e2c3fb1..7619a2268 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -813,11 +813,10 @@ l2_normalize(PG_FUNCTION_ARGS)
if (norm > 0)
{
for (int i = 0; i < a->dim; i++)
+ {
rx[i] = ax[i] / norm;
- /* Check for overflow */
- for (int i = 0; i < a->dim; i++)
- {
+ /* Check for overflow */
if (isinf(rx[i]))
float_overflow_error();
}
@@ -847,11 +846,10 @@ vector_add(PG_FUNCTION_ARGS)
/* Auto-vectorized */
for (int i = 0, imax = a->dim; i < imax; i++)
+ {
rx[i] = ax[i] + bx[i];
- /* Check for overflow */
- for (int i = 0, imax = a->dim; i < imax; i++)
- {
+ /* Check for overflow */
if (isinf(rx[i]))
float_overflow_error();
}
@@ -880,11 +878,10 @@ vector_sub(PG_FUNCTION_ARGS)
/* Auto-vectorized */
for (int i = 0, imax = a->dim; i < imax; i++)
+ {
rx[i] = ax[i] - bx[i];
- /* Check for overflow */
- for (int i = 0, imax = a->dim; i < imax; i++)
- {
+ /* Check for overflow */
if (isinf(rx[i]))
float_overflow_error();
}
@@ -913,11 +910,10 @@ vector_mul(PG_FUNCTION_ARGS)
/* Auto-vectorized */
for (int i = 0, imax = a->dim; i < imax; i++)
+ {
rx[i] = ax[i] * bx[i];
- /* Check for overflow and underflow */
- for (int i = 0, imax = a->dim; i < imax; i++)
- {
+ /* Check for overflow and underflow */
if (isinf(rx[i]))
float_overflow_error();
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgvector/pgvector] PR #639: Combine overflow checks and computation steps.
@ 2024-08-08 03:20 ` "ankane (@ankane)" <noreply+ankane@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: ankane (@ankane) @ 2024-08-08 03:20 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @higuoxing, thanks for the PR. However, this breaks auto-vectorization (see the `Makefile` for flags to debug).
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgvector/pgvector] PR #639: Combine overflow checks and computation steps.
@ 2024-08-08 03:20 ` "ankane (@ankane)" <noreply+ankane@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: ankane (@ankane) @ 2024-08-08 03:20 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 3+ messages in thread
end of thread, other threads:[~2024-08-08 03:20 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-08-08 03:08 [pgvector/pgvector] PR #639: Combine overflow checks and computation steps. "higuoxing (@higuoxing)" <noreply+higuoxing@github.com>
2024-08-08 03:20 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-08 03:20 ` "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