aboutsummaryrefslogtreecommitdiff
path: root/pplx_embed.c
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 /pplx_embed.c
parent9bfcc72ee6b2568d0f9fc05a28f9ccc3da176713 (diff)
downloadffwd-8d3cd6f2ec22dc3079a9ce5f632d09a0526954b9.tar.gz
ffwd-8d3cd6f2ec22dc3079a9ce5f632d09a0526954b9.zip
server: report workspace memory
Diffstat (limited to 'pplx_embed.c')
-rw-r--r--pplx_embed.c58
1 files changed, 58 insertions, 0 deletions
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
* ======================================================================== */