aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/alloc.c89
-rw-r--r--src/alloc.h34
-rw-r--r--src/embed.c126
-rw-r--r--src/vec.c46
4 files changed, 173 insertions, 122 deletions
diff --git a/src/alloc.c b/src/alloc.c
new file mode 100644
index 0000000..e3b82d7
--- /dev/null
+++ b/src/alloc.c
@@ -0,0 +1,89 @@
+#include "alloc.h"
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <limits.h>
+
+/* Sequence-capacity growth policy for the forward-pass workspaces: never
+ * allocate fewer than the minimum, and round up to the granularity so small
+ * length changes do not trigger a realloc on every call. */
+#define EMBED_MIN_WORKSPACE_SEQ_CAP 16
+#define EMBED_WORKSPACE_SEQ_GRANULARITY 16
+
+int mul_size(size_t a, size_t b, size_t *out) {
+ if (a != 0 && b > SIZE_MAX / a)
+ return -1;
+ *out = a * b;
+ return 0;
+}
+
+int add_size(size_t a, size_t b, size_t *out) {
+ if (b > SIZE_MAX - a)
+ return -1;
+ *out = a + b;
+ return 0;
+}
+
+int grow_cap(int current, int needed, int *out) {
+ if (needed <= 0)
+ return -1;
+
+ int cap = needed;
+ if (cap < EMBED_MIN_WORKSPACE_SEQ_CAP)
+ cap = EMBED_MIN_WORKSPACE_SEQ_CAP;
+
+ int rem = cap % EMBED_WORKSPACE_SEQ_GRANULARITY;
+ if (rem != 0) {
+ int add = EMBED_WORKSPACE_SEQ_GRANULARITY - rem;
+ if (cap > INT_MAX - add)
+ return -1;
+ cap += add;
+ }
+
+ if (cap < current)
+ cap = current;
+
+ *out = cap;
+ return 0;
+}
+
+int realloc_floats(float **ptr, size_t count) {
+ size_t bytes;
+ if (mul_size(count, sizeof(float), &bytes) != 0)
+ return -1;
+
+ void *p = realloc(*ptr, bytes);
+ if (!p && bytes != 0)
+ return -1;
+ *ptr = (float *)p;
+ return 0;
+}
+
+float *malloc_floats(size_t count) {
+ size_t bytes;
+ if (mul_size(count, sizeof(float), &bytes) != 0)
+ return NULL;
+ return (float *)malloc(bytes);
+}
+
+int realloc_floats_2d(float **ptr, int rows, int cols) {
+ if (rows < 0 || cols < 0)
+ return -1;
+
+ size_t count;
+ if (mul_size((size_t)rows, (size_t)cols, &count) != 0)
+ return -1;
+ return realloc_floats(ptr, count);
+}
+
+int realloc_ints(int **ptr, size_t count) {
+ size_t bytes;
+ if (mul_size(count, sizeof(int), &bytes) != 0)
+ return -1;
+
+ void *p = realloc(*ptr, bytes);
+ if (!p && bytes != 0)
+ return -1;
+ *ptr = (int *)p;
+ return 0;
+}
diff --git a/src/alloc.h b/src/alloc.h
new file mode 100644
index 0000000..bc40e8b
--- /dev/null
+++ b/src/alloc.h
@@ -0,0 +1,34 @@
+#ifndef EMBED_ALLOC_H
+#define EMBED_ALLOC_H
+
+#include <stddef.h>
+
+/* Overflow-checked size arithmetic and typed (re)allocation helpers shared by
+ * the model loader, the forward pass, and the late-interaction code. Every
+ * helper returns -1 (or NULL) on overflow or allocation failure so callers can
+ * bail without a partial buffer. realloc_* keep the original pointer intact on
+ * failure; the caller still owns it. */
+
+/* out = a * b, or -1 if the product overflows size_t. */
+int mul_size(size_t a, size_t b, size_t *out);
+
+/* out = a + b, or -1 if the sum overflows size_t. */
+int add_size(size_t a, size_t b, size_t *out);
+
+/* Round a requested sequence capacity up to the workspace growth granularity,
+ * never shrinking below the current capacity. Returns -1 on bad input/overflow. */
+int grow_cap(int current, int needed, int *out);
+
+/* realloc *ptr to hold `count` floats; *ptr unchanged on failure. */
+int realloc_floats(float **ptr, size_t count);
+
+/* malloc `count` floats, NULL on overflow/failure. */
+float *malloc_floats(size_t count);
+
+/* realloc *ptr to hold rows*cols floats; -1 on negative dims/overflow/failure. */
+int realloc_floats_2d(float **ptr, int rows, int cols);
+
+/* realloc *ptr to hold `count` ints; *ptr unchanged on failure. */
+int realloc_ints(int **ptr, size_t count);
+
+#endif /* EMBED_ALLOC_H */
diff --git a/src/embed.c b/src/embed.c
index 9b6ccd9..e265427 100644
--- a/src/embed.c
+++ b/src/embed.c
@@ -1,4 +1,5 @@
#include "embed.h"
+#include "alloc.h"
#include "config.h"
#include "internal.h"
#include "dtype.h"
@@ -32,71 +33,13 @@ struct embed_late_workspace {
int embed_verbose = 0;
-#define EMBED_BF16_QKV_FUSE_MAX_SEQ 16
-#define EMBED_BF16_PAIR_FUSE_MAX_SEQ 4
-#define EMBED_MIN_WORKSPACE_SEQ_CAP 16
-#define EMBED_WORKSPACE_SEQ_GRANULARITY 16
+#define EMBED_BF16_QKV_FUSE_MAX_SEQ 16
+#define EMBED_BF16_PAIR_FUSE_MAX_SEQ 4
/* ========================================================================
- * Size helpers
+ * Model-directory probes
* ======================================================================== */
-static int mul_size(size_t a, size_t b, size_t *out) {
- if (a != 0 && b > SIZE_MAX / a)
- return -1;
- *out = a * b;
- return 0;
-}
-
-static int add_size(size_t a, size_t b, size_t *out) {
- if (b > SIZE_MAX - a)
- return -1;
- *out = a + b;
- return 0;
-}
-
-static int grow_cap(int current, int needed, int *out) {
- if (needed <= 0)
- return -1;
-
- int cap = needed;
- if (cap < EMBED_MIN_WORKSPACE_SEQ_CAP)
- cap = EMBED_MIN_WORKSPACE_SEQ_CAP;
-
- int rem = cap % EMBED_WORKSPACE_SEQ_GRANULARITY;
- if (rem != 0) {
- int add = EMBED_WORKSPACE_SEQ_GRANULARITY - rem;
- if (cap > INT_MAX - add)
- return -1;
- cap += add;
- }
-
- if (cap < current)
- cap = current;
-
- *out = cap;
- return 0;
-}
-
-static int realloc_floats(float **ptr, size_t count) {
- size_t bytes;
- if (mul_size(count, sizeof(float), &bytes) != 0)
- return -1;
-
- void *p = realloc(*ptr, bytes);
- if (!p && bytes != 0)
- return -1;
- *ptr = (float *)p;
- return 0;
-}
-
-static float *malloc_floats(size_t count) {
- size_t bytes;
- if (mul_size(count, sizeof(float), &bytes) != 0)
- return NULL;
- return (float *)malloc(bytes);
-}
-
static int model_dir_has_file(const char *model_dir, const char *rel) {
char path[1024];
int n = snprintf(path, sizeof(path), "%s/%s", model_dir, rel);
@@ -114,16 +57,6 @@ static int model_dir_has_late_projection(const char *model_dir) {
model_dir_has_file(model_dir, "1_Dense/model.safetensors");
}
-static int realloc_floats_2d(float **ptr, int rows, int cols) {
- if (rows < 0 || cols < 0)
- return -1;
-
- size_t count;
- if (mul_size((size_t)rows, (size_t)cols, &count) != 0)
- return -1;
- return realloc_floats(ptr, count);
-}
-
static int ensure_late_state_buffer(embed_late_workspace_t *ws, int n_tokens) {
if (!ws || !ws->model || n_tokens <= 0)
return -1;
@@ -136,18 +69,6 @@ static int ensure_late_state_buffer(embed_late_workspace_t *ws, int n_tokens) {
return 0;
}
-static int realloc_ints(int **ptr, size_t count) {
- size_t bytes;
- if (mul_size(count, sizeof(int), &bytes) != 0)
- return -1;
-
- void *p = realloc(*ptr, bytes);
- if (!p && bytes != 0)
- return -1;
- *ptr = (int *)p;
- return 0;
-}
-
/* ========================================================================
* Weight loading (direct mmap pointers into safetensors)
* ======================================================================== */
@@ -1772,42 +1693,3 @@ float *embed_model_encode(const embed_model_t *model,
}
return emb;
}
-
-/* ========================================================================
- * Vector helpers
- * ======================================================================== */
-
-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++) {
- dot += a[i] * b[i];
- norm_a_sq += a[i] * a[i];
- norm_b_sq += b[i] * b[i];
- }
-
- if (!(norm_a_sq > 0.0f) || !(norm_b_sq > 0.0f) || !isfinite(norm_a_sq) || !isfinite(norm_b_sq))
- return 0.0f;
- return dot / sqrtf(norm_a_sq * norm_b_sq);
-}
diff --git a/src/vec.c b/src/vec.c
new file mode 100644
index 0000000..63bd4bc
--- /dev/null
+++ b/src/vec.c
@@ -0,0 +1,46 @@
+#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++) {
+ dot += a[i] * b[i];
+ norm_a_sq += a[i] * a[i];
+ norm_b_sq += b[i] * b[i];
+ }
+
+ if (!(norm_a_sq > 0.0f) || !(norm_b_sq > 0.0f) || !isfinite(norm_a_sq) || !isfinite(norm_b_sq))
+ return 0.0f;
+ return dot / sqrtf(norm_a_sq * norm_b_sq);
+}