aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-05-24 01:10:23 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-14 08:18:59 +0200
commit8d3cd6f2ec22dc3079a9ce5f632d09a0526954b9 (patch)
tree71b912a23feb29f22583a2e5c467829014166bfe
parent9bfcc72ee6b2568d0f9fc05a28f9ccc3da176713 (diff)
downloadffwd-8d3cd6f2ec22dc3079a9ce5f632d09a0526954b9.tar.gz
ffwd-8d3cd6f2ec22dc3079a9ce5f632d09a0526954b9.zip
server: report workspace memory
-rw-r--r--main.c14
-rw-r--r--pplx_embed.c58
-rw-r--r--pplx_embed.h8
3 files changed, 77 insertions, 3 deletions
diff --git a/main.c b/main.c
index 837434b..2c3a0ae 100644
--- a/main.c
+++ b/main.c
@@ -121,14 +121,21 @@ static void print_embedding_raw(const float *emb, int dim)
putchar('\n');
}
-static void print_embedding_json(const float *emb, int dim, int n_tokens, double ms)
+static size_t engine_workspace_nbytes(const engine_t *e)
+{
+ return (e && e->workspace) ? pplx_workspace_nbytes(e->workspace) : 0;
+}
+
+static void print_embedding_json(const float *emb, int dim, int n_tokens,
+ double ms, size_t workspace_bytes)
{
printf("{\"embedding\":[");
for (int i = 0; i < dim; i++) {
if (i > 0) putchar(',');
printf("%.8f", (double)emb[i]);
}
- printf("],\"dim\":%d,\"tokens\":%d,\"ms\":%.1f}\n", dim, n_tokens, ms);
+ printf("],\"dim\":%d,\"tokens\":%d,\"ms\":%.1f,\"workspace_bytes\":%zu}\n",
+ dim, n_tokens, ms, workspace_bytes);
fflush(stdout);
}
@@ -190,7 +197,8 @@ static int process_daemon_batch(engine_t *e, char **lines, int n_lines)
continue;
}
print_embedding_json(embs + (size_t)next_valid * e->dim,
- e->dim, tokens[i].n_tokens, ms);
+ e->dim, tokens[i].n_tokens, ms,
+ engine_workspace_nbytes(e));
next_valid++;
}
fflush(stdout);
diff --git a/pplx_embed.c b/pplx_embed.c
index ecc7fa0..ebe945c 100644
--- a/pplx_embed.c
+++ b/pplx_embed.c
@@ -79,6 +79,13 @@ static int mul_size(size_t a, size_t b, size_t *out)
return 0;
}
+static int add_size(size_t a, size_t b, size_t *out)
+{
+ if (b > SIZE_MAX - a) return -1;
+ *out = a + b;
+ return 0;
+}
+
static int grow_cap(int current, int needed, int *out)
{
if (needed <= 0) return -1;
@@ -656,6 +663,57 @@ const pplx_config_t *pplx_model_config(const pplx_model_t *model)
return model ? &model->config : NULL;
}
+size_t pplx_workspace_nbytes(const pplx_workspace_t *ws)
+{
+ if (!ws) return 0;
+
+ size_t total = sizeof(*ws);
+ const pplx_model_t *model = ws->model;
+ if (!model) return total;
+
+ const pplx_config_t *c = &model->config;
+ size_t row_floats = 0;
+ const int dims[] = {
+ c->hidden_size, /* x */
+ c->hidden_size, /* x_norm */
+ c->q_dim, /* q */
+ c->kv_dim, /* k */
+ c->kv_dim, /* v */
+ c->q_dim, /* attn_out */
+ c->hidden_size, /* proj_out */
+ c->intermediate_size, /* ffn_gate */
+ c->intermediate_size, /* ffn_up */
+ };
+
+ for (size_t i = 0; i < sizeof(dims) / sizeof(dims[0]); i++) {
+ if (dims[i] < 0 ||
+ add_size(row_floats, (size_t)dims[i], &row_floats) != 0)
+ return SIZE_MAX;
+ }
+
+ size_t buf_floats = 0;
+ if (ws->buf_seq_cap > 0 &&
+ mul_size((size_t)ws->buf_seq_cap, row_floats, &buf_floats) != 0)
+ return SIZE_MAX;
+
+ size_t rope_floats = 0;
+ if (ws->rope_cache_cap > 0) {
+ size_t rope_rows = 0;
+ if (mul_size((size_t)ws->rope_cache_cap, (size_t)c->head_dim,
+ &rope_rows) != 0 ||
+ mul_size(rope_rows, 2, &rope_floats) != 0)
+ return SIZE_MAX;
+ }
+
+ size_t all_floats = 0, bytes = 0;
+ if (add_size(buf_floats, rope_floats, &all_floats) != 0 ||
+ mul_size(all_floats, sizeof(float), &bytes) != 0 ||
+ add_size(total, bytes, &total) != 0)
+ return SIZE_MAX;
+
+ return total;
+}
+
/* ========================================================================
* Packed forward pass
* ======================================================================== */
diff --git a/pplx_embed.h b/pplx_embed.h
index 1bac5f6..23ca2a9 100644
--- a/pplx_embed.h
+++ b/pplx_embed.h
@@ -75,6 +75,14 @@ void pplx_workspace_free(pplx_workspace_t *ws);
const pplx_config_t *pplx_model_config(const pplx_model_t *model);
/*
+ * Current mutable workspace memory owned by this workspace, in bytes.
+ *
+ * This includes the workspace struct, scratch buffers, and RoPE cache capacity.
+ * It does not include immutable mmap'd model weights.
+ */
+size_t pplx_workspace_nbytes(const pplx_workspace_t *ws);
+
+/*
* Compute embedding for a token sequence.
*
* 1. Token embedding lookup