aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-10 14:32:11 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-14 08:27:03 +0200
commit3f6853a7cb8fc766fea72611779915915e912501 (patch)
tree96dc47713376fc44c1e9d16de55444309f2abb4e /tests
parent4723631fe34e7aae4e9def3aeb0c923de2a50cf8 (diff)
downloadffwd-3f6853a7cb8fc766fea72611779915915e912501.tar.gz
ffwd-3f6853a7cb8fc766fea72611779915915e912501.zip
tests: cover the late-interaction API and the CLI binary
Diffstat (limited to 'tests')
-rw-r--r--tests/test_cli.c198
-rw-r--r--tests/test_late.c333
-rw-r--r--tests/test_workspace.c89
-rw-r--r--tests/tiny_model.h114
4 files changed, 698 insertions, 36 deletions
diff --git a/tests/test_cli.c b/tests/test_cli.c
new file mode 100644
index 0000000..fd464f0
--- /dev/null
+++ b/tests/test_cli.c
@@ -0,0 +1,198 @@
+/* tests/test_cli.c - drives the pplx-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`. */
+
+#include "tiny_model.h"
+#include "tok_fixture.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+static char g_dir[1024]; /* fixture model dir */
+static char g_out[1100]; /* captured stdout */
+static char g_err[1100]; /* captured stderr */
+static char g_in[1100]; /* stdin source */
+static const char *g_cli;
+static int g_failures;
+
+/* Run the CLI with sh-formatted args; returns its exit status (-1 on a
+ * harness error). stdin_text == NULL feeds /dev/null. */
+static int run_cli(const char *args, const char *stdin_text)
+{
+ if (stdin_text) {
+ FILE *f = fopen(g_in, "w");
+ if (!f) return -1;
+ fputs(stdin_text, f);
+ fclose(f);
+ }
+ char cmd[4096];
+ int n = snprintf(cmd, sizeof(cmd), "'%s' %s > '%s' 2> '%s' < '%s'",
+ g_cli, args, g_out, g_err,
+ stdin_text ? g_in : "/dev/null");
+ if (n < 0 || (size_t)n >= sizeof(cmd)) return -1;
+ int st = system(cmd);
+ if (st == -1 || !WIFEXITED(st)) return -1;
+ return WEXITSTATUS(st);
+}
+
+static void expect(int cond, const char *what)
+{
+ if (!cond) {
+ fprintf(stderr, "FAIL: %s\n", what);
+ g_failures++;
+ }
+}
+
+static int file_contains(const char *path, const char *needle)
+{
+ static char buf[1 << 16];
+ FILE *f = fopen(path, "rb");
+ if (!f) return 0;
+ size_t n = fread(buf, 1, sizeof(buf) - 1, f);
+ fclose(f);
+ buf[n] = '\0';
+ return strstr(buf, needle) != NULL;
+}
+
+static int count_lines(const char *path)
+{
+ FILE *f = fopen(path, "rb");
+ if (!f) return -1;
+ int lines = 0, c;
+ while ((c = fgetc(f)) != EOF)
+ if (c == '\n') lines++;
+ fclose(f);
+ return lines;
+}
+
+/* First stdout line must hold exactly `dim` finite floats. */
+static int stdout_is_embedding(int dim)
+{
+ FILE *f = fopen(g_out, "r");
+ if (!f) return 0;
+ int count = 0;
+ float v;
+ while (fscanf(f, "%f", &v) == 1) {
+ if (!isfinite((double)v)) {
+ fclose(f);
+ return 0;
+ }
+ count++;
+ }
+ fclose(f);
+ return count == dim;
+}
+
+int main(int argc, char **argv)
+{
+ g_cli = argc > 1 ? argv[1] : "./pplx-embed";
+
+ tm_dims_t dims = {4, 2, 1, 2, 8, TF_VOCAB_SIZE};
+ snprintf(g_dir, sizeof(g_dir), "%s/pplx-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) {
+ fprintf(stderr, "fixture creation failed\n");
+ return 2;
+ }
+ snprintf(g_out, sizeof(g_out), "%s/stdout.txt", g_dir);
+ snprintf(g_err, sizeof(g_err), "%s/stderr.txt", g_dir);
+ snprintf(g_in, sizeof(g_in), "%s/stdin.txt", g_dir);
+
+ char args[2048];
+
+ /* 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,
+ "missing model dir exits 1");
+
+ /* Mode 1: one text prints one embedding line. */
+ snprintf(args, sizeof(args), "-d '%s' 'hello world'", g_dir);
+ expect(run_cli(args, NULL) == 0, "single text exits 0");
+ expect(stdout_is_embedding(dims.hidden), "single text prints dim floats");
+
+ /* Explicit CPU backend and thread count. */
+ snprintf(args, sizeof(args),
+ "-d '%s' --backend cpu -t 2 -v 'hello world'", g_dir);
+ expect(run_cli(args, NULL) == 0, "--backend cpu -t 2 -v exits 0");
+ expect(file_contains(g_err, "Using 2 CPU thread(s)"),
+ "-v reports the thread count");
+
+ /* Mode 2+: several texts print a similarity matrix. */
+ snprintf(args, sizeof(args), "-d '%s' 'hello world' 'held'", g_dir);
+ expect(run_cli(args, NULL) == 0, "two texts exit 0");
+ expect(file_contains(g_out, "Cosine similarity matrix (2 texts):"),
+ "two texts print a similarity matrix");
+ expect(file_contains(g_out, "1.0000"), "matrix diagonal is 1.0000");
+
+ snprintf(args, sizeof(args),
+ "-d '%s' -e -b 1 'hello' 'world' 'held'", g_dir);
+ expect(run_cli(args, NULL) == 0, "-e -b 1 with three texts exits 0");
+ expect(file_contains(g_out, "Cosine similarity matrix (3 texts):"),
+ "-b 1 still prints the full matrix");
+ expect(file_contains(g_out, "Embeddings:"), "-e prints raw embeddings");
+
+ /* Batch mode reads stdin when no texts are given. */
+ snprintf(args, sizeof(args), "-d '%s'", g_dir);
+ expect(run_cli(args, "hello world\nheld\n") == 0, "stdin batch exits 0");
+ expect(file_contains(g_out, "Cosine similarity matrix (2 texts):"),
+ "stdin batch prints a similarity matrix");
+ expect(run_cli(args, "\n\n") == 1, "blank stdin exits 1");
+
+ /* Streaming mode writes one JSON object per input line. */
+ snprintf(args, sizeof(args), "-d '%s' --stream", g_dir);
+ expect(run_cli(args, "hello\nworld\n") == 0, "--stream exits 0");
+ expect(count_lines(g_out) == 2, "--stream writes one line per input");
+ expect(file_contains(g_out, "{\"embedding\":["),
+ "--stream writes embedding JSON");
+
+ snprintf(args, sizeof(args), "-d '%s' --stream -b 2 -v", g_dir);
+ expect(run_cli(args, "hello\nworld\nheld\n") == 0,
+ "--stream -b 2 exits 0");
+ expect(count_lines(g_out) == 3, "--stream -b 2 covers the tail batch");
+
+ /* Flag validation: every bad invocation must exit 1.
+ * This is a CPU-only build, so the GPU flags are rejected too. */
+ static const struct { const char *args; const char *what; } bad[] = {
+ {"--backend foo", "invalid --backend"},
+ {"--backend mlx", "--backend mlx without MLX build"},
+ {"--backend cuda", "--backend cuda without CUDA build"},
+ {"--mlx", "--mlx without MLX build"},
+ {"--cuda", "--cuda without CUDA build"},
+ {"--mlx-quant-bits 4", "--mlx-quant-bits 4"},
+ {"--mlx-quant-group-size 0", "--mlx-quant-group-size 0"},
+ {"--layers 5:2", "inverted --layers range"},
+ {"--layers nope", "malformed --layers"},
+ {"--dist-activation-bits 8", "--dist-activation-bits 8"},
+ {"--cuda-gemm-mode f32 -d X t", "--cuda-gemm-mode without CUDA"},
+ {"--cuda-weight-dtype bf16 -d X t", "--cuda-weight-dtype without CUDA"},
+ };
+ for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); i++)
+ expect(run_cli(bad[i].args, NULL) == 1, bad[i].what);
+
+ /* Validation that runs after the model dir is accepted. */
+ snprintf(args, sizeof(args), "-d '%s' --mlx-quant-bits 8 t", g_dir);
+ expect(run_cli(args, NULL) == 1, "--mlx-quant-bits without --mlx");
+ snprintf(args, sizeof(args), "-d '%s' --dist-worker", g_dir);
+ expect(run_cli(args, NULL) == 1, "--dist-worker without --layers");
+ snprintf(args, sizeof(args),
+ "-d '%s' --dist-worker --dist-remote h:1 --layers 0:1", g_dir);
+ expect(run_cli(args, NULL) == 1, "--dist-worker with --dist-remote");
+ snprintf(args, sizeof(args),
+ "-d '%s' --dist-activation-bits 16 t", g_dir);
+ expect(run_cli(args, NULL) == 1,
+ "--dist-activation-bits without --dist-remote");
+
+ if (g_failures) {
+ fprintf(stderr, "%d CLI check(s) failed\n", g_failures);
+ return 1;
+ }
+ printf("ok: CLI modes and flag validation against %s\n", g_cli);
+ return 0;
+}
diff --git a/tests/test_late.c b/tests/test_late.c
new file mode 100644
index 0000000..2243057
--- /dev/null
+++ b/tests/test_late.c
@@ -0,0 +1,333 @@
+/* tests/test_late.c - late-interaction (ColBERT-style MaxSim) API tests.
+ * Hermetic: synthesizes a tiny base model, a 1_Dense projection head, and a
+ * byte-complete tokenizer fixture. Runs via `make test`. */
+
+#include "embed.h"
+#include "qwen_tokenizer.h"
+#include "tiny_model.h"
+#include "tok_fixture.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+enum { TOKEN_DIM = 6 };
+
+static float max_abs_diff(const float *a, const float *b, size_t n)
+{
+ float m = 0.0f;
+ for (size_t i = 0; i < n; i++) {
+ float d = fabsf(a[i] - b[i]);
+ if (d > m) m = d;
+ }
+ return m;
+}
+
+static float row_norm(const float *v, int n)
+{
+ float s = 0.0f;
+ for (int i = 0; i < n; i++) s += v[i] * v[i];
+ return sqrtf(s);
+}
+
+/* Parity between the grouped batch scorer and per-document MaxSim. */
+static int check_maxsim_batch_parity(const float *query, int query_tokens,
+ const float *docs, const int *offsets,
+ int n_docs, int dim, const char *what)
+{
+ 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,
+ 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,
+ docs + (size_t)offsets[i] * dim,
+ offsets[i + 1] - offsets[i], dim);
+ if (fabsf(scores[i] - one) > 0.0001f) {
+ fprintf(stderr, "maxsim_batch (%s) doc %d: %g vs %g\n",
+ what, i, scores[i], one);
+ return 1;
+ }
+ }
+ return 0;
+}
+
+int main(void)
+{
+ int rc = 1;
+ tm_dims_t dims = {4, 2, 1, 2, 8, TF_VOCAB_SIZE};
+ 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;
+ int *ids = NULL;
+ float *states = NULL, *raw = NULL, *vecs = NULL, *expected = NULL;
+ float *doc_vecs = NULL;
+ float *big_q = NULL, *big_d = NULL;
+ int *doc_ids[3] = {0};
+
+ char dir[1024];
+ snprintf(dir, sizeof(dir), "%s/pplx-late-test-XXXXXX",
+ getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
+ if (!mkdtemp(dir) || tf_write_vocab(dir) != 0 ||
+ tm_write_model_dims(dir, "F32", &dims) != 0) {
+ fprintf(stderr, "fixture creation failed\n");
+ return 2;
+ }
+
+ /* 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))) {
+ 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) {
+ 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) {
+ fprintf(stderr, "NULL-argument handling failed\n");
+ goto fail;
+ }
+ pplx_late_workspace_free(NULL);
+ pplx_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,
+ "dense.weight") != 0) {
+ fprintf(stderr, "failed to write malformed projection\n");
+ goto fail;
+ }
+ if (pplx_late_model_load(dir) != NULL) {
+ fprintf(stderr, "late load accepted a head without linear.weight\n");
+ goto fail;
+ }
+
+ if (tm_write_late_projection(dir, "F32", TOKEN_DIM, hidden) != 0) {
+ fprintf(stderr, "failed to write projection\n");
+ goto fail;
+ }
+ late = pplx_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) {
+ fprintf(stderr, "pooled load accepted a late-interaction snapshot\n");
+ goto fail;
+ }
+
+ const pplx_config_t *cfg = pplx_late_model_config(late);
+ if (!cfg || cfg->hidden_size != hidden ||
+ pplx_late_model_token_dim(late) != TOKEN_DIM) {
+ fprintf(stderr, "late config/token_dim mismatch\n");
+ goto fail;
+ }
+
+ ws = pplx_late_workspace_new(late);
+ if (!ws) {
+ fprintf(stderr, "late workspace allocation failed\n");
+ goto fail;
+ }
+
+ char vocab_path[1280];
+ snprintf(vocab_path, sizeof(vocab_path), "%s/vocab.json", dir);
+ tok = qwen_tokenizer_load(vocab_path);
+ if (!tok) {
+ fprintf(stderr, "tokenizer load failed\n");
+ goto fail;
+ }
+
+ int n_tokens = 0;
+ ids = qwen_tokenizer_encode(tok, "hello world", &n_tokens);
+ if (!ids || n_tokens <= 1) {
+ fprintf(stderr, "tokenization failed\n");
+ goto fail;
+ }
+
+ size_t out_count = (size_t)n_tokens * TOKEN_DIM;
+ raw = (float *)calloc(out_count, sizeof(float));
+ vecs = (float *)calloc(out_count, sizeof(float));
+ expected = (float *)calloc(out_count, sizeof(float));
+ if (!raw || !vecs || !expected) {
+ fprintf(stderr, "allocation failure\n");
+ 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) {
+ 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) {
+ 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);
+ if (!states) {
+ fprintf(stderr, "pplx_model_forward failed\n");
+ goto fail;
+ }
+ for (int t = 0; t < n_tokens; t++) {
+ for (int r = 0; r < TOKEN_DIM; r++) {
+ float acc = 0.0f;
+ for (int k = 0; k < hidden; k++)
+ acc += states[(size_t)t * hidden + k] *
+ tm_value("linear.weight", (size_t)r * hidden + k);
+ expected[(size_t)t * TOKEN_DIM + r] = acc;
+ }
+ }
+ if (max_abs_diff(raw, expected, out_count) > 0.0001f) {
+ fprintf(stderr, "projection mismatch vs reference: %g\n",
+ max_abs_diff(raw, expected, out_count));
+ goto fail;
+ }
+
+ /* normalize=1 returns the raw rows scaled to unit length. */
+ for (int t = 0; t < n_tokens; t++) {
+ const float *nv = vecs + (size_t)t * TOKEN_DIM;
+ const float *rv = raw + (size_t)t * TOKEN_DIM;
+ if (fabsf(row_norm(nv, TOKEN_DIM) - 1.0f) > 0.0001f) {
+ fprintf(stderr, "row %d is not unit norm\n", t);
+ goto fail;
+ }
+ float scale = row_norm(rv, TOKEN_DIM);
+ for (int r = 0; r < TOKEN_DIM; r++) {
+ if (fabsf(nv[r] * scale - rv[r]) > 0.0001f) {
+ fprintf(stderr, "row %d direction changed by normalize\n", t);
+ goto fail;
+ }
+ }
+ }
+
+ /* Hand-computed MaxSim: best dot per query token, summed.
+ * q0 matches d0 exactly (1.0); q1's best is d0 (0.0). */
+ {
+ 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);
+ 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) {
+ fprintf(stderr, "maxsim accepted invalid arguments\n");
+ goto fail;
+ }
+ }
+
+ /* Batch scoring parity on real encoded vectors (three small docs share
+ * one similarity group). */
+ {
+ const char *doc_texts[3] = {"hello world", "held", "wor world hello"};
+ int lens[3] = {0};
+ int offsets[4] = {0};
+ for (int i = 0; i < 3; i++) {
+ doc_ids[i] = qwen_tokenizer_encode(tok, doc_texts[i], &lens[i]);
+ if (!doc_ids[i] || lens[i] <= 0) {
+ fprintf(stderr, "doc tokenization failed\n");
+ goto fail;
+ }
+ offsets[i + 1] = offsets[i] + lens[i];
+ }
+ doc_vecs = (float *)calloc((size_t)offsets[3] * TOKEN_DIM,
+ sizeof(float));
+ if (!doc_vecs) {
+ fprintf(stderr, "allocation failure\n");
+ goto fail;
+ }
+ for (int i = 0; i < 3; i++) {
+ if (pplx_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");
+ goto fail;
+ }
+ }
+ if (check_maxsim_batch_parity(vecs, n_tokens, doc_vecs, offsets, 3,
+ TOKEN_DIM, "encoded docs") != 0)
+ goto fail;
+
+ 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,
+ TOKEN_DIM, scores) == 0 ||
+ pplx_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,
+ TOKEN_DIM, NULL) == 0) {
+ fprintf(stderr, "maxsim_batch accepted invalid arguments\n");
+ goto fail;
+ }
+ }
+
+ /* One document larger than the similarity-GEMM token budget must score
+ * identically to the scalar path (it runs alone in its own group). */
+ {
+ enum { BIG_DIM = 4, BIG_Q = 3, BIG_DOC0 = 4100, BIG_DOC1 = 10 };
+ int offsets[3] = {0, BIG_DOC0, BIG_DOC0 + BIG_DOC1};
+ big_q = (float *)malloc((size_t)BIG_Q * BIG_DIM * sizeof(float));
+ big_d = (float *)malloc((size_t)offsets[2] * BIG_DIM * sizeof(float));
+ if (!big_q || !big_d) {
+ fprintf(stderr, "allocation failure\n");
+ goto fail;
+ }
+ unsigned s = 12345u;
+ for (size_t i = 0; i < (size_t)BIG_Q * BIG_DIM; i++) {
+ s = s * 1664525u + 1013904223u;
+ big_q[i] = (float)((s >> 8) & 0xFFFF) / 32768.0f - 1.0f;
+ }
+ for (size_t i = 0; i < (size_t)offsets[2] * BIG_DIM; i++) {
+ s = s * 1664525u + 1013904223u;
+ big_d[i] = (float)((s >> 8) & 0xFFFF) / 32768.0f - 1.0f;
+ }
+ if (check_maxsim_batch_parity(big_q, BIG_Q, big_d, offsets, 2,
+ BIG_DIM, "oversized doc") != 0)
+ goto fail;
+ }
+
+ printf("ok: late API token_dim=%d tokens=%d hidden=%d\n",
+ TOKEN_DIM, n_tokens, hidden);
+ rc = 0;
+
+fail:
+ free(big_d);
+ free(big_q);
+ free(doc_vecs);
+ for (int i = 0; i < 3; i++) free(doc_ids[i]);
+ free(expected);
+ free(vecs);
+ free(raw);
+ 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);
+ return rc;
+}
diff --git a/tests/test_workspace.c b/tests/test_workspace.c
index ef5c68c..66f6b53 100644
--- a/tests/test_workspace.c
+++ b/tests/test_workspace.c
@@ -24,6 +24,86 @@ static float max_abs_diff(const float *a, const float *b, int n)
return m;
}
+/* 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,
+ const int *ids0, int n0,
+ const float *states,
+ const float *reference,
+ const pplx_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);
+ pooled = (float *)calloc((size_t)dim, sizeof(float));
+ span_out = (float *)calloc((size_t)2 * dim, sizeof(float));
+ if (!emb || !fwd || !pooled || !span_out) {
+ fprintf(stderr, "alloc-variant allocation failure\n");
+ 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) {
+ 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");
+ goto done;
+ }
+ if (max_abs_diff(fwd, states, n0 * dim) > 0.000001f) {
+ fprintf(stderr, "pplx_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 ||
+ max_abs_diff(pooled, reference, dim) > 0.00005f) {
+ fprintf(stderr, "pplx_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,
+ 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,
+ span_out) != 0) {
+ fprintf(stderr, "pplx_model_embed_spans failed for two spans\n");
+ goto done;
+ }
+ for (int d = 0; d < dim; d++) {
+ float combined = ((float)h * span_out[d] +
+ (float)(n0 - h) * span_out[dim + d]) / (float)n0;
+ if (fabsf(combined - reference[d]) > 0.00005f) {
+ fprintf(stderr, "half spans do not recombine into embedding\n");
+ goto done;
+ }
+ }
+
+ rc = 0;
+done:
+ free(span_out);
+ free(pooled);
+ free(fwd);
+ free(emb);
+ return rc;
+}
+
int main(int argc, char **argv)
{
char fixture_dir[1024];
@@ -186,6 +266,15 @@ int main(int argc, char **argv)
goto fail;
}
+ if (check_alloc_and_pooling_variants(model, batch_ws, ids[0], ntok[0],
+ states, batched, cfg) != 0) {
+ free(states);
+ free(normalized);
+ free(single);
+ free(batched);
+ goto fail;
+ }
+
free(states);
free(normalized);
free(single);
diff --git a/tests/tiny_model.h b/tests/tiny_model.h
index 0a4e67b..8c23e2c 100644
--- a/tests/tiny_model.h
+++ b/tests/tiny_model.h
@@ -5,11 +5,14 @@
#ifndef TINY_MODEL_H
#define TINY_MODEL_H
+#include <errno.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
static uint16_t tm_f32_to_bf16(float x)
{
@@ -54,47 +57,18 @@ typedef struct {
enum { TM_N_SPECS = 13 };
-/* Write config.json + model.safetensors with the given dimensions into dir.
+/* Write a safetensors file holding `specs` with tm_value() contents.
* dtype: "F32" or "BF16". Returns 0 on success. */
-static int tm_write_model_dims(const char *dir, const char *dtype,
- const tm_dims_t *d)
+static int tm_write_safetensors(const char *path, const char *dtype,
+ const tm_spec_t *specs, int n_specs)
{
- int q = d->heads * d->head_dim, kv = d->kv_heads * d->head_dim;
- const tm_spec_t specs[TM_N_SPECS] = {
- {"embed_tokens.weight", d->vocab, d->hidden},
- {"layers.0.self_attn.q_proj.weight", q, d->hidden},
- {"layers.0.self_attn.k_proj.weight", kv, d->hidden},
- {"layers.0.self_attn.v_proj.weight", kv, d->hidden},
- {"layers.0.self_attn.o_proj.weight", d->hidden, q},
- {"layers.0.self_attn.q_norm.weight", d->head_dim, 0},
- {"layers.0.self_attn.k_norm.weight", d->head_dim, 0},
- {"layers.0.input_layernorm.weight", d->hidden, 0},
- {"layers.0.post_attention_layernorm.weight", d->hidden, 0},
- {"layers.0.mlp.gate_proj.weight", d->intermediate, d->hidden},
- {"layers.0.mlp.up_proj.weight", d->intermediate, d->hidden},
- {"layers.0.mlp.down_proj.weight", d->hidden, d->intermediate},
- {"norm.weight", d->hidden, 0},
- };
-
- char path[2048];
- snprintf(path, sizeof(path), "%s/config.json", dir);
- FILE *f = fopen(path, "w");
- if (!f) return -1;
- fprintf(f, "{\"hidden_size\":%d,\"num_hidden_layers\":1,"
- "\"num_attention_heads\":%d,\"num_key_value_heads\":%d,"
- "\"head_dim\":%d,\"intermediate_size\":%d,\"vocab_size\":%d,"
- "\"rms_norm_eps\":1e-6,\"rope_theta\":10000.0}",
- d->hidden, d->heads, d->kv_heads, d->head_dim,
- d->intermediate, d->vocab);
- fclose(f);
-
int bf16 = strcmp(dtype, "BF16") == 0;
size_t esize = bf16 ? 2 : 4;
char header[4096];
size_t hoff = 0, doff = 0;
hoff += (size_t)snprintf(header + hoff, sizeof(header) - hoff, "{");
- for (int t = 0; t < TM_N_SPECS; t++) {
+ for (int t = 0; t < n_specs; t++) {
const tm_spec_t *s = &specs[t];
size_t n = (size_t)s->rows * (s->cols ? (size_t)s->cols : 1);
size_t end = doff + n * esize;
@@ -113,14 +87,13 @@ static int tm_write_model_dims(const char *dir, const char *dtype,
hoff += (size_t)snprintf(header + hoff, sizeof(header) - hoff, "}");
if (hoff >= sizeof(header) - 1) return -1;
- snprintf(path, sizeof(path), "%s/model.safetensors", dir);
- f = fopen(path, "wb");
+ FILE *f = fopen(path, "wb");
if (!f) return -1;
unsigned char len8[8];
for (int i = 0; i < 8; i++) len8[i] = (unsigned char)(hoff >> (8 * i));
fwrite(len8, 1, 8, f);
fwrite(header, 1, hoff, f);
- for (int t = 0; t < TM_N_SPECS; t++) {
+ for (int t = 0; t < n_specs; t++) {
const tm_spec_t *s = &specs[t];
size_t n = (size_t)s->rows * (s->cols ? (size_t)s->cols : 1);
for (size_t i = 0; i < n; i++) {
@@ -137,6 +110,75 @@ static int tm_write_model_dims(const char *dir, const char *dtype,
return 0;
}
+/* Write config.json + model.safetensors with the given dimensions into dir.
+ * dtype: "F32" or "BF16". Returns 0 on success. */
+static int tm_write_model_dims(const char *dir, const char *dtype,
+ const tm_dims_t *d)
+{
+ int q = d->heads * d->head_dim, kv = d->kv_heads * d->head_dim;
+ const tm_spec_t specs[TM_N_SPECS] = {
+ {"embed_tokens.weight", d->vocab, d->hidden},
+ {"layers.0.self_attn.q_proj.weight", q, d->hidden},
+ {"layers.0.self_attn.k_proj.weight", kv, d->hidden},
+ {"layers.0.self_attn.v_proj.weight", kv, d->hidden},
+ {"layers.0.self_attn.o_proj.weight", d->hidden, q},
+ {"layers.0.self_attn.q_norm.weight", d->head_dim, 0},
+ {"layers.0.self_attn.k_norm.weight", d->head_dim, 0},
+ {"layers.0.input_layernorm.weight", d->hidden, 0},
+ {"layers.0.post_attention_layernorm.weight", d->hidden, 0},
+ {"layers.0.mlp.gate_proj.weight", d->intermediate, d->hidden},
+ {"layers.0.mlp.up_proj.weight", d->intermediate, d->hidden},
+ {"layers.0.mlp.down_proj.weight", d->hidden, d->intermediate},
+ {"norm.weight", d->hidden, 0},
+ };
+
+ char path[2048];
+ snprintf(path, sizeof(path), "%s/config.json", dir);
+ FILE *f = fopen(path, "w");
+ if (!f) return -1;
+ fprintf(f, "{\"hidden_size\":%d,\"num_hidden_layers\":1,"
+ "\"num_attention_heads\":%d,\"num_key_value_heads\":%d,"
+ "\"head_dim\":%d,\"intermediate_size\":%d,\"vocab_size\":%d,"
+ "\"rms_norm_eps\":1e-6,\"rope_theta\":10000.0}",
+ d->hidden, d->heads, d->kv_heads, d->head_dim,
+ d->intermediate, d->vocab);
+ fclose(f);
+
+ snprintf(path, sizeof(path), "%s/model.safetensors", dir);
+ return tm_write_safetensors(path, dtype, specs, TM_N_SPECS);
+}
+
+/* Add the 1_Dense per-token projection head that turns a base model dir into
+ * a late-interaction snapshot. weight_name is "linear.weight" for a valid
+ * head; pass another name to synthesize a malformed head. */
+static inline int tm_write_late_projection_named(const char *dir,
+ const char *dtype,
+ int token_dim, int hidden,
+ const char *weight_name)
+{
+ char path[2048];
+ snprintf(path, sizeof(path), "%s/1_Dense", dir);
+ if (mkdir(path, 0755) != 0 && errno != EEXIST) return -1;
+
+ snprintf(path, sizeof(path), "%s/1_Dense/config.json", dir);
+ FILE *f = fopen(path, "w");
+ if (!f) return -1;
+ fprintf(f, "{\"in_features\":%d,\"out_features\":%d,\"bias\":false}",
+ hidden, token_dim);
+ fclose(f);
+
+ const tm_spec_t spec = {weight_name, token_dim, hidden};
+ snprintf(path, sizeof(path), "%s/1_Dense/model.safetensors", dir);
+ return tm_write_safetensors(path, dtype, &spec, 1);
+}
+
+static inline int tm_write_late_projection(const char *dir, const char *dtype,
+ int token_dim, int hidden)
+{
+ return tm_write_late_projection_named(dir, dtype, token_dim, hidden,
+ "linear.weight");
+}
+
/* The historical default fixture: 4-dim hidden, 16-token vocab. */
static inline int tm_write_model(const char *dir, const char *dtype)
{