From cca81e59a9afeebe1237f713e919b5002df3d749 Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Tue, 16 Jun 2026 17:11:55 +0200 Subject: cpu: extract alloc.{c,h} and vec.c from embed.c --- Makefile | 2 +- src/alloc.c | 89 ++++++++++++++++++++++++++++++++++++++++++ src/alloc.h | 34 ++++++++++++++++ src/embed.c | 126 ++---------------------------------------------------------- src/vec.c | 46 ++++++++++++++++++++++ 5 files changed, 174 insertions(+), 123 deletions(-) create mode 100644 src/alloc.c create mode 100644 src/alloc.h create mode 100644 src/vec.c diff --git a/Makefile b/Makefile index d0bf87c..7df355b 100644 --- a/Makefile +++ b/Makefile @@ -78,7 +78,7 @@ LDFLAGS = -lm -lpthread $(BACKEND_LDFLAGS) # in one variable stops the per-test source lists from drifting apart. KERNEL_SRCS = src/kernels.c src/kernels_generic.c src/kernels_neon.c src/kernels_avx.c TOKENIZER_SRCS = src/tokenizer_bpe.c src/tokenizer_wordpiece.c -CORE_SRCS = src/embed.c src/config.c src/safetensors.c $(KERNEL_SRCS) +CORE_SRCS = src/embed.c src/alloc.c src/vec.c src/config.c src/safetensors.c $(KERNEL_SRCS) SRCS = $(CORE_SRCS) $(TOKENIZER_SRCS) \ deps/ae/ae.c deps/ae/anet.c deps/ae/monotonic.c 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 +#include +#include + +/* 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 + +/* 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 + +/* ======================================================================== + * 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); +} -- cgit v1.3.1