aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--main.c32
-rw-r--r--pplx_embed.c126
-rw-r--r--pplx_embed.h102
-rw-r--r--pplx_embed_mlx.c2
5 files changed, 88 insertions, 178 deletions
diff --git a/.gitignore b/.gitignore
index 6a85c96..00f2d25 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,6 @@
*.o
pplx_embed
+book/
+.venv/
+__pycache__/
+*.pyc
diff --git a/main.c b/main.c
index 5638d31..837434b 100644
--- a/main.c
+++ b/main.c
@@ -53,7 +53,8 @@ static void print_usage(const char *prog)
* ======================================================================== */
typedef struct {
- pplx_ctx_t *ctx;
+ pplx_model_t *model;
+ pplx_workspace_t *workspace;
#ifdef USE_MLX
pplx_mlx_ctx_t *mlx_ctx;
#endif
@@ -74,7 +75,8 @@ static int engine_embed_batch(engine_t *e, const pplx_input_t *inputs,
return pplx_mlx_embed_batch(e->mlx_ctx, inputs, batch, out_embeddings);
else
#endif
- return pplx_embed_batch(e->ctx, inputs, batch, out_embeddings);
+ return pplx_model_embed_batch(e->model, e->workspace,
+ inputs, batch, out_embeddings);
}
static int tokenize_text(engine_t *e, const char *text, token_buf_t *out)
@@ -457,14 +459,29 @@ int main(int argc, char *argv[])
#ifdef USE_MLX
if (use_mlx) {
e.mlx_ctx = pplx_mlx_load(model_dir);
- if (!e.mlx_ctx) { fprintf(stderr, "failed to load model\n"); return 1; }
+ 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;
} else
#endif
{
- e.ctx = pplx_load(model_dir);
- if (!e.ctx) { fprintf(stderr, "failed to load model\n"); return 1; }
- e.dim = pplx_config(e.ctx)->hidden_size;
+ e.model = pplx_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);
+ if (!e.workspace) {
+ fprintf(stderr, "failed to allocate workspace\n");
+ pplx_model_free(e.model);
+ qwen_tokenizer_free(tok);
+ return 1;
+ }
+ e.dim = pplx_model_config(e.model)->hidden_size;
}
if (verbose >= 1)
fprintf(stderr, "Model: %d-dim, %.0f ms%s\n",
@@ -478,7 +495,8 @@ int main(int argc, char *argv[])
rc = run_batch(&e, argc, argv, arg_start, print_embs, batch_size);
/* Cleanup */
- if (e.ctx) pplx_free(e.ctx);
+ if (e.workspace) pplx_workspace_free(e.workspace);
+ if (e.model) pplx_model_free(e.model);
#ifdef USE_MLX
if (e.mlx_ctx) pplx_mlx_free(e.mlx_ctx);
#endif
diff --git a/pplx_embed.c b/pplx_embed.c
index 7f48ba8..5fbc865 100644
--- a/pplx_embed.c
+++ b/pplx_embed.c
@@ -7,12 +7,52 @@
#include <string.h>
#include <math.h>
+typedef struct {
+ const float *wq; /* [q_dim, hidden] */
+ const float *wk; /* [kv_dim, hidden] */
+ const float *wv; /* [kv_dim, hidden] */
+ const float *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] */
+ const float *gate_proj; /* [intermediate, hidden] */
+ const float *up_proj; /* [intermediate, hidden] */
+ const float *down_proj; /* [hidden, intermediate] */
+} pplx_layer_t;
+
+typedef struct {
+ const float *embed_tokens; /* [vocab_size, hidden] */
+ pplx_layer_t *layers; /* [n_layers] heap-allocated */
+ const float *norm; /* [hidden] */
+} pplx_weights_t;
+
struct pplx_model {
pplx_config_t config;
pplx_weights_t weights;
void *safetensors; /* multi_safetensors_t*, keeps mmap alive */
};
+struct pplx_workspace {
+ const pplx_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 *ffn_out; /* [seq, hidden] */
+
+ /* RoPE cosine/sine cache [n_pos, head_dim], grown lazily. */
+ float *rope_cos;
+ float *rope_sin;
+ int rope_cache_cap;
+};
+
/* ========================================================================
* Globals
* ======================================================================== */
@@ -70,7 +110,7 @@ static int parse_config(pplx_config_t *cfg, const char *model_dir)
FILE *f = fopen(path, "rb");
if (!f) {
- fprintf(stderr, "pplx_load: cannot open %s\n", path);
+ fprintf(stderr, "pplx_model_load: cannot open %s\n", path);
return -1;
}
@@ -101,13 +141,13 @@ static int parse_config(pplx_config_t *cfg, const char *model_dir)
/* Sanity checks */
if (cfg->hidden_size <= 0 || cfg->n_layers <= 0 || cfg->n_heads <= 0) {
- fprintf(stderr, "pplx_load: invalid config in %s "
+ fprintf(stderr, "pplx_model_load: invalid config in %s "
"(hidden=%d, layers=%d, heads=%d)\n",
path, cfg->hidden_size, cfg->n_layers, cfg->n_heads);
return -1;
}
if (cfg->n_layers > PPLX_MAX_LAYERS) {
- fprintf(stderr, "pplx_load: too many layers (%d > %d)\n",
+ fprintf(stderr, "pplx_model_load: too many layers (%d > %d)\n",
cfg->n_layers, PPLX_MAX_LAYERS);
return -1;
}
@@ -240,14 +280,14 @@ pplx_model_t *pplx_model_load(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_load: failed to open safetensors in %s\n", model_dir);
+ fprintf(stderr, "pplx_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_load: loading weights from %s\n", model_dir);
+ fprintf(stderr, "pplx_model_load: loading weights from %s\n", model_dir);
/* Token embeddings */
pplx_weights_t *w = &model->weights;
@@ -292,7 +332,7 @@ pplx_model_t *pplx_model_load(const char *model_dir)
if (!w->norm) goto fail;
if (pplx_verbose >= 1)
- fprintf(stderr, "pplx_load: %d layers loaded (%d-dim embeddings)\n",
+ fprintf(stderr, "pplx_model_load: %d layers loaded (%d-dim embeddings)\n",
cfg->n_layers, cfg->hidden_size);
return model;
@@ -336,44 +376,6 @@ const pplx_config_t *pplx_model_config(const pplx_model_t *model)
return model ? &model->config : NULL;
}
-const pplx_config_t *pplx_config(const pplx_ctx_t *ctx)
-{
- return (ctx && ctx->model) ? pplx_model_config(ctx->model) : NULL;
-}
-
-pplx_ctx_t *pplx_load(const char *model_dir)
-{
- pplx_model_t *model = pplx_model_load(model_dir);
- if (!model) return NULL;
-
- pplx_workspace_t *ws = pplx_workspace_new(model);
- if (!ws) {
- pplx_model_free(model);
- return NULL;
- }
-
- pplx_ctx_t *ctx = (pplx_ctx_t *)calloc(1, sizeof(pplx_ctx_t));
- if (!ctx) {
- pplx_workspace_free(ws);
- pplx_model_free(model);
- return NULL;
- }
-
- ctx->model = model;
- ctx->workspace = ws;
- ctx->config = model->config;
- ctx->weights = model->weights;
- return ctx;
-}
-
-void pplx_free(pplx_ctx_t *ctx)
-{
- if (!ctx) return;
- pplx_workspace_free(ctx->workspace);
- pplx_model_free(ctx->model);
- free(ctx);
-}
-
/* ========================================================================
* Packed forward pass
* ======================================================================== */
@@ -624,42 +626,6 @@ float *pplx_model_embed(const pplx_model_t *model, pplx_workspace_t *ws,
return emb;
}
-int pplx_forward_into(pplx_ctx_t *ctx, const int *token_ids, int n_tokens,
- float *out_states)
-{
- if (!ctx) return -1;
- return pplx_model_forward_into(ctx->model, ctx->workspace,
- token_ids, n_tokens, out_states);
-}
-
-float *pplx_forward(pplx_ctx_t *ctx, const int *token_ids, int n_tokens)
-{
- if (!ctx) return NULL;
- return pplx_model_forward(ctx->model, ctx->workspace, token_ids, n_tokens);
-}
-
-int pplx_embed_batch(pplx_ctx_t *ctx, const pplx_input_t *inputs, int batch,
- float *out_embeddings)
-{
- if (!ctx) return -1;
- return pplx_model_embed_batch(ctx->model, ctx->workspace,
- inputs, batch, out_embeddings);
-}
-
-int pplx_embed_into(pplx_ctx_t *ctx, const int *token_ids, int n_tokens,
- float *out_embedding)
-{
- if (!ctx) return -1;
- return pplx_model_embed_into(ctx->model, ctx->workspace,
- token_ids, n_tokens, out_embedding);
-}
-
-float *pplx_embed(pplx_ctx_t *ctx, const int *token_ids, int n_tokens)
-{
- if (!ctx) return NULL;
- return pplx_model_embed(ctx->model, ctx->workspace, token_ids, n_tokens);
-}
-
/* ========================================================================
* Cosine similarity
* ======================================================================== */
diff --git a/pplx_embed.h b/pplx_embed.h
index 081b49c..6012576 100644
--- a/pplx_embed.h
+++ b/pplx_embed.h
@@ -37,67 +37,11 @@ typedef struct {
} pplx_config_t;
/* ========================================================================
- * Per-Layer Weights (all F32, direct mmap pointers - read-only)
- * ======================================================================== */
-
-typedef struct {
- const float *wq; /* [q_dim, hidden] */
- const float *wk; /* [kv_dim, hidden] */
- const float *wv; /* [kv_dim, hidden] */
- const float *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] */
- const float *gate_proj; /* [intermediate, hidden] */
- const float *up_proj; /* [intermediate, hidden] */
- const float *down_proj; /* [hidden, intermediate] */
-} pplx_layer_t;
-
-/* ========================================================================
- * Full Model Weights
- * ======================================================================== */
-
-typedef struct {
- const float *embed_tokens; /* [vocab_size, hidden] */
- pplx_layer_t *layers; /* [n_layers] heap-allocated */
- const float *norm; /* [hidden] */
-} pplx_weights_t;
-
-/* ========================================================================
* Model + workspace ownership
* ======================================================================== */
typedef struct pplx_model pplx_model_t;
-
-typedef struct {
- const pplx_model_t *model; /* immutable model this workspace belongs to */
- /* Working buffers, grown lazily to buf_seq_cap */
- 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 *ffn_out; /* [seq, hidden] */
-
- /* RoPE cosine/sine cache [n_pos, head_dim], grown lazily */
- float *rope_cos;
- float *rope_sin;
- int rope_cache_cap;
-} pplx_workspace_t;
-
-/* Convenience context: owns one model and one workspace. */
-typedef struct {
- pplx_model_t *model;
- pplx_workspace_t *workspace;
- pplx_config_t config; /* backward-compatible snapshot */
- pplx_weights_t weights; /* backward-compatible read-only aliases */
-} pplx_ctx_t;
+typedef struct pplx_workspace pplx_workspace_t;
typedef struct {
const int *ids;
@@ -109,7 +53,7 @@ typedef struct {
* ======================================================================== */
/*
- * Load model from directory containing:
+ * Load immutable model data from a directory containing:
* config.json - model hyperparameters
* model.safetensors (or sharded) - F32 weights
* vocab.json + merges.txt - BPE tokenizer
@@ -117,30 +61,18 @@ typedef struct {
* Reads config.json to determine model size (0.6B vs 4B).
* Returns NULL on error.
*/
-pplx_ctx_t *pplx_load(const char *model_dir);
-
-/* Free all resources */
-void pplx_free(pplx_ctx_t *ctx);
-
-/*
- * Load/free immutable model data only. A model owns config, mmap'd
- * safetensors, and read-only weight pointers. It can be shared by multiple
- * workspaces, but the model must outlive all workspaces created from it.
- */
pplx_model_t *pplx_model_load(const char *model_dir);
void pplx_model_free(pplx_model_t *model);
/*
- * Allocate/free mutable scratch buffers for a model. A workspace is not
- * thread-safe, but multiple workspaces can use the same immutable model in
- * different threads.
+ * Allocate/free mutable scratch buffers for a model. The model must outlive
+ * workspaces created from it.
*/
pplx_workspace_t *pplx_workspace_new(const pplx_model_t *model);
void pplx_workspace_free(pplx_workspace_t *ws);
-/* Config accessors for opaque-ish model/context ownership. */
+/* Config accessor for opaque model ownership. */
const pplx_config_t *pplx_model_config(const pplx_model_t *model);
-const pplx_config_t *pplx_config(const pplx_ctx_t *ctx);
/*
* Compute embedding for a token sequence.
@@ -153,14 +85,16 @@ const pplx_config_t *pplx_config(const pplx_ctx_t *ctx);
*
* Returns malloc'd float[hidden_size] (caller frees). NULL on error.
*/
-float *pplx_embed(pplx_ctx_t *ctx, const int *token_ids, int n_tokens);
+float *pplx_model_embed(const pplx_model_t *model, pplx_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.
*/
-int pplx_embed_into(pplx_ctx_t *ctx, const int *token_ids, int n_tokens,
- float *out_embedding);
+int pplx_model_embed_into(const pplx_model_t *model, pplx_workspace_t *ws,
+ const int *token_ids, int n_tokens,
+ float *out_embedding);
/*
* Compute embeddings for a true packed/ragged batch.
@@ -171,17 +105,9 @@ int pplx_embed_into(pplx_ctx_t *ctx, const int *token_ids, int n_tokens,
*
* Returns 0 on success, -1 on error.
*/
-int pplx_embed_batch(pplx_ctx_t *ctx, const pplx_input_t *inputs, int batch,
- float *out_embeddings);
-
int pplx_model_embed_batch(const pplx_model_t *model, pplx_workspace_t *ws,
const pplx_input_t *inputs, int batch,
float *out_embeddings);
-int pplx_model_embed_into(const pplx_model_t *model, pplx_workspace_t *ws,
- const int *token_ids, int n_tokens,
- float *out_embedding);
-float *pplx_model_embed(const pplx_model_t *model, pplx_workspace_t *ws,
- const int *token_ids, int n_tokens);
/*
* Run the transformer forward pass WITHOUT pooling.
@@ -191,20 +117,16 @@ float *pplx_model_embed(const pplx_model_t *model, pplx_workspace_t *ws,
* Useful for contextual (late-chunking) models where you need
* per-token embeddings before splitting by separator positions.
*/
-float *pplx_forward(pplx_ctx_t *ctx, const int *token_ids, int n_tokens);
+float *pplx_model_forward(const pplx_model_t *model, pplx_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.
*/
-int pplx_forward_into(pplx_ctx_t *ctx, const int *token_ids, int n_tokens,
- float *out_states);
-
int pplx_model_forward_into(const pplx_model_t *model, pplx_workspace_t *ws,
const int *token_ids, int n_tokens,
float *out_states);
-float *pplx_model_forward(const pplx_model_t *model, pplx_workspace_t *ws,
- const int *token_ids, int n_tokens);
/*
* Cosine similarity between two L2-normalized vectors.
diff --git a/pplx_embed_mlx.c b/pplx_embed_mlx.c
index d6fff8f..74eb51d 100644
--- a/pplx_embed_mlx.c
+++ b/pplx_embed_mlx.c
@@ -68,7 +68,7 @@ const pplx_config_t *pplx_mlx_config(const pplx_mlx_ctx_t *ctx)
}
/* ========================================================================
- * Load (reuses the same parse_config logic via pplx_load then copies)
+ * Load
* ======================================================================== */
/* We duplicate a minimal config parser here so MLX can load standalone.