aboutsummaryrefslogtreecommitdiff
path: root/src/vec.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-20 20:10:56 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-21 05:57:19 +0200
commit6b969d25ce136eaf695222bd1a10bff1ecac0220 (patch)
treec23afb50c28b9deaddae5c6526a74ee11cf2d865 /src/vec.c
parentd4bdb8dae753462f621507d5fc3582e0e53da35c (diff)
downloadffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.tar.gz
ffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.zip
all: split Makefile and reorg library/tools
Diffstat (limited to 'src/vec.c')
-rw-r--r--src/vec.c53
1 files changed, 0 insertions, 53 deletions
diff --git a/src/vec.c b/src/vec.c
deleted file mode 100644
index cea1576..0000000
--- a/src/vec.c
+++ /dev/null
@@ -1,53 +0,0 @@
-#include "embed.h"
-
-#include <math.h>
-
-/* ========================================================================
- * Vector helpers
- *
- * Standalone post-processing on embedding vectors: callers (CLI, server) use
- * these to L2-normalize outputs and to score pooled embeddings with cosine
- * similarity. They touch no model or tensor internals, only the public API.
- * ======================================================================== */
-
-int embed_l2_normalize(float *vec, int dim) {
- if (!vec || dim <= 0)
- return -1;
-
- float norm_sq = 0.0f;
- for (int i = 0; i < dim; i++)
- norm_sq += vec[i] * vec[i];
-
- if (!(norm_sq > 0.0f) || !isfinite(norm_sq))
- return -1;
-
- float inv_norm = 1.0f / sqrtf(norm_sq);
- for (int i = 0; i < dim; i++)
- vec[i] *= inv_norm;
- return 0;
-}
-
-float embed_cosine_similarity(const float *a, const float *b, int dim) {
- if (!a || !b || dim <= 0)
- return 0.0f;
-
- float dot = 0.0f;
- float norm_a_sq = 0.0f;
- float norm_b_sq = 0.0f;
- for (int i = 0; i < dim; i++) {
- float av = a[i];
- float bv = b[i];
- dot += av * bv;
- norm_a_sq += av * av;
- norm_b_sq += bv * bv;
- }
-
- if (!(norm_a_sq > 0.0f) || !(norm_b_sq > 0.0f) || !isfinite(norm_a_sq) ||
- !isfinite(norm_b_sq) || !isfinite(dot))
- return 0.0f;
-
- double denom = sqrt((double)norm_a_sq) * sqrt((double)norm_b_sq);
- if (!(denom > 0.0) || !isfinite(denom))
- return 0.0f;
- return (float)((double)dot / denom);
-}