diff options
| -rw-r--r-- | .gitignore | 9 | ||||
| -rw-r--r-- | Makefile | 28 | ||||
| -rw-r--r-- | bench/bench_model.c | 20 | ||||
| -rw-r--r-- | embed.c | 292 | ||||
| -rw-r--r-- | embed.h | 114 | ||||
| -rw-r--r-- | embed_cli.c | 82 | ||||
| -rw-r--r-- | embed_cuda.cu | 110 | ||||
| -rw-r--r-- | embed_cuda.h | 38 | ||||
| -rw-r--r-- | embed_internal.h | 72 | ||||
| -rw-r--r-- | embed_mlx.c | 206 | ||||
| -rw-r--r-- | embed_mlx.h | 88 | ||||
| -rw-r--r-- | embed_server.c | 256 | ||||
| -rw-r--r-- | embed_server.h | 20 | ||||
| -rw-r--r-- | qwen_kernels.h | 14 | ||||
| -rw-r--r-- | qwen_tokenizer.h | 26 | ||||
| -rw-r--r-- | tests/test_bf16_model.c | 28 | ||||
| -rw-r--r-- | tests/test_cli.c | 8 | ||||
| -rw-r--r-- | tests/test_late.c | 80 | ||||
| -rw-r--r-- | tests/test_safetensors.c | 2 | ||||
| -rw-r--r-- | tests/test_server.c | 8 | ||||
| -rw-r--r-- | tests/test_tokenizer.c | 2 | ||||
| -rw-r--r-- | tests/test_workspace.c | 116 |
22 files changed, 805 insertions, 814 deletions
@@ -1,7 +1,7 @@ *.o -pplx-embed -pplx-embed-server -libpplxembed.* +embed +embed-server +libembed.* tests/test_kernels_* tests/test_safetensors tests/test_bf16_model @@ -17,13 +17,10 @@ bench/bench_model bench/results/ coverage/ *.profraw -book/ .venv/ __pycache__/ *.pyc .DS_Store -pplx_server -client tmp private .vscode @@ -3,7 +3,7 @@ CC = gcc # not assume finite math or approximate libm calls: full -ffast-math produced # NaN embeddings on GCC/OpenBLAS Linux builds (-ffinite-math-only and # -fapprox-func are the unsafe parts and stay off). -# -fvisibility=hidden pairs with PPLX_API in the public headers: a new +# -fvisibility=hidden pairs with EMBED_API in the public headers: a new # public function without the annotation will be missing from the shared lib. CFLAGS_BASE = -Wall -Wextra -O3 -march=native -fPIC -fvisibility=hidden \ -fno-math-errno -ffp-contract=fast -fno-trapping-math \ @@ -34,18 +34,18 @@ CUDA_SRCS = embed_cuda.cu OBJS = $(SRCS:.c=.o) MLX_OBJS = $(MLX_SRCS:.c=.o) -TARGET = pplx-embed -SERVER_TARGET = pplx-embed-server -LIB = libpplxembed.a +TARGET = embed +SERVER_TARGET = embed-server +LIB = libembed.a KERNEL_SRCS = qwen_kernels.c qwen_kernels_generic.c \ qwen_kernels_neon.c qwen_kernels_avx.c ifeq ($(shell uname -s),Darwin) -SHARED_LIB = libpplxembed.dylib +SHARED_LIB = libembed.dylib SHARED_FLAGS = -dynamiclib else -SHARED_LIB = libpplxembed.so +SHARED_LIB = libembed.so SHARED_FLAGS = -shared endif @@ -55,23 +55,17 @@ endif all: help help: - @echo "pplx-embed - Inference for pplx-embed-v1 and pplx-embed-context-v1 embedding models" - @echo "" - @echo "Build targets:" + @echo "Targets:" @echo " make cpu Build the CPU backend (BLAS: Accelerate on macOS, OpenBLAS on Linux)" @echo " make metal Build the Apple GPU backend via MLX (recommended on Apple Silicon)" @echo " make cuda Build with CUDA/cuBLAS backend (Linux/NVIDIA)" @echo " make test Build and run the C test suite (no model files needed)" @echo " make coverage Test-suite line coverage report (clang/llvm-cov)" @echo " make bench Kernel microbenchmarks; records bench/results/*.json" - @echo " make bench-model MODEL_DIR=... End-to-end embedding benchmark" - @echo " (compare records with scripts/benchstat.py)" - @echo " make debug Debug build with AddressSanitizer" + @echo " make bench-model MODEL_DIR=/path/to/model" + @echo " End-to-end embedding benchmark" + @echo " make debug Debug build" @echo " make clean Remove build artifacts" - @echo "" - @echo "Usage:" - @echo " ./pplx-embed -d /path/to/model-dir \"text1\" \"text2\"" - @echo " ./pplx-embed-server --model pplx-embed-v1-0.6b=/path/to/model-dir" # ============================================================================= # CPU build (BLAS: Apple Accelerate on macOS, OpenBLAS on Linux) @@ -363,7 +357,7 @@ deps/ae/%.o: deps/ae/%.c deps/ae/ae.h deps/ae/anet.h deps/ae/monotonic.h # ============================================================================= clean: rm -f $(OBJS) embed_mlx.o embed_cuda.o embed_cli.o embed_server.o \ - $(TARGET) $(SERVER_TARGET) $(LIB) libpplxembed.dylib libpplxembed.so \ + $(TARGET) $(SERVER_TARGET) $(LIB) libembed.dylib libembed.so \ tests/test_kernels_generic tests/test_kernels_blas \ tests/test_safetensors tests/test_bf16_model tests/test_server \ tests/test_tokenizer tests/test_workspace tests/test_cli tests/test_late \ diff --git a/bench/bench_model.c b/bench/bench_model.c index edf0d0a..9b897e9 100644 --- a/bench/bench_model.c +++ b/bench/bench_model.c @@ -13,8 +13,8 @@ #include "embed.h" #include "qwen_kernels.h" -static pplx_model_t *g_model; -static pplx_workspace_t *g_ws; +static embed_model_t *g_model; +static embed_workspace_t *g_ws; static int g_hidden; enum { MAX_BATCH = 8 }; @@ -29,14 +29,14 @@ static void bm_embed(bench_state_t *b, int batch, int len) s = s * 1664525u + 1013904223u; ids[i] = (int)(s % 1000u); /* ordinary vocab rows */ } - pplx_input_t inputs[MAX_BATCH]; + embed_input_t inputs[MAX_BATCH]; for (int i = 0; i < batch; i++) { inputs[i].ids = ids + (size_t)i * len; inputs[i].n_tokens = len; } bench_begin(b); for (long i = 0; i < b->n; i++) { - if (pplx_model_embed_batch(g_model, g_ws, inputs, batch, out) != 0) + if (embed_model_encode_batch(g_model, g_ws, inputs, batch, out) != 0) exit(2); bench_sink += out[0]; } @@ -70,17 +70,17 @@ int main(int argc, char **argv) if (threads < 1) threads = 1; qwen_set_threads(threads); - g_model = pplx_model_load(argv[1]); + g_model = embed_model_load(argv[1]); if (!g_model) { fprintf(stderr, "failed to load model from %s\n", argv[1]); return 1; } - g_ws = pplx_workspace_new(g_model); + g_ws = embed_workspace_new(g_model); if (!g_ws) { - pplx_model_free(g_model); + embed_model_free(g_model); return 1; } - g_hidden = pplx_model_config(g_model)->hidden_size; + g_hidden = embed_model_config(g_model)->hidden_size; char meta[160]; snprintf(meta, sizeof(meta), "suite=model threads=%d hidden=%d", @@ -91,7 +91,7 @@ int main(int argc, char **argv) bench_parse_args(&opts, argc - 1, argv + 1); int rc = bench_main(&opts, CASES, (int)(sizeof(CASES) / sizeof(CASES[0]))); - pplx_workspace_free(g_ws); - pplx_model_free(g_model); + embed_workspace_free(g_ws); + embed_model_free(g_model); return rc; } @@ -10,16 +10,16 @@ #include <limits.h> #include <float.h> -struct pplx_late_model { - pplx_model_t *base; +struct embed_late_model { + embed_model_t *base; safetensors_file_t *dense_sf; - pplx_weight_ref_t projection; /* [token_dim, hidden] */ + embed_weight_ref_t projection; /* [token_dim, hidden] */ int token_dim; }; -struct pplx_late_workspace { - const pplx_late_model_t *model; - pplx_workspace_t *base_ws; +struct embed_late_workspace { + const embed_late_model_t *model; + embed_workspace_t *base_ws; float *states; int states_seq_cap; }; @@ -28,13 +28,13 @@ struct pplx_late_workspace { * Globals * ======================================================================== */ -int pplx_verbose = 0; +int embed_verbose = 0; int qwen_verbose = 0; -#define PPLX_BF16_QKV_FUSE_MAX_SEQ 16 -#define PPLX_BF16_PAIR_FUSE_MAX_SEQ 4 -#define PPLX_MIN_WORKSPACE_SEQ_CAP 16 -#define PPLX_WORKSPACE_SEQ_GRANULARITY 16 +#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 /* ======================================================================== * Size helpers @@ -59,12 +59,12 @@ static int grow_cap(int current, int needed, int *out) if (needed <= 0) return -1; int cap = needed; - if (cap < PPLX_MIN_WORKSPACE_SEQ_CAP) - cap = PPLX_MIN_WORKSPACE_SEQ_CAP; + if (cap < EMBED_MIN_WORKSPACE_SEQ_CAP) + cap = EMBED_MIN_WORKSPACE_SEQ_CAP; - int rem = cap % PPLX_WORKSPACE_SEQ_GRANULARITY; + int rem = cap % EMBED_WORKSPACE_SEQ_GRANULARITY; if (rem != 0) { - int add = PPLX_WORKSPACE_SEQ_GRANULARITY - rem; + int add = EMBED_WORKSPACE_SEQ_GRANULARITY - rem; if (cap > INT_MAX - add) return -1; cap += add; } @@ -120,7 +120,7 @@ static int realloc_floats_2d(float **ptr, int rows, int cols) return realloc_floats(ptr, count); } -static int ensure_late_state_buffer(pplx_late_workspace_t *ws, int n_tokens) +static int ensure_late_state_buffer(embed_late_workspace_t *ws, int n_tokens) { if (!ws || !ws->model || n_tokens <= 0) return -1; if (n_tokens <= ws->states_seq_cap) return 0; @@ -185,18 +185,18 @@ static double json_get_double(const char *json, const char *key, double fallback return atof(v); } -static int parse_config(pplx_config_t *cfg, const char *model_dir) +static int parse_config(embed_config_t *cfg, const char *model_dir) { char path[1024]; int n = snprintf(path, sizeof(path), "%s/config.json", model_dir); if (n < 0 || (size_t)n >= sizeof(path)) { - fprintf(stderr, "pplx_model_load: model path too long\n"); + fprintf(stderr, "embed_model_load: model path too long\n"); return -1; } FILE *f = fopen(path, "rb"); if (!f) { - fprintf(stderr, "pplx_model_load: cannot open %s\n", path); + fprintf(stderr, "embed_model_load: cannot open %s\n", path); return -1; } @@ -220,9 +220,9 @@ static int parse_config(pplx_config_t *cfg, const char *model_dir) cfg->n_layers = json_get_int(buf, "num_hidden_layers", 0); cfg->n_heads = json_get_int(buf, "num_attention_heads", 0); cfg->n_kv_heads = json_get_int(buf, "num_key_value_heads", 0); - cfg->head_dim = json_get_int(buf, "head_dim", PPLX_HEAD_DIM); + cfg->head_dim = json_get_int(buf, "head_dim", EMBED_HEAD_DIM); cfg->intermediate_size = json_get_int(buf, "intermediate_size", 0); - cfg->vocab_size = json_get_int(buf, "vocab_size", PPLX_VOCAB_SIZE); + cfg->vocab_size = json_get_int(buf, "vocab_size", EMBED_VOCAB_SIZE); cfg->rms_norm_eps = (float)json_get_double(buf, "rms_norm_eps", 1e-6); cfg->rope_theta = (float)json_get_double(buf, "rope_theta", 1000000.0); @@ -245,19 +245,19 @@ static int parse_config(pplx_config_t *cfg, const char *model_dir) (cfg->head_dim & 1) || cfg->n_heads % cfg->n_kv_heads != 0 || !isfinite(cfg->rms_norm_eps) || cfg->rms_norm_eps <= 0.0f || !isfinite(cfg->rope_theta) || cfg->rope_theta <= 0.0f) { - fprintf(stderr, "pplx_model_load: invalid config in %s " + fprintf(stderr, "embed_model_load: invalid config in %s " "(hidden=%d, layers=%d, heads=%d/%d, head_dim=%d, inter=%d)\n", path, cfg->hidden_size, cfg->n_layers, cfg->n_heads, cfg->n_kv_heads, cfg->head_dim, cfg->intermediate_size); return -1; } - if (cfg->n_layers > PPLX_MAX_LAYERS) { - fprintf(stderr, "pplx_model_load: too many layers (%d > %d)\n", - cfg->n_layers, PPLX_MAX_LAYERS); + if (cfg->n_layers > EMBED_MAX_LAYERS) { + fprintf(stderr, "embed_model_load: too many layers (%d > %d)\n", + cfg->n_layers, EMBED_MAX_LAYERS); return -1; } - if (pplx_verbose >= 1) + if (embed_verbose >= 1) fprintf(stderr, "config: hidden=%d, layers=%d, heads=%d/%d, " "inter=%d, head_dim=%d\n", cfg->hidden_size, cfg->n_layers, @@ -299,12 +299,12 @@ static int tensor_has_supported_shape(const safetensors_file_t *sf, int bf16_ok) { if (t->dtype != DTYPE_F32 && !(bf16_ok && t->dtype == DTYPE_BF16)) { - fprintf(stderr, "pplx: unsupported dtype for %s: got %s, expected %s\n", + fprintf(stderr, "embed: unsupported dtype for %s: got %s, expected %s\n", name, dtype_name(t->dtype), bf16_ok ? "F32 or BF16" : "F32"); return 0; } if (t->ndim != ndim) { - fprintf(stderr, "pplx: bad rank for %s: got %d, expected %d\n", + fprintf(stderr, "embed: bad rank for %s: got %d, expected %d\n", name, t->ndim, ndim); return 0; } @@ -312,13 +312,13 @@ static int tensor_has_supported_shape(const safetensors_file_t *sf, size_t numel = 1; for (int i = 0; i < ndim; i++) { if (t->shape[i] != shape[i]) { - fprintf(stderr, "pplx: bad shape for %s at dim %d: " + fprintf(stderr, "embed: bad shape for %s at dim %d: " "got %lld, expected %lld\n", name, i, (long long)t->shape[i], (long long)shape[i]); return 0; } if (shape[i] < 0 || (uint64_t)shape[i] > SIZE_MAX / numel) { - fprintf(stderr, "pplx: shape too large for %s\n", name); + fprintf(stderr, "embed: shape too large for %s\n", name); return 0; } numel *= (size_t)shape[i]; @@ -326,18 +326,18 @@ static int tensor_has_supported_shape(const safetensors_file_t *sf, size_t elem_size = dtype_size(t->dtype); if (elem_size == 0) { - fprintf(stderr, "pplx: unsupported dtype for %s: got %s\n", + fprintf(stderr, "embed: unsupported dtype for %s: got %s\n", name, dtype_name(t->dtype)); return 0; } size_t bytes; if (mul_size(numel, elem_size, &bytes) != 0) { - fprintf(stderr, "pplx: tensor data too large for %s\n", name); + fprintf(stderr, "embed: tensor data too large for %s\n", name); return 0; } if (t->data_size != bytes) { - fprintf(stderr, "pplx: bad data size for %s: got %zu, expected %zu\n", + fprintf(stderr, "embed: bad data size for %s: got %zu, expected %zu\n", name, t->data_size, bytes); return 0; } @@ -346,21 +346,21 @@ static int tensor_has_supported_shape(const safetensors_file_t *sf, if (data_start > sf->file_size || t->data_offset > sf->file_size - data_start || t->data_size > sf->file_size - data_start - t->data_offset) { - fprintf(stderr, "pplx: tensor data out of bounds for %s\n", name); + fprintf(stderr, "embed: tensor data out of bounds for %s\n", name); return 0; } return 1; } -static pplx_weight_ref_t load_weight_direct(multi_safetensors_t *ms, +static embed_weight_ref_t load_weight_direct(multi_safetensors_t *ms, const char *name, const int64_t *shape, int ndim) { - pplx_weight_ref_t ref = { DTYPE_UNKNOWN, NULL }; + embed_weight_ref_t ref = { DTYPE_UNKNOWN, NULL }; safetensors_file_t *sf = NULL; const safetensor_t *t = multi_safetensors_find(ms, name, &sf); if (!t) { - fprintf(stderr, "pplx: tensor not found: %s\n", name); + fprintf(stderr, "embed: tensor not found: %s\n", name); return ref; } if (!tensor_has_supported_shape(sf, t, name, shape, ndim, 1)) @@ -377,7 +377,7 @@ static float *load_norm_f32(multi_safetensors_t *ms, const char *name, safetensors_file_t *sf = NULL; const safetensor_t *t = multi_safetensors_find(ms, name, &sf); if (!t) { - fprintf(stderr, "pplx: tensor not found: %s\n", name); + fprintf(stderr, "embed: tensor not found: %s\n", name); return NULL; } if (!tensor_has_supported_shape(sf, t, name, shape, ndim, 1)) @@ -393,7 +393,7 @@ static void bf16_row_to_f32(float *dst, const uint16_t *src, int n) } } -static void copy_weight_row(float *dst, const pplx_weight_ref_t *w, +static void copy_weight_row(float *dst, const embed_weight_ref_t *w, size_t row, int row_width) { if (w->dtype == DTYPE_F32) { @@ -405,7 +405,7 @@ static void copy_weight_row(float *dst, const pplx_weight_ref_t *w, } } -static int ensure_bf16_widen(pplx_workspace_t *ws, size_t count) +static int ensure_bf16_widen(embed_workspace_t *ws, size_t count) { if (ws->bf16_widen_count >= count) return 0; float *p = (float *)realloc(ws->bf16_widen, count * sizeof(float)); @@ -415,8 +415,8 @@ static int ensure_bf16_widen(pplx_workspace_t *ws, size_t count) return 0; } -static void linear_nobias_weight(pplx_workspace_t *ws, float *y, - const float *x, const pplx_weight_ref_t *w, +static void linear_nobias_weight(embed_workspace_t *ws, float *y, + const float *x, const embed_weight_ref_t *w, int seq_len, int in_dim, int out_dim) { if (w->dtype == DTYPE_F32) { @@ -439,16 +439,16 @@ static void linear_nobias_weight(pplx_workspace_t *ws, float *y, seq_len, in_dim, out_dim); } -static void linear_qkv_weight(pplx_workspace_t *ws, float *q, float *k, +static void linear_qkv_weight(embed_workspace_t *ws, float *q, float *k, float *v, const float *x, - const pplx_weight_ref_t *wq, - const pplx_weight_ref_t *wk, - const pplx_weight_ref_t *wv, + const embed_weight_ref_t *wq, + const embed_weight_ref_t *wk, + const embed_weight_ref_t *wv, int seq_len, int in_dim, int q_dim, int kv_dim) { if (wq->dtype == DTYPE_BF16 && wk->dtype == DTYPE_BF16 && wv->dtype == DTYPE_BF16 && - seq_len <= PPLX_BF16_QKV_FUSE_MAX_SEQ) { + seq_len <= EMBED_BF16_QKV_FUSE_MAX_SEQ) { qwen_linear_nobias_bf16_qkv(q, k, v, x, (const uint16_t *)wq->data, (const uint16_t *)wk->data, @@ -462,14 +462,14 @@ static void linear_qkv_weight(pplx_workspace_t *ws, float *q, float *k, linear_nobias_weight(ws, v, x, wv, seq_len, in_dim, kv_dim); } -static void linear_pair_weight(pplx_workspace_t *ws, float *a, float *b, +static void linear_pair_weight(embed_workspace_t *ws, float *a, float *b, const float *x, - const pplx_weight_ref_t *wa, - const pplx_weight_ref_t *wb, + const embed_weight_ref_t *wa, + const embed_weight_ref_t *wb, int seq_len, int in_dim, int a_dim, int b_dim) { if (wa->dtype == DTYPE_BF16 && wb->dtype == DTYPE_BF16 && - seq_len <= PPLX_BF16_PAIR_FUSE_MAX_SEQ) { + seq_len <= EMBED_BF16_PAIR_FUSE_MAX_SEQ) { qwen_linear_nobias_bf16_pair(a, b, x, (const uint16_t *)wa->data, (const uint16_t *)wb->data, @@ -485,7 +485,7 @@ static void linear_pair_weight(pplx_workspace_t *ws, float *a, float *b, * Working buffer management * ======================================================================== */ -static int ensure_buffers(pplx_workspace_t *ws, const pplx_config_t *c, int seq) +static int ensure_buffers(embed_workspace_t *ws, const embed_config_t *c, int seq) { if (seq <= ws->buf_seq_cap) return 0; @@ -512,7 +512,7 @@ static int ensure_buffers(pplx_workspace_t *ws, const pplx_config_t *c, int seq) return 0; } -static void ensure_attention_scores(pplx_workspace_t *ws, const int *offsets, +static void ensure_attention_scores(embed_workspace_t *ws, const int *offsets, int batch) { size_t bytes = @@ -529,7 +529,7 @@ static void ensure_attention_scores(pplx_workspace_t *ws, const int *offsets, * RoPE cache * ======================================================================== */ -static int ensure_rope_cache(pplx_workspace_t *ws, const pplx_config_t *cfg, +static int ensure_rope_cache(embed_workspace_t *ws, const embed_config_t *cfg, int n_pos) { if (n_pos <= ws->rope_cache_cap) return 0; @@ -569,7 +569,7 @@ static int ensure_rope_cache(pplx_workspace_t *ws, const pplx_config_t *cfg, return 0; } -static int ensure_offsets(pplx_workspace_t *ws, int batch) +static int ensure_offsets(embed_workspace_t *ws, int batch) { if (!ws || batch <= 0 || batch == INT_MAX) return -1; @@ -586,18 +586,18 @@ static int ensure_offsets(pplx_workspace_t *ws, int batch) * Model / workspace load and free * ======================================================================== */ -static pplx_model_t *model_load_range_ex(const char *model_dir, +static embed_model_t *model_load_range_ex(const char *model_dir, int layer_start, int layer_end, int allow_late) { if (!allow_late && model_dir_has_late_projection(model_dir)) { - fprintf(stderr, "pplx_model_load: late-interaction models require " + fprintf(stderr, "embed_model_load: late-interaction models require " "token-level MaxSim support and are not valid pooled " "embedding models yet\n"); return NULL; } - pplx_model_t *model = (pplx_model_t *)calloc(1, sizeof(pplx_model_t)); + embed_model_t *model = (embed_model_t *)calloc(1, sizeof(embed_model_t)); if (!model) return NULL; /* Parse config.json */ @@ -606,12 +606,12 @@ static pplx_model_t *model_load_range_ex(const char *model_dir, return NULL; } - const pplx_config_t *cfg = &model->config; + const embed_config_t *cfg = &model->config; if (layer_end == -1) layer_end = cfg->n_layers; if (layer_start < 0 || layer_start >= layer_end || layer_end > cfg->n_layers) { - fprintf(stderr, "pplx_model_load: invalid layer range [%d, %d) " + fprintf(stderr, "embed_model_load: invalid layer range [%d, %d) " "for %d-layer model\n", layer_start, layer_end, cfg->n_layers); free(model); return NULL; @@ -622,17 +622,17 @@ static pplx_model_t *model_load_range_ex(const char *model_dir, /* Open safetensors (handles single file or multi-shard) */ multi_safetensors_t *ms = multi_safetensors_open(model_dir); if (!ms) { - fprintf(stderr, "pplx_model_load: failed to open safetensors in %s\n", model_dir); + fprintf(stderr, "embed_model_load: failed to open safetensors in %s\n", model_dir); free(model); return NULL; } model->safetensors = ms; - if (pplx_verbose >= 1) - fprintf(stderr, "pplx_model_load: loading weights from %s\n", model_dir); + if (embed_verbose >= 1) + fprintf(stderr, "embed_model_load: loading weights from %s\n", model_dir); /* Only the first stage needs token embeddings. */ - pplx_weights_t *w = &model->weights; + embed_weights_t *w = &model->weights; if (layer_start == 0) { const int64_t embed_shape[2] = { cfg->vocab_size, cfg->hidden_size }; w->embed_tokens = load_weight_direct(ms, "embed_tokens.weight", @@ -641,13 +641,13 @@ static pplx_model_t *model_load_range_ex(const char *model_dir, } /* Allocate layer array */ - w->layers = (pplx_layer_t *)calloc(cfg->n_layers, sizeof(pplx_layer_t)); + w->layers = (embed_layer_t *)calloc(cfg->n_layers, sizeof(embed_layer_t)); if (!w->layers) goto fail; /* Per-layer weights */ char name[256]; for (int i = layer_start; i < layer_end; i++) { - pplx_layer_t *l = &w->layers[i]; + embed_layer_t *l = &w->layers[i]; #define LOAD_NORM(field, fmt, d0) do { \ const int64_t expect[1] = { (d0) }; \ @@ -689,7 +689,7 @@ static pplx_model_t *model_load_range_ex(const char *model_dir, #undef LOAD2 #undef LOAD_NORM - if (pplx_verbose >= 2) + if (embed_verbose >= 2) fprintf(stderr, " layer %d loaded\n", i); } @@ -700,29 +700,29 @@ static pplx_model_t *model_load_range_ex(const char *model_dir, if (!w->norm) goto fail; } - if (pplx_verbose >= 1) - fprintf(stderr, "pplx_model_load: layers [%d, %d) loaded " + if (embed_verbose >= 1) + fprintf(stderr, "embed_model_load: layers [%d, %d) loaded " "(%d-dim embeddings)\n", layer_start, layer_end, cfg->hidden_size); return model; fail: - pplx_model_free(model); + embed_model_free(model); return NULL; } -pplx_model_t *pplx_model_load(const char *model_dir) +embed_model_t *embed_model_load(const char *model_dir) { return model_load_range_ex(model_dir, 0, -1, 0); } -void pplx_model_free(pplx_model_t *model) +void embed_model_free(embed_model_t *model) { if (!model) return; if (model->weights.layers) { for (int i = 0; i < model->config.n_layers; i++) { - pplx_layer_t *l = &model->weights.layers[i]; + embed_layer_t *l = &model->weights.layers[i]; free((void *)l->q_norm); free((void *)l->k_norm); free((void *)l->input_norm); @@ -736,16 +736,16 @@ void pplx_model_free(pplx_model_t *model) free(model); } -pplx_workspace_t *pplx_workspace_new(const pplx_model_t *model) +embed_workspace_t *embed_workspace_new(const embed_model_t *model) { if (!model) return NULL; - pplx_workspace_t *ws = (pplx_workspace_t *)calloc(1, sizeof(pplx_workspace_t)); + embed_workspace_t *ws = (embed_workspace_t *)calloc(1, sizeof(embed_workspace_t)); if (!ws) return NULL; ws->model = model; return ws; } -void pplx_workspace_free(pplx_workspace_t *ws) +void embed_workspace_free(embed_workspace_t *ws) { if (!ws) return; free(ws->x); free(ws->x_norm); @@ -759,7 +759,7 @@ void pplx_workspace_free(pplx_workspace_t *ws) free(ws); } -const pplx_config_t *pplx_model_config(const pplx_model_t *model) +const embed_config_t *embed_model_config(const embed_model_t *model) { return model ? &model->config : NULL; } @@ -775,14 +775,14 @@ static const safetensor_t *find_single_safetensor(const safetensors_file_t *sf, return NULL; } -pplx_late_model_t *pplx_late_model_load(const char *model_dir) +embed_late_model_t *embed_late_model_load(const char *model_dir) { if (!model_dir_has_late_projection(model_dir)) { - fprintf(stderr, "pplx_late_model_load: missing 1_Dense projection\n"); + fprintf(stderr, "embed_late_model_load: missing 1_Dense projection\n"); return NULL; } - pplx_late_model_t *late = (pplx_late_model_t *)calloc(1, sizeof(*late)); + embed_late_model_t *late = (embed_late_model_t *)calloc(1, sizeof(*late)); if (!late) return NULL; late->base = model_load_range_ex(model_dir, 0, -1, 1); @@ -792,24 +792,24 @@ pplx_late_model_t *pplx_late_model_load(const char *model_dir) int n = snprintf(path, sizeof(path), "%s/1_Dense/model.safetensors", model_dir); if (n < 0 || (size_t)n >= sizeof(path)) { - fprintf(stderr, "pplx_late_model_load: model path too long\n"); + fprintf(stderr, "embed_late_model_load: model path too long\n"); goto fail; } late->dense_sf = safetensors_open(path); if (!late->dense_sf) { - fprintf(stderr, "pplx_late_model_load: cannot open %s\n", path); + fprintf(stderr, "embed_late_model_load: cannot open %s\n", path); goto fail; } const safetensor_t *t = find_single_safetensor(late->dense_sf, "linear.weight"); if (!t) { - fprintf(stderr, "pplx_late_model_load: missing linear.weight\n"); + fprintf(stderr, "embed_late_model_load: missing linear.weight\n"); goto fail; } if (t->ndim != 2 || t->shape[0] <= 0 || t->shape[0] > INT_MAX) { - fprintf(stderr, "pplx_late_model_load: bad projection shape\n"); + fprintf(stderr, "embed_late_model_load: bad projection shape\n"); goto fail; } @@ -824,25 +824,25 @@ pplx_late_model_t *pplx_late_model_load(const char *model_dir) return late; fail: - pplx_late_model_free(late); + embed_late_model_free(late); return NULL; } -void pplx_late_model_free(pplx_late_model_t *model) +void embed_late_model_free(embed_late_model_t *model) { if (!model) return; - pplx_model_free(model->base); + embed_model_free(model->base); safetensors_close(model->dense_sf); free(model); } -pplx_late_workspace_t *pplx_late_workspace_new(const pplx_late_model_t *model) +embed_late_workspace_t *embed_late_workspace_new(const embed_late_model_t *model) { if (!model || !model->base) return NULL; - pplx_late_workspace_t *ws = (pplx_late_workspace_t *)calloc(1, sizeof(*ws)); + embed_late_workspace_t *ws = (embed_late_workspace_t *)calloc(1, sizeof(*ws)); if (!ws) return NULL; ws->model = model; - ws->base_ws = pplx_workspace_new(model->base); + ws->base_ws = embed_workspace_new(model->base); if (!ws->base_ws) { free(ws); return NULL; @@ -850,26 +850,26 @@ pplx_late_workspace_t *pplx_late_workspace_new(const pplx_late_model_t *model) return ws; } -void pplx_late_workspace_free(pplx_late_workspace_t *ws) +void embed_late_workspace_free(embed_late_workspace_t *ws) { if (!ws) return; - pplx_workspace_free(ws->base_ws); + embed_workspace_free(ws->base_ws); free(ws->states); free(ws); } -const pplx_config_t *pplx_late_model_config(const pplx_late_model_t *model) +const embed_config_t *embed_late_model_config(const embed_late_model_t *model) { return model && model->base ? &model->base->config : NULL; } -int pplx_late_model_token_dim(const pplx_late_model_t *model) +int embed_late_model_token_dim(const embed_late_model_t *model) { return model ? model->token_dim : 0; } -int pplx_late_model_encode_tokens(const pplx_late_model_t *model, - pplx_late_workspace_t *ws, +int embed_late_model_encode_tokens(const embed_late_model_t *model, + embed_late_workspace_t *ws, const int *token_ids, int n_tokens, int normalize, float *out_vectors) { @@ -881,7 +881,7 @@ int pplx_late_model_encode_tokens(const pplx_late_model_t *model, int dim = model->token_dim; if (ensure_late_state_buffer(ws, n_tokens) != 0) return -1; - if (pplx_model_forward_into(model->base, ws->base_ws, token_ids, + if (embed_model_forward_into(model->base, ws->base_ws, token_ids, n_tokens, ws->states) != 0) return -1; @@ -889,14 +889,14 @@ int pplx_late_model_encode_tokens(const pplx_late_model_t *model, &model->projection, n_tokens, hidden, dim); if (normalize) { for (int i = 0; i < n_tokens; i++) { - if (pplx_l2_normalize(out_vectors + (size_t)i * dim, dim) != 0) + if (embed_l2_normalize(out_vectors + (size_t)i * dim, dim) != 0) return -1; } } return 0; } -float pplx_late_maxsim(const float *query_vectors, int query_tokens, +float embed_late_maxsim(const float *query_vectors, int query_tokens, const float *doc_vectors, int doc_tokens, int dim) { @@ -918,7 +918,7 @@ float pplx_late_maxsim(const float *query_vectors, int query_tokens, return score; } -int pplx_late_maxsim_batch(const float *query_vectors, int query_tokens, +int embed_late_maxsim_batch(const float *query_vectors, int query_tokens, const float *doc_vectors, const int *doc_offsets, int docs, int dim, float *scores) @@ -990,22 +990,22 @@ int pplx_late_maxsim_batch(const float *query_vectors, int query_tokens, for (int i = 0; i < docs; i++) { int start = doc_offsets[i]; int end = doc_offsets[i + 1]; - scores[i] = pplx_late_maxsim(query_vectors, query_tokens, + scores[i] = embed_late_maxsim(query_vectors, query_tokens, doc_vectors + (size_t)start * dim, end - start, dim); } return 0; } -size_t pplx_workspace_nbytes(const pplx_workspace_t *ws) +size_t embed_workspace_nbytes(const embed_workspace_t *ws) { if (!ws) return 0; size_t total = sizeof(*ws); - const pplx_model_t *model = ws->model; + const embed_model_t *model = ws->model; if (!model) return total; - const pplx_config_t *c = &model->config; + const embed_config_t *c = &model->config; size_t row_floats = 0; const int dims[] = { c->hidden_size, /* x */ @@ -1068,7 +1068,7 @@ size_t pplx_workspace_nbytes(const pplx_workspace_t *ws) * Packed forward pass * ======================================================================== */ -static int build_offsets(const pplx_input_t *inputs, int batch, int require_ids, +static int build_offsets(const embed_input_t *inputs, int batch, int require_ids, int *offsets, int *total_out, int *max_out) { if (!inputs || batch <= 0 || !offsets || !total_out || !max_out) @@ -1092,9 +1092,9 @@ static int build_offsets(const pplx_input_t *inputs, int batch, int require_ids, return 0; } -static int forward_packed_slice_inplace(const pplx_model_t *model, - pplx_workspace_t *ws, - const pplx_input_t *inputs, +static int forward_packed_slice_inplace(const embed_model_t *model, + embed_workspace_t *ws, + const embed_input_t *inputs, int batch, const int *offsets, int total_seq, int max_seq, const float *input_states, @@ -1105,8 +1105,8 @@ static int forward_packed_slice_inplace(const pplx_model_t *model, batch <= 0 || !offsets || total_seq <= 0 || max_seq <= 0) return -1; - const pplx_config_t *cfg = &model->config; - const pplx_weights_t *w = &model->weights; + const embed_config_t *cfg = &model->config; + const embed_weights_t *w = &model->weights; if (layer_start < 0 || layer_start > layer_end || layer_end > cfg->n_layers || layer_start < model->layer_start || layer_end > model->layer_end || @@ -1150,7 +1150,7 @@ static int forward_packed_slice_inplace(const pplx_model_t *model, for (int i = 0; i < inputs[b].n_tokens; i++) { int id = inputs[b].ids[i]; if (id < 0 || id >= cfg->vocab_size) { - fprintf(stderr, "pplx: invalid token id %d at input %d position %d\n", + fprintf(stderr, "embed: invalid token id %d at input %d position %d\n", id, b, i); return -1; } @@ -1167,7 +1167,7 @@ static int forward_packed_slice_inplace(const pplx_model_t *model, /* 2. Transformer layers. Linear/MLP ops run over all packed tokens; * attention is explicitly block-diagonal by sequence offsets. */ for (int layer = layer_start; layer < layer_end; layer++) { - const pplx_layer_t *l = &w->layers[layer]; + const embed_layer_t *l = &w->layers[layer]; qwen_rms_norm(x_norm, x, l->input_norm, total_seq, hidden, eps); @@ -1210,9 +1210,9 @@ static int forward_packed_slice_inplace(const pplx_model_t *model, return 0; } -static int forward_packed_inplace(const pplx_model_t *model, - pplx_workspace_t *ws, - const pplx_input_t *inputs, +static int forward_packed_inplace(const embed_model_t *model, + embed_workspace_t *ws, + const embed_input_t *inputs, int batch, const int *offsets, int total_seq, int max_seq, int apply_final_norm) @@ -1223,12 +1223,12 @@ static int forward_packed_inplace(const pplx_model_t *model, apply_final_norm); } -static void pool_embeddings(const pplx_model_t *model, - const pplx_workspace_t *ws, +static void pool_embeddings(const embed_model_t *model, + const embed_workspace_t *ws, const int *offsets, int batch, float *out_embeddings) { - const pplx_config_t *cfg = &model->config; + const embed_config_t *cfg = &model->config; int hidden = cfg->hidden_size; float eps = cfg->rms_norm_eps; const float *norm_weight = model->weights.norm; @@ -1264,7 +1264,7 @@ static void pool_embeddings(const pplx_model_t *model, * Public forward/embed APIs * ======================================================================== */ -int pplx_model_forward_into(const pplx_model_t *model, pplx_workspace_t *ws, +int embed_model_forward_into(const embed_model_t *model, embed_workspace_t *ws, const int *token_ids, int n_tokens, float *out_states) { @@ -1276,7 +1276,7 @@ int pplx_model_forward_into(const pplx_model_t *model, pplx_workspace_t *ws, mul_size(count, sizeof(float), &bytes) != 0) return -1; - pplx_input_t input = { token_ids, n_tokens }; + embed_input_t input = { token_ids, n_tokens }; int offsets[2] = { 0, n_tokens }; if (forward_packed_inplace(model, ws, &input, 1, offsets, n_tokens, n_tokens, 1) != 0) @@ -1286,8 +1286,8 @@ int pplx_model_forward_into(const pplx_model_t *model, pplx_workspace_t *ws, return 0; } -int pplx_pool_spans(const pplx_config_t *cfg, const float *states, int n_tokens, - const pplx_span_t *spans, int n_spans, +int embed_pool_spans(const embed_config_t *cfg, const float *states, int n_tokens, + const embed_span_t *spans, int n_spans, float *out_embeddings) { if (!cfg || !states || n_tokens <= 0 || !spans || n_spans <= 0 || @@ -1320,24 +1320,24 @@ int pplx_pool_spans(const pplx_config_t *cfg, const float *states, int n_tokens, return 0; } -int pplx_model_embed_spans_batch(const pplx_model_t *model, - pplx_workspace_t *ws, - const pplx_context_input_t *inputs, int batch, +int embed_model_encode_spans_batch(const embed_model_t *model, + embed_workspace_t *ws, + const embed_context_input_t *inputs, int batch, float *out_embeddings) { if (!model || !ws || !inputs || batch <= 0 || !out_embeddings) return -1; if (ensure_offsets(ws, batch) != 0) return -1; - if ((size_t)batch > SIZE_MAX / sizeof(pplx_input_t)) return -1; - pplx_input_t *packed_inputs = malloc((size_t)batch * sizeof(*packed_inputs)); + if ((size_t)batch > SIZE_MAX / sizeof(embed_input_t)) return -1; + embed_input_t *packed_inputs = malloc((size_t)batch * sizeof(*packed_inputs)); if (!packed_inputs) return -1; int *offsets = ws->offsets; int total_seq = 0; int max_seq = 0; int total_spans = 0; for (int b = 0; b < batch; b++) { - const pplx_context_input_t *input = &inputs[b]; + const embed_context_input_t *input = &inputs[b]; int n_tokens = input->input.n_tokens; if (!input->input.ids || n_tokens <= 0 || !input->spans || input->n_spans <= 0 || total_seq > INT_MAX - n_tokens || @@ -1372,8 +1372,8 @@ int pplx_model_embed_spans_batch(const pplx_model_t *model, int span_offset = 0; int hidden = model->config.hidden_size; for (int b = 0; b < batch; b++) { - const pplx_context_input_t *input = &inputs[b]; - if (pplx_pool_spans(&model->config, + const embed_context_input_t *input = &inputs[b]; + if (embed_pool_spans(&model->config, ws->x + (size_t)offsets[b] * hidden, input->input.n_tokens, input->spans, input->n_spans, out_embeddings + (size_t)span_offset * hidden) != 0) @@ -1383,20 +1383,20 @@ int pplx_model_embed_spans_batch(const pplx_model_t *model, return 0; } -int pplx_model_embed_spans(const pplx_model_t *model, pplx_workspace_t *ws, +int embed_model_encode_spans(const embed_model_t *model, embed_workspace_t *ws, const int *token_ids, int n_tokens, - const pplx_span_t *spans, int n_spans, + const embed_span_t *spans, int n_spans, float *out_embeddings) { - pplx_context_input_t input = { + embed_context_input_t input = { .input = { token_ids, n_tokens }, .spans = spans, .n_spans = n_spans, }; - return pplx_model_embed_spans_batch(model, ws, &input, 1, out_embeddings); + return embed_model_encode_spans_batch(model, ws, &input, 1, out_embeddings); } -float *pplx_model_forward(const pplx_model_t *model, pplx_workspace_t *ws, +float *embed_model_forward(const embed_model_t *model, embed_workspace_t *ws, const int *token_ids, int n_tokens) { if (!model || !ws || !token_ids || n_tokens <= 0) return NULL; @@ -1407,15 +1407,15 @@ float *pplx_model_forward(const pplx_model_t *model, pplx_workspace_t *ws, float *out = malloc_floats(n); if (!out) return NULL; - if (pplx_model_forward_into(model, ws, token_ids, n_tokens, out) != 0) { + if (embed_model_forward_into(model, ws, token_ids, n_tokens, out) != 0) { free(out); return NULL; } return out; } -int pplx_model_embed_batch(const pplx_model_t *model, pplx_workspace_t *ws, - const pplx_input_t *inputs, int batch, +int embed_model_encode_batch(const embed_model_t *model, embed_workspace_t *ws, + const embed_input_t *inputs, int batch, float *out_embeddings) { if (!model || !ws || !inputs || batch <= 0 || !out_embeddings) return -1; @@ -1436,7 +1436,7 @@ int pplx_model_embed_batch(const pplx_model_t *model, pplx_workspace_t *ws, return rc; } -int pplx_pool_batch(const pplx_config_t *cfg, const float *states, +int embed_pool_batch(const embed_config_t *cfg, const float *states, const int *seq_lengths, int batch, float *out_embeddings) { @@ -1466,15 +1466,15 @@ int pplx_pool_batch(const pplx_config_t *cfg, const float *states, return 0; } -int pplx_model_embed_into(const pplx_model_t *model, pplx_workspace_t *ws, +int embed_model_encode_into(const embed_model_t *model, embed_workspace_t *ws, const int *token_ids, int n_tokens, float *out_embedding) { - pplx_input_t input = { token_ids, n_tokens }; - return pplx_model_embed_batch(model, ws, &input, 1, out_embedding); + embed_input_t input = { token_ids, n_tokens }; + return embed_model_encode_batch(model, ws, &input, 1, out_embedding); } -float *pplx_model_embed(const pplx_model_t *model, pplx_workspace_t *ws, +float *embed_model_encode(const embed_model_t *model, embed_workspace_t *ws, const int *token_ids, int n_tokens) { if (!model || !ws || !token_ids || n_tokens <= 0) return NULL; @@ -1483,7 +1483,7 @@ float *pplx_model_embed(const pplx_model_t *model, pplx_workspace_t *ws, float *emb = malloc_floats((size_t)hidden); if (!emb) return NULL; - if (pplx_model_embed_into(model, ws, token_ids, n_tokens, emb) != 0) { + if (embed_model_encode_into(model, ws, token_ids, n_tokens, emb) != 0) { free(emb); return NULL; } @@ -1494,7 +1494,7 @@ float *pplx_model_embed(const pplx_model_t *model, pplx_workspace_t *ws, * Vector helpers * ======================================================================== */ -int pplx_l2_normalize(float *vec, int dim) +int embed_l2_normalize(float *vec, int dim) { if (!vec || dim <= 0) return -1; @@ -1510,7 +1510,7 @@ int pplx_l2_normalize(float *vec, int dim) return 0; } -float pplx_cosine_similarity(const float *a, const float *b, int dim) +float embed_cosine_similarity(const float *a, const float *b, int dim) { if (!a || !b || dim <= 0) return 0.0f; @@ -1,18 +1,18 @@ /* - * embed.h - pplx-embed API + * embed.h - embed API */ -#ifndef PPLX_EMBED_H -#define PPLX_EMBED_H +#ifndef EMBED_H +#define EMBED_H /* Public-API export annotation. The libraries are built with - * -fvisibility=hidden, so only declarations carrying PPLX_API are exported + * -fvisibility=hidden, so only declarations carrying EMBED_API are exported * from the shared library; everything else stays internal. */ -#ifndef PPLX_API +#ifndef EMBED_API #if defined(__GNUC__) -#define PPLX_API __attribute__((visibility("default"))) +#define EMBED_API __attribute__((visibility("default"))) #else -#define PPLX_API +#define EMBED_API #endif #endif @@ -23,10 +23,10 @@ * Shared constants (identical across all models) * ======================================================================== */ -#define PPLX_VOCAB_SIZE 151936 -#define PPLX_HEAD_DIM 128 -#define PPLX_MAX_LAYERS 64 /* upper bound for stack arrays */ -#define PPLX_CONTEXT_SEPARATOR_TOKEN_ID 151643 /* <|endoftext|> */ +#define EMBED_VOCAB_SIZE 151936 +#define EMBED_HEAD_DIM 128 +#define EMBED_MAX_LAYERS 64 /* upper bound for stack arrays */ +#define EMBED_CONTEXT_SEPARATOR_TOKEN_ID 151643 /* <|endoftext|> */ /* ======================================================================== * Model Configuration (populated from config.json at load time) @@ -44,32 +44,32 @@ typedef struct { int vocab_size; /* 151936 */ float rms_norm_eps; /* 1e-6 */ float rope_theta; /* 1e6 */ -} pplx_config_t; +} embed_config_t; /* ======================================================================== * Model + workspace ownership * ======================================================================== */ -typedef struct pplx_model pplx_model_t; -typedef struct pplx_workspace pplx_workspace_t; -typedef struct pplx_late_model pplx_late_model_t; -typedef struct pplx_late_workspace pplx_late_workspace_t; +typedef struct embed_model embed_model_t; +typedef struct embed_workspace embed_workspace_t; +typedef struct embed_late_model embed_late_model_t; +typedef struct embed_late_workspace embed_late_workspace_t; typedef struct { const int *ids; int n_tokens; -} pplx_input_t; +} embed_input_t; typedef struct { int start; int n_tokens; -} pplx_span_t; +} embed_span_t; typedef struct { - pplx_input_t input; - const pplx_span_t *spans; + embed_input_t input; + const embed_span_t *spans; int n_spans; -} pplx_context_input_t; +} embed_context_input_t; /* ======================================================================== * API @@ -84,19 +84,19 @@ typedef struct { * Reads config.json to determine model size (0.6B vs 4B). * Returns NULL on error. */ -PPLX_API pplx_model_t *pplx_model_load(const char *model_dir); +EMBED_API embed_model_t *embed_model_load(const char *model_dir); -PPLX_API void pplx_model_free(pplx_model_t *model); +EMBED_API void embed_model_free(embed_model_t *model); /* * Allocate/free mutable scratch buffers for a model. The model must outlive * workspaces created from it. */ -PPLX_API pplx_workspace_t *pplx_workspace_new(const pplx_model_t *model); -PPLX_API void pplx_workspace_free(pplx_workspace_t *ws); +EMBED_API embed_workspace_t *embed_workspace_new(const embed_model_t *model); +EMBED_API void embed_workspace_free(embed_workspace_t *ws); /* Config accessor for opaque model ownership. */ -PPLX_API const pplx_config_t *pplx_model_config(const pplx_model_t *model); +EMBED_API const embed_config_t *embed_model_config(const embed_model_t *model); /* * Current mutable workspace memory owned by this workspace, in bytes. @@ -104,7 +104,7 @@ PPLX_API const pplx_config_t *pplx_model_config(const pplx_model_t *model); * This includes the workspace struct, scratch buffers, and RoPE cache capacity. * It does not include immutable mmap'd model weights. */ -PPLX_API size_t pplx_workspace_nbytes(const pplx_workspace_t *ws); +EMBED_API size_t embed_workspace_nbytes(const embed_workspace_t *ws); /* * Compute embedding for a token sequence. @@ -120,14 +120,14 @@ PPLX_API size_t pplx_workspace_nbytes(const pplx_workspace_t *ws); * * Returns malloc'd float[hidden_size] (caller frees). NULL on error. */ -PPLX_API float *pplx_model_embed(const pplx_model_t *model, pplx_workspace_t *ws, +EMBED_API float *embed_model_encode(const embed_model_t *model, embed_workspace_t *ws, const int *token_ids, int n_tokens); /* * Compute one embedding into caller-provided out[hidden_size]. * Returns 0 on success, -1 on error. */ -PPLX_API int pplx_model_embed_into(const pplx_model_t *model, pplx_workspace_t *ws, +EMBED_API int embed_model_encode_into(const embed_model_t *model, embed_workspace_t *ws, const int *token_ids, int n_tokens, float *out_embedding); @@ -140,8 +140,8 @@ PPLX_API int pplx_model_embed_into(const pplx_model_t *model, pplx_workspace_t * * * Returns 0 on success, -1 on error. */ -PPLX_API int pplx_model_embed_batch(const pplx_model_t *model, pplx_workspace_t *ws, - const pplx_input_t *inputs, int batch, +EMBED_API int embed_model_encode_batch(const embed_model_t *model, embed_workspace_t *ws, + const embed_input_t *inputs, int batch, float *out_embeddings); /* @@ -150,7 +150,7 @@ PPLX_API int pplx_model_embed_batch(const pplx_model_t *model, pplx_workspace_t * states is [sum(n_tokens), hidden_size]. seq_lengths contains one positive * token count per sequence. The states must already include final RMSNorm. */ -PPLX_API int pplx_pool_batch(const pplx_config_t *cfg, const float *states, +EMBED_API int embed_pool_batch(const embed_config_t *cfg, const float *states, const int *seq_lengths, int batch, float *out_embeddings); @@ -162,14 +162,14 @@ PPLX_API int pplx_pool_batch(const pplx_config_t *cfg, const float *states, * Useful for contextual (late-chunking) models where you need * per-token embeddings before splitting by separator positions. */ -PPLX_API float *pplx_model_forward(const pplx_model_t *model, pplx_workspace_t *ws, +EMBED_API float *embed_model_forward(const embed_model_t *model, embed_workspace_t *ws, const int *token_ids, int n_tokens); /* * Run the transformer forward pass into caller-provided * out_states[n_tokens * hidden_size]. Returns 0 on success, -1 on error. */ -PPLX_API int pplx_model_forward_into(const pplx_model_t *model, pplx_workspace_t *ws, +EMBED_API int embed_model_forward_into(const embed_model_t *model, embed_workspace_t *ws, const int *token_ids, int n_tokens, float *out_states); @@ -177,20 +177,20 @@ PPLX_API int pplx_model_forward_into(const pplx_model_t *model, pplx_workspace_t * Pool embeddings from final hidden states. * * states is [n_tokens, hidden_size], normally produced by - * pplx_model_forward_into(). Each span is mean pooled into + * embed_model_forward_into(). Each span is mean pooled into * out_embeddings[n_spans, hidden_size] without L2 normalization. */ -PPLX_API int pplx_pool_spans(const pplx_config_t *cfg, const float *states, int n_tokens, - const pplx_span_t *spans, int n_spans, +EMBED_API int embed_pool_spans(const embed_config_t *cfg, const float *states, int n_tokens, + const embed_span_t *spans, int n_spans, float *out_embeddings); /* * Run one contextual sequence and pool selected token spans. * Returns 0 on success, -1 on error. */ -PPLX_API int pplx_model_embed_spans(const pplx_model_t *model, pplx_workspace_t *ws, +EMBED_API int embed_model_encode_spans(const embed_model_t *model, embed_workspace_t *ws, const int *token_ids, int n_tokens, - const pplx_span_t *spans, int n_spans, + const embed_span_t *spans, int n_spans, float *out_embeddings); /* @@ -199,9 +199,9 @@ PPLX_API int pplx_model_embed_spans(const pplx_model_t *model, pplx_workspace_t * Documents attend independently, RoPE positions restart for each document, * and out_embeddings contains spans in document order. */ -PPLX_API int pplx_model_embed_spans_batch(const pplx_model_t *model, - pplx_workspace_t *ws, - const pplx_context_input_t *inputs, int batch, +EMBED_API int embed_model_encode_spans_batch(const embed_model_t *model, + embed_workspace_t *ws, + const embed_context_input_t *inputs, int batch, float *out_embeddings); /* @@ -210,13 +210,13 @@ PPLX_API int pplx_model_embed_spans_batch(const pplx_model_t *model, * Returns 0 on success. Returns -1 for invalid arguments or a zero-length * vector, which cannot be normalized. */ -PPLX_API int pplx_l2_normalize(float *vec, int dim); +EMBED_API int embed_l2_normalize(float *vec, int dim); /* * Cosine similarity between two vectors. * Returns 0 for invalid arguments or if either vector has zero length. */ -PPLX_API float pplx_cosine_similarity(const float *a, const float *b, int dim); +EMBED_API float embed_cosine_similarity(const float *a, const float *b, int dim); /* * Late-interaction model API. @@ -225,29 +225,29 @@ PPLX_API float pplx_cosine_similarity(const float *a, const float *b, int dim); * projected by the snapshot's 1_Dense head and L2-normalized per token by * default, matching ColBERT/MaxSim scoring semantics. */ -PPLX_API pplx_late_model_t *pplx_late_model_load(const char *model_dir); -PPLX_API void pplx_late_model_free(pplx_late_model_t *model); +EMBED_API embed_late_model_t *embed_late_model_load(const char *model_dir); +EMBED_API void embed_late_model_free(embed_late_model_t *model); -PPLX_API pplx_late_workspace_t *pplx_late_workspace_new(const pplx_late_model_t *model); -PPLX_API void pplx_late_workspace_free(pplx_late_workspace_t *ws); +EMBED_API embed_late_workspace_t *embed_late_workspace_new(const embed_late_model_t *model); +EMBED_API void embed_late_workspace_free(embed_late_workspace_t *ws); -PPLX_API const pplx_config_t *pplx_late_model_config(const pplx_late_model_t *model); -PPLX_API int pplx_late_model_token_dim(const pplx_late_model_t *model); +EMBED_API const embed_config_t *embed_late_model_config(const embed_late_model_t *model); +EMBED_API int embed_late_model_token_dim(const embed_late_model_t *model); /* * Encode token_ids into out_vectors[n_tokens, token_dim]. * * If normalize is non-zero, every non-zero token vector is L2-normalized. */ -PPLX_API int pplx_late_model_encode_tokens(const pplx_late_model_t *model, - pplx_late_workspace_t *ws, +EMBED_API int embed_late_model_encode_tokens(const embed_late_model_t *model, + embed_late_workspace_t *ws, const int *token_ids, int n_tokens, int normalize, float *out_vectors); /* * Sum over query tokens of max dot product against document tokens. */ -PPLX_API float pplx_late_maxsim(const float *query_vectors, int query_tokens, +EMBED_API float embed_late_maxsim(const float *query_vectors, int query_tokens, const float *doc_vectors, int doc_tokens, int dim); @@ -258,13 +258,13 @@ PPLX_API float pplx_late_maxsim(const float *query_vectors, int query_tokens, * doc_vectors[doc_offsets[i] * dim .. doc_offsets[i + 1] * dim). * scores is caller-provided [docs]. */ -PPLX_API int pplx_late_maxsim_batch(const float *query_vectors, int query_tokens, +EMBED_API int embed_late_maxsim_batch(const float *query_vectors, int query_tokens, const float *doc_vectors, const int *doc_offsets, int docs, int dim, float *scores); /* Verbose level: 0=quiet, 1=info, 2=debug */ -PPLX_API extern int pplx_verbose; -PPLX_API extern int qwen_verbose; +EMBED_API extern int embed_verbose; +EMBED_API extern int qwen_verbose; -#endif /* PPLX_EMBED_H */ +#endif /* EMBED_H */ diff --git a/embed_cli.c b/embed_cli.c index 10f992d..03121a2 100644 --- a/embed_cli.c +++ b/embed_cli.c @@ -1,4 +1,4 @@ -/* embed_cli.c - pplx-embed command-line tool */ +/* embed_cli.c - embed command-line tool */ #include "embed.h" #include "qwen_kernels.h" @@ -70,13 +70,13 @@ static void print_usage(const char *prog) * ======================================================================== */ typedef struct { - pplx_model_t *model; - pplx_workspace_t *workspace; + embed_model_t *model; + embed_workspace_t *workspace; #ifdef USE_MLX - pplx_mlx_ctx_t *mlx_ctx; + embed_mlx_ctx_t *mlx_ctx; #endif #ifdef USE_CUDA - pplx_cuda_ctx_t *cuda_ctx; + embed_cuda_ctx_t *cuda_ctx; #endif qwen_tokenizer_t *tok; qwen_tokenizer_workspace_t *tok_ws; @@ -88,18 +88,18 @@ typedef struct { int n_tokens; } token_buf_t; -static int engine_embed_batch(engine_t *e, const pplx_input_t *inputs, +static int engine_embed_batch(engine_t *e, const embed_input_t *inputs, int batch, float *out_embeddings) { #ifdef USE_MLX if (e->mlx_ctx) - return pplx_mlx_embed_batch(e->mlx_ctx, inputs, batch, out_embeddings); + return embed_mlx_encode_batch(e->mlx_ctx, inputs, batch, out_embeddings); #endif #ifdef USE_CUDA if (e->cuda_ctx) - return pplx_cuda_embed_batch(e->cuda_ctx, inputs, batch, out_embeddings); + return embed_cuda_encode_batch(e->cuda_ctx, inputs, batch, out_embeddings); #endif - return pplx_model_embed_batch(e->model, e->workspace, + return embed_model_encode_batch(e->model, e->workspace, inputs, batch, out_embeddings); } @@ -116,7 +116,7 @@ static int tokenize_text(engine_t *e, const char *text, token_buf_t *out) return -1; } - if (pplx_verbose >= 1) { + if (embed_verbose >= 1) { fprintf(stderr, "tokens (%d): ", out->n_tokens); for (int i = 0; i < out->n_tokens && i < 20; i++) fprintf(stderr, "%d ", out->ids[i]); @@ -148,7 +148,7 @@ static void print_embedding_raw(const float *emb, int dim) static size_t engine_workspace_nbytes(const engine_t *e) { - return (e && e->workspace) ? pplx_workspace_nbytes(e->workspace) : 0; + return (e && e->workspace) ? embed_workspace_nbytes(e->workspace) : 0; } static void print_embedding_json(const float *emb, int dim, int n_tokens, @@ -173,7 +173,7 @@ static int process_stdin_batch(engine_t *e, char **lines, int n_lines) if (n_lines <= 0) return 0; token_buf_t *tokens = (token_buf_t *)calloc((size_t)n_lines, sizeof(token_buf_t)); - pplx_input_t *inputs = (pplx_input_t *)malloc((size_t)n_lines * sizeof(pplx_input_t)); + embed_input_t *inputs = (embed_input_t *)malloc((size_t)n_lines * sizeof(embed_input_t)); int *input_to_line = (int *)malloc((size_t)n_lines * sizeof(int)); float *embs = (float *)malloc((size_t)n_lines * e->dim * sizeof(float)); if (!tokens || !inputs || !input_to_line || !embs) { @@ -201,7 +201,7 @@ static int process_stdin_batch(engine_t *e, char **lines, int n_lines) double t0 = now_ms(); rc = engine_embed_batch(e, inputs, n_inputs, embs); ms = now_ms() - t0; - if (pplx_verbose >= 1) + if (embed_verbose >= 1) fprintf(stderr, "embed batch: %d texts in %.1f ms\n", n_inputs, ms); } @@ -238,7 +238,7 @@ static int run_stdin(engine_t *e, int batch_size) char line[65536]; if (batch_size <= 0) batch_size = 1; - if (pplx_verbose >= 1) + if (embed_verbose >= 1) fprintf(stderr, "stdin: ready, reading from stdin (batch_size=%d)\n", batch_size); @@ -257,7 +257,7 @@ static int run_stdin(engine_t *e, int batch_size) line[--len] = '\0'; if (len == 0) continue; - if (pplx_verbose >= 1) + if (embed_verbose >= 1) fprintf(stderr, "stdin: \"%.*s%s\"\n", (int)(len > 60 ? 60 : len), line, len > 60 ? "..." : ""); @@ -288,7 +288,7 @@ static int run_stdin(engine_t *e, int batch_size) } free(batch_lines); - if (pplx_verbose >= 1) + if (embed_verbose >= 1) fprintf(stderr, "stdin: EOF\n"); return rc; @@ -346,7 +346,7 @@ static int run_batch(engine_t *e, int argc, char **argv, int arg_start, int dim = e->dim; token_buf_t *tokens = (token_buf_t *)calloc((size_t)n_texts, sizeof(token_buf_t)); - pplx_input_t *inputs = (pplx_input_t *)malloc((size_t)n_texts * sizeof(pplx_input_t)); + embed_input_t *inputs = (embed_input_t *)malloc((size_t)n_texts * sizeof(embed_input_t)); float *embs = (float *)malloc((size_t)n_texts * dim * sizeof(float)); if (!tokens || !inputs || !embs) { fprintf(stderr, "OOM\n"); @@ -357,7 +357,7 @@ static int run_batch(engine_t *e, int argc, char **argv, int arg_start, } for (int i = 0; i < n_texts; i++) { - if (pplx_verbose >= 1) + if (embed_verbose >= 1) fprintf(stderr, "[%d/%d] \"%s\"\n", i+1, n_texts, texts[i]); if (tokenize_text(e, texts[i], &tokens[i]) != 0) { rc = 1; @@ -379,7 +379,7 @@ static int run_batch(engine_t *e, int argc, char **argv, int arg_start, rc = 1; goto done; } - if (pplx_verbose >= 1) + if (embed_verbose >= 1) fprintf(stderr, "embed batch: [%d..%d] %d texts in %.1f ms\n", start, start + cur - 1, cur, now_ms() - t0); } @@ -394,7 +394,7 @@ static int run_batch(engine_t *e, int argc, char **argv, int arg_start, for (int i = 0; i < n_texts; i++) { printf("[%d] ", i); for (int j = 0; j < n_texts; j++) - printf(" %.4f", (double)pplx_cosine_similarity( + printf(" %.4f", (double)embed_cosine_similarity( embs + (size_t)i * dim, embs + (size_t)j * dim, dim)); printf(" \"%s\"\n", texts[i]); } @@ -527,7 +527,7 @@ int main(int argc, char *argv[]) arg_start++; } - pplx_verbose = verbose; + embed_verbose = verbose; qwen_verbose = verbose; if (cuda_fast_gemm) { @@ -536,7 +536,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "--cuda-gemm-mode requires --cuda or --backend cuda\n"); return 1; } - if (pplx_cuda_set_fast_gemm(cuda_fast_gemm) != 0) { + if (embed_cuda_set_fast_gemm(cuda_fast_gemm) != 0) { fprintf(stderr, "--cuda-gemm-mode must be f32, tf32, bf16, or 16f\n"); return 1; } @@ -553,9 +553,9 @@ int main(int argc, char *argv[]) return 1; } if (!strcmp(cuda_weights, "bf16")) { - pplx_cuda_set_weights_bf16(1); + embed_cuda_set_weights_bf16(1); } else if (!strcmp(cuda_weights, "f32")) { - pplx_cuda_set_weights_bf16(0); + embed_cuda_set_weights_bf16(0); } else { fprintf(stderr, "--cuda-weight-dtype must be f32 or bf16\n"); return 1; @@ -602,45 +602,45 @@ int main(int argc, char *argv[]) #ifdef USE_MLX if (use_mlx) { - pplx_mlx_options_t mlx_opts = { + embed_mlx_options_t mlx_opts = { .quantize_bits = mlx_quantize_bits, .quantize_group_size = mlx_quantize_group_size, }; - e.mlx_ctx = pplx_mlx_load_with_options(model_dir, &mlx_opts); + e.mlx_ctx = embed_mlx_load_with_options(model_dir, &mlx_opts); if (!e.mlx_ctx) { fprintf(stderr, "failed to load model\n"); qwen_tokenizer_free(tok); return 1; } - e.dim = pplx_mlx_config(e.mlx_ctx)->hidden_size; + e.dim = embed_mlx_config(e.mlx_ctx)->hidden_size; } else #endif #ifdef USE_CUDA if (use_cuda) { - e.cuda_ctx = pplx_cuda_load(model_dir); + e.cuda_ctx = embed_cuda_load(model_dir); if (!e.cuda_ctx) { fprintf(stderr, "failed to load CUDA model\n"); qwen_tokenizer_free(tok); return 1; } - e.dim = pplx_cuda_config(e.cuda_ctx)->hidden_size; + e.dim = embed_cuda_config(e.cuda_ctx)->hidden_size; } else #endif { - e.model = pplx_model_load(model_dir); + e.model = embed_model_load(model_dir); if (!e.model) { fprintf(stderr, "failed to load model\n"); qwen_tokenizer_free(tok); return 1; } - e.workspace = pplx_workspace_new(e.model); + e.workspace = embed_workspace_new(e.model); if (!e.workspace) { fprintf(stderr, "failed to allocate workspace\n"); - pplx_model_free(e.model); + embed_model_free(e.model); qwen_tokenizer_free(tok); return 1; } - e.dim = pplx_model_config(e.model)->hidden_size; + e.dim = embed_model_config(e.model)->hidden_size; } if (verbose >= 1) fprintf(stderr, "Model: %d-dim, %.0f ms%s%s\n", @@ -651,13 +651,13 @@ int main(int argc, char *argv[]) e.tok_ws = qwen_tokenizer_workspace_new(); if (!e.tok_ws) { fprintf(stderr, "failed to allocate tokenizer workspace\n"); - if (e.workspace) pplx_workspace_free(e.workspace); - if (e.model) pplx_model_free(e.model); + if (e.workspace) embed_workspace_free(e.workspace); + if (e.model) embed_model_free(e.model); #ifdef USE_MLX - if (e.mlx_ctx) pplx_mlx_free(e.mlx_ctx); + if (e.mlx_ctx) embed_mlx_free(e.mlx_ctx); #endif #ifdef USE_CUDA - if (e.cuda_ctx) pplx_cuda_free(e.cuda_ctx); + if (e.cuda_ctx) embed_cuda_free(e.cuda_ctx); #endif qwen_tokenizer_free(tok); return 1; @@ -671,13 +671,13 @@ int main(int argc, char *argv[]) rc = run_batch(&e, argc, argv, arg_start, print_embs, batch_size); /* Cleanup */ - if (e.workspace) pplx_workspace_free(e.workspace); - if (e.model) pplx_model_free(e.model); + if (e.workspace) embed_workspace_free(e.workspace); + if (e.model) embed_model_free(e.model); #ifdef USE_MLX - if (e.mlx_ctx) pplx_mlx_free(e.mlx_ctx); + if (e.mlx_ctx) embed_mlx_free(e.mlx_ctx); #endif #ifdef USE_CUDA - if (e.cuda_ctx) pplx_cuda_free(e.cuda_ctx); + if (e.cuda_ctx) embed_cuda_free(e.cuda_ctx); #endif qwen_tokenizer_workspace_free(e.tok_ws); qwen_tokenizer_free(tok); diff --git a/embed_cuda.cu b/embed_cuda.cu index 2dbe138..2e4cd28 100644 --- a/embed_cuda.cu +++ b/embed_cuda.cu @@ -30,9 +30,9 @@ typedef struct { cuda_matrix_t gate_up_proj, down_proj; } cuda_layer_t; -struct pplx_cuda_ctx { - pplx_model_t *cpu; - pplx_config_t config; +struct embed_cuda_ctx { + embed_model_t *cpu; + embed_config_t config; cudaStream_t stream; cublasHandle_t blas; cuda_matrix_t embed_tokens; @@ -125,7 +125,7 @@ static int parse_gemm_compute(const char *mode, cublasComputeType_t *out) static cublasComputeType_t g_gemm_compute = CUBLAS_COMPUTE_32F; static int g_gemm_compute_set = 0; -int pplx_cuda_set_fast_gemm(const char *mode) +int embed_cuda_set_fast_gemm(const char *mode) { cublasComputeType_t ct; if (parse_gemm_compute(mode, &ct) != 0) @@ -169,14 +169,14 @@ static uint16_t f32_to_bf16(float f) static int g_weights_bf16 = 0; static int g_weights_bf16_set = 0; -int pplx_cuda_set_weights_bf16(int on) +int embed_cuda_set_weights_bf16(int on) { g_weights_bf16 = on ? 1 : 0; g_weights_bf16_set = 1; return 0; } -static int copy_weight_host_f32(float *dst, const pplx_weight_ref_t *w, +static int copy_weight_host_f32(float *dst, const embed_weight_ref_t *w, size_t count) { if (!dst || !w || !w->data) return -1; @@ -233,7 +233,7 @@ static int upload_weight(cuda_matrix_t *m, const float *src, size_t count, return 0; } -static int load_matrix(cuda_matrix_t *m, const pplx_weight_ref_t *w, +static int load_matrix(cuda_matrix_t *m, const embed_weight_ref_t *w, int rows, int cols) { if (!m || !w || rows <= 0 || cols <= 0) return -1; @@ -249,8 +249,8 @@ static int load_matrix(cuda_matrix_t *m, const pplx_weight_ref_t *w, return r; } -static int load_qkv_matrix(cuda_matrix_t *m, const pplx_layer_t *src, - const pplx_config_t *c) +static int load_qkv_matrix(cuda_matrix_t *m, const embed_layer_t *src, + const embed_config_t *c) { if (!m || !src || !c) return -1; int rows = c->q_dim + 2 * c->kv_dim; @@ -271,8 +271,8 @@ static int load_qkv_matrix(cuda_matrix_t *m, const pplx_layer_t *src, return r; } -static int load_gate_up_matrix(cuda_matrix_t *m, const pplx_layer_t *src, - const pplx_config_t *c) +static int load_gate_up_matrix(cuda_matrix_t *m, const embed_layer_t *src, + const embed_config_t *c) { if (!m || !src || !c) return -1; int rows = 2 * c->intermediate_size; @@ -574,14 +574,14 @@ static int launch_check(void) } // Compute type for CUDA GEMMs. Default is exact F32 (== cublasSgemm). -// PPLX_CUDA_GEMM_MODE={bf16,tf32,16f} selects a reduced-precision tensor-core +// EMBED_CUDA_GEMM_MODE={bf16,tf32,16f} selects a reduced-precision tensor-core // matmul that keeps F32 inputs/outputs and F32 accumulation but rounds operands // internally. static cublasComputeType_t gemm_compute(void) { static int env_init = 0; if (!g_gemm_compute_set && !env_init) { - const char *e = getenv("PPLX_CUDA_GEMM_MODE"); + const char *e = getenv("EMBED_CUDA_GEMM_MODE"); if (e && parse_gemm_compute(e, &g_gemm_compute) == 0) g_gemm_compute_set = 1; env_init = 1; @@ -594,7 +594,7 @@ static cublasComputeType_t gemm_compute(void) // already produced in BF16 by the upstream kernel, otherwise the F32 operand // is cast into ctx->act_bf16 first. With F32 weights this is the exact path, // honoring gemm_compute(). -static int linear_ex(pplx_cuda_ctx_t *ctx, const cuda_matrix_t *w, +static int linear_ex(embed_cuda_ctx_t *ctx, const cuda_matrix_t *w, const void *x, int x_is_bf16, float *y, int rows, int in_dim, int out_dim, int x_stride, float beta) { @@ -627,21 +627,21 @@ static int linear_ex(pplx_cuda_ctx_t *ctx, const cuda_matrix_t *w, return 0; } -static int linear(pplx_cuda_ctx_t *ctx, const cuda_matrix_t *w, +static int linear(embed_cuda_ctx_t *ctx, const cuda_matrix_t *w, const float *x, float *y, int rows, int in_dim, int out_dim) { return linear_ex(ctx, w, x, 0, y, rows, in_dim, out_dim, in_dim, 0.0f); } // x points at activations already stored as BF16 (e.g. ctx->act_bf16). -static int linear_bf16x(pplx_cuda_ctx_t *ctx, const cuda_matrix_t *w, +static int linear_bf16x(embed_cuda_ctx_t *ctx, const cuda_matrix_t *w, const void *x, float *y, int rows, int in_dim, int out_dim, int x_stride, float beta) { return linear_ex(ctx, w, x, 1, y, rows, in_dim, out_dim, x_stride, beta); } -static int linear_accum(pplx_cuda_ctx_t *ctx, const cuda_matrix_t *w, +static int linear_accum(embed_cuda_ctx_t *ctx, const cuda_matrix_t *w, const float *x, float *y, int rows, int in_dim, int out_dim) { @@ -683,10 +683,10 @@ static int gemm_strided_batched(cublasHandle_t blas, cublasOperation_t transa, // layer-invariant). attn_G stays 0 - per-sequence fallback - for ragged // batches, single sequences, or when one sequence's scores exceed the // budget. -static int attn_batched_setup(pplx_cuda_ctx_t *ctx, int batch, int q_offset, +static int attn_batched_setup(embed_cuda_ctx_t *ctx, int batch, int q_offset, int qkv_dim) { - const pplx_config_t *c = &ctx->config; + const embed_config_t *c = &ctx->config; ctx->attn_G = 0; if (batch < 2) return 0; int L = ctx->offsets_host[1] - ctx->offsets_host[0]; @@ -773,12 +773,12 @@ static int attn_batched_setup(pplx_cuda_ctx_t *ctx, int batch, int q_offset, // out_bf16 != NULL selects the BF16-weight layout: V and the softmax // probabilities are stored BF16 and the @V GEMM emits BF16 attention output // at out_bf16 (stride q_dim), ready for the wo projection without a cast. -static int cuda_attention_gemm(pplx_cuda_ctx_t *ctx, const int *h_offsets, +static int cuda_attention_gemm(embed_cuda_ctx_t *ctx, const int *h_offsets, int batch, int q_offset, int k_offset, int v_offset, int qkv_dim, float scale, __nv_bfloat16 *out_bf16) { - const pplx_config_t *c = &ctx->config; + const embed_config_t *c = &ctx->config; int q_dim = c->q_dim; int hd = c->head_dim; int H = c->n_heads; @@ -884,9 +884,9 @@ static int cuda_attention_gemm(pplx_cuda_ctx_t *ctx, const int *h_offsets, return 0; } -static int ensure_buffers(pplx_cuda_ctx_t *ctx, int total, int batch, int max_seq) +static int ensure_buffers(embed_cuda_ctx_t *ctx, int total, int batch, int max_seq) { - const pplx_config_t *c = &ctx->config; + const embed_config_t *c = &ctx->config; if (total > ctx->seq_cap) { cudaFree(ctx->x); cudaFree(ctx->x_norm); @@ -985,10 +985,10 @@ static int ensure_buffers(pplx_cuda_ctx_t *ctx, int total, int batch, int max_se return 0; } -static int ensure_pooled_rows(pplx_cuda_ctx_t *ctx, int rows) +static int ensure_pooled_rows(embed_cuda_ctx_t *ctx, int rows) { if (rows <= ctx->pooled_rows_cap) return 0; - const pplx_config_t *c = &ctx->config; + const embed_config_t *c = &ctx->config; cudaFree(ctx->pooled_out); ctx->pooled_out = NULL; CUDA_CHECK(cudaMalloc((void **)&ctx->pooled_out, @@ -997,7 +997,7 @@ static int ensure_pooled_rows(pplx_cuda_ctx_t *ctx, int rows) return 0; } -static int ensure_span_buffers(pplx_cuda_ctx_t *ctx, int n_spans) +static int ensure_span_buffers(embed_cuda_ctx_t *ctx, int n_spans) { if (n_spans <= ctx->span_cap) return 0; cudaFree(ctx->span_starts); @@ -1012,8 +1012,8 @@ static int ensure_span_buffers(pplx_cuda_ctx_t *ctx, int n_spans) return 0; } -static int load_layer(cuda_layer_t *dst, const pplx_layer_t *src, - const pplx_config_t *c) +static int load_layer(cuda_layer_t *dst, const embed_layer_t *src, + const embed_config_t *c) { return load_qkv_matrix(&dst->qkv, src, c) || @@ -1039,14 +1039,14 @@ static void free_layer(cuda_layer_t *l) cuda_matrix_free(&l->down_proj); } -pplx_cuda_ctx_t *pplx_cuda_load(const char *model_dir) +embed_cuda_ctx_t *embed_cuda_load(const char *model_dir) { - pplx_model_t *cpu = pplx_model_load(model_dir); + embed_model_t *cpu = embed_model_load(model_dir); if (!cpu) return NULL; - pplx_cuda_ctx_t *ctx = (pplx_cuda_ctx_t *)calloc(1, sizeof(*ctx)); + embed_cuda_ctx_t *ctx = (embed_cuda_ctx_t *)calloc(1, sizeof(*ctx)); if (!ctx) { - pplx_model_free(cpu); + embed_model_free(cpu); return NULL; } ctx->cpu = cpu; @@ -1065,26 +1065,26 @@ pplx_cuda_ctx_t *pplx_cuda_load(const char *model_dir) if (cudaStreamCreate(&ctx->stream) != cudaSuccess || cublasCreate(&ctx->blas) != CUBLAS_STATUS_SUCCESS || cublasSetStream(ctx->blas, ctx->stream) != CUBLAS_STATUS_SUCCESS) { - pplx_cuda_free(ctx); + embed_cuda_free(ctx); return NULL; } - const pplx_config_t *c = &ctx->config; + const embed_config_t *c = &ctx->config; ctx->layers = (cuda_layer_t *)calloc((size_t)c->n_layers, sizeof(*ctx->layers)); if (!ctx->layers) { - pplx_cuda_free(ctx); + embed_cuda_free(ctx); return NULL; } if (load_matrix(&ctx->embed_tokens, &cpu->weights.embed_tokens, c->vocab_size, c->hidden_size) != 0 || load_vector(&ctx->norm, cpu->weights.norm, c->hidden_size) != 0) { - pplx_cuda_free(ctx); + embed_cuda_free(ctx); return NULL; } for (int i = 0; i < c->n_layers; i++) { if (load_layer(&ctx->layers[i], &cpu->weights.layers[i], c) != 0) { fprintf(stderr, "cuda: failed to load layer %d\n", i); - pplx_cuda_free(ctx); + embed_cuda_free(ctx); return NULL; } } @@ -1092,7 +1092,7 @@ pplx_cuda_ctx_t *pplx_cuda_load(const char *model_dir) return ctx; } -void pplx_cuda_free(pplx_cuda_ctx_t *ctx) +void embed_cuda_free(embed_cuda_ctx_t *ctx) { if (!ctx) return; cuda_matrix_free(&ctx->embed_tokens); @@ -1125,20 +1125,20 @@ void pplx_cuda_free(pplx_cuda_ctx_t *ctx) cudaFree(ctx->span_lens); if (ctx->blas) cublasDestroy(ctx->blas); if (ctx->stream) cudaStreamDestroy(ctx->stream); - pplx_model_free(ctx->cpu); + embed_model_free(ctx->cpu); free(ctx); } -const pplx_config_t *pplx_cuda_config(const pplx_cuda_ctx_t *ctx) +const embed_config_t *embed_cuda_config(const embed_cuda_ctx_t *ctx) { return ctx ? &ctx->config : NULL; } -static int cuda_forward_batch(pplx_cuda_ctx_t *ctx, const pplx_input_t *inputs, +static int cuda_forward_batch(embed_cuda_ctx_t *ctx, const embed_input_t *inputs, int batch) { if (!ctx || !inputs || batch <= 0) return -1; - const pplx_config_t *c = &ctx->config; + const embed_config_t *c = &ctx->config; int *h_offsets = (int *)malloc((size_t)(batch + 1) * sizeof(int)); if (!h_offsets) return -1; int total = 0, max_seq = 0; @@ -1299,11 +1299,11 @@ static int cuda_forward_batch(pplx_cuda_ctx_t *ctx, const pplx_input_t *inputs, return 0; } -int pplx_cuda_embed_batch(pplx_cuda_ctx_t *ctx, const pplx_input_t *inputs, +int embed_cuda_encode_batch(embed_cuda_ctx_t *ctx, const embed_input_t *inputs, int batch, float *out_embeddings) { if (!ctx || !inputs || batch <= 0 || !out_embeddings) return -1; - const pplx_config_t *c = &ctx->config; + const embed_config_t *c = &ctx->config; if (cuda_forward_batch(ctx, inputs, batch) != 0) return -1; if (ensure_pooled_rows(ctx, batch) != 0) @@ -1323,16 +1323,16 @@ int pplx_cuda_embed_batch(pplx_cuda_ctx_t *ctx, const pplx_input_t *inputs, return 0; } -int pplx_cuda_embed_spans_batch(pplx_cuda_ctx_t *ctx, - const pplx_context_input_t *inputs, +int embed_cuda_encode_spans_batch(embed_cuda_ctx_t *ctx, + const embed_context_input_t *inputs, int batch, float *out_embeddings) { if (!ctx || !inputs || batch <= 0 || !out_embeddings) return -1; - const pplx_config_t *c = &ctx->config; + const embed_config_t *c = &ctx->config; int total_spans = 0; int total_tokens = 0; for (int b = 0; b < batch; b++) { - const pplx_context_input_t *input = &inputs[b]; + const embed_context_input_t *input = &inputs[b]; int n_tokens = input->input.n_tokens; if (!input->input.ids || n_tokens <= 0 || !input->spans || input->n_spans <= 0 || total_spans > INT_MAX - input->n_spans || @@ -1349,7 +1349,7 @@ int pplx_cuda_embed_spans_batch(pplx_cuda_ctx_t *ctx, total_tokens += n_tokens; } - pplx_input_t *packed = (pplx_input_t *)malloc((size_t)batch * sizeof(*packed)); + embed_input_t *packed = (embed_input_t *)malloc((size_t)batch * sizeof(*packed)); int *h_starts = (int *)malloc((size_t)total_spans * sizeof(int)); int *h_lens = (int *)malloc((size_t)total_spans * sizeof(int)); if (!packed || !h_starts || !h_lens) { @@ -1362,7 +1362,7 @@ int pplx_cuda_embed_spans_batch(pplx_cuda_ctx_t *ctx, int token_offset = 0; int span_offset = 0; for (int b = 0; b < batch; b++) { - const pplx_context_input_t *input = &inputs[b]; + const embed_context_input_t *input = &inputs[b]; packed[b] = input->input; for (int s = 0; s < input->n_spans; s++) { h_starts[span_offset] = token_offset + input->spans[s].start; @@ -1414,21 +1414,21 @@ cleanup: return rc; } -int pplx_cuda_embed_into(pplx_cuda_ctx_t *ctx, const int *token_ids, +int embed_cuda_encode_into(embed_cuda_ctx_t *ctx, const int *token_ids, int n_tokens, float *out_embedding) { - pplx_input_t input = { token_ids, n_tokens }; - return pplx_cuda_embed_batch(ctx, &input, 1, out_embedding); + embed_input_t input = { token_ids, n_tokens }; + return embed_cuda_encode_batch(ctx, &input, 1, out_embedding); } -float *pplx_cuda_embed(pplx_cuda_ctx_t *ctx, const int *token_ids, +float *embed_cuda_encode(embed_cuda_ctx_t *ctx, const int *token_ids, int n_tokens) { if (!ctx) return NULL; int hidden = ctx->config.hidden_size; float *out = (float *)malloc((size_t)hidden * sizeof(float)); if (!out) return NULL; - if (pplx_cuda_embed_into(ctx, token_ids, n_tokens, out) != 0) { + if (embed_cuda_encode_into(ctx, token_ids, n_tokens, out) != 0) { free(out); return NULL; } diff --git a/embed_cuda.h b/embed_cuda.h index 3c9011d..b3d2c3c 100644 --- a/embed_cuda.h +++ b/embed_cuda.h @@ -1,41 +1,41 @@ /* - * embed_cuda.h - CUDA/cuBLAS backend for pplx-embed inference. + * embed_cuda.h - CUDA/cuBLAS backend */ -#ifndef PPLX_EMBED_CUDA_H -#define PPLX_EMBED_CUDA_H +#ifndef EMBED_CUDA_H +#define EMBED_CUDA_H /* Public-API export annotation. The libraries are built with - * -fvisibility=hidden, so only declarations carrying PPLX_API are exported + * -fvisibility=hidden, so only declarations carrying EMBED_API are exported * from the shared library; everything else stays internal. */ -#ifndef PPLX_API +#ifndef EMBED_API #if defined(__GNUC__) -#define PPLX_API __attribute__((visibility("default"))) +#define EMBED_API __attribute__((visibility("default"))) #else -#define PPLX_API +#define EMBED_API #endif #endif #include "embed.h" -typedef struct pplx_cuda_ctx pplx_cuda_ctx_t; +typedef struct embed_cuda_ctx embed_cuda_ctx_t; -PPLX_API int pplx_cuda_set_fast_gemm(const char *mode); -PPLX_API int pplx_cuda_set_weights_bf16(int on); +EMBED_API int embed_cuda_set_fast_gemm(const char *mode); +EMBED_API int embed_cuda_set_weights_bf16(int on); -PPLX_API pplx_cuda_ctx_t *pplx_cuda_load(const char *model_dir); -PPLX_API void pplx_cuda_free(pplx_cuda_ctx_t *ctx); +EMBED_API embed_cuda_ctx_t *embed_cuda_load(const char *model_dir); +EMBED_API void embed_cuda_free(embed_cuda_ctx_t *ctx); -PPLX_API const pplx_config_t *pplx_cuda_config(const pplx_cuda_ctx_t *ctx); +EMBED_API const embed_config_t *embed_cuda_config(const embed_cuda_ctx_t *ctx); -PPLX_API int pplx_cuda_embed_into(pplx_cuda_ctx_t *ctx, const int *token_ids, +EMBED_API int embed_cuda_encode_into(embed_cuda_ctx_t *ctx, const int *token_ids, int n_tokens, float *out_embedding); -PPLX_API float *pplx_cuda_embed(pplx_cuda_ctx_t *ctx, const int *token_ids, +EMBED_API float *embed_cuda_encode(embed_cuda_ctx_t *ctx, const int *token_ids, int n_tokens); -PPLX_API int pplx_cuda_embed_batch(pplx_cuda_ctx_t *ctx, const pplx_input_t *inputs, +EMBED_API int embed_cuda_encode_batch(embed_cuda_ctx_t *ctx, const embed_input_t *inputs, int batch, float *out_embeddings); -PPLX_API int pplx_cuda_embed_spans_batch(pplx_cuda_ctx_t *ctx, - const pplx_context_input_t *inputs, +EMBED_API int embed_cuda_encode_spans_batch(embed_cuda_ctx_t *ctx, + const embed_context_input_t *inputs, int batch, float *out_embeddings); -#endif /* PPLX_EMBED_CUDA_H */ +#endif /* EMBED_CUDA_H */ diff --git a/embed_internal.h b/embed_internal.h index 53debee..a7365a2 100644 --- a/embed_internal.h +++ b/embed_internal.h @@ -1,5 +1,5 @@ -#ifndef PPLX_EMBED_INTERNAL_H -#define PPLX_EMBED_INTERNAL_H +#ifndef EMBED_INTERNAL_H +#define EMBED_INTERNAL_H #include "embed.h" #include "qwen_safetensors.h" @@ -7,52 +7,52 @@ typedef struct { safetensor_dtype_t dtype; const void *data; -} pplx_weight_ref_t; +} embed_weight_ref_t; typedef struct { - pplx_weight_ref_t wq; /* [q_dim, hidden] */ - pplx_weight_ref_t wk; /* [kv_dim, hidden] */ - pplx_weight_ref_t wv; /* [kv_dim, hidden] */ - pplx_weight_ref_t wo; /* [hidden, q_dim] */ - const float *q_norm; /* [head_dim] */ - const float *k_norm; /* [head_dim] */ - const float *input_norm; /* [hidden] */ - const float *post_attn_norm; /* [hidden] */ - pplx_weight_ref_t gate_proj; /* [intermediate, hidden] */ - pplx_weight_ref_t up_proj; /* [intermediate, hidden] */ - pplx_weight_ref_t down_proj; /* [hidden, intermediate] */ -} pplx_layer_t; + embed_weight_ref_t wq; /* [q_dim, hidden] */ + embed_weight_ref_t wk; /* [kv_dim, hidden] */ + embed_weight_ref_t wv; /* [kv_dim, hidden] */ + embed_weight_ref_t wo; /* [hidden, q_dim] */ + const float *q_norm; /* [head_dim] */ + const float *k_norm; /* [head_dim] */ + const float *input_norm; /* [hidden] */ + const float *post_attn_norm; /* [hidden] */ + embed_weight_ref_t gate_proj; /* [intermediate, hidden] */ + embed_weight_ref_t up_proj; /* [intermediate, hidden] */ + embed_weight_ref_t down_proj; /* [hidden, intermediate] */ +} embed_layer_t; typedef struct { - pplx_weight_ref_t embed_tokens; /* [vocab_size, hidden] */ - pplx_layer_t *layers; /* [n_layers] heap-allocated */ - const float *norm; /* [hidden] */ -} pplx_weights_t; + embed_weight_ref_t embed_tokens; /* [vocab_size, hidden] */ + embed_layer_t *layers; /* [n_layers] heap-allocated */ + const float *norm; /* [hidden] */ +} embed_weights_t; -struct pplx_model { - pplx_config_t config; - pplx_weights_t weights; +struct embed_model { + embed_config_t config; + embed_weights_t weights; void *safetensors; /* multi_safetensors_t*, keeps mmap alive */ int layer_start; /* loaded transformer range, inclusive */ int layer_end; /* loaded transformer range, exclusive */ }; -struct pplx_workspace { - const pplx_model_t *model; /* immutable model this workspace belongs to */ +struct embed_workspace { + const embed_model_t *model; /* immutable model this workspace belongs to */ int buf_seq_cap; - float *x; /* [seq, hidden] */ - float *x_norm; /* [seq, hidden] */ - float *q; /* [seq, q_dim] */ - float *k; /* [seq, kv_dim] */ - float *v; /* [seq, kv_dim] */ - float *attn_out; /* [seq, q_dim] */ - float *proj_out; /* [seq, hidden] */ - float *ffn_gate; /* [seq, intermediate] */ - float *ffn_up; /* [seq, intermediate] */ - float *attn_scores; /* reusable BLAS attention score tiles */ + float *x; /* [seq, hidden] */ + float *x_norm; /* [seq, hidden] */ + float *q; /* [seq, q_dim] */ + float *k; /* [seq, kv_dim] */ + float *v; /* [seq, kv_dim] */ + float *attn_out; /* [seq, q_dim] */ + float *proj_out; /* [seq, hidden] */ + float *ffn_gate; /* [seq, intermediate] */ + float *ffn_up; /* [seq, intermediate] */ + float *attn_scores; /* reusable BLAS attention score tiles */ size_t attn_scores_bytes; - int *offsets; /* [batch + 1] packed sequence offsets */ + int *offsets; /* [batch + 1] packed sequence offsets */ int offsets_cap; /* RoPE cosine/sine cache [n_pos, head_dim], grown lazily. */ @@ -67,4 +67,4 @@ struct pplx_workspace { size_t bf16_widen_count; }; -#endif /* PPLX_EMBED_INTERNAL_H */ +#endif /* EMBED_INTERNAL_H */ diff --git a/embed_mlx.c b/embed_mlx.c index aa0d1ed..5de4068 100644 --- a/embed_mlx.c +++ b/embed_mlx.c @@ -32,8 +32,8 @@ typedef struct { mlx_linear_t gate_proj, up_proj, down_proj; } mlx_layer_t; -struct pplx_mlx_ctx { - pplx_config_t config; +struct embed_mlx_ctx { + embed_config_t config; mlx_array embed_tokens; mlx_layer_t *layers; /* heap-allocated [n_layers] */ mlx_array norm; @@ -46,15 +46,15 @@ struct pplx_mlx_ctx { mlx_dtype weight_dtype; }; -struct pplx_mlx_late_ctx { - pplx_mlx_ctx_t *base; +struct embed_mlx_late_ctx { + embed_mlx_ctx_t *base; multi_safetensors_t *dense_ms; mlx_linear_t projection; int token_dim; }; -struct pplx_mlx_late_vectors { - pplx_mlx_late_ctx_t *owner; +struct embed_mlx_late_vectors { + embed_mlx_late_ctx_t *owner; mlx_array vectors; /* [tokens, dim] */ int tokens; int dim; @@ -73,7 +73,7 @@ static int mlx_mul_size(size_t a, size_t b, size_t *out) return 0; } -static mlx_dtype mlx_weight_dtype(const pplx_mlx_ctx_t *ctx) +static mlx_dtype mlx_weight_dtype(const embed_mlx_ctx_t *ctx) { return ctx->weight_dtype; } @@ -311,7 +311,7 @@ static mlx_array linear(mlx_array x, const mlx_linear_t *W, mlx_stream s) return res; } -const pplx_config_t *pplx_mlx_config(const pplx_mlx_ctx_t *ctx) +const embed_config_t *embed_mlx_config(const embed_mlx_ctx_t *ctx) { return ctx ? &ctx->config : NULL; } @@ -358,7 +358,7 @@ static const char *json_find_m(const char *json, const char *key) return NULL; } -static int mlx_parse_config(pplx_config_t *cfg, const char *model_dir) +static int mlx_parse_config(embed_config_t *cfg, const char *model_dir) { char path[1024]; int path_len = snprintf(path, sizeof(path), "%s/config.json", model_dir); @@ -387,9 +387,9 @@ static int mlx_parse_config(pplx_config_t *cfg, const char *model_dir) GI("num_hidden_layers", n_layers, 0); GI("num_attention_heads", n_heads, 0); GI("num_key_value_heads", n_kv_heads, 0); - GI("head_dim", head_dim, PPLX_HEAD_DIM); + GI("head_dim", head_dim, EMBED_HEAD_DIM); GI("intermediate_size", intermediate_size, 0); - GI("vocab_size", vocab_size, PPLX_VOCAB_SIZE); + GI("vocab_size", vocab_size, EMBED_VOCAB_SIZE); GF("rms_norm_eps", rms_norm_eps, 1e-6f); GF("rope_theta", rope_theta, 1000000.0f); #undef GI @@ -418,9 +418,9 @@ static int mlx_parse_config(pplx_config_t *cfg, const char *model_dir) cfg->n_kv_heads, cfg->head_dim, cfg->intermediate_size); return -1; } - if (cfg->n_layers > PPLX_MAX_LAYERS) { + if (cfg->n_layers > EMBED_MAX_LAYERS) { fprintf(stderr, "mlx: too many layers (%d > %d)\n", - cfg->n_layers, PPLX_MAX_LAYERS); + cfg->n_layers, EMBED_MAX_LAYERS); return -1; } return 0; @@ -443,9 +443,9 @@ static int mlx_model_dir_has_late_projection(const char *model_dir) mlx_model_dir_has_file(model_dir, "1_Dense/model.safetensors"); } -static pplx_mlx_options_t normalize_mlx_options(const pplx_mlx_options_t *opts) +static embed_mlx_options_t normalize_mlx_options(const embed_mlx_options_t *opts) { - pplx_mlx_options_t out = {0}; + embed_mlx_options_t out = {0}; if (opts) out = *opts; if (out.quantize_bits != 0 && out.quantize_bits != 8) { @@ -481,12 +481,12 @@ static int prepare_layer_transposes(mlx_layer_t *l, mlx_stream s) prepare_linear_transpose(&l->down_proj, s); } -static pplx_mlx_ctx_t *mlx_load_range_ex(const char *model_dir, +static embed_mlx_ctx_t *mlx_load_range_ex(const char *model_dir, int layer_start, int layer_end, - const pplx_mlx_options_t *opts, + const embed_mlx_options_t *opts, int allow_late) { - pplx_mlx_options_t options = normalize_mlx_options(opts); + embed_mlx_options_t options = normalize_mlx_options(opts); if (options.quantize_bits < 0) return NULL; if (!allow_late && mlx_model_dir_has_late_projection(model_dir)) { fprintf(stderr, "mlx: late-interaction models require token-level " @@ -495,14 +495,14 @@ static pplx_mlx_ctx_t *mlx_load_range_ex(const char *model_dir, return NULL; } - pplx_mlx_ctx_t *ctx = calloc(1, sizeof(pplx_mlx_ctx_t)); + embed_mlx_ctx_t *ctx = calloc(1, sizeof(embed_mlx_ctx_t)); if (!ctx) return NULL; ctx->quantize_bits = options.quantize_bits; ctx->quantize_group_size = options.quantize_group_size; if (mlx_parse_config(&ctx->config, model_dir) != 0) { free(ctx); return NULL; } - const pplx_config_t *c = &ctx->config; + const embed_config_t *c = &ctx->config; if (layer_end == -1) layer_end = c->n_layers; if (layer_start < 0 || layer_start >= layer_end || @@ -578,7 +578,7 @@ static pplx_mlx_ctx_t *mlx_load_range_ex(const char *model_dir, LD(down_proj, "layers.%d.mlp.down_proj.weight", h, inter); #undef LD - if (pplx_verbose >= 2) fprintf(stderr, " mlx layer %d loaded\n", i); + if (embed_verbose >= 2) fprintf(stderr, " mlx layer %d loaded\n", i); } if (layer_end == c->n_layers) { @@ -609,7 +609,7 @@ static pplx_mlx_ctx_t *mlx_load_range_ex(const char *model_dir, goto fail; } mlx_clear_cache(); - if (pplx_verbose >= 1) + if (embed_verbose >= 1) fprintf(stderr, "mlx: quantized linear weights to %d-bit " "(group_size=%d)\n", ctx->quantize_bits, ctx->quantize_group_size); @@ -620,7 +620,7 @@ static pplx_mlx_ctx_t *mlx_load_range_ex(const char *model_dir, } } - if (pplx_verbose >= 2) { + if (embed_verbose >= 2) { size_t active = 0, cache = 0, peak = 0; mlx_get_active_memory(&active); mlx_get_cache_memory(&cache); @@ -632,24 +632,24 @@ static pplx_mlx_ctx_t *mlx_load_range_ex(const char *model_dir, (double)peak / (1024.0 * 1024.0 * 1024.0)); } - if (pplx_verbose >= 1) + if (embed_verbose >= 1) fprintf(stderr, "mlx: layers [%d, %d) loaded (%d-dim)\n", layer_start, layer_end, h); return ctx; fail: - pplx_mlx_free(ctx); + embed_mlx_free(ctx); return NULL; } -pplx_mlx_ctx_t *pplx_mlx_load(const char *model_dir) +embed_mlx_ctx_t *embed_mlx_load(const char *model_dir) { return mlx_load_range_ex(model_dir, 0, -1, NULL, 0); } -pplx_mlx_ctx_t *pplx_mlx_load_with_options(const char *model_dir, - const pplx_mlx_options_t *opts) +embed_mlx_ctx_t *embed_mlx_load_with_options(const char *model_dir, + const embed_mlx_options_t *opts) { return mlx_load_range_ex(model_dir, 0, -1, opts, 0); } @@ -668,7 +668,7 @@ static void free_mlx_layer(mlx_layer_t *l) free_linear(&l->down_proj); } -void pplx_mlx_free(pplx_mlx_ctx_t *ctx) +void embed_mlx_free(embed_mlx_ctx_t *ctx) { if (!ctx) return; mlx_array_free(ctx->embed_tokens); @@ -683,15 +683,15 @@ void pplx_mlx_free(pplx_mlx_ctx_t *ctx) free(ctx); } -pplx_mlx_late_ctx_t *pplx_mlx_late_load_with_options( - const char *model_dir, const pplx_mlx_options_t *opts) +embed_mlx_late_ctx_t *embed_mlx_late_load_with_options( + const char *model_dir, const embed_mlx_options_t *opts) { if (!mlx_model_dir_has_late_projection(model_dir)) { fprintf(stderr, "mlx late: missing 1_Dense projection\n"); return NULL; } - pplx_mlx_late_ctx_t *late = calloc(1, sizeof(*late)); + embed_mlx_late_ctx_t *late = calloc(1, sizeof(*late)); if (!late) return NULL; late->base = mlx_load_range_ex(model_dir, 0, -1, opts, 1); @@ -731,36 +731,36 @@ pplx_mlx_late_ctx_t *pplx_mlx_late_load_with_options( goto fail; late->token_dim = shape[0]; - if (pplx_verbose >= 1) + if (embed_verbose >= 1) fprintf(stderr, "mlx late: token projection loaded (%d-dim)\n", late->token_dim); return late; fail: - pplx_mlx_late_free(late); + embed_mlx_late_free(late); return NULL; } -pplx_mlx_late_ctx_t *pplx_mlx_late_load(const char *model_dir) +embed_mlx_late_ctx_t *embed_mlx_late_load(const char *model_dir) { - return pplx_mlx_late_load_with_options(model_dir, NULL); + return embed_mlx_late_load_with_options(model_dir, NULL); } -void pplx_mlx_late_free(pplx_mlx_late_ctx_t *ctx) +void embed_mlx_late_free(embed_mlx_late_ctx_t *ctx) { if (!ctx) return; free_linear(&ctx->projection); if (ctx->dense_ms) multi_safetensors_close(ctx->dense_ms); - pplx_mlx_free(ctx->base); + embed_mlx_free(ctx->base); free(ctx); } -const pplx_config_t *pplx_mlx_late_config(const pplx_mlx_late_ctx_t *ctx) +const embed_config_t *embed_mlx_late_config(const embed_mlx_late_ctx_t *ctx) { return ctx && ctx->base ? &ctx->base->config : NULL; } -int pplx_mlx_late_token_dim(const pplx_mlx_late_ctx_t *ctx) +int embed_mlx_late_token_dim(const embed_mlx_late_ctx_t *ctx) { return ctx ? ctx->token_dim : 0; } @@ -769,12 +769,12 @@ int pplx_mlx_late_token_dim(const pplx_mlx_late_ctx_t *ctx) * Forward pass * ======================================================================== */ -static mlx_array mlx_forward_layers(pplx_mlx_ctx_t *ctx, mlx_array x, +static mlx_array mlx_forward_layers(embed_mlx_ctx_t *ctx, mlx_array x, int batch, int max_seq, int has_padding, mlx_array attn_mask, int layer_start, int layer_end) { - const pplx_config_t *c = &ctx->config; + const embed_config_t *c = &ctx->config; mlx_stream S = ctx->stream; int n_heads = c->n_heads; int n_kv_heads = c->n_kv_heads; @@ -899,20 +899,20 @@ static mlx_array mlx_forward_layers(pplx_mlx_ctx_t *ctx, mlx_array x, return x; } -pplx_mlx_late_vectors_t *pplx_mlx_late_encode_tokens_device( - pplx_mlx_late_ctx_t *ctx, const int *token_ids, int n_tokens, +embed_mlx_late_vectors_t *embed_mlx_late_encode_tokens_device( + embed_mlx_late_ctx_t *ctx, const int *token_ids, int n_tokens, int normalize) { if (!ctx || !ctx->base || !token_ids || n_tokens <= 0 || ctx->token_dim <= 0 || !linear_ok(ctx->projection)) return NULL; - pplx_mlx_ctx_t *base = ctx->base; + embed_mlx_ctx_t *base = ctx->base; if (base->layer_start != 0 || base->layer_end != base->config.n_layers || !arr_ok(base->embed_tokens) || !arr_ok(base->norm)) return NULL; - const pplx_config_t *c = &base->config; + const embed_config_t *c = &base->config; mlx_stream S = base->stream; for (int i = 0; i < n_tokens; i++) { if (token_ids[i] < 0 || token_ids[i] >= c->vocab_size) { @@ -935,7 +935,7 @@ pplx_mlx_late_vectors_t *pplx_mlx_late_encode_tokens_device( free(ids_data); if (!arr_ok(ids)) return NULL; - pplx_mlx_late_vectors_t *result = NULL; + embed_mlx_late_vectors_t *result = NULL; mlx_array x = mlx_array_new(); mlx_array x_normed = (mlx_array){0}; mlx_array projected = (mlx_array){0}; @@ -997,7 +997,7 @@ pplx_mlx_late_vectors_t *pplx_mlx_late_encode_tokens_device( mlx_reshape(&reshaped, projected, vec_shape, 2, S); if (!arr_ok(reshaped)) goto cleanup; - result = (pplx_mlx_late_vectors_t *)calloc(1, sizeof(*result)); + result = (embed_mlx_late_vectors_t *)calloc(1, sizeof(*result)); if (!result) goto cleanup; result->owner = ctx; result->vectors = reshaped; @@ -1013,24 +1013,24 @@ cleanup: return result; } -void pplx_mlx_late_vectors_free(pplx_mlx_late_vectors_t *vecs) +void embed_mlx_late_vectors_free(embed_mlx_late_vectors_t *vecs) { if (!vecs) return; mlx_array_free(vecs->vectors); free(vecs); } -int pplx_mlx_late_vectors_token_count(const pplx_mlx_late_vectors_t *vecs) +int embed_mlx_late_vectors_token_count(const embed_mlx_late_vectors_t *vecs) { return vecs ? vecs->tokens : 0; } -int pplx_mlx_late_vectors_dim(const pplx_mlx_late_vectors_t *vecs) +int embed_mlx_late_vectors_dim(const embed_mlx_late_vectors_t *vecs) { return vecs ? vecs->dim : 0; } -int pplx_mlx_late_vectors_copy(const pplx_mlx_late_vectors_t *vecs, +int embed_mlx_late_vectors_copy(const embed_mlx_late_vectors_t *vecs, float *out_vectors) { if (!vecs || !out_vectors || !arr_ok(vecs->vectors) || @@ -1061,15 +1061,15 @@ int pplx_mlx_late_vectors_copy(const pplx_mlx_late_vectors_t *vecs, return rc; } -pplx_mlx_late_vectors_t *pplx_mlx_late_vectors_concat( - pplx_mlx_late_ctx_t *ctx, - const pplx_mlx_late_vectors_t *const *items, int count) +embed_mlx_late_vectors_t *embed_mlx_late_vectors_concat( + embed_mlx_late_ctx_t *ctx, + const embed_mlx_late_vectors_t *const *items, int count) { if (!ctx || !ctx->base || !items || count <= 0) return NULL; int dim = 0; int total = 0; for (int i = 0; i < count; i++) { - const pplx_mlx_late_vectors_t *v = items[i]; + const embed_mlx_late_vectors_t *v = items[i]; if (!v || v->owner != ctx || !arr_ok(v->vectors) || v->tokens <= 0 || v->dim <= 0) return NULL; @@ -1097,8 +1097,8 @@ pplx_mlx_late_vectors_t *pplx_mlx_late_vectors_concat( return NULL; } - pplx_mlx_late_vectors_t *out = - (pplx_mlx_late_vectors_t *)calloc(1, sizeof(*out)); + embed_mlx_late_vectors_t *out = + (embed_mlx_late_vectors_t *)calloc(1, sizeof(*out)); if (!out) { mlx_array_free(joined); return NULL; @@ -1110,8 +1110,8 @@ pplx_mlx_late_vectors_t *pplx_mlx_late_vectors_concat( return out; } -pplx_mlx_late_vectors_t *pplx_mlx_late_vectors_select( - pplx_mlx_late_ctx_t *ctx, const pplx_mlx_late_vectors_t *vecs, +embed_mlx_late_vectors_t *embed_mlx_late_vectors_select( + embed_mlx_late_ctx_t *ctx, const embed_mlx_late_vectors_t *vecs, const int *token_indices, int count) { if (!ctx || !ctx->base || !vecs || vecs->owner != ctx || @@ -1145,8 +1145,8 @@ pplx_mlx_late_vectors_t *pplx_mlx_late_vectors_select( return NULL; } - pplx_mlx_late_vectors_t *out = - (pplx_mlx_late_vectors_t *)calloc(1, sizeof(*out)); + embed_mlx_late_vectors_t *out = + (embed_mlx_late_vectors_t *)calloc(1, sizeof(*out)); if (!out) { mlx_array_free(selected); return NULL; @@ -1158,9 +1158,9 @@ pplx_mlx_late_vectors_t *pplx_mlx_late_vectors_select( return out; } -int pplx_mlx_late_maxsim_batch_device( - pplx_mlx_late_ctx_t *ctx, const pplx_mlx_late_vectors_t *query, - const pplx_mlx_late_vectors_t *docs, const int *doc_offsets, +int embed_mlx_late_maxsim_batch_device( + embed_mlx_late_ctx_t *ctx, const embed_mlx_late_vectors_t *query, + const embed_mlx_late_vectors_t *docs, const int *doc_offsets, int docs_count, float *scores) { if (!ctx || !query || !docs || !doc_offsets || docs_count <= 0 || @@ -1346,28 +1346,28 @@ cleanup: return rc; } -int pplx_mlx_late_encode_tokens(pplx_mlx_late_ctx_t *ctx, +int embed_mlx_late_encode_tokens(embed_mlx_late_ctx_t *ctx, const int *token_ids, int n_tokens, int normalize, float *out_vectors) { - pplx_mlx_late_vectors_t *vecs = - pplx_mlx_late_encode_tokens_device(ctx, token_ids, n_tokens, + embed_mlx_late_vectors_t *vecs = + embed_mlx_late_encode_tokens_device(ctx, token_ids, n_tokens, normalize); if (!vecs) return -1; - int rc = pplx_mlx_late_vectors_copy(vecs, out_vectors); - pplx_mlx_late_vectors_free(vecs); + int rc = embed_mlx_late_vectors_copy(vecs, out_vectors); + embed_mlx_late_vectors_free(vecs); return rc; } -static int pplx_mlx_embed_batch_dense(pplx_mlx_ctx_t *ctx, - const pplx_input_t *inputs, int batch, +static int embed_mlx_encode_batch_dense(embed_mlx_ctx_t *ctx, + const embed_input_t *inputs, int batch, float *out_embeddings) { if (!ctx || !inputs || batch <= 0 || !out_embeddings || ctx->layer_start != 0 || ctx->layer_end != ctx->config.n_layers) return -1; - const pplx_config_t *c = &ctx->config; + const embed_config_t *c = &ctx->config; mlx_stream S = ctx->stream; int hidden = c->hidden_size; @@ -1516,7 +1516,7 @@ static int pplx_mlx_embed_batch_dense(pplx_mlx_ctx_t *ctx, typedef struct { int index; - const pplx_input_t *input; + const embed_input_t *input; } mlx_batch_item_t; static int mlx_batch_item_cmp(const void *a, const void *b) @@ -1535,7 +1535,7 @@ static int mlx_padding_exceeds_limit(size_t padded, size_t tokens) return padded * 5 > tokens * 11; } -static int mlx_batch_should_split(const pplx_input_t *inputs, int batch, +static int mlx_batch_should_split(const embed_input_t *inputs, int batch, int *out_total_tokens, int *out_max_seq) { int max_seq = 0; @@ -1557,7 +1557,7 @@ static int mlx_batch_should_split(const pplx_input_t *inputs, int batch, return mlx_padding_exceeds_limit(padded, (size_t)total_tokens); } -static int mlx_should_add_to_group(const pplx_mlx_ctx_t *ctx, +static int mlx_should_add_to_group(const embed_mlx_ctx_t *ctx, int group_count, int group_max, int next_tokens) { @@ -1579,19 +1579,19 @@ static int mlx_should_add_to_group(const pplx_mlx_ctx_t *ctx, return merged <= split_total; } -int pplx_mlx_embed_batch(pplx_mlx_ctx_t *ctx, const pplx_input_t *inputs, +int embed_mlx_encode_batch(embed_mlx_ctx_t *ctx, const embed_input_t *inputs, int batch, float *out_embeddings) { if (!ctx || !inputs || batch <= 0 || !out_embeddings) return -1; if (!mlx_batch_should_split(inputs, batch, NULL, NULL)) - return pplx_mlx_embed_batch_dense(ctx, inputs, batch, out_embeddings); + return embed_mlx_encode_batch_dense(ctx, inputs, batch, out_embeddings); const int hidden = ctx->config.hidden_size; mlx_batch_item_t *items = (mlx_batch_item_t *)malloc( (size_t)batch * sizeof(*items)); - pplx_input_t *group_inputs = (pplx_input_t *)malloc( + embed_input_t *group_inputs = (embed_input_t *)malloc( (size_t)batch * sizeof(*group_inputs)); size_t group_values, group_bytes; if (mlx_mul_size((size_t)batch, (size_t)hidden, &group_values) != 0 || @@ -1638,7 +1638,7 @@ int pplx_mlx_embed_batch(pplx_mlx_ctx_t *ctx, const pplx_input_t *inputs, int group_count = end - start; for (int i = 0; i < group_count; i++) group_inputs[i] = *items[start + i].input; - rc = pplx_mlx_embed_batch_dense(ctx, group_inputs, group_count, + rc = embed_mlx_encode_batch_dense(ctx, group_inputs, group_count, group_out); if (rc != 0) break; for (int i = 0; i < group_count; i++) { @@ -1654,17 +1654,17 @@ int pplx_mlx_embed_batch(pplx_mlx_ctx_t *ctx, const pplx_input_t *inputs, return rc; } -int pplx_mlx_embed_into(pplx_mlx_ctx_t *ctx, const int *token_ids, +int embed_mlx_encode_into(embed_mlx_ctx_t *ctx, const int *token_ids, int n_tokens, float *out_embedding) { - pplx_input_t input = { token_ids, n_tokens }; - return pplx_mlx_embed_batch(ctx, &input, 1, out_embedding); + embed_input_t input = { token_ids, n_tokens }; + return embed_mlx_encode_batch(ctx, &input, 1, out_embedding); } typedef struct { int index; int span_offset; - const pplx_context_input_t *input; + const embed_context_input_t *input; } mlx_context_batch_item_t; static int mlx_context_batch_item_cmp(const void *a, const void *b) @@ -1683,7 +1683,7 @@ static int mlx_context_padding_exceeds_limit(size_t padded, size_t tokens) return padded * 4 > tokens * 5; } -static int mlx_context_batch_should_split(const pplx_context_input_t *inputs, +static int mlx_context_batch_should_split(const embed_context_input_t *inputs, int batch, int *out_total_spans) { int max_seq = 0; @@ -1710,8 +1710,8 @@ static int mlx_context_batch_should_split(const pplx_context_input_t *inputs, return mlx_context_padding_exceeds_limit(padded, (size_t)total_tokens); } -static int pplx_mlx_embed_spans_batch_dense(pplx_mlx_ctx_t *ctx, - const pplx_context_input_t *inputs, +static int embed_mlx_encode_spans_batch_dense(embed_mlx_ctx_t *ctx, + const embed_context_input_t *inputs, int batch, float *out_embeddings) { if (!ctx || !inputs || batch <= 0 || !out_embeddings || @@ -1720,14 +1720,14 @@ static int pplx_mlx_embed_spans_batch_dense(pplx_mlx_ctx_t *ctx, !arr_ok(ctx->embed_tokens) || !arr_ok(ctx->norm)) return -1; - const pplx_config_t *c = &ctx->config; + const embed_config_t *c = &ctx->config; mlx_stream S = ctx->stream; int hidden = c->hidden_size; int max_seq = 0; int total_spans = 0; int has_padding = 0; for (int b = 0; b < batch; b++) { - const pplx_context_input_t *input = &inputs[b]; + const embed_context_input_t *input = &inputs[b]; int n_tokens = input->input.n_tokens; if (!input->input.ids || n_tokens <= 0 || !input->spans || input->n_spans <= 0 || total_spans > INT_MAX - input->n_spans) @@ -1773,7 +1773,7 @@ static int pplx_mlx_embed_spans_batch_dense(pplx_mlx_ctx_t *ctx, return -1; } for (int b = 0; b < batch; b++) { - const pplx_input_t *input = &inputs[b].input; + const embed_input_t *input = &inputs[b].input; for (int t = 0; t < max_seq; t++) { size_t row = (size_t)b * max_seq + t; if (t < input->n_tokens) { @@ -1855,7 +1855,7 @@ static int pplx_mlx_embed_spans_batch_dense(pplx_mlx_ctx_t *ctx, int pooled_count = 0; for (int b = 0; b < batch; b++) { for (int s = 0; s < inputs[b].n_spans; s++) { - const pplx_span_t *span = &inputs[b].spans[s]; + const embed_span_t *span = &inputs[b].spans[s]; int start[] = {b, span->start, 0}; int stop[] = {b + 1, span->start + span->n_tokens, hidden}; int strides[] = {1, 1, 1}; @@ -1891,7 +1891,7 @@ static int pplx_mlx_embed_spans_batch_dense(pplx_mlx_ctx_t *ctx, int span_count = 0; for (int b = 0; b < batch; b++) { for (int s = 0; s < inputs[b].n_spans; s++) { - const pplx_span_t *span = &inputs[b].spans[s]; + const embed_span_t *span = &inputs[b].spans[s]; int base = b * max_seq; int start = span->start; int stop = span->start + span->n_tokens; @@ -1994,8 +1994,8 @@ cleanup: return rc; } -int pplx_mlx_embed_spans_batch(pplx_mlx_ctx_t *ctx, - const pplx_context_input_t *inputs, int batch, +int embed_mlx_encode_spans_batch(embed_mlx_ctx_t *ctx, + const embed_context_input_t *inputs, int batch, float *out_embeddings) { if (!ctx || !inputs || batch <= 0 || !out_embeddings) @@ -2003,13 +2003,13 @@ int pplx_mlx_embed_spans_batch(pplx_mlx_ctx_t *ctx, int total_spans = 0; if (!mlx_context_batch_should_split(inputs, batch, &total_spans)) - return pplx_mlx_embed_spans_batch_dense(ctx, inputs, batch, + return embed_mlx_encode_spans_batch_dense(ctx, inputs, batch, out_embeddings); const int hidden = ctx->config.hidden_size; mlx_context_batch_item_t *items = (mlx_context_batch_item_t *)malloc( (size_t)batch * sizeof(*items)); - pplx_context_input_t *group_inputs = (pplx_context_input_t *)malloc( + embed_context_input_t *group_inputs = (embed_context_input_t *)malloc( (size_t)batch * sizeof(*group_inputs)); size_t group_values, group_bytes; if (mlx_mul_size((size_t)total_spans, (size_t)hidden, &group_values) != 0 || @@ -2060,7 +2060,7 @@ int pplx_mlx_embed_spans_batch(pplx_mlx_ctx_t *ctx, int group_count = end - start; for (int i = 0; i < group_count; i++) group_inputs[i] = *items[start + i].input; - rc = pplx_mlx_embed_spans_batch_dense(ctx, group_inputs, + rc = embed_mlx_encode_spans_batch_dense(ctx, group_inputs, group_count, group_out); if (rc != 0) break; int src_span = 0; @@ -2079,19 +2079,19 @@ int pplx_mlx_embed_spans_batch(pplx_mlx_ctx_t *ctx, return rc; } -int pplx_mlx_embed_spans(pplx_mlx_ctx_t *ctx, const int *token_ids, - int n_tokens, const pplx_span_t *spans, +int embed_mlx_encode_spans(embed_mlx_ctx_t *ctx, const int *token_ids, + int n_tokens, const embed_span_t *spans, int n_spans, float *out_embeddings) { - pplx_context_input_t input = { + embed_context_input_t input = { .input = { token_ids, n_tokens }, .spans = spans, .n_spans = n_spans, }; - return pplx_mlx_embed_spans_batch(ctx, &input, 1, out_embeddings); + return embed_mlx_encode_spans_batch(ctx, &input, 1, out_embeddings); } -float *pplx_mlx_embed(pplx_mlx_ctx_t *ctx, const int *token_ids, int n_tokens) +float *embed_mlx_encode(embed_mlx_ctx_t *ctx, const int *token_ids, int n_tokens) { if (!ctx || !token_ids || n_tokens <= 0) return NULL; @@ -2102,7 +2102,7 @@ float *pplx_mlx_embed(pplx_mlx_ctx_t *ctx, const int *token_ids, int n_tokens) float *out = (float *)malloc(out_bytes); if (!out) return NULL; - if (pplx_mlx_embed_into(ctx, token_ids, n_tokens, out) != 0) { + if (embed_mlx_encode_into(ctx, token_ids, n_tokens, out) != 0) { free(out); return NULL; } diff --git a/embed_mlx.h b/embed_mlx.h index be467d0..267c454 100644 --- a/embed_mlx.h +++ b/embed_mlx.h @@ -4,17 +4,17 @@ * Uses Apple's mlx-c to run the transformer on Metal. */ -#ifndef PPLX_EMBED_MLX_H -#define PPLX_EMBED_MLX_H +#ifndef EMBED_MLX_H +#define EMBED_MLX_H /* Public-API export annotation. The libraries are built with - * -fvisibility=hidden, so only declarations carrying PPLX_API are exported + * -fvisibility=hidden, so only declarations carrying EMBED_API are exported * from the shared library; everything else stays internal. */ -#ifndef PPLX_API +#ifndef EMBED_API #if defined(__GNUC__) -#define PPLX_API __attribute__((visibility("default"))) +#define EMBED_API __attribute__((visibility("default"))) #else -#define PPLX_API +#define EMBED_API #endif #endif @@ -23,37 +23,37 @@ #include "embed.h" /* Opaque MLX model context */ -typedef struct pplx_mlx_ctx pplx_mlx_ctx_t; -typedef struct pplx_mlx_late_ctx pplx_mlx_late_ctx_t; -typedef struct pplx_mlx_late_vectors pplx_mlx_late_vectors_t; +typedef struct embed_mlx_ctx embed_mlx_ctx_t; +typedef struct embed_mlx_late_ctx embed_mlx_late_ctx_t; +typedef struct embed_mlx_late_vectors embed_mlx_late_vectors_t; typedef struct { int quantize_bits; /* 0 disables; currently 8 */ int quantize_group_size; /* default 64 when quantize_bits is set */ -} pplx_mlx_options_t; +} embed_mlx_options_t; /* * Load model into MLX arrays from safetensors + config.json. * Returns NULL on error. */ -PPLX_API pplx_mlx_ctx_t *pplx_mlx_load(const char *model_dir); -PPLX_API pplx_mlx_ctx_t *pplx_mlx_load_with_options(const char *model_dir, - const pplx_mlx_options_t *opts); +EMBED_API embed_mlx_ctx_t *embed_mlx_load(const char *model_dir); +EMBED_API embed_mlx_ctx_t *embed_mlx_load_with_options(const char *model_dir, + const embed_mlx_options_t *opts); -PPLX_API void pplx_mlx_free(pplx_mlx_ctx_t *ctx); +EMBED_API void embed_mlx_free(embed_mlx_ctx_t *ctx); /* * Late-interaction MLX path. This is separate from pooled embeddings because * late snapshots produce one projected token vector per input token for MaxSim * scoring, not one pooled document vector. */ -PPLX_API pplx_mlx_late_ctx_t *pplx_mlx_late_load(const char *model_dir); -PPLX_API pplx_mlx_late_ctx_t *pplx_mlx_late_load_with_options( - const char *model_dir, const pplx_mlx_options_t *opts); -PPLX_API void pplx_mlx_late_free(pplx_mlx_late_ctx_t *ctx); +EMBED_API embed_mlx_late_ctx_t *embed_mlx_late_load(const char *model_dir); +EMBED_API embed_mlx_late_ctx_t *embed_mlx_late_load_with_options( + const char *model_dir, const embed_mlx_options_t *opts); +EMBED_API void embed_mlx_late_free(embed_mlx_late_ctx_t *ctx); -PPLX_API const pplx_config_t *pplx_mlx_late_config(const pplx_mlx_late_ctx_t *ctx); -PPLX_API int pplx_mlx_late_token_dim(const pplx_mlx_late_ctx_t *ctx); +EMBED_API const embed_config_t *embed_mlx_late_config(const embed_mlx_late_ctx_t *ctx); +EMBED_API int embed_mlx_late_token_dim(const embed_mlx_late_ctx_t *ctx); /* * Encode token_ids into out_vectors[n_tokens, token_dim]. @@ -61,7 +61,7 @@ PPLX_API int pplx_mlx_late_token_dim(const pplx_mlx_late_ctx_t *ctx); * If normalize is non-zero, every token vector is L2-normalized on device * before copying back to CPU memory. */ -PPLX_API int pplx_mlx_late_encode_tokens(pplx_mlx_late_ctx_t *ctx, +EMBED_API int embed_mlx_late_encode_tokens(embed_mlx_late_ctx_t *ctx, const int *token_ids, int n_tokens, int normalize, float *out_vectors); @@ -72,36 +72,36 @@ PPLX_API int pplx_mlx_late_encode_tokens(pplx_mlx_late_ctx_t *ctx, * outlive ctx. Copying is optional: MaxSim can consume these handles directly * and copy back only final candidate scores. */ -PPLX_API pplx_mlx_late_vectors_t *pplx_mlx_late_encode_tokens_device( - pplx_mlx_late_ctx_t *ctx, const int *token_ids, int n_tokens, +EMBED_API embed_mlx_late_vectors_t *embed_mlx_late_encode_tokens_device( + embed_mlx_late_ctx_t *ctx, const int *token_ids, int n_tokens, int normalize); -PPLX_API void pplx_mlx_late_vectors_free(pplx_mlx_late_vectors_t *vecs); -PPLX_API int pplx_mlx_late_vectors_token_count(const pplx_mlx_late_vectors_t *vecs); -PPLX_API int pplx_mlx_late_vectors_dim(const pplx_mlx_late_vectors_t *vecs); -PPLX_API int pplx_mlx_late_vectors_copy(const pplx_mlx_late_vectors_t *vecs, +EMBED_API void embed_mlx_late_vectors_free(embed_mlx_late_vectors_t *vecs); +EMBED_API int embed_mlx_late_vectors_token_count(const embed_mlx_late_vectors_t *vecs); +EMBED_API int embed_mlx_late_vectors_dim(const embed_mlx_late_vectors_t *vecs); +EMBED_API int embed_mlx_late_vectors_copy(const embed_mlx_late_vectors_t *vecs, float *out_vectors); -PPLX_API pplx_mlx_late_vectors_t *pplx_mlx_late_vectors_concat( - pplx_mlx_late_ctx_t *ctx, - const pplx_mlx_late_vectors_t *const *items, int count); -PPLX_API pplx_mlx_late_vectors_t *pplx_mlx_late_vectors_select( - pplx_mlx_late_ctx_t *ctx, const pplx_mlx_late_vectors_t *vecs, +EMBED_API embed_mlx_late_vectors_t *embed_mlx_late_vectors_concat( + embed_mlx_late_ctx_t *ctx, + const embed_mlx_late_vectors_t *const *items, int count); +EMBED_API embed_mlx_late_vectors_t *embed_mlx_late_vectors_select( + embed_mlx_late_ctx_t *ctx, const embed_mlx_late_vectors_t *vecs, const int *token_indices, int count); -PPLX_API int pplx_mlx_late_maxsim_batch_device( - pplx_mlx_late_ctx_t *ctx, const pplx_mlx_late_vectors_t *query, - const pplx_mlx_late_vectors_t *docs, const int *doc_offsets, +EMBED_API int embed_mlx_late_maxsim_batch_device( + embed_mlx_late_ctx_t *ctx, const embed_mlx_late_vectors_t *query, + const embed_mlx_late_vectors_t *docs, const int *doc_offsets, int docs_count, float *scores); /* * Compute embedding for token_ids[0..n_tokens-1]. * Returns malloc'd float[hidden_size] (caller frees). NULL on error. */ -PPLX_API float *pplx_mlx_embed(pplx_mlx_ctx_t *ctx, const int *token_ids, int n_tokens); +EMBED_API float *embed_mlx_encode(embed_mlx_ctx_t *ctx, const int *token_ids, int n_tokens); /* * Compute one embedding into caller-provided out[hidden_size]. * Returns 0 on success, -1 on error. */ -PPLX_API int pplx_mlx_embed_into(pplx_mlx_ctx_t *ctx, const int *token_ids, +EMBED_API int embed_mlx_encode_into(embed_mlx_ctx_t *ctx, const int *token_ids, int n_tokens, float *out_embedding); /* @@ -111,26 +111,26 @@ PPLX_API int pplx_mlx_embed_into(pplx_mlx_ctx_t *ctx, const int *token_ids, * Padded tokens are masked out of attention keys and mean pooling. * Returns 0 on success, -1 on error. */ -PPLX_API int pplx_mlx_embed_batch(pplx_mlx_ctx_t *ctx, const pplx_input_t *inputs, +EMBED_API int embed_mlx_encode_batch(embed_mlx_ctx_t *ctx, const embed_input_t *inputs, int batch, float *out_embeddings); /* * Run one contextual sequence and pool selected token spans. * Returns 0 on success, -1 on error. */ -PPLX_API int pplx_mlx_embed_spans(pplx_mlx_ctx_t *ctx, const int *token_ids, - int n_tokens, const pplx_span_t *spans, +EMBED_API int embed_mlx_encode_spans(embed_mlx_ctx_t *ctx, const int *token_ids, + int n_tokens, const embed_span_t *spans, int n_spans, float *out_embeddings); /* * Run a padded dense contextual document batch and pool every selected span. * out_embeddings contains spans in document order. */ -PPLX_API int pplx_mlx_embed_spans_batch(pplx_mlx_ctx_t *ctx, - const pplx_context_input_t *inputs, int batch, +EMBED_API int embed_mlx_encode_spans_batch(embed_mlx_ctx_t *ctx, + const embed_context_input_t *inputs, int batch, float *out_embeddings); /* Get the config (so embed_cli.c can read hidden_size etc.) */ -PPLX_API const pplx_config_t *pplx_mlx_config(const pplx_mlx_ctx_t *ctx); +EMBED_API const embed_config_t *embed_mlx_config(const embed_mlx_ctx_t *ctx); -#endif /* PPLX_EMBED_MLX_H */ +#endif /* EMBED_MLX_H */ diff --git a/embed_server.c b/embed_server.c index 061930c..4a9eeb8 100644 --- a/embed_server.c +++ b/embed_server.c @@ -46,29 +46,29 @@ #include <cjson/cJSON.h> -#define PPLX_HTTP_IO_BUF 8192 -#define PPLX_HTTP_MAX_HEADER (64u * 1024u) -#define PPLX_HTTP_MAX_BODY (64u * 1024u * 1024u) -#define PPLX_HTTP_MAX_PATH 255u -#define PPLX_HTTP_BACKLOG 128 -#define PPLX_HTTP_SETSIZE 10240 -#define PPLX_HTTP_CLIENT_TIMEOUT_MS 30000 -#define PPLX_HTTP_SWEEP_MS 1000 +#define EMBED_HTTP_IO_BUF 8192 +#define EMBED_HTTP_MAX_HEADER (64u * 1024u) +#define EMBED_HTTP_MAX_BODY (64u * 1024u * 1024u) +#define EMBED_HTTP_MAX_PATH 255u +#define EMBED_HTTP_BACKLOG 128 +#define EMBED_HTTP_SETSIZE 10240 +#define EMBED_HTTP_CLIENT_TIMEOUT_MS 30000 +#define EMBED_HTTP_SWEEP_MS 1000 -#define PPLX_API_MAX_STANDARD_INPUTS 512 -#define PPLX_API_MAX_CONTEXT_DOCS 512 -#define PPLX_API_MAX_CONTEXT_CHUNKS 16000 -#define PPLX_API_MAX_ITEM_TOKENS 32768 -#define PPLX_API_MAX_TOTAL_TOKENS 120000 +#define EMBED_API_MAX_STANDARD_INPUTS 512 +#define EMBED_API_MAX_CONTEXT_DOCS 512 +#define EMBED_API_MAX_CONTEXT_CHUNKS 16000 +#define EMBED_API_MAX_ITEM_TOKENS 32768 +#define EMBED_API_MAX_TOTAL_TOKENS 120000 /* Chosen from the L4 scheduler sweep: -b 32 gained ~24% concurrent * short-request throughput over 8 with no long-document penalty, while 128 * was no faster and inflated long-document tail latency. */ -#define PPLX_SERVER_DEFAULT_BATCH_SIZE 32 -#define PPLX_SERVER_DEFAULT_MAX_BATCH_TOKENS 16384 -#define PPLX_SERVER_BATCH_WAIT_US 1000 -#define PPLX_SERVER_MICROBATCH_MAX_JOBS 128 -#define PPLX_MLX_MEMORY_BUDGET_PERCENT 90 -#define PPLX_MLX_RESIDENT_MULTIPLIER 2 +#define EMBED_SERVER_DEFAULT_BATCH_SIZE 32 +#define EMBED_SERVER_DEFAULT_MAX_BATCH_TOKENS 16384 +#define EMBED_SERVER_BATCH_WAIT_US 1000 +#define EMBED_SERVER_MICROBATCH_MAX_JOBS 128 +#define EMBED_MLX_MEMORY_BUDGET_PERCENT 90 +#define EMBED_MLX_RESIDENT_MULTIPLIER 2 typedef struct { char *ptr; @@ -77,7 +77,7 @@ typedef struct { } sbuf; static void die_oom(void) { - fprintf(stderr, "pplx-serve: out of memory\n"); + fprintf(stderr, "embed-server: out of memory\n"); exit(1); } @@ -232,13 +232,13 @@ typedef struct { qwen_tokenizer_t *tok; qwen_tokenizer_workspace_t *tok_ws; int context_separator_id; - pplx_model_t *cpu_model; - pplx_workspace_t *cpu_ws; + embed_model_t *cpu_model; + embed_workspace_t *cpu_ws; #ifdef USE_MLX - pplx_mlx_ctx_t *mlx_ctx; + embed_mlx_ctx_t *mlx_ctx; #endif #ifdef USE_CUDA - pplx_cuda_ctx_t *cuda_ctx; + embed_cuda_ctx_t *cuda_ctx; #endif } loaded_model; @@ -280,7 +280,7 @@ typedef struct { typedef struct { char method[8]; - char path[PPLX_HTTP_MAX_PATH + 1]; + char path[EMBED_HTTP_MAX_PATH + 1]; char version[16]; char *body; size_t body_len; @@ -310,7 +310,7 @@ struct job { http_server *srv; client *c; char method[8]; - char path[PPLX_HTTP_MAX_PATH + 1]; + char path[EMBED_HTTP_MAX_PATH + 1]; char *body; size_t body_len; char *auth; @@ -576,7 +576,7 @@ static int parse_request_headers(client *c) { char *end = NULL; errno = 0; unsigned long long v = strtoull(cl, &end, 10); - if (errno || !end || *end || v > PPLX_HTTP_MAX_BODY) { + if (errno || !end || *end || v > EMBED_HTTP_MAX_BODY) { free(cl); return -1; } @@ -591,14 +591,14 @@ static int complete_request(client *c) { if (!c->header_done) { ssize_t hend = find_header_end(c->in.ptr, c->in.len); if (hend < 0) { - if (c->in.len > PPLX_HTTP_MAX_HEADER) return -1; + if (c->in.len > EMBED_HTTP_MAX_HEADER) return -1; return 0; } c->header_done = 1; c->header_len = (size_t)hend; if (parse_request_headers(c) != 0) return -1; } - if (c->content_length > PPLX_HTTP_MAX_BODY) return -1; + if (c->content_length > EMBED_HTTP_MAX_BODY) return -1; if (c->in.len < c->header_len + c->content_length) return 0; c->req.body_len = c->content_length; c->req.body = xmalloc(c->content_length + 1); @@ -824,7 +824,7 @@ static void dispatch_request(client *c) { static void read_cb(aeEventLoop *loop, int fd, void *clientData, int mask) { (void)loop; (void)mask; client *c = clientData; - char tmp[PPLX_HTTP_IO_BUF]; + char tmp[EMBED_HTTP_IO_BUF]; for (;;) { ssize_t n = read(fd, tmp, sizeof(tmp)); if (n > 0) { @@ -891,11 +891,11 @@ static int timeout_cb(aeEventLoop *loop, long long id, void *clientData) { * processed by the worker; long batch queues (e.g. 4B long-document * batches) must not be reaped as idle. */ if (!c->cancelled && c->refcount <= 1 && - now - c->last_active_ms > PPLX_HTTP_CLIENT_TIMEOUT_MS) + now - c->last_active_ms > EMBED_HTTP_CLIENT_TIMEOUT_MS) close_client(c); c = next; } - return PPLX_HTTP_SWEEP_MS; + return EMBED_HTTP_SWEEP_MS; } static void signal_cb(aeEventLoop *loop, int fd, void *clientData, int mask) { @@ -1095,7 +1095,7 @@ typedef struct { const char *encoding; int n_inputs; token_buf *tokens; - pplx_input_t *inputs; + embed_input_t *inputs; int total_tokens; int ready; } embedding_request; @@ -1103,7 +1103,7 @@ typedef struct { typedef struct { int *ids; int n_tokens; - pplx_span_t *spans; + embed_span_t *spans; int n_spans; } contextual_doc; @@ -1160,31 +1160,31 @@ static int tokenize_one(loaded_model *m, job *j, const char *text, return 0; } -static int model_embed_batch(loaded_model *m, const pplx_input_t *inputs, +static int model_embed_batch(loaded_model *m, const embed_input_t *inputs, int batch, float *out) { #ifdef USE_MLX if (m->mlx_ctx) - return pplx_mlx_embed_batch(m->mlx_ctx, inputs, batch, out); + return embed_mlx_encode_batch(m->mlx_ctx, inputs, batch, out); #endif #ifdef USE_CUDA if (m->cuda_ctx) - return pplx_cuda_embed_batch(m->cuda_ctx, inputs, batch, out); + return embed_cuda_encode_batch(m->cuda_ctx, inputs, batch, out); #endif - return pplx_model_embed_batch(m->cpu_model, m->cpu_ws, inputs, batch, out); + return embed_model_encode_batch(m->cpu_model, m->cpu_ws, inputs, batch, out); } static int model_embed_spans_batch(loaded_model *m, - const pplx_context_input_t *inputs, + const embed_context_input_t *inputs, int batch, float *out) { #ifdef USE_MLX if (m->mlx_ctx) - return pplx_mlx_embed_spans_batch(m->mlx_ctx, inputs, batch, out); + return embed_mlx_encode_spans_batch(m->mlx_ctx, inputs, batch, out); #endif #ifdef USE_CUDA if (m->cuda_ctx) - return pplx_cuda_embed_spans_batch(m->cuda_ctx, inputs, batch, out); + return embed_cuda_encode_spans_batch(m->cuda_ctx, inputs, batch, out); #endif - return pplx_model_embed_spans_batch(m->cpu_model, m->cpu_ws, inputs, batch, + return embed_model_encode_spans_batch(m->cpu_model, m->cpu_ws, inputs, batch, out); } @@ -1274,7 +1274,7 @@ static int embedding_request_compatible(const embedding_request *a, } typedef struct { - pplx_input_t input; + embed_input_t input; int output_index; } embedding_batch_item; @@ -1319,7 +1319,7 @@ static void execute_embedding_request_list(embedding_request **reqs, int n_reqs) ? reqs[0]->j->srv->batch_size : total_inputs; if (max_batch > total_inputs) max_batch = total_inputs; int max_batch_tokens = reqs[0]->j->srv->max_batch_tokens; - pplx_input_t *inputs = xmalloc((size_t)max_batch * sizeof(*inputs)); + embed_input_t *inputs = xmalloc((size_t)max_batch * sizeof(*inputs)); float *batch_embs = xmalloc((size_t)max_batch * dim * sizeof(float)); int failed = 0; @@ -1414,7 +1414,7 @@ static int contextual_request_fits_group(const contextual_request *r, } typedef struct { - pplx_context_input_t input; + embed_context_input_t input; int output_span_index; int order; } contextual_batch_item; @@ -1476,7 +1476,7 @@ static void execute_contextual_request_list(contextual_request **reqs, ? reqs[0]->j->srv->batch_size : total_docs; if (max_batch > total_docs) max_batch = total_docs; int max_batch_tokens = reqs[0]->j->srv->max_batch_tokens; - pplx_context_input_t *inputs = + embed_context_input_t *inputs = xmalloc((size_t)max_batch * sizeof(*inputs)); float *batch_embs = NULL; size_t batch_emb_cap = 0; @@ -1488,7 +1488,7 @@ static void execute_contextual_request_list(contextual_request **reqs, int tokens = 0; int chunks = 0; while (start + cur < total_docs && cur < max_batch) { - const pplx_context_input_t *input = &items[start + cur].input; + const embed_context_input_t *input = &items[start + cur].input; if (!inference_batch_accepts_input(m, cur, tokens, input->input.n_tokens, max_batch_tokens)) @@ -1605,7 +1605,7 @@ static void prepare_embedding_request(job *j, cJSON *root, http_server *s, if (n_inputs < 1) ve_add(detail, "[\"body\",\"input\"]", "input must contain at least 1 item", "value_error.list.min_items"); - else if (n_inputs > PPLX_API_MAX_STANDARD_INPUTS) + else if (n_inputs > EMBED_API_MAX_STANDARD_INPUTS) ve_add(detail, "[\"body\",\"input\"]", "input must contain at most 512 items", "value_error.list.max_items"); cJSON *item; @@ -1646,7 +1646,7 @@ static void prepare_embedding_request(job *j, cJSON *root, http_server *s, out->inputs[0].ids = out->tokens[0].ids; out->inputs[0].n_tokens = out->tokens[0].n_tokens; out->total_tokens = out->tokens[0].n_tokens; - if (out->tokens[0].n_tokens > PPLX_API_MAX_ITEM_TOKENS) + if (out->tokens[0].n_tokens > EMBED_API_MAX_ITEM_TOKENS) ve_add(detail, "[\"body\",\"input\"]", "input exceeds 32768 token limit", "value_error.context_length"); @@ -1670,7 +1670,7 @@ static void prepare_embedding_request(job *j, cJSON *root, http_server *s, out->inputs[idx].ids = out->tokens[idx].ids; out->inputs[idx].n_tokens = out->tokens[idx].n_tokens; out->total_tokens += out->tokens[idx].n_tokens; - if (out->tokens[idx].n_tokens > PPLX_API_MAX_ITEM_TOKENS) + if (out->tokens[idx].n_tokens > EMBED_API_MAX_ITEM_TOKENS) ve_add(detail, loc, "input exceeds 32768 token limit", "value_error.context_length"); } @@ -1678,7 +1678,7 @@ static void prepare_embedding_request(job *j, cJSON *root, http_server *s, } } - if (out->total_tokens > PPLX_API_MAX_TOTAL_TOKENS) + if (out->total_tokens > EMBED_API_MAX_TOTAL_TOKENS) ve_add(detail, "[\"body\",\"input\"]", "request exceeds 120000 token limit", "value_error.context_length"); } @@ -1721,7 +1721,7 @@ static void prepare_contextual_request(job *j, cJSON *root, http_server *s, if (n_docs < 1) ve_add(detail, "[\"body\",\"input\"]", "input must contain at least 1 document", "value_error.list.min_items"); - else if (n_docs > PPLX_API_MAX_CONTEXT_DOCS) + else if (n_docs > EMBED_API_MAX_CONTEXT_DOCS) ve_add(detail, "[\"body\",\"input\"]", "input must contain at most 512 documents", "value_error.list.max_items"); cJSON *doc_arr; @@ -1736,8 +1736,8 @@ static void prepare_contextual_request(job *j, cJSON *root, http_server *s, continue; } int doc_chunks = cJSON_GetArraySize(doc_arr); - if (doc_chunks > PPLX_API_MAX_CONTEXT_CHUNKS - total_chunks) - total_chunks = PPLX_API_MAX_CONTEXT_CHUNKS + 1; + if (doc_chunks > EMBED_API_MAX_CONTEXT_CHUNKS - total_chunks) + total_chunks = EMBED_API_MAX_CONTEXT_CHUNKS + 1; else total_chunks += doc_chunks; if (doc_chunks < 1) { @@ -1760,7 +1760,7 @@ static void prepare_contextual_request(job *j, cJSON *root, http_server *s, } di++; } - if (total_chunks > PPLX_API_MAX_CONTEXT_CHUNKS) + if (total_chunks > EMBED_API_MAX_CONTEXT_CHUNKS) ve_add(detail, "[\"body\",\"input\"]", "input must contain at most 16000 chunks", "value_error.list.max_items"); } @@ -1804,7 +1804,7 @@ static void prepare_contextual_request(job *j, cJSON *root, http_server *s, ci++; } - if (doc_tokens > PPLX_API_MAX_ITEM_TOKENS) { + if (doc_tokens > EMBED_API_MAX_ITEM_TOKENS) { char loc[64]; snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d]", di); ve_add(detail, loc, "document exceeds 32768 token limit", @@ -1832,7 +1832,7 @@ static void prepare_contextual_request(job *j, cJSON *root, http_server *s, free(chunk_tokens); di++; } - if (request_tokens > PPLX_API_MAX_TOTAL_TOKENS) { + if (request_tokens > EMBED_API_MAX_TOTAL_TOKENS) { ve_add(detail, "[\"body\",\"input\"]", "request exceeds 120000 token limit", "value_error.context_length"); @@ -1994,21 +1994,21 @@ static void *worker_main(void *arg) { for (int i = 0; i < 4; i++) { loaded_model *m = &s->models[i]; if (!m->info) continue; - pplx_mlx_options_t mlx_opts = { + embed_mlx_options_t mlx_opts = { .quantize_bits = s->mlx_quantize_bits, .quantize_group_size = s->mlx_quantize_group_size, }; - m->mlx_ctx = pplx_mlx_load_with_options(m->path, &mlx_opts); + m->mlx_ctx = embed_mlx_load_with_options(m->path, &mlx_opts); if (!m->mlx_ctx) { - server_log("pplx-serve: failed to load MLX model on worker: %s", + server_log("embed-server: failed to load MLX model on worker: %s", m->path); rc = 1; break; } - if (pplx_mlx_config(m->mlx_ctx)->hidden_size != m->info->dim) { - server_log("pplx-serve: model %s has unexpected dimension %d", + if (embed_mlx_config(m->mlx_ctx)->hidden_size != m->info->dim) { + server_log("embed-server: model %s has unexpected dimension %d", m->info->id, - pplx_mlx_config(m->mlx_ctx)->hidden_size); + embed_mlx_config(m->mlx_ctx)->hidden_size); rc = 1; break; } @@ -2023,7 +2023,7 @@ static void *worker_main(void *arg) { for (int i = 0; i < 4; i++) { loaded_model *m = &s->models[i]; if (m->mlx_ctx) { - pplx_mlx_free(m->mlx_ctx); + embed_mlx_free(m->mlx_ctx); m->mlx_ctx = NULL; } } @@ -2037,17 +2037,17 @@ static void *worker_main(void *arg) { for (int i = 0; i < 4; i++) { loaded_model *m = &s->models[i]; if (!m->info) continue; - m->cuda_ctx = pplx_cuda_load(m->path); + m->cuda_ctx = embed_cuda_load(m->path); if (!m->cuda_ctx) { - server_log("pplx-serve: failed to load CUDA model on worker: %s", + server_log("embed-server: failed to load CUDA model on worker: %s", m->path); rc = 1; break; } - if (pplx_cuda_config(m->cuda_ctx)->hidden_size != m->info->dim) { - server_log("pplx-serve: model %s has unexpected dimension %d", + if (embed_cuda_config(m->cuda_ctx)->hidden_size != m->info->dim) { + server_log("embed-server: model %s has unexpected dimension %d", m->info->id, - pplx_cuda_config(m->cuda_ctx)->hidden_size); + embed_cuda_config(m->cuda_ctx)->hidden_size); rc = 1; break; } @@ -2062,7 +2062,7 @@ static void *worker_main(void *arg) { for (int i = 0; i < 4; i++) { loaded_model *m = &s->models[i]; if (m->cuda_ctx) { - pplx_cuda_free(m->cuda_ctx); + embed_cuda_free(m->cuda_ctx); m->cuda_ctx = NULL; } } @@ -2080,7 +2080,7 @@ static void *worker_main(void *arg) { for (;;) { job *j = dequeue_job(s); if (!j) break; - job *jobs[PPLX_SERVER_MICROBATCH_MAX_JOBS]; + job *jobs[EMBED_SERVER_MICROBATCH_MAX_JOBS]; int n_jobs = 1; jobs[0] = j; @@ -2090,7 +2090,7 @@ static void *worker_main(void *arg) { if (s->batch_wait_us > 0) usleep((useconds_t)s->batch_wait_us); n_jobs += drain_ready_jobs(s, jobs + 1, - PPLX_SERVER_MICROBATCH_MAX_JOBS - 1); + EMBED_SERVER_MICROBATCH_MAX_JOBS - 1); } process_job_group(s, jobs, n_jobs); @@ -2100,7 +2100,7 @@ static void *worker_main(void *arg) { for (int i = 0; i < 4; i++) { loaded_model *m = &s->models[i]; if (m->mlx_ctx) { - pplx_mlx_free(m->mlx_ctx); + embed_mlx_free(m->mlx_ctx); m->mlx_ctx = NULL; } } @@ -2111,7 +2111,7 @@ static void *worker_main(void *arg) { for (int i = 0; i < 4; i++) { loaded_model *m = &s->models[i]; if (m->cuda_ctx) { - pplx_cuda_free(m->cuda_ctx); + embed_cuda_free(m->cuda_ctx); m->cuda_ctx = NULL; } } @@ -2124,10 +2124,10 @@ static int listen_on(const char *host, int port) { char portbuf[32]; snprintf(portbuf, sizeof(portbuf), "%d", port); char err[ANET_ERR_LEN]; - int fd = anetTcpServer(err, port, (char *)host, PPLX_HTTP_BACKLOG); + int fd = anetTcpServer(err, port, (char *)host, EMBED_HTTP_BACKLOG); (void)portbuf; if (fd == ANET_ERR) { - server_log("pplx-serve: listen failed on %s:%d: %s", host, port, err); + server_log("embed-server: listen failed on %s:%d: %s", host, port, err); return -1; } set_nonblock(fd); @@ -2142,12 +2142,12 @@ static int load_one_model(http_server *s, model_slot slot, const char *path) { snprintf(vocab_path, sizeof(vocab_path), "%s/vocab.json", path); m->tok = qwen_tokenizer_load(vocab_path); if (!m->tok) { - server_log("pplx-serve: failed to load tokenizer: %s", vocab_path); + server_log("embed-server: failed to load tokenizer: %s", vocab_path); return -1; } m->tok_ws = qwen_tokenizer_workspace_new(); if (!m->tok_ws) { - server_log("pplx-serve: failed to allocate tokenizer workspace: %s", + server_log("embed-server: failed to allocate tokenizer workspace: %s", path); return -1; } @@ -2158,7 +2158,7 @@ static int load_one_model(http_server *s, model_slot slot, const char *path) { * inside that model's embedding table. */ int sep_id = qwen_tokenizer_token_id(m->tok, "<|endoftext|>"); m->context_separator_id = sep_id >= 0 - ? sep_id : PPLX_CONTEXT_SEPARATOR_TOKEN_ID; + ? sep_id : EMBED_CONTEXT_SEPARATOR_TOKEN_ID; #ifdef USE_MLX if (s->use_mlx) { /* MLX streams are thread-local. The inference worker loads the MLX @@ -2172,19 +2172,19 @@ static int load_one_model(http_server *s, model_slot slot, const char *path) { return 0; } #endif - m->cpu_model = pplx_model_load(path); + m->cpu_model = embed_model_load(path); if (!m->cpu_model) { - server_log("pplx-serve: failed to load model: %s", path); + server_log("embed-server: failed to load model: %s", path); return -1; } - m->cpu_ws = pplx_workspace_new(m->cpu_model); + m->cpu_ws = embed_workspace_new(m->cpu_model); if (!m->cpu_ws) { - server_log("pplx-serve: failed to allocate workspace: %s", path); + server_log("embed-server: failed to allocate workspace: %s", path); return -1; } - if (pplx_model_config(m->cpu_model)->hidden_size != m->info->dim) { - server_log("pplx-serve: model %s has unexpected dimension %d", - m->info->id, pplx_model_config(m->cpu_model)->hidden_size); + if (embed_model_config(m->cpu_model)->hidden_size != m->info->dim) { + server_log("embed-server: model %s has unexpected dimension %d", + m->info->id, embed_model_config(m->cpu_model)->hidden_size); return -1; } return 0; @@ -2196,13 +2196,13 @@ static void free_models(http_server *s) { free(m->path); if (m->tok_ws) qwen_tokenizer_workspace_free(m->tok_ws); if (m->tok) qwen_tokenizer_free(m->tok); - if (m->cpu_ws) pplx_workspace_free(m->cpu_ws); - if (m->cpu_model) pplx_model_free(m->cpu_model); + if (m->cpu_ws) embed_workspace_free(m->cpu_ws); + if (m->cpu_model) embed_model_free(m->cpu_model); #ifdef USE_MLX - if (m->mlx_ctx) pplx_mlx_free(m->mlx_ctx); + if (m->mlx_ctx) embed_mlx_free(m->mlx_ctx); #endif #ifdef USE_CUDA - if (m->cuda_ctx) pplx_cuda_free(m->cuda_ctx); + if (m->cuda_ctx) embed_cuda_free(m->cuda_ctx); #endif } } @@ -2223,23 +2223,23 @@ static uint64_t physical_memory_nbytes(void) { return (uint64_t)pages * (uint64_t)page_size; } -static int validate_model_specs(const pplx_server_config_t *cfg) { +static int validate_model_specs(const embed_server_config_t *cfg) { int seen[4] = {0}; for (int i = 0; i < cfg->n_models; i++) { - const pplx_server_model_spec_t *spec = &cfg->models[i]; + const embed_server_model_spec_t *spec = &cfg->models[i]; model_slot slot = model_slot_for_id(spec->id); if (slot == MODEL_UNKNOWN) { - fprintf(stderr, "pplx-serve: unknown model id: %s\n", + fprintf(stderr, "embed-server: unknown model id: %s\n", spec->id ? spec->id : "<null>"); return -1; } if (!spec->path || !spec->path[0]) { - fprintf(stderr, "pplx-serve: model %s has an empty path\n", + fprintf(stderr, "embed-server: model %s has an empty path\n", spec->id); return -1; } if (seen[slot]) { - fprintf(stderr, "pplx-serve: duplicate model id: %s\n", spec->id); + fprintf(stderr, "embed-server: duplicate model id: %s\n", spec->id); return -1; } seen[slot] = 1; @@ -2247,7 +2247,7 @@ static int validate_model_specs(const pplx_server_config_t *cfg) { return 0; } -static int mlx_memory_preflight(const pplx_server_config_t *cfg) { +static int mlx_memory_preflight(const embed_server_config_t *cfg) { if (!cfg->use_mlx) return 0; uint64_t payload = 0; @@ -2257,55 +2257,55 @@ static int mlx_memory_preflight(const pplx_server_config_t *cfg) { size_t model_payload = 0; if (!ms || multi_safetensors_data_nbytes(ms, &model_payload) != 0) { multi_safetensors_close(ms); - fprintf(stderr, "pplx-serve: failed to inspect MLX model: %s\n", + fprintf(stderr, "embed-server: failed to inspect MLX model: %s\n", cfg->models[i].path); return -1; } multi_safetensors_close(ms); if ((uint64_t)model_payload > UINT64_MAX - payload) { - fprintf(stderr, "pplx-serve: MLX model payload size overflow\n"); + fprintf(stderr, "embed-server: MLX model payload size overflow\n"); return -1; } payload += (uint64_t)model_payload; } - if (payload > UINT64_MAX / PPLX_MLX_RESIDENT_MULTIPLIER) { - fprintf(stderr, "pplx-serve: MLX resident-memory estimate overflow\n"); + if (payload > UINT64_MAX / EMBED_MLX_RESIDENT_MULTIPLIER) { + fprintf(stderr, "embed-server: MLX resident-memory estimate overflow\n"); return -1; } - uint64_t estimated = payload * PPLX_MLX_RESIDENT_MULTIPLIER; + uint64_t estimated = payload * EMBED_MLX_RESIDENT_MULTIPLIER; uint64_t physical = physical_memory_nbytes(); if (physical == 0) { - server_log("pplx-serve: warning: could not determine physical memory; " + server_log("embed-server: warning: could not determine physical memory; " "skipping MLX memory preflight"); return 0; } double utilization = cfg->memory_utilization > 0.0 ? cfg->memory_utilization - : PPLX_MLX_MEMORY_BUDGET_PERCENT / 100.0; + : EMBED_MLX_MEMORY_BUDGET_PERCENT / 100.0; uint64_t budget = (uint64_t)((double)physical * utilization); const double gib = 1024.0 * 1024.0 * 1024.0; - server_log("pplx-serve: MLX memory preflight: %.1f GiB tensors, " + server_log("embed-server: MLX memory preflight: %.1f GiB tensors, " "%.1f GiB estimated resident, %.1f GiB budget " "(%.2f of physical memory)", (double)payload / gib, (double)estimated / gib, (double)budget / gib, utilization); if (cfg->mlx_quantize_bits) { - server_log("pplx-serve: MLX %d-bit quantization enabled; preflight " + server_log("embed-server: MLX %d-bit quantization enabled; preflight " "uses source payload as a conservative peak estimate", cfg->mlx_quantize_bits); } if (estimated <= budget) return 0; - server_log("pplx-serve: refusing MLX model set above the host-memory " + server_log("embed-server: refusing MLX model set above the host-memory " "budget"); - server_log("pplx-serve: use BF16 artifacts, load fewer models, or raise " + server_log("embed-server: use BF16 artifacts, load fewer models, or raise " "--memory-utilization (values above 1.0 overcommit)"); return -1; } -int pplx_run_server(const pplx_server_config_t *cfg) { +int embed_run_server(const embed_server_config_t *cfg) { if (!cfg || !cfg->models || cfg->n_models <= 0) { - fprintf(stderr, "pplx-serve: at least one --model MODEL_ID=PATH is required\n"); + fprintf(stderr, "embed-server: at least one --model MODEL_ID=PATH is required\n"); return 1; } if (validate_model_specs(cfg) != 0 || mlx_memory_preflight(cfg) != 0) @@ -2316,11 +2316,11 @@ int pplx_run_server(const pplx_server_config_t *cfg) { s.host = cfg->host && cfg->host[0] ? cfg->host : "127.0.0.1"; s.port = cfg->port > 0 ? cfg->port : 8000; s.batch_size = cfg->batch_size > 0 - ? cfg->batch_size : PPLX_SERVER_DEFAULT_BATCH_SIZE; + ? cfg->batch_size : EMBED_SERVER_DEFAULT_BATCH_SIZE; s.max_batch_tokens = cfg->max_batch_tokens > 0 - ? cfg->max_batch_tokens : PPLX_SERVER_DEFAULT_MAX_BATCH_TOKENS; + ? cfg->max_batch_tokens : EMBED_SERVER_DEFAULT_MAX_BATCH_TOKENS; s.batch_wait_us = cfg->batch_wait_us >= 0 - ? cfg->batch_wait_us : PPLX_SERVER_BATCH_WAIT_US; + ? cfg->batch_wait_us : EMBED_SERVER_BATCH_WAIT_US; s.enable_cors = cfg->enable_cors; s.use_mlx = cfg->use_mlx; s.use_cuda = cfg->use_cuda; @@ -2330,7 +2330,7 @@ int pplx_run_server(const pplx_server_config_t *cfg) { if (cfg->api_key && cfg->api_key[0]) s.api_key = xstrdup(cfg->api_key); else { - const char *env_key = getenv("PPLX_API_KEY"); + const char *env_key = getenv("EMBED_API_KEY"); if (env_key && env_key[0]) s.api_key = xstrdup(env_key); } @@ -2340,7 +2340,7 @@ int pplx_run_server(const pplx_server_config_t *cfg) { for (int i = 0; i < cfg->n_models; i++) { model_slot slot = model_slot_for_id(cfg->models[i].id); if (slot == MODEL_UNKNOWN) { - fprintf(stderr, "pplx-serve: unknown model id: %s\n", cfg->models[i].id); + fprintf(stderr, "embed-server: unknown model id: %s\n", cfg->models[i].id); free_models(&s); return 1; } @@ -2369,15 +2369,15 @@ int pplx_run_server(const pplx_server_config_t *cfg) { signal(SIGPIPE, SIG_IGN); g_signal_wfd = s.signal_pipe[1]; - s.loop = aeCreateEventLoop(PPLX_HTTP_SETSIZE); + s.loop = aeCreateEventLoop(EMBED_HTTP_SETSIZE); if (!s.loop) { - fprintf(stderr, "pplx-serve: failed to create event loop\n"); + fprintf(stderr, "embed-server: failed to create event loop\n"); free_models(&s); return 1; } if (pthread_create(&s.worker, NULL, worker_main, &s) != 0) { - fprintf(stderr, "pplx-serve: failed to start worker\n"); + fprintf(stderr, "embed-server: failed to start worker\n"); aeDeleteEventLoop(s.loop); free_models(&s); return 1; @@ -2412,12 +2412,12 @@ int pplx_run_server(const pplx_server_config_t *cfg) { aeCreateFileEvent(s.loop, s.listen_fd, AE_READABLE, accept_cb, &s); aeCreateFileEvent(s.loop, s.completion_pipe[0], AE_READABLE, completion_cb, &s); aeCreateFileEvent(s.loop, s.signal_pipe[0], AE_READABLE, signal_cb, &s); - aeCreateTimeEvent(s.loop, PPLX_HTTP_SWEEP_MS, timeout_cb, &s, NULL); + aeCreateTimeEvent(s.loop, EMBED_HTTP_SWEEP_MS, timeout_cb, &s, NULL); - server_log("pplx-serve: listening on http://%s:%d", s.host, s.port); + server_log("embed-server: listening on http://%s:%d", s.host, s.port); aeMain(s.loop); - server_log("pplx-serve: shutdown requested"); + server_log("embed-server: shutdown requested"); if (s.listen_fd >= 0) close(s.listen_fd); client *c = s.clients; while (c) { @@ -2490,7 +2490,7 @@ static void print_usage(const char *prog) } typedef struct { - pplx_server_model_spec_t *v; + embed_server_model_spec_t *v; int n; int cap; } model_specs_t; @@ -2684,7 +2684,7 @@ int main(int argc, char *argv[]) arg++; } - pplx_verbose = verbose; + embed_verbose = verbose; qwen_verbose = verbose; if (arg < argc) { @@ -2712,7 +2712,7 @@ int main(int argc, char *argv[]) free_model_specs(&model_specs); return 1; } - if (pplx_cuda_set_fast_gemm(cuda_fast_gemm) != 0) { + if (embed_cuda_set_fast_gemm(cuda_fast_gemm) != 0) { fprintf(stderr, "--cuda-gemm-mode must be f32, tf32, bf16, or 16f\n"); free_model_specs(&model_specs); return 1; @@ -2732,9 +2732,9 @@ int main(int argc, char *argv[]) return 1; } if (!strcmp(cuda_weights, "bf16")) { - pplx_cuda_set_weights_bf16(1); + embed_cuda_set_weights_bf16(1); } else if (!strcmp(cuda_weights, "f32")) { - pplx_cuda_set_weights_bf16(0); + embed_cuda_set_weights_bf16(0); } else { fprintf(stderr, "--cuda-weight-dtype must be f32 or bf16\n"); free_model_specs(&model_specs); @@ -2755,12 +2755,12 @@ int main(int argc, char *argv[]) fprintf(stderr, "Using %s GPU backend\n", use_mlx ? "MLX" : "CUDA"); } - pplx_server_config_t scfg = { + embed_server_config_t scfg = { .models = model_specs.v, .n_models = model_specs.n, .host = host, .port = port, - .batch_size = batch_size > 0 ? batch_size : PPLX_SERVER_DEFAULT_BATCH_SIZE, + .batch_size = batch_size > 0 ? batch_size : EMBED_SERVER_DEFAULT_BATCH_SIZE, .max_batch_tokens = max_batch_tokens, .batch_wait_us = batch_wait_us, .use_mlx = use_mlx, @@ -2771,7 +2771,7 @@ int main(int argc, char *argv[]) .enable_cors = cors, .api_key = api_key, }; - int rc = pplx_run_server(&scfg); + int rc = embed_run_server(&scfg); free_model_specs(&model_specs); return rc; } diff --git a/embed_server.h b/embed_server.h index 179ed0b..80c9aca 100644 --- a/embed_server.h +++ b/embed_server.h @@ -1,14 +1,14 @@ -#ifndef PPLX_SERVER_H -#define PPLX_SERVER_H +#ifndef EMBED_SERVER_H +#define EMBED_SERVER_H /* Public-API export annotation. The libraries are built with - * -fvisibility=hidden, so only declarations carrying PPLX_API are exported + * -fvisibility=hidden, so only declarations carrying EMBED_API are exported * from the shared library; everything else stays internal. */ -#ifndef PPLX_API +#ifndef EMBED_API #if defined(__GNUC__) -#define PPLX_API __attribute__((visibility("default"))) +#define EMBED_API __attribute__((visibility("default"))) #else -#define PPLX_API +#define EMBED_API #endif #endif @@ -17,10 +17,10 @@ typedef struct { const char *id; const char *path; -} pplx_server_model_spec_t; +} embed_server_model_spec_t; typedef struct { - const pplx_server_model_spec_t *models; + const embed_server_model_spec_t *models; int n_models; const char *host; int port; @@ -36,8 +36,8 @@ typedef struct { values above 1.0 allow overcommit */ int enable_cors; const char *api_key; -} pplx_server_config_t; +} embed_server_config_t; -PPLX_API int pplx_run_server(const pplx_server_config_t *cfg); +EMBED_API int embed_run_server(const embed_server_config_t *cfg); #endif diff --git a/qwen_kernels.h b/qwen_kernels.h index 9b8156c..71f77f1 100644 --- a/qwen_kernels.h +++ b/qwen_kernels.h @@ -8,13 +8,13 @@ #define QWEN_KERNELS_H /* Public-API export annotation. The libraries are built with - * -fvisibility=hidden, so only declarations carrying PPLX_API are exported + * -fvisibility=hidden, so only declarations carrying EMBED_API are exported * from the shared library; everything else stays internal. */ -#ifndef PPLX_API +#ifndef EMBED_API #if defined(__GNUC__) -#define PPLX_API __attribute__((visibility("default"))) +#define EMBED_API __attribute__((visibility("default"))) #else -#define PPLX_API +#define EMBED_API #endif #endif @@ -142,12 +142,12 @@ void qwen_apply_rope_neox(float *x, const float *cos_vals, const float *sin_vals /* Set number of threads for parallel operations (default: 1). * Creates a persistent thread pool. Call before inference. */ -PPLX_API void qwen_set_threads(int n); +EMBED_API void qwen_set_threads(int n); /* Get number of available CPU cores */ -PPLX_API int qwen_get_num_cpus(void); +EMBED_API int qwen_get_num_cpus(void); /* Global verbose flag */ -PPLX_API extern int qwen_verbose; +EMBED_API extern int qwen_verbose; #endif /* QWEN_KERNELS_H */ diff --git a/qwen_tokenizer.h b/qwen_tokenizer.h index 4c3834c..d7670b7 100644 --- a/qwen_tokenizer.h +++ b/qwen_tokenizer.h @@ -10,13 +10,13 @@ #define QWEN_TOKENIZER_H /* Public-API export annotation. The libraries are built with - * -fvisibility=hidden, so only declarations carrying PPLX_API are exported + * -fvisibility=hidden, so only declarations carrying EMBED_API are exported * from the shared library; everything else stays internal. */ -#ifndef PPLX_API +#ifndef EMBED_API #if defined(__GNUC__) -#define PPLX_API __attribute__((visibility("default"))) +#define EMBED_API __attribute__((visibility("default"))) #else -#define PPLX_API +#define EMBED_API #endif #endif @@ -43,29 +43,29 @@ typedef struct { typedef struct qwen_tokenizer_workspace qwen_tokenizer_workspace_t; /* Load tokenizer from vocab.json in model directory */ -PPLX_API qwen_tokenizer_t *qwen_tokenizer_load(const char *vocab_json_path); +EMBED_API qwen_tokenizer_t *qwen_tokenizer_load(const char *vocab_json_path); /* Decode a single token ID to text. Returns pointer to internal string. */ -PPLX_API const char *qwen_tokenizer_decode(const qwen_tokenizer_t *tok, int token_id); +EMBED_API const char *qwen_tokenizer_decode(const qwen_tokenizer_t *tok, int token_id); /* Look up the id of a raw vocab.json token string (e.g. "<|endoftext|>"). * Returns -1 when the token is not in the vocab. */ -PPLX_API int qwen_tokenizer_token_id(const qwen_tokenizer_t *tok, const char *token); +EMBED_API int qwen_tokenizer_token_id(const qwen_tokenizer_t *tok, const char *token); /* Encode UTF-8 text into token IDs using BPE. * Returns malloc'd array of token IDs and sets *out_n_tokens. * Returns NULL on error (and sets *out_n_tokens to 0). */ -PPLX_API int *qwen_tokenizer_encode(const qwen_tokenizer_t *tok, const char *text, int *out_n_tokens); +EMBED_API int *qwen_tokenizer_encode(const qwen_tokenizer_t *tok, const char *text, int *out_n_tokens); /* Reusable scratch for allocation-light repeated encoding. */ -PPLX_API qwen_tokenizer_workspace_t *qwen_tokenizer_workspace_new(void); -PPLX_API void qwen_tokenizer_workspace_free(qwen_tokenizer_workspace_t *ws); +EMBED_API qwen_tokenizer_workspace_t *qwen_tokenizer_workspace_new(void); +EMBED_API void qwen_tokenizer_workspace_free(qwen_tokenizer_workspace_t *ws); /* Encode into caller-provided out_ids[0..out_cap-1]. * Returns 0 on success, -1 on error, -2 if out_cap is too small. * In all cases *out_n_tokens is set to the number of tokens needed/emitted * when out_n_tokens is non-NULL. */ -PPLX_API int qwen_tokenizer_encode_into(const qwen_tokenizer_t *tok, +EMBED_API int qwen_tokenizer_encode_into(const qwen_tokenizer_t *tok, qwen_tokenizer_workspace_t *ws, const char *text, int *out_ids, int out_cap, @@ -73,12 +73,12 @@ PPLX_API int qwen_tokenizer_encode_into(const qwen_tokenizer_t *tok, /* Same tokenization as qwen_tokenizer_encode(), but reuses ws internally and * performs only the final returned int[] allocation. */ -PPLX_API int *qwen_tokenizer_encode_with_workspace(const qwen_tokenizer_t *tok, +EMBED_API int *qwen_tokenizer_encode_with_workspace(const qwen_tokenizer_t *tok, qwen_tokenizer_workspace_t *ws, const char *text, int *out_n_tokens); /* Free tokenizer */ -PPLX_API void qwen_tokenizer_free(qwen_tokenizer_t *tok); +EMBED_API void qwen_tokenizer_free(qwen_tokenizer_t *tok); #endif /* QWEN_TOKENIZER_H */ diff --git a/tests/test_bf16_model.c b/tests/test_bf16_model.c index 392f040..cc9b2fb 100644 --- a/tests/test_bf16_model.c +++ b/tests/test_bf16_model.c @@ -13,7 +13,7 @@ int main(void) { char root[1024], f32_dir[1088], bf16_dir[1088]; - snprintf(root, sizeof(root), "%s/pplx-bf16-test-XXXXXX", + snprintf(root, sizeof(root), "%s/embed-bf16-test-XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); if (!mkdtemp(root)) { perror("mkdtemp"); return 2; } snprintf(f32_dir, sizeof(f32_dir), "%s/f32", root); @@ -26,26 +26,26 @@ int main(void) { } int ids[] = {1, 2, 3, 4}; - pplx_model_t *mf32 = pplx_model_load(f32_dir); - pplx_model_t *mbf16 = pplx_model_load(bf16_dir); + embed_model_t *mf32 = embed_model_load(f32_dir); + embed_model_t *mbf16 = embed_model_load(bf16_dir); if (!mf32 || !mbf16) { fprintf(stderr, "model load failed\n"); return 1; } - const pplx_config_t *cfg = pplx_model_config(mf32); + const embed_config_t *cfg = embed_model_config(mf32); int dim = cfg->hidden_size; float *a = (float *)malloc((size_t)dim * sizeof(float)); float *b = (float *)malloc((size_t)dim * sizeof(float)); - pplx_workspace_t *wf32 = pplx_workspace_new(mf32); - pplx_workspace_t *wbf16 = pplx_workspace_new(mbf16); + embed_workspace_t *wf32 = embed_workspace_new(mf32); + embed_workspace_t *wbf16 = embed_workspace_new(mbf16); if (!a || !b || !wf32 || !wbf16) { fprintf(stderr, "allocation failed\n"); return 1; } - if (pplx_model_embed_into(mf32, wf32, ids, 4, a) != 0 || - pplx_model_embed_into(mbf16, wbf16, ids, 4, b) != 0) { + if (embed_model_encode_into(mf32, wf32, ids, 4, a) != 0 || + embed_model_encode_into(mbf16, wbf16, ids, 4, b) != 0) { fprintf(stderr, "embedding failed\n"); return 1; } @@ -74,8 +74,8 @@ int main(void) { enum { LONG_N = 24 }; int long_ids[LONG_N]; for (int i = 0; i < LONG_N; i++) long_ids[i] = (i % 14) + 1; - if (pplx_model_embed_into(mf32, wf32, long_ids, LONG_N, a) != 0 || - pplx_model_embed_into(mbf16, wbf16, long_ids, LONG_N, b) != 0) { + if (embed_model_encode_into(mf32, wf32, long_ids, LONG_N, a) != 0 || + embed_model_encode_into(mbf16, wbf16, long_ids, LONG_N, b) != 0) { fprintf(stderr, "long-sequence embedding failed\n"); return 1; } @@ -96,10 +96,10 @@ int main(void) { printf("ok: bf16 model parity dim=%d max_abs_diff=%.9g norm=%.9g\n", dim, max_diff, norm); - pplx_workspace_free(wf32); - pplx_workspace_free(wbf16); - pplx_model_free(mf32); - pplx_model_free(mbf16); + embed_workspace_free(wf32); + embed_workspace_free(wbf16); + embed_model_free(mf32); + embed_model_free(mbf16); free(a); free(b); return 0; diff --git a/tests/test_cli.c b/tests/test_cli.c index 0a5212a..a6a065c 100644 --- a/tests/test_cli.c +++ b/tests/test_cli.c @@ -1,4 +1,4 @@ -/* tests/test_cli.c - drives the pplx-embed CLI binary end to end. +/* tests/test_cli.c - drives the embed CLI binary end to end. * Hermetic: synthesizes a tiny model + tokenizer fixture, then runs the * binary given as argv[1] through its modes and flag validation paths, * checking exit codes and output shapes. Runs via `make test`. */ @@ -90,10 +90,10 @@ static int stdout_is_embedding(int dim) int main(int argc, char **argv) { - g_cli = argc > 1 ? argv[1] : "./pplx-embed"; + g_cli = argc > 1 ? argv[1] : "./embed"; tm_dims_t dims = {4, 2, 1, 2, 8, TF_VOCAB_SIZE}; - snprintf(g_dir, sizeof(g_dir), "%s/pplx-cli-test-XXXXXX", + snprintf(g_dir, sizeof(g_dir), "%s/embed-cli-test-XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); if (!mkdtemp(g_dir) || tf_write_vocab(g_dir) != 0 || tm_write_model_dims(g_dir, "F32", &dims) != 0) { @@ -109,7 +109,7 @@ int main(int argc, char **argv) /* Help and missing-argument handling. */ expect(run_cli("-h", NULL) == 0, "-h exits 0"); expect(run_cli("", NULL) == 1, "no arguments exits 1"); - expect(run_cli("-d /nonexistent-pplx-dir hi", NULL) == 1, + expect(run_cli("-d /nonexistent-embed-dir hi", NULL) == 1, "missing model dir exits 1"); /* Mode 1: one text prints one embedding line. */ diff --git a/tests/test_late.c b/tests/test_late.c index 2243057..94bdfe2 100644 --- a/tests/test_late.c +++ b/tests/test_late.c @@ -39,13 +39,13 @@ static int check_maxsim_batch_parity(const float *query, int query_tokens, { float scores[8]; if (n_docs > (int)(sizeof(scores) / sizeof(scores[0]))) return 1; - if (pplx_late_maxsim_batch(query, query_tokens, docs, offsets, n_docs, + if (embed_late_maxsim_batch(query, query_tokens, docs, offsets, n_docs, dim, scores) != 0) { fprintf(stderr, "maxsim_batch (%s) failed\n", what); return 1; } for (int i = 0; i < n_docs; i++) { - float one = pplx_late_maxsim(query, query_tokens, + float one = embed_late_maxsim(query, query_tokens, docs + (size_t)offsets[i] * dim, offsets[i + 1] - offsets[i], dim); if (fabsf(scores[i] - one) > 0.0001f) { @@ -64,10 +64,10 @@ int main(void) int hidden = dims.hidden; qwen_tokenizer_t *tok = NULL; - pplx_model_t *base = NULL; - pplx_workspace_t *base_ws = NULL; - pplx_late_model_t *late = NULL; - pplx_late_workspace_t *ws = NULL; + embed_model_t *base = NULL; + embed_workspace_t *base_ws = NULL; + embed_late_model_t *late = NULL; + embed_late_workspace_t *ws = NULL; int *ids = NULL; float *states = NULL, *raw = NULL, *vecs = NULL, *expected = NULL; float *doc_vecs = NULL; @@ -75,7 +75,7 @@ int main(void) int *doc_ids[3] = {0}; char dir[1024]; - snprintf(dir, sizeof(dir), "%s/pplx-late-test-XXXXXX", + snprintf(dir, sizeof(dir), "%s/embed-late-test-XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); if (!mkdtemp(dir) || tf_write_vocab(dir) != 0 || tm_write_model_dims(dir, "F32", &dims) != 0) { @@ -85,25 +85,25 @@ int main(void) /* The base model must load before the projection exists; keep it for * the reference forward pass below. */ - base = pplx_model_load(dir); - if (!base || !(base_ws = pplx_workspace_new(base))) { + base = embed_model_load(dir); + if (!base || !(base_ws = embed_workspace_new(base))) { fprintf(stderr, "failed to load base model\n"); goto fail; } /* No 1_Dense head yet: not a late model. */ - if (pplx_late_model_load(dir) != NULL) { + if (embed_late_model_load(dir) != NULL) { fprintf(stderr, "late load succeeded without 1_Dense\n"); goto fail; } - if (pplx_late_model_config(NULL) != NULL || - pplx_late_model_token_dim(NULL) != 0 || - pplx_late_workspace_new(NULL) != NULL) { + if (embed_late_model_config(NULL) != NULL || + embed_late_model_token_dim(NULL) != 0 || + embed_late_workspace_new(NULL) != NULL) { fprintf(stderr, "NULL-argument handling failed\n"); goto fail; } - pplx_late_workspace_free(NULL); - pplx_late_model_free(NULL); + embed_late_workspace_free(NULL); + embed_late_model_free(NULL); /* A head whose tensor is not named linear.weight must be rejected. */ if (tm_write_late_projection_named(dir, "F32", TOKEN_DIM, hidden, @@ -111,7 +111,7 @@ int main(void) fprintf(stderr, "failed to write malformed projection\n"); goto fail; } - if (pplx_late_model_load(dir) != NULL) { + if (embed_late_model_load(dir) != NULL) { fprintf(stderr, "late load accepted a head without linear.weight\n"); goto fail; } @@ -120,26 +120,26 @@ int main(void) fprintf(stderr, "failed to write projection\n"); goto fail; } - late = pplx_late_model_load(dir); + late = embed_late_model_load(dir); if (!late) { fprintf(stderr, "late model load failed\n"); goto fail; } /* With the projection present the dir is no longer a pooled model. */ - if (pplx_model_load(dir) != NULL) { + if (embed_model_load(dir) != NULL) { fprintf(stderr, "pooled load accepted a late-interaction snapshot\n"); goto fail; } - const pplx_config_t *cfg = pplx_late_model_config(late); + const embed_config_t *cfg = embed_late_model_config(late); if (!cfg || cfg->hidden_size != hidden || - pplx_late_model_token_dim(late) != TOKEN_DIM) { + embed_late_model_token_dim(late) != TOKEN_DIM) { fprintf(stderr, "late config/token_dim mismatch\n"); goto fail; } - ws = pplx_late_workspace_new(late); + ws = embed_late_workspace_new(late); if (!ws) { fprintf(stderr, "late workspace allocation failed\n"); goto fail; @@ -169,25 +169,25 @@ int main(void) goto fail; } - if (pplx_late_model_encode_tokens(late, ws, NULL, n_tokens, 1, vecs) == 0 || - pplx_late_model_encode_tokens(late, ws, ids, 0, 1, vecs) == 0 || - pplx_late_model_encode_tokens(NULL, ws, ids, n_tokens, 1, vecs) == 0 || - pplx_late_model_encode_tokens(late, NULL, ids, n_tokens, 1, vecs) == 0) { + if (embed_late_model_encode_tokens(late, ws, NULL, n_tokens, 1, vecs) == 0 || + embed_late_model_encode_tokens(late, ws, ids, 0, 1, vecs) == 0 || + embed_late_model_encode_tokens(NULL, ws, ids, n_tokens, 1, vecs) == 0 || + embed_late_model_encode_tokens(late, NULL, ids, n_tokens, 1, vecs) == 0) { fprintf(stderr, "encode accepted invalid arguments\n"); goto fail; } - if (pplx_late_model_encode_tokens(late, ws, ids, n_tokens, 0, raw) != 0 || - pplx_late_model_encode_tokens(late, ws, ids, n_tokens, 1, vecs) != 0) { + if (embed_late_model_encode_tokens(late, ws, ids, n_tokens, 0, raw) != 0 || + embed_late_model_encode_tokens(late, ws, ids, n_tokens, 1, vecs) != 0) { fprintf(stderr, "encode_tokens failed\n"); goto fail; } /* Reference: run the same tokens through the standalone base model and * apply the projection by hand from the fixture's deterministic values. */ - states = pplx_model_forward(base, base_ws, ids, n_tokens); + states = embed_model_forward(base, base_ws, ids, n_tokens); if (!states) { - fprintf(stderr, "pplx_model_forward failed\n"); + fprintf(stderr, "embed_model_forward failed\n"); goto fail; } for (int t = 0; t < n_tokens; t++) { @@ -227,13 +227,13 @@ int main(void) { static const float q2[4] = {1.0f, 0.0f, 0.0f, 1.0f}; static const float d2[4] = {1.0f, 0.0f, 0.0f, -1.0f}; - float s = pplx_late_maxsim(q2, 2, d2, 2, 2); + float s = embed_late_maxsim(q2, 2, d2, 2, 2); if (fabsf(s - 1.0f) > 0.000001f) { fprintf(stderr, "maxsim hand check: got %g, want 1\n", s); goto fail; } - if (pplx_late_maxsim(NULL, 2, d2, 2, 2) != 0.0f || - pplx_late_maxsim(q2, 0, d2, 2, 2) != 0.0f) { + if (embed_late_maxsim(NULL, 2, d2, 2, 2) != 0.0f || + embed_late_maxsim(q2, 0, d2, 2, 2) != 0.0f) { fprintf(stderr, "maxsim accepted invalid arguments\n"); goto fail; } @@ -260,7 +260,7 @@ int main(void) goto fail; } for (int i = 0; i < 3; i++) { - if (pplx_late_model_encode_tokens( + if (embed_late_model_encode_tokens( late, ws, doc_ids[i], lens[i], 1, doc_vecs + (size_t)offsets[i] * TOKEN_DIM) != 0) { fprintf(stderr, "doc encode failed\n"); @@ -274,11 +274,11 @@ int main(void) float scores[3]; int bad_first[4] = {1, 2, 3, 4}; int empty_doc[3] = {0, 2, 2}; - if (pplx_late_maxsim_batch(vecs, n_tokens, doc_vecs, bad_first, 3, + if (embed_late_maxsim_batch(vecs, n_tokens, doc_vecs, bad_first, 3, TOKEN_DIM, scores) == 0 || - pplx_late_maxsim_batch(vecs, n_tokens, doc_vecs, empty_doc, 2, + embed_late_maxsim_batch(vecs, n_tokens, doc_vecs, empty_doc, 2, TOKEN_DIM, scores) == 0 || - pplx_late_maxsim_batch(vecs, n_tokens, doc_vecs, offsets, 3, + embed_late_maxsim_batch(vecs, n_tokens, doc_vecs, offsets, 3, TOKEN_DIM, NULL) == 0) { fprintf(stderr, "maxsim_batch accepted invalid arguments\n"); goto fail; @@ -325,9 +325,9 @@ fail: free(states); free(ids); qwen_tokenizer_free(tok); - pplx_late_workspace_free(ws); - pplx_late_model_free(late); - pplx_workspace_free(base_ws); - pplx_model_free(base); + embed_late_workspace_free(ws); + embed_late_model_free(late); + embed_workspace_free(base_ws); + embed_model_free(base); return rc; } diff --git a/tests/test_safetensors.c b/tests/test_safetensors.c index fea570b..d527951 100644 --- a/tests/test_safetensors.c +++ b/tests/test_safetensors.c @@ -64,7 +64,7 @@ static const char ONE_TENSOR_X[] = int main(void) { - snprintf(tmproot, sizeof(tmproot), "%s/pplx-st-test-XXXXXX", + snprintf(tmproot, sizeof(tmproot), "%s/embed-st-test-XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); if (!mkdtemp(tmproot)) { perror("mkdtemp"); return 2; } diff --git a/tests/test_server.c b/tests/test_server.c index 5f1dbed..6f1e48e 100644 --- a/tests/test_server.c +++ b/tests/test_server.c @@ -228,15 +228,15 @@ static int http_req(int port, const char *method, const char *path, } typedef struct { - pplx_server_model_spec_t spec[2]; - pplx_server_config_t cfg; + embed_server_model_spec_t spec[2]; + embed_server_config_t cfg; int rc; } srv_ctx; static void *srv_thread_main(void *arg) { srv_ctx *s = (srv_ctx *)arg; - s->rc = pplx_run_server(&s->cfg); + s->rc = embed_run_server(&s->cfg); return NULL; } @@ -548,7 +548,7 @@ static void test_http_contextual(int port) static void test_http_server(void) { char dir[1024]; - snprintf(dir, sizeof(dir), "%s/pplx-srv-test-XXXXXX", + snprintf(dir, sizeof(dir), "%s/embed-srv-test-XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); if (!mkdtemp(dir)) { fprintf(stderr, "FAIL: mkdtemp\n"); diff --git a/tests/test_tokenizer.c b/tests/test_tokenizer.c index b0c694e..bed5b7d 100644 --- a/tests/test_tokenizer.c +++ b/tests/test_tokenizer.c @@ -271,7 +271,7 @@ static void test_loader_edges(const char *root) int main(void) { char dir[1024]; - snprintf(dir, sizeof(dir), "%s/pplx-tok-test-XXXXXX", + snprintf(dir, sizeof(dir), "%s/embed-tok-test-XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); if (!mkdtemp(dir) || tf_write_vocab(dir) != 0) { fprintf(stderr, "fixture creation failed\n"); diff --git a/tests/test_workspace.c b/tests/test_workspace.c index 334c4f2..4f83936 100644 --- a/tests/test_workspace.c +++ b/tests/test_workspace.c @@ -26,19 +26,19 @@ static float max_abs_diff(const float *a, const float *b, int n) /* The allocating and pooling API variants must agree with the *_into * results validated in main. reference is the batched row for ids0. */ -static int check_alloc_and_pooling_variants(const pplx_model_t *model, - pplx_workspace_t *ws, +static int check_alloc_and_pooling_variants(const embed_model_t *model, + embed_workspace_t *ws, const int *ids0, int n0, const float *states, const float *reference, - const pplx_config_t *cfg) + const embed_config_t *cfg) { int rc = 1; int dim = cfg->hidden_size; float *emb = NULL, *fwd = NULL, *pooled = NULL, *span_out = NULL; - emb = pplx_model_embed(model, ws, ids0, n0); - fwd = pplx_model_forward(model, ws, ids0, n0); + emb = embed_model_encode(model, ws, ids0, n0); + fwd = embed_model_forward(model, ws, ids0, n0); pooled = (float *)calloc((size_t)dim, sizeof(float)); span_out = (float *)calloc((size_t)2 * dim, sizeof(float)); if (!emb || !fwd || !pooled || !span_out) { @@ -46,44 +46,44 @@ static int check_alloc_and_pooling_variants(const pplx_model_t *model, goto done; } - if (pplx_model_embed(NULL, ws, ids0, n0) != NULL || - pplx_model_embed(model, ws, NULL, n0) != NULL || - pplx_model_forward(model, ws, ids0, 0) != NULL || - pplx_pool_batch(cfg, states, NULL, 1, pooled) == 0) { + if (embed_model_encode(NULL, ws, ids0, n0) != NULL || + embed_model_encode(model, ws, NULL, n0) != NULL || + embed_model_forward(model, ws, ids0, 0) != NULL || + embed_pool_batch(cfg, states, NULL, 1, pooled) == 0) { fprintf(stderr, "alloc variants accepted invalid arguments\n"); goto done; } if (max_abs_diff(emb, reference, dim) > 0.00005f) { - fprintf(stderr, "pplx_model_embed disagrees with batched row\n"); + fprintf(stderr, "embed_model_encode disagrees with batched row\n"); goto done; } if (max_abs_diff(fwd, states, n0 * dim) > 0.000001f) { - fprintf(stderr, "pplx_model_forward disagrees with forward_into\n"); + fprintf(stderr, "embed_model_forward disagrees with forward_into\n"); goto done; } /* Mean-pooling the final states reproduces the embedding. */ - if (pplx_pool_batch(cfg, states, &n0, 1, pooled) != 0 || + if (embed_pool_batch(cfg, states, &n0, 1, pooled) != 0 || max_abs_diff(pooled, reference, dim) > 0.00005f) { - fprintf(stderr, "pplx_pool_batch disagrees with embedding\n"); + fprintf(stderr, "embed_pool_batch disagrees with embedding\n"); goto done; } /* One span covering the whole sequence pools to the embedding, and * two halves recombine into it by token-count weighting. */ - pplx_span_t whole = {0, n0}; - if (pplx_model_embed_spans(model, ws, ids0, n0, &whole, 1, + embed_span_t whole = {0, n0}; + if (embed_model_encode_spans(model, ws, ids0, n0, &whole, 1, span_out) != 0 || max_abs_diff(span_out, reference, dim) > 0.00005f) { fprintf(stderr, "whole-sequence span disagrees with embedding\n"); goto done; } int h = n0 / 2; - pplx_span_t halves[2] = {{0, h}, {h, n0 - h}}; - if (pplx_model_embed_spans(model, ws, ids0, n0, halves, 2, + embed_span_t halves[2] = {{0, h}, {h, n0 - h}}; + if (embed_model_encode_spans(model, ws, ids0, n0, halves, 2, span_out) != 0) { - fprintf(stderr, "pplx_model_embed_spans failed for two spans\n"); + fprintf(stderr, "embed_model_encode_spans failed for two spans\n"); goto done; } for (int d = 0; d < dim; d++) { @@ -106,11 +106,11 @@ done: /* A multi-document contextual batch must match per-document runs. * doc0 is chunks 0 and 1 joined by a separator token; doc1 is chunk 2. */ -static int check_spans_batch_parity(const pplx_model_t *model, - pplx_workspace_t *ws, +static int check_spans_batch_parity(const embed_model_t *model, + embed_workspace_t *ws, int *const ids[], const int *ntok, int separator_id, - const pplx_config_t *cfg) + const embed_config_t *cfg) { int rc = 1; int dim = cfg->hidden_size; @@ -126,26 +126,26 @@ static int check_spans_batch_parity(const pplx_model_t *model, doc0[ntok[0]] = separator_id; memcpy(doc0 + ntok[0] + 1, ids[1], (size_t)ntok[1] * sizeof(int)); - pplx_span_t spans0[2] = {{0, ntok[0]}, {ntok[0] + 1, ntok[1]}}; - pplx_span_t span1 = {0, ntok[2]}; - if (pplx_model_embed_spans(model, ws, doc0, n0, spans0, 2, + embed_span_t spans0[2] = {{0, ntok[0]}, {ntok[0] + 1, ntok[1]}}; + embed_span_t span1 = {0, ntok[2]}; + if (embed_model_encode_spans(model, ws, doc0, n0, spans0, 2, expected) != 0 || - pplx_model_embed_spans(model, ws, ids[2], ntok[2], &span1, 1, + embed_model_encode_spans(model, ws, ids[2], ntok[2], &span1, 1, expected + (size_t)2 * dim) != 0) { fprintf(stderr, "singleton contextual embedding failed\n"); goto done; } - pplx_context_input_t inputs[2] = { + embed_context_input_t inputs[2] = { {{doc0, n0}, spans0, 2}, {{ids[2], ntok[2]}, &span1, 1}, }; - if (pplx_model_embed_spans_batch(model, ws, inputs, 2, actual) != 0) { + if (embed_model_encode_spans_batch(model, ws, inputs, 2, actual) != 0) { fprintf(stderr, "batched contextual embedding failed\n"); goto done; } - if (pplx_model_embed_spans_batch(model, ws, NULL, 2, actual) == 0 || - pplx_model_embed_spans_batch(model, ws, inputs, 0, actual) == 0) { + if (embed_model_encode_spans_batch(model, ws, NULL, 2, actual) == 0 || + embed_model_encode_spans_batch(model, ws, inputs, 0, actual) == 0) { fprintf(stderr, "spans batch accepted invalid arguments\n"); goto done; } @@ -174,7 +174,7 @@ int main(int argc, char **argv) /* Tiny hidden size keeps it fast; the vocab must cover every id the * byte-complete tokenizer fixture can emit. */ tm_dims_t dims = {4, 2, 1, 2, 8, TF_VOCAB_SIZE}; - snprintf(fixture_dir, sizeof(fixture_dir), "%s/pplx-ws-test-XXXXXX", + snprintf(fixture_dir, sizeof(fixture_dir), "%s/embed-ws-test-XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); if (!mkdtemp(fixture_dir) || tf_write_vocab(fixture_dir) != 0 || tm_write_model_dims(fixture_dir, "F32", &dims) != 0) { @@ -189,28 +189,28 @@ int main(int argc, char **argv) char vocab_path[4096]; snprintf(vocab_path, sizeof(vocab_path), "%s/vocab.json", model_dir); - pplx_model_t *model = pplx_model_load(model_dir); + embed_model_t *model = embed_model_load(model_dir); if (!model) { fprintf(stderr, "failed to load model\n"); return 1; } - const pplx_config_t *cfg = pplx_model_config(model); + const embed_config_t *cfg = embed_model_config(model); if (!cfg || cfg->hidden_size <= 0) { fprintf(stderr, "invalid model config\n"); - pplx_model_free(model); + embed_model_free(model); return 1; } - pplx_workspace_t *batch_ws = pplx_workspace_new(model); - pplx_workspace_t *single_ws = pplx_workspace_new(model); + embed_workspace_t *batch_ws = embed_workspace_new(model); + embed_workspace_t *single_ws = embed_workspace_new(model); qwen_tokenizer_t *tok = qwen_tokenizer_load(vocab_path); if (!batch_ws || !single_ws || !tok) { fprintf(stderr, "failed to allocate workspace or tokenizer\n"); qwen_tokenizer_free(tok); - pplx_workspace_free(single_ws); - pplx_workspace_free(batch_ws); - pplx_model_free(model); + embed_workspace_free(single_ws); + embed_workspace_free(batch_ws); + embed_model_free(model); return 1; } @@ -225,9 +225,9 @@ int main(int argc, char **argv) * sees indeterminate pointers. */ int *ids[4] = {0}; int ntok[4] = {0}; - pplx_input_t inputs[4]; + embed_input_t inputs[4]; - size_t initial_ws_bytes = pplx_workspace_nbytes(batch_ws); + size_t initial_ws_bytes = embed_workspace_nbytes(batch_ws); if (initial_ws_bytes == 0) { fprintf(stderr, "workspace byte accounting returned zero\n"); goto fail; @@ -257,8 +257,8 @@ int main(int argc, char **argv) goto fail; } - if (pplx_model_embed_batch(model, batch_ws, inputs, batch, batched) != 0) { - fprintf(stderr, "pplx_model_embed_batch failed\n"); + if (embed_model_encode_batch(model, batch_ws, inputs, batch, batched) != 0) { + fprintf(stderr, "embed_model_encode_batch failed\n"); free(states); free(normalized); free(single); @@ -266,7 +266,7 @@ int main(int argc, char **argv) goto fail; } - size_t batch_ws_bytes = pplx_workspace_nbytes(batch_ws); + size_t batch_ws_bytes = embed_workspace_nbytes(batch_ws); if (batch_ws_bytes <= initial_ws_bytes) { fprintf(stderr, "workspace byte accounting did not grow after embedding\n"); free(states); @@ -279,8 +279,8 @@ int main(int argc, char **argv) float worst_diff = 0.0f; float worst_cos = 1.0f; for (int i = 0; i < batch; i++) { - if (pplx_model_embed_into(model, single_ws, ids[i], ntok[i], single) != 0) { - fprintf(stderr, "pplx_model_embed_into failed at %d\n", i); + if (embed_model_encode_into(model, single_ws, ids[i], ntok[i], single) != 0) { + fprintf(stderr, "embed_model_encode_into failed at %d\n", i); free(states); free(normalized); free(single); @@ -290,14 +290,14 @@ int main(int argc, char **argv) const float *row = batched + (size_t)i * dim; float diff = max_abs_diff(row, single, dim); - float cos = pplx_cosine_similarity(row, single, dim); + float cos = embed_cosine_similarity(row, single, dim); if (diff > worst_diff) worst_diff = diff; if (cos < worst_cos) worst_cos = cos; } memcpy(normalized, batched, (size_t)dim * sizeof(float)); - if (pplx_l2_normalize(normalized, dim) != 0) { - fprintf(stderr, "pplx_l2_normalize failed\n"); + if (embed_l2_normalize(normalized, dim) != 0) { + fprintf(stderr, "embed_l2_normalize failed\n"); free(states); free(normalized); free(single); @@ -308,7 +308,7 @@ int main(int argc, char **argv) for (int i = 0; i < dim; i++) normalized_norm_sq += normalized[i] * normalized[i]; if (fabsf(normalized_norm_sq - 1.0f) > 0.00005f) { - fprintf(stderr, "pplx_l2_normalize produced norm_sq=%g\n", + fprintf(stderr, "embed_l2_normalize produced norm_sq=%g\n", normalized_norm_sq); free(states); free(normalized); @@ -317,8 +317,8 @@ int main(int argc, char **argv) goto fail; } - if (pplx_model_forward_into(model, batch_ws, ids[0], ntok[0], states) != 0) { - fprintf(stderr, "pplx_model_forward_into failed\n"); + if (embed_model_forward_into(model, batch_ws, ids[0], ntok[0], states) != 0) { + fprintf(stderr, "embed_model_forward_into failed\n"); free(states); free(normalized); free(single); @@ -338,7 +338,7 @@ int main(int argc, char **argv) /* Same separator resolution as the server: the fixture vocab defines * <|endoftext|>, real snapshots use the reserved id. */ int sep_id = qwen_tokenizer_token_id(tok, "<|endoftext|>"); - if (sep_id < 0) sep_id = PPLX_CONTEXT_SEPARATOR_TOKEN_ID; + if (sep_id < 0) sep_id = EMBED_CONTEXT_SEPARATOR_TOKEN_ID; if (check_spans_batch_parity(model, batch_ws, ids, ntok, sep_id, cfg) != 0) { free(states); @@ -355,9 +355,9 @@ int main(int argc, char **argv) for (int i = 0; i < batch; i++) free(ids[i]); qwen_tokenizer_free(tok); - pplx_workspace_free(single_ws); - pplx_workspace_free(batch_ws); - pplx_model_free(model); + embed_workspace_free(single_ws); + embed_workspace_free(batch_ws); + embed_model_free(model); if (worst_diff > 0.00005f || worst_cos < 0.99999f) { fprintf(stderr, "workspace API parity failed: max_abs_diff=%g cosine=%g\n", @@ -372,8 +372,8 @@ int main(int argc, char **argv) fail: for (int i = 0; i < batch; i++) free(ids[i]); qwen_tokenizer_free(tok); - pplx_workspace_free(single_ws); - pplx_workspace_free(batch_ws); - pplx_model_free(model); + embed_workspace_free(single_ws); + embed_workspace_free(batch_ws); + embed_model_free(model); return 1; } |
