aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-15 08:40:11 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-15 08:40:11 +0200
commitc4a1c97a09da6d8d88c2ac834bf6234506fc41bc (patch)
tree81ce5675ce886aad0309a41f4d6a696826e0a061 /tests
parentb377ba8656bfb807ed8dda6c9f26942b50ce626d (diff)
downloadffwd-c4a1c97a09da6d8d88c2ac834bf6234506fc41bc.tar.gz
ffwd-c4a1c97a09da6d8d88c2ac834bf6234506fc41bc.zip
chore: run clang-format
Diffstat (limited to 'tests')
-rw-r--r--tests/test_bf16_model.c20
-rw-r--r--tests/test_cli.c76
-rw-r--r--tests/test_kernels.c119
-rw-r--r--tests/test_late.c85
-rw-r--r--tests/test_qwen3.c25
-rw-r--r--tests/test_safetensors.c67
-rw-r--r--tests/test_server.c436
-rw-r--r--tests/test_tokenizer.c159
-rw-r--r--tests/test_workspace.c78
-rw-r--r--tests/tiny_model.h97
-rw-r--r--tests/tok_fixture.h36
11 files changed, 596 insertions, 602 deletions
diff --git a/tests/test_bf16_model.c b/tests/test_bf16_model.c
index cc9b2fb..9a5d842 100644
--- a/tests/test_bf16_model.c
+++ b/tests/test_bf16_model.c
@@ -15,12 +15,14 @@ int main(void) {
char root[1024], f32_dir[1088], bf16_dir[1088];
snprintf(root, sizeof(root), "%s/embed-bf16-test-XXXXXX",
getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
- if (!mkdtemp(root)) { perror("mkdtemp"); return 2; }
+ if (!mkdtemp(root)) {
+ perror("mkdtemp");
+ return 2;
+ }
snprintf(f32_dir, sizeof(f32_dir), "%s/f32", root);
snprintf(bf16_dir, sizeof(bf16_dir), "%s/bf16", root);
if (mkdir(f32_dir, 0755) != 0 || mkdir(bf16_dir, 0755) != 0 ||
- tm_write_model(f32_dir, "F32") != 0 ||
- tm_write_model(bf16_dir, "BF16") != 0) {
+ tm_write_model(f32_dir, "F32") != 0 || tm_write_model(bf16_dir, "BF16") != 0) {
fprintf(stderr, "fixture creation failed\n");
return 2;
}
@@ -58,7 +60,8 @@ int main(void) {
return 1;
}
float d = fabsf(a[i] - b[i]);
- if (d > max_diff) max_diff = d;
+ if (d > max_diff)
+ max_diff = d;
norm += b[i] * b[i];
}
norm = sqrtf(norm);
@@ -73,7 +76,8 @@ int main(void) {
* (linear_nobias_weight); parity must hold there too. */
enum { LONG_N = 24 };
int long_ids[LONG_N];
- for (int i = 0; i < LONG_N; i++) long_ids[i] = (i % 14) + 1;
+ for (int i = 0; i < LONG_N; i++)
+ long_ids[i] = (i % 14) + 1;
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");
@@ -86,15 +90,15 @@ int main(void) {
return 1;
}
float d = fabsf(a[i] - b[i]);
- if (d > long_diff) long_diff = d;
+ if (d > long_diff)
+ long_diff = d;
}
if (long_diff > 2e-5f) {
fprintf(stderr, "bad widen-path parity: max_diff=%.9g\n", long_diff);
return 1;
}
- printf("ok: bf16 model parity dim=%d max_abs_diff=%.9g norm=%.9g\n",
- dim, max_diff, norm);
+ printf("ok: bf16 model parity dim=%d max_abs_diff=%.9g norm=%.9g\n", dim, max_diff, norm);
embed_workspace_free(wf32);
embed_workspace_free(wbf16);
diff --git a/tests/test_cli.c b/tests/test_cli.c
index 58cbad3..8d56f09 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -13,68 +13,69 @@
#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 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)
-{
+static int run_cli(const char *args, const char *stdin_text) {
if (stdin_text) {
FILE *f = fopen(g_in, "w");
- if (!f) return -1;
+ 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,
+ 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;
+ if (n < 0 || (size_t)n >= sizeof(cmd))
+ return -1;
int st = system(cmd);
- if (st == -1 || !WIFEXITED(st)) return -1;
+ if (st == -1 || !WIFEXITED(st))
+ return -1;
return WEXITSTATUS(st);
}
-static void expect(int cond, const char *what)
-{
+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 int file_contains(const char *path, const char *needle) {
static char buf[1 << 16];
FILE *f = fopen(path, "rb");
- if (!f) return 0;
+ 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)
-{
+static int count_lines(const char *path) {
FILE *f = fopen(path, "rb");
- if (!f) return -1;
+ if (!f)
+ return -1;
int lines = 0, c;
while ((c = fgetc(f)) != EOF)
- if (c == '\n') lines++;
+ if (c == '\n')
+ lines++;
fclose(f);
return lines;
}
/* First stdout line must hold exactly `dim` finite floats. */
-static int stdout_is_embedding(int dim)
-{
+static int stdout_is_embedding(int dim) {
FILE *f = fopen(g_out, "r");
- if (!f) return 0;
+ if (!f)
+ return 0;
int count = 0;
float v;
while (fscanf(f, "%f", &v) == 1) {
@@ -88,8 +89,7 @@ static int stdout_is_embedding(int dim)
return count == dim;
}
-int main(int argc, char **argv)
-{
+int main(int argc, char **argv) {
g_cli = argc > 1 ? argv[1] : "./embed";
tm_dims_t dims = {4, 2, 1, 2, 8, TF_VOCAB_SIZE};
@@ -109,13 +109,11 @@ 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-embed-dir hi", NULL) == 1,
- "missing model dir exits 1");
+ expect(run_cli("-d /nonexistent-embed-dir hi", NULL) == 1, "missing model dir exits 1");
/* -V reports the program's base name, not the path it was invoked by. */
expect(run_cli("-V", NULL) == 0, "-V exits 0");
- expect(!file_contains(g_out, "tests/"),
- "-V strips the directory from the program name");
+ expect(!file_contains(g_out, "tests/"), "-V strips the directory from the program name");
/* Mode 1: one text prints one embedding line. */
snprintf(args, sizeof(args), "-d '%s' 'hello world'", g_dir);
@@ -123,11 +121,9 @@ int main(int argc, char **argv)
expect(stdout_is_embedding(dims.hidden), "single text prints dim floats");
/* Thread count. */
- snprintf(args, sizeof(args),
- "-d '%s' -t 2 -v 'hello world'", g_dir);
+ snprintf(args, sizeof(args), "-d '%s' -t 2 -v 'hello world'", g_dir);
expect(run_cli(args, NULL) == 0, "-t 2 -v exits 0");
- expect(file_contains(g_err, "Using 2 CPU thread(s)"),
- "-v reports the thread count");
+ expect(file_contains(g_err, "Using 2 CPU thread(s)"), "-v reports the thread count");
/* Mode 2+: several texts print a bare numeric matrix, one row per line,
* with no titles or labels. Two texts give a 2x2 with a 1.0 diagonal. */
@@ -152,8 +148,7 @@ int main(int argc, char **argv)
/* -e appends the raw embeddings (label-free) after the matrix: three
* matrix rows, a blank separator, then three embedding rows. */
- snprintf(args, sizeof(args),
- "-d '%s' -e -b 1 'hello' 'world' 'held'", g_dir);
+ 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(count_lines(g_out) == 7, "-e prints matrix then raw embeddings");
@@ -167,16 +162,17 @@ int main(int argc, char **argv)
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");
+ 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(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: missing model dir must exit 1. */
- static const struct { const char *args; const char *what; } bad[] = {
+ static const struct {
+ const char *args;
+ const char *what;
+ } bad[] = {
{"-d '' t", "empty model dir"},
};
for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); i++)
diff --git a/tests/test_kernels.c b/tests/test_kernels.c
index 4a2a57d..fb3d2df 100644
--- a/tests/test_kernels.c
+++ b/tests/test_kernels.c
@@ -18,8 +18,7 @@ static void expect_close(const char *name, float got, float want, float tol) {
float diff = fabsf(got - want);
float scale = fmaxf(1.0f, fmaxf(fabsf(got), fabsf(want)));
if (diff > tol * scale) {
- fprintf(stderr, "%s: got %.9g want %.9g diff %.9g\n",
- name, got, want, diff);
+ fprintf(stderr, "%s: got %.9g want %.9g diff %.9g\n", name, got, want, diff);
failures++;
}
}
@@ -39,10 +38,7 @@ static float bf16_to_f32(uint16_t x) {
static void test_rms_norm(void) {
const int seq = 2, hidden = 4;
- const float x[8] = {
- 1.0f, 2.0f, -3.0f, 4.0f,
- 0.5f, -1.0f, 2.0f, -0.5f
- };
+ const float x[8] = {1.0f, 2.0f, -3.0f, 4.0f, 0.5f, -1.0f, 2.0f, -0.5f};
const float w[4] = {1.0f, 0.5f, -1.0f, 2.0f};
float out[8];
@@ -66,10 +62,7 @@ static void test_rope_neox(void) {
const int seq = 2, heads = 1, head_dim = 4;
const int positions[2] = {0, 3};
float cosv[8], sinv[8];
- float x[8] = {
- 1.0f, 2.0f, 3.0f, 4.0f,
- -1.0f, 0.5f, 2.0f, -0.25f
- };
+ float x[8] = {1.0f, 2.0f, 3.0f, 4.0f, -1.0f, 0.5f, 2.0f, -0.25f};
float orig[8];
memcpy(orig, x, sizeof(x));
@@ -92,10 +85,17 @@ static void test_rope_neox(void) {
}
}
-static void reference_packed_gqa(float *out, const float *Q, const float *K,
- const float *V, const int *offsets, int batch,
- int n_heads, int n_kv_heads, int head_dim,
- float scale, int causal) {
+static void reference_packed_gqa(float *out,
+ const float *Q,
+ const float *K,
+ const float *V,
+ const int *offsets,
+ int batch,
+ int n_heads,
+ int n_kv_heads,
+ int head_dim,
+ float scale,
+ int causal) {
int q_hidden = n_heads * head_dim;
int kv_hidden = n_kv_heads * head_dim;
int heads_per_kv = n_heads / n_kv_heads;
@@ -112,22 +112,28 @@ static void reference_packed_gqa(float *out, const float *Q, const float *K,
for (int j = start; j < key_end; j++) {
const float *k = K + j * kv_hidden + kv_h * head_dim;
float score = 0.0f;
- for (int d = 0; d < head_dim; d++) score += q[d] * k[d];
+ for (int d = 0; d < head_dim; d++)
+ score += q[d] * k[d];
score *= scale;
- if (score > max_score) max_score = score;
+ if (score > max_score)
+ max_score = score;
}
float denom = 0.0f;
- for (int d = 0; d < head_dim; d++) o[d] = 0.0f;
+ for (int d = 0; d < head_dim; d++)
+ o[d] = 0.0f;
for (int j = start; j < key_end; j++) {
const float *k = K + j * kv_hidden + kv_h * head_dim;
const float *v = V + j * kv_hidden + kv_h * head_dim;
float score = 0.0f;
- for (int d = 0; d < head_dim; d++) score += q[d] * k[d];
+ for (int d = 0; d < head_dim; d++)
+ score += q[d] * k[d];
float wt = expf(score * scale - max_score);
denom += wt;
- for (int d = 0; d < head_dim; d++) o[d] += wt * v[d];
+ for (int d = 0; d < head_dim; d++)
+ o[d] += wt * v[d];
}
- for (int d = 0; d < head_dim; d++) o[d] /= denom;
+ for (int d = 0; d < head_dim; d++)
+ o[d] /= denom;
}
}
}
@@ -151,11 +157,9 @@ static void test_packed_gqa_attention(void) {
V[i] = sinf((float)i * 0.07f) - cosf((float)i * 0.13f);
}
- qwen_bidirectional_gqa_attention_packed(got, Q, K, V, offsets, 2,
- n_heads, n_kv_heads, head_dim,
+ qwen_bidirectional_gqa_attention_packed(got, Q, K, V, offsets, 2, n_heads, n_kv_heads, head_dim,
1.0f / sqrtf((float)head_dim));
- reference_packed_gqa(want, Q, K, V, offsets, 2,
- n_heads, n_kv_heads, head_dim,
+ reference_packed_gqa(want, Q, K, V, offsets, 2, n_heads, n_kv_heads, head_dim,
1.0f / sqrtf((float)head_dim), 0);
for (int i = 0; i < total * q_hidden; i++)
@@ -172,7 +176,8 @@ static void test_packed_gqa_attention_long(void) {
float *V = malloc((size_t)total * kv_hidden * sizeof(*V));
float *got = malloc((size_t)total * q_hidden * sizeof(*got));
float *want = malloc((size_t)total * q_hidden * sizeof(*want));
- if (!Q || !K || !V || !got || !want) exit(2);
+ if (!Q || !K || !V || !got || !want)
+ exit(2);
for (int i = 0; i < total * q_hidden; i++)
Q[i] = sinf((float)i * 0.017f) - 0.25f;
@@ -182,11 +187,9 @@ static void test_packed_gqa_attention_long(void) {
}
qwen_set_threads(4);
- qwen_bidirectional_gqa_attention_packed(got, Q, K, V, offsets, 2,
- n_heads, n_kv_heads, head_dim,
+ qwen_bidirectional_gqa_attention_packed(got, Q, K, V, offsets, 2, n_heads, n_kv_heads, head_dim,
1.0f / sqrtf((float)head_dim));
- reference_packed_gqa(want, Q, K, V, offsets, 2,
- n_heads, n_kv_heads, head_dim,
+ reference_packed_gqa(want, Q, K, V, offsets, 2, n_heads, n_kv_heads, head_dim,
1.0f / sqrtf((float)head_dim), 0);
qwen_set_threads(1);
@@ -209,7 +212,8 @@ static void test_packed_causal_gqa_attention(void) {
float *V = malloc((size_t)total * kv_hidden * sizeof(*V));
float *got = malloc((size_t)total * q_hidden * sizeof(*got));
float *want = malloc((size_t)total * q_hidden * sizeof(*want));
- if (!Q || !K || !V || !got || !want) exit(2);
+ if (!Q || !K || !V || !got || !want)
+ exit(2);
for (int i = 0; i < total * q_hidden; i++)
Q[i] = sinf((float)i * 0.019f) - 0.2f;
@@ -219,11 +223,9 @@ static void test_packed_causal_gqa_attention(void) {
}
qwen_set_threads(4);
- qwen_causal_gqa_attention_packed(got, Q, K, V, offsets, 2,
- n_heads, n_kv_heads, head_dim,
+ qwen_causal_gqa_attention_packed(got, Q, K, V, offsets, 2, n_heads, n_kv_heads, head_dim,
1.0f / sqrtf((float)head_dim));
- reference_packed_gqa(want, Q, K, V, offsets, 2,
- n_heads, n_kv_heads, head_dim,
+ reference_packed_gqa(want, Q, K, V, offsets, 2, n_heads, n_kv_heads, head_dim,
1.0f / sqrtf((float)head_dim), 1);
qwen_set_threads(1);
@@ -238,18 +240,13 @@ static void test_packed_causal_gqa_attention(void) {
static void test_bf16_linear(void) {
enum { seq = 2, in_dim = 3, out_dim = 2 };
- const float x[6] = {
- 1.0f, -2.0f, 0.5f,
- -1.0f, 0.25f, 2.0f
- };
- const float wf32[6] = {
- 0.5f, -1.0f, 2.0f,
- -0.25f, 0.75f, 1.5f
- };
+ const float x[6] = {1.0f, -2.0f, 0.5f, -1.0f, 0.25f, 2.0f};
+ const float wf32[6] = {0.5f, -1.0f, 2.0f, -0.25f, 0.75f, 1.5f};
uint16_t wbf16[6];
float got[seq * out_dim];
- for (int i = 0; i < 6; i++) wbf16[i] = f32_to_bf16(wf32[i]);
+ for (int i = 0; i < 6; i++)
+ wbf16[i] = f32_to_bf16(wf32[i]);
qwen_linear_nobias_bf16(got, x, wbf16, seq, in_dim, out_dim);
for (int s = 0; s < seq; s++) {
@@ -287,8 +284,7 @@ static void check_bf16_qkv(int n_threads) {
}
qwen_set_threads(n_threads);
- qwen_linear_nobias_bf16_qkv(got_q, got_k, got_v, x, wq, wk, wv,
- seq, in_dim, q_dim, kv_dim);
+ qwen_linear_nobias_bf16_qkv(got_q, got_k, got_v, x, wq, wk, wv, seq, in_dim, q_dim, kv_dim);
for (int s = 0; s < seq; s++) {
for (int o = 0; o < q_dim; o++) {
@@ -349,7 +345,7 @@ static void test_bf16_matvec(void) {
* rms_norm_worker; row-wise math is identical, so results must match the
* single-thread run exactly. */
static void test_rms_norm_threaded(void) {
- enum { seq = 96, hidden = 3072 }; /* 294912 elements */
+ enum { seq = 96, hidden = 3072 }; /* 294912 elements */
size_t elems = (size_t)seq * hidden;
float *x = (float *)malloc(elems * sizeof(float));
float *w = (float *)malloc((size_t)hidden * sizeof(float));
@@ -358,7 +354,10 @@ static void test_rms_norm_threaded(void) {
if (!x || !w || !one || !many) {
fprintf(stderr, "rms_norm_threaded: allocation failure\n");
failures++;
- free(many); free(one); free(w); free(x);
+ free(many);
+ free(one);
+ free(w);
+ free(x);
return;
}
unsigned s = 7u;
@@ -377,13 +376,15 @@ static void test_rms_norm_threaded(void) {
for (size_t i = 0; i < elems; i++) {
if (one[i] != many[i]) {
- fprintf(stderr, "rms_norm_threaded: mismatch at %zu: %.9g %.9g\n",
- i, one[i], many[i]);
+ fprintf(stderr, "rms_norm_threaded: mismatch at %zu: %.9g %.9g\n", i, one[i], many[i]);
failures++;
break;
}
}
- free(many); free(one); free(w); free(x);
+ free(many);
+ free(one);
+ free(w);
+ free(x);
}
/* bf16->f32 widening is an exact bit shift; check the SIMD body and the
@@ -407,8 +408,7 @@ static void test_bf16_widen_buf(void) {
memcpy(&got_bits, &got[i], sizeof(got_bits));
memcpy(&want_bits, &want, sizeof(want_bits));
if (got_bits != want_bits) {
- fprintf(stderr, "bf16_widen n=%d at %d: %.9g want %.9g\n",
- n, i, got[i], want);
+ fprintf(stderr, "bf16_widen n=%d at %d: %.9g want %.9g\n", n, i, got[i], want);
failures++;
}
}
@@ -436,8 +436,7 @@ static void check_bf16_pair(int n_threads) {
}
qwen_set_threads(n_threads);
- qwen_linear_nobias_bf16_pair(got_a, got_b, x, wa, wb,
- seq, in_dim, a_dim, b_dim);
+ qwen_linear_nobias_bf16_pair(got_a, got_b, x, wa, wb, seq, in_dim, a_dim, b_dim);
for (int s = 0; s < seq; s++) {
for (int o = 0; o < a_dim; o++) {
@@ -477,8 +476,10 @@ static void test_generic_vs_impl(void) {
s = s * 1664525u + 1013904223u;
b[i] = (float)(int32_t)s * 1e-9f;
}
- for (int i = 0; i < IN; i++) x[i] = a[i];
- for (int o = 0; o < OUT; o++) bias[o] = b[o];
+ for (int i = 0; i < IN; i++)
+ x[i] = a[i];
+ for (int o = 0; o < OUT; o++)
+ bias[o] = b[o];
for (size_t i = 0; i < sizeof(W) / sizeof(W[0]); i++) {
s = s * 1664525u + 1013904223u;
W[i] = f32_to_bf16((float)(int32_t)s * 1e-9f);
@@ -497,8 +498,7 @@ static void test_generic_vs_impl(void) {
const int sizes[] = {1, 7, 64, NMAX};
for (size_t t = 0; t < sizeof(sizes) / sizeof(sizes[0]); t++) {
int n = sizes[t];
- expect_close("dot generic-vs-impl",
- qwen_dot_f32_generic(a, b, n),
+ expect_close("dot generic-vs-impl", qwen_dot_f32_generic(a, b, n),
qwen_dot_f32_impl(a, b, n), 1e-5f);
memcpy(d0, a, (size_t)n * sizeof(float));
@@ -538,7 +538,8 @@ int main(void) {
test_bf16_widen_buf();
test_bf16_pair();
test_generic_vs_impl();
- if (failures != 0) return 1;
+ if (failures != 0)
+ return 1;
puts("ok: kernel golden tests passed");
return 0;
}
diff --git a/tests/test_late.c b/tests/test_late.c
index 94bdfe2..e491596 100644
--- a/tests/test_late.c
+++ b/tests/test_late.c
@@ -15,50 +15,50 @@
enum { TOKEN_DIM = 6 };
-static float max_abs_diff(const float *a, const float *b, size_t n)
-{
+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;
+ if (d > m)
+ m = d;
}
return m;
}
-static float row_norm(const float *v, int n)
-{
+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];
+ 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)
-{
+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 (embed_late_maxsim_batch(query, query_tokens, docs, offsets, n_docs,
- dim, scores) != 0) {
+ if (n_docs > (int)(sizeof(scores) / sizeof(scores[0])))
+ return 1;
+ 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 = embed_late_maxsim(query, query_tokens,
- docs + (size_t)offsets[i] * dim,
- offsets[i + 1] - offsets[i], dim);
+ 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) {
- fprintf(stderr, "maxsim_batch (%s) doc %d: %g vs %g\n",
- what, i, scores[i], one);
+ fprintf(stderr, "maxsim_batch (%s) doc %d: %g vs %g\n", what, i, scores[i], one);
return 1;
}
}
return 0;
}
-int main(void)
-{
+int main(void) {
int rc = 1;
tm_dims_t dims = {4, 2, 1, 2, 8, TF_VOCAB_SIZE};
int hidden = dims.hidden;
@@ -77,8 +77,7 @@ int main(void)
char dir[1024];
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) {
+ if (!mkdtemp(dir) || tf_write_vocab(dir) != 0 || tm_write_model_dims(dir, "F32", &dims) != 0) {
fprintf(stderr, "fixture creation failed\n");
return 2;
}
@@ -96,8 +95,7 @@ int main(void)
fprintf(stderr, "late load succeeded without 1_Dense\n");
goto fail;
}
- if (embed_late_model_config(NULL) != NULL ||
- embed_late_model_token_dim(NULL) != 0 ||
+ 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;
@@ -106,8 +104,7 @@ int main(void)
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,
- "dense.weight") != 0) {
+ if (tm_write_late_projection_named(dir, "F32", TOKEN_DIM, hidden, "dense.weight") != 0) {
fprintf(stderr, "failed to write malformed projection\n");
goto fail;
}
@@ -133,8 +130,7 @@ int main(void)
}
const embed_config_t *cfg = embed_late_model_config(late);
- if (!cfg || cfg->hidden_size != hidden ||
- embed_late_model_token_dim(late) != TOKEN_DIM) {
+ if (!cfg || cfg->hidden_size != hidden || embed_late_model_token_dim(late) != TOKEN_DIM) {
fprintf(stderr, "late config/token_dim mismatch\n");
goto fail;
}
@@ -253,33 +249,30 @@ int main(void)
}
offsets[i + 1] = offsets[i] + lens[i];
}
- doc_vecs = (float *)calloc((size_t)offsets[3] * TOKEN_DIM,
- sizeof(float));
+ 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 (embed_late_model_encode_tokens(
- late, ws, doc_ids[i], lens[i], 1,
- doc_vecs + (size_t)offsets[i] * TOKEN_DIM) != 0) {
+ 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");
goto fail;
}
}
- if (check_maxsim_batch_parity(vecs, n_tokens, doc_vecs, offsets, 3,
- TOKEN_DIM, "encoded docs") != 0)
+ 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 (embed_late_maxsim_batch(vecs, n_tokens, doc_vecs, bad_first, 3,
- TOKEN_DIM, scores) == 0 ||
- embed_late_maxsim_batch(vecs, n_tokens, doc_vecs, empty_doc, 2,
- TOKEN_DIM, scores) == 0 ||
- embed_late_maxsim_batch(vecs, n_tokens, doc_vecs, offsets, 3,
- TOKEN_DIM, NULL) == 0) {
+ if (embed_late_maxsim_batch(vecs, n_tokens, doc_vecs, bad_first, 3, TOKEN_DIM, scores) ==
+ 0 ||
+ embed_late_maxsim_batch(vecs, n_tokens, doc_vecs, empty_doc, 2, TOKEN_DIM, scores) ==
+ 0 ||
+ 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;
}
@@ -305,20 +298,20 @@ int main(void)
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)
+ 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);
+ 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]);
+ for (int i = 0; i < 3; i++)
+ free(doc_ids[i]);
free(expected);
free(vecs);
free(raw);
diff --git a/tests/test_qwen3.c b/tests/test_qwen3.c
index 4d63a35..d66e0f7 100644
--- a/tests/test_qwen3.c
+++ b/tests/test_qwen3.c
@@ -9,24 +9,22 @@
#include <string.h>
#include <unistd.h>
-static float max_abs_diff(const float *a, const float *b, int n)
-{
+static float max_abs_diff(const float *a, const float *b, int n) {
float out = 0.0f;
for (int i = 0; i < n; i++) {
float d = fabsf(a[i] - b[i]);
- if (d > out) out = d;
+ if (d > out)
+ out = d;
}
return out;
}
-int main(void)
-{
+int main(void) {
char dir[1024];
snprintf(dir, sizeof(dir), "%s/embed-qwen3-test-XXXXXX",
getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
tm_dims_t dims = {4, 2, 1, 2, 8, 16};
- if (!mkdtemp(dir) ||
- tm_write_qwen3_model_dims(dir, "F32", &dims, 15) != 0) {
+ if (!mkdtemp(dir) || tm_write_qwen3_model_dims(dir, "F32", &dims, 15) != 0) {
fprintf(stderr, "fixture creation failed\n");
return 2;
}
@@ -39,8 +37,8 @@ int main(void)
return 1;
}
if (cfg->attention_mode != EMBED_ATTENTION_CAUSAL ||
- cfg->pooling_mode != EMBED_POOL_LAST_TOKEN ||
- !cfg->normalize_embeddings || !cfg->append_terminal_token) {
+ cfg->pooling_mode != EMBED_POOL_LAST_TOKEN || !cfg->normalize_embeddings ||
+ !cfg->append_terminal_token) {
fprintf(stderr, "Qwen3 config semantics were not detected\n");
return 1;
}
@@ -58,15 +56,15 @@ int main(void)
float expected[4];
memcpy(expected, states + 3 * 4, sizeof(expected));
- if (embed_l2_normalize(expected, 4) != 0 ||
- max_abs_diff(embedding, expected, 4) > 1e-5f ||
+ if (embed_l2_normalize(expected, 4) != 0 || max_abs_diff(embedding, expected, 4) > 1e-5f ||
max_abs_diff(pooled, expected, 4) > 1e-5f) {
fprintf(stderr, "last-token pooling mismatch\n");
return 1;
}
float norm_sq = 0.0f;
- for (int d = 0; d < 4; d++) norm_sq += embedding[d] * embedding[d];
+ for (int d = 0; d < 4; d++)
+ norm_sq += embedding[d] * embedding[d];
if (fabsf(norm_sq - 1.0f) > 1e-5f) {
fprintf(stderr, "Qwen3 embedding is not normalized: %.9g\n", norm_sq);
return 1;
@@ -78,8 +76,7 @@ int main(void)
float single[4];
if (embed_model_encode_batch(model, ws, inputs, 2, batch) != 0 ||
embed_model_encode_into(model, ws, short_ids, 2, single) != 0 ||
- max_abs_diff(batch, embedding, 4) > 1e-5f ||
- max_abs_diff(batch + 4, single, 4) > 1e-5f) {
+ max_abs_diff(batch, embedding, 4) > 1e-5f || max_abs_diff(batch + 4, single, 4) > 1e-5f) {
fprintf(stderr, "Qwen3 packed batch parity failed\n");
return 1;
}
diff --git a/tests/test_safetensors.c b/tests/test_safetensors.c
index d527951..36ec8c5 100644
--- a/tests/test_safetensors.c
+++ b/tests/test_safetensors.c
@@ -15,29 +15,31 @@ static int failures = 0;
static char tmproot[1024];
/* Write u64-LE header length + JSON header + payload (safetensors layout). */
-static void write_st(const char *path, const char *header,
- const void *payload, size_t payload_len)
-{
+static void
+write_st(const char *path, const char *header, const void *payload, size_t payload_len) {
FILE *f = fopen(path, "wb");
- if (!f) { perror(path); exit(2); }
+ if (!f) {
+ perror(path);
+ exit(2);
+ }
uint64_t n = strlen(header);
unsigned char len8[8];
- for (int i = 0; i < 8; i++) len8[i] = (unsigned char)(n >> (8 * i));
+ for (int i = 0; i < 8; i++)
+ len8[i] = (unsigned char)(n >> (8 * i));
fwrite(len8, 1, 8, f);
fwrite(header, 1, n, f);
- if (payload_len) fwrite(payload, 1, payload_len, f);
+ if (payload_len)
+ fwrite(payload, 1, payload_len, f);
fclose(f);
}
-static const char *path_in(const char *rel)
-{
+static const char *path_in(const char *rel) {
static char buf[2048];
snprintf(buf, sizeof(buf), "%s/%s", tmproot, rel);
return buf;
}
-static void check(const char *mode, const char *path, int want_ok)
-{
+static void check(const char *mode, const char *path, int want_ok) {
int got_ok;
if (strcmp(mode, "file") == 0) {
safetensors_file_t *sf = safetensors_open(path);
@@ -49,8 +51,8 @@ static void check(const char *mode, const char *path, int want_ok)
multi_safetensors_close(ms);
}
if (got_ok != want_ok) {
- fprintf(stderr, "%s %s: got %s want %s\n", mode, path,
- got_ok ? "ok" : "fail", want_ok ? "ok" : "fail");
+ fprintf(stderr, "%s %s: got %s want %s\n", mode, path, got_ok ? "ok" : "fail",
+ want_ok ? "ok" : "fail");
failures++;
}
}
@@ -62,11 +64,13 @@ static const char ONE_TENSOR_B[] =
static const char ONE_TENSOR_X[] =
"{\"x\":{\"dtype\":\"F32\",\"shape\":[1],\"data_offsets\":[0,4]}}";
-int main(void)
-{
+int main(void) {
snprintf(tmproot, sizeof(tmproot), "%s/embed-st-test-XXXXXX",
getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
- if (!mkdtemp(tmproot)) { perror("mkdtemp"); return 2; }
+ if (!mkdtemp(tmproot)) {
+ perror("mkdtemp");
+ return 2;
+ }
static const char zeros[4 * 1025] = {0};
@@ -77,14 +81,15 @@ int main(void)
{
size_t cap = 1025 * 64 + 16;
char *hdr = malloc(cap);
- if (!hdr) return 2;
+ if (!hdr)
+ return 2;
size_t off = 0;
off += (size_t)snprintf(hdr + off, cap - off, "{");
for (int i = 0; i < 1025; i++) {
off += (size_t)snprintf(hdr + off, cap - off,
- "%s\"t%d\":{\"dtype\":\"F32\",\"shape\":[1],"
- "\"data_offsets\":[%d,%d]}",
- i ? "," : "", i, i * 4, i * 4 + 4);
+ "%s\"t%d\":{\"dtype\":\"F32\",\"shape\":[1],"
+ "\"data_offsets\":[%d,%d]}",
+ i ? "," : "", i, i * 4, i * 4 + 4);
}
snprintf(hdr + off, cap - off, "}");
write_st(path_in("too_many.safetensors"), hdr, zeros, sizeof(zeros));
@@ -94,27 +99,26 @@ int main(void)
write_st(path_in("too_many_dims.safetensors"),
"{\"x\":{\"dtype\":\"F32\",\"shape\":[1,1,1,1,1,1,1,1,1],"
- "\"data_offsets\":[0,4]}}", zeros, 4);
+ "\"data_offsets\":[0,4]}}",
+ zeros, 4);
check("file", path_in("too_many_dims.safetensors"), 0);
write_st(path_in("bad_offsets.safetensors"),
"{\"x\":{\"dtype\":\"F32\",\"shape\":[1],"
- "\"data_offsets\":[4,0]}}", zeros, 4);
+ "\"data_offsets\":[4,0]}}",
+ zeros, 4);
check("file", path_in("bad_offsets.safetensors"), 0);
mkdir(path_in("valid_multi"), 0755);
- write_st(path_in("valid_multi/model-00001-of-00002.safetensors"),
- ONE_TENSOR_A, zeros, 4);
- write_st(path_in("valid_multi/model-00002-of-00002.safetensors"),
- ONE_TENSOR_B, zeros, 4);
+ write_st(path_in("valid_multi/model-00001-of-00002.safetensors"), ONE_TENSOR_A, zeros, 4);
+ write_st(path_in("valid_multi/model-00002-of-00002.safetensors"), ONE_TENSOR_B, zeros, 4);
check("multi", path_in("valid_multi"), 1);
/* Payload accounting across shards: two 4-byte F32 tensors. */
{
multi_safetensors_t *ms = multi_safetensors_open(path_in("valid_multi"));
size_t nbytes = 0;
- if (!ms || multi_safetensors_data_nbytes(ms, &nbytes) != 0 ||
- nbytes != 8) {
+ if (!ms || multi_safetensors_data_nbytes(ms, &nbytes) != 0 || nbytes != 8) {
fprintf(stderr, "multi data_nbytes: got %zu want 8\n", nbytes);
failures++;
}
@@ -122,18 +126,15 @@ int main(void)
}
mkdir(path_in("missing"), 0755);
- write_st(path_in("missing/model-00001-of-00002.safetensors"),
- ONE_TENSOR_X, zeros, 4);
+ write_st(path_in("missing/model-00001-of-00002.safetensors"), ONE_TENSOR_X, zeros, 4);
check("multi", path_in("missing"), 0);
mkdir(path_in("too_many_shards"), 0755);
- write_st(path_in("too_many_shards/model-00001-of-00009.safetensors"),
- ONE_TENSOR_X, zeros, 4);
+ write_st(path_in("too_many_shards/model-00001-of-00009.safetensors"), ONE_TENSOR_X, zeros, 4);
check("multi", path_in("too_many_shards"), 0);
mkdir(path_in("bad_name"), 0755);
- write_st(path_in("bad_name/model-foo.safetensors"),
- ONE_TENSOR_X, zeros, 4);
+ write_st(path_in("bad_name/model-foo.safetensors"), ONE_TENSOR_X, zeros, 4);
check("multi", path_in("bad_name"), 0);
if (failures) {
diff --git a/tests/test_server.c b/tests/test_server.c
index a55462c..d799107 100644
--- a/tests/test_server.c
+++ b/tests/test_server.c
@@ -16,30 +16,35 @@
static int test_failures = 0;
-#define TEST_ASSERT(cond) do { \
- if (!(cond)) { \
- fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
- test_failures++; \
- } \
-} while (0)
+#define TEST_ASSERT(cond) \
+ do { \
+ if (!(cond)) { \
+ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
+ test_failures++; \
+ } \
+ } while (0)
-static void test_base64_rfc4648(void)
-{
+static void test_base64_rfc4648(void) {
/* RFC 4648 test vectors. */
- static const struct { const char *in, *out; } cases[] = {
- {"", ""}, {"f", "Zg=="}, {"fo", "Zm8="}, {"foo", "Zm9v"},
- {"foob", "Zm9vYg=="}, {"fooba", "Zm9vYmE="}, {"foobar", "Zm9vYmFy"},
+ static const struct {
+ const char *in, *out;
+ } cases[] = {
+ {"", ""},
+ {"f", "Zg=="},
+ {"fo", "Zm8="},
+ {"foo", "Zm9v"},
+ {"foob", "Zm9vYg=="},
+ {"fooba", "Zm9vYmE="},
+ {"foobar", "Zm9vYmFy"},
};
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
- char *got = base64_encode((const unsigned char *)cases[i].in,
- strlen(cases[i].in));
+ char *got = base64_encode((const unsigned char *)cases[i].in, strlen(cases[i].in));
TEST_ASSERT(strcmp(got, cases[i].out) == 0);
free(got);
}
}
-static void test_quantize_int8_tanh(void)
-{
+static void test_quantize_int8_tanh(void) {
TEST_ASSERT(quantize_int8_tanh(0.0f) == 0);
/* tanh saturates to +-1, scaled by 127. */
TEST_ASSERT(quantize_int8_tanh(100.0f) == 127);
@@ -50,8 +55,7 @@ static void test_quantize_int8_tanh(void)
TEST_ASSERT(quantize_int8_tanh(0.5f) == 59);
}
-static void test_encode_embedding_int8(void)
-{
+static void test_encode_embedding_int8(void) {
float emb[4] = {0.0f, 0.5f, -0.5f, 100.0f};
signed char expect[4] = {0, 59, -59, 127};
char *got = encode_embedding(emb, 4, "base64_int8");
@@ -61,8 +65,7 @@ static void test_encode_embedding_int8(void)
free(want);
}
-static void test_encode_embedding_binary(void)
-{
+static void test_encode_embedding_binary(void) {
/* Sign bits pack LSB-first: bit i of byte i/8 is 1 when emb[i] >= 0. */
float emb[8] = {1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f};
unsigned char expect = 0x85; /* bits 0, 2, 7 set */
@@ -82,8 +85,7 @@ static void test_encode_embedding_binary(void)
free(want);
}
-static void test_encode_embedding_base64_float32(void)
-{
+static void test_encode_embedding_base64_float32(void) {
/* OpenAI/DashScope "base64" is base64 of the raw little-endian float32
* vector - lossless, unlike the int8 formats. */
float emb[4] = {0.0f, 0.5f, -0.5f, 100.0f};
@@ -98,8 +100,7 @@ static void test_encode_embedding_base64_float32(void)
free(want);
}
-static void test_append_embedding_value_float(void)
-{
+static void test_append_embedding_value_float(void) {
float emb[1] = {0.5f};
/* OpenAI/DashScope (Qwen3): the true float32 value. */
@@ -115,16 +116,14 @@ static void test_append_embedding_value_float(void)
sbuf_free(&b2);
}
-static void test_encoding_from_root_family(void)
-{
+static void test_encoding_from_root_family(void) {
cJSON *detail = cJSON_CreateArray();
/* Default encoding differs by family when the field is absent. */
cJSON *empty = cJSON_CreateObject();
- TEST_ASSERT(strcmp(encoding_from_root(empty, detail, EMBED_API_OPENAI),
- "float") == 0);
- TEST_ASSERT(strcmp(encoding_from_root(empty, detail, EMBED_API_PERPLEXITY),
- "base64_int8") == 0);
+ TEST_ASSERT(strcmp(encoding_from_root(empty, detail, EMBED_API_OPENAI), "float") == 0);
+ TEST_ASSERT(strcmp(encoding_from_root(empty, detail, EMBED_API_PERPLEXITY), "base64_int8") ==
+ 0);
cJSON_Delete(empty);
TEST_ASSERT(cJSON_GetArraySize(detail) == 0);
@@ -144,8 +143,7 @@ static void test_encoding_from_root_family(void)
cJSON_Delete(detail);
}
-static void test_collect_job_batch_deadline(void)
-{
+static void test_collect_job_batch_deadline(void) {
http_server s;
memset(&s, 0, sizeof(s));
s.batch_size = 32;
@@ -158,10 +156,8 @@ static void test_collect_job_batch_deadline(void)
job before_deadline = {.srv = &s, .created_ns = first_ns + 500000u};
job after_deadline = {.srv = &s, .created_ns = first_ns + 2000000u};
snprintf(first.path, sizeof(first.path), "%s", "/v1/embeddings");
- snprintf(before_deadline.path, sizeof(before_deadline.path), "%s",
- "/v1/embeddings");
- snprintf(after_deadline.path, sizeof(after_deadline.path), "%s",
- "/v1/embeddings");
+ snprintf(before_deadline.path, sizeof(before_deadline.path), "%s", "/v1/embeddings");
+ snprintf(after_deadline.path, sizeof(after_deadline.path), "%s", "/v1/embeddings");
enqueue_job(&first);
enqueue_job(&before_deadline);
enqueue_job(&after_deadline);
@@ -179,8 +175,7 @@ static void test_collect_job_batch_deadline(void)
pthread_mutex_destroy(&s.mu);
}
-static void test_collect_job_batch_zero_wait(void)
-{
+static void test_collect_job_batch_zero_wait(void) {
http_server s;
memset(&s, 0, sizeof(s));
s.batch_size = 32;
@@ -211,10 +206,10 @@ static void test_collect_job_batch_zero_wait(void)
#define TEST_API_KEY "tt-test-key"
-static int find_free_port(void)
-{
+static int find_free_port(void) {
int fd = socket(AF_INET, SOCK_STREAM, 0);
- if (fd < 0) return -1;
+ if (fd < 0)
+ return -1;
struct sockaddr_in a;
memset(&a, 0, sizeof(a));
a.sin_family = AF_INET;
@@ -230,10 +225,10 @@ static int find_free_port(void)
return port;
}
-static int tcp_connect(int port)
-{
+static int tcp_connect(int port) {
int fd = socket(AF_INET, SOCK_STREAM, 0);
- if (fd < 0) return -1;
+ if (fd < 0)
+ return -1;
struct sockaddr_in a;
memset(&a, 0, sizeof(a));
a.sin_family = AF_INET;
@@ -246,13 +241,13 @@ static int tcp_connect(int port)
return fd;
}
-static int send_all(int fd, const char *buf, size_t n)
-{
+static int send_all(int fd, const char *buf, size_t n) {
size_t off = 0;
while (off < n) {
ssize_t w = write(fd, buf + off, n - off);
if (w < 0) {
- if (errno == EINTR) continue;
+ if (errno == EINTR)
+ continue;
return -1;
}
off += (size_t)w;
@@ -262,10 +257,14 @@ static int send_all(int fd, const char *buf, size_t n)
/* Send one request; raw == NULL builds a normal request, otherwise `raw`
* bytes go on the wire verbatim (for malformed-HTTP checks). */
-static int http_send(int fd, const char *method, const char *path,
- const char *auth, const char *body, const char *raw)
-{
- if (raw) return send_all(fd, raw, strlen(raw));
+static int http_send(int fd,
+ const char *method,
+ const char *path,
+ const char *auth,
+ const char *body,
+ const char *raw) {
+ if (raw)
+ return send_all(fd, raw, strlen(raw));
char head[512];
size_t blen = body ? strlen(body) : 0;
int n = snprintf(head, sizeof(head),
@@ -275,55 +274,71 @@ static int http_send(int fd, const char *method, const char *path,
"Content-Type: application/json\r\n"
"Content-Length: %zu\r\n"
"\r\n",
- method, path,
- auth ? "Authorization: Bearer " : "",
- auth ? auth : "", auth ? "\r\n" : "",
- blen);
- if (n < 0 || (size_t)n >= sizeof(head)) return -1;
- if (send_all(fd, head, (size_t)n) != 0) return -1;
+ method, path, auth ? "Authorization: Bearer " : "", auth ? auth : "",
+ auth ? "\r\n" : "", blen);
+ if (n < 0 || (size_t)n >= sizeof(head))
+ return -1;
+ if (send_all(fd, head, (size_t)n) != 0)
+ return -1;
return blen ? send_all(fd, body, blen) : 0;
}
/* Read one response: returns the status code (or -1) and the body, which
* the caller frees. Reads exactly Content-Length body bytes so it works
* with keep-alive connections. */
-static int http_recv(int fd, char **out_body)
-{
+static int http_recv(int fd, char **out_body) {
*out_body = NULL;
size_t cap = 8192, len = 0;
char *buf = malloc(cap);
- if (!buf) return -1;
+ if (!buf)
+ return -1;
char *hdr_end = NULL;
while (!hdr_end) {
if (len + 1 >= cap) {
cap *= 2;
char *nb = realloc(buf, cap);
- if (!nb) { free(buf); return -1; }
+ if (!nb) {
+ free(buf);
+ return -1;
+ }
buf = nb;
}
ssize_t r = read(fd, buf + len, cap - len - 1);
- if (r <= 0) { free(buf); return -1; }
+ if (r <= 0) {
+ free(buf);
+ return -1;
+ }
len += (size_t)r;
buf[len] = '\0';
hdr_end = strstr(buf, "\r\n\r\n");
}
int status = -1;
- if (sscanf(buf, "HTTP/1.1 %d", &status) != 1) { free(buf); return -1; }
+ if (sscanf(buf, "HTTP/1.1 %d", &status) != 1) {
+ free(buf);
+ return -1;
+ }
size_t content_length = 0;
const char *cl = strstr(buf, "Content-Length:");
- if (cl && cl < hdr_end) content_length = strtoul(cl + 15, NULL, 10);
+ if (cl && cl < hdr_end)
+ content_length = strtoul(cl + 15, NULL, 10);
size_t body_off = (size_t)(hdr_end - buf) + 4;
size_t need = body_off + content_length;
if (need + 1 > cap) {
char *nb = realloc(buf, need + 1);
- if (!nb) { free(buf); return -1; }
+ if (!nb) {
+ free(buf);
+ return -1;
+ }
buf = nb;
}
while (len < need) {
ssize_t r = read(fd, buf + len, need - len);
- if (r <= 0) { free(buf); return -1; }
+ if (r <= 0) {
+ free(buf);
+ return -1;
+ }
len += (size_t)r;
}
buf[need] = '\0';
@@ -334,19 +349,25 @@ static int http_recv(int fd, char **out_body)
/* One-shot request helper; returns status, body via out_body (caller frees,
* may be NULL when only the status matters). */
-static int http_req(int port, const char *method, const char *path,
- const char *auth, const char *body, const char *raw,
- char **out_body)
-{
+static int http_req(int port,
+ const char *method,
+ const char *path,
+ const char *auth,
+ const char *body,
+ const char *raw,
+ char **out_body) {
int fd = tcp_connect(port);
- if (fd < 0) return -1;
+ if (fd < 0)
+ return -1;
char *resp = NULL;
int status = -1;
if (http_send(fd, method, path, auth, body, raw) == 0)
status = http_recv(fd, &resp);
close(fd);
- if (out_body) *out_body = resp;
- else free(resp);
+ if (out_body)
+ *out_body = resp;
+ else
+ free(resp);
return status;
}
@@ -356,18 +377,17 @@ typedef struct {
int rc;
} srv_ctx;
-static void *srv_thread_main(void *arg)
-{
+static void *srv_thread_main(void *arg) {
srv_ctx *s = (srv_ctx *)arg;
s->rc = embed_run_server(&s->cfg);
return NULL;
}
-static cJSON *parse_embeddings_body(const char *body, int want_items)
-{
+static cJSON *parse_embeddings_body(const char *body, int want_items) {
cJSON *root = cJSON_Parse(body);
TEST_ASSERT(root != NULL);
- if (!root) return NULL;
+ if (!root)
+ return NULL;
cJSON *data = cJSON_GetObjectItemCaseSensitive(root, "data");
TEST_ASSERT(cJSON_IsArray(data));
TEST_ASSERT(cJSON_GetArraySize(data) == want_items);
@@ -378,71 +398,63 @@ static cJSON *parse_embeddings_body(const char *body, int want_items)
return root;
}
-static void test_http_embeddings(int port)
-{
+static void test_http_embeddings(int port) {
char *body = NULL;
/* Routing. */
- TEST_ASSERT(http_req(port, "OPTIONS", "/v1/embeddings", NULL, NULL, NULL,
- NULL) == 204);
- TEST_ASSERT(http_req(port, "GET", "/v1/embeddings", TEST_API_KEY, NULL,
- NULL, NULL) == 404);
- TEST_ASSERT(http_req(port, "POST", "/nope", TEST_API_KEY, "{}", NULL,
- NULL) == 404);
+ TEST_ASSERT(http_req(port, "OPTIONS", "/v1/embeddings", NULL, NULL, NULL, NULL) == 204);
+ TEST_ASSERT(http_req(port, "GET", "/v1/embeddings", TEST_API_KEY, NULL, NULL, NULL) == 404);
+ TEST_ASSERT(http_req(port, "POST", "/nope", TEST_API_KEY, "{}", NULL, NULL) == 404);
/* Auth: required, and the exact key. */
- const char *ok_req =
- "{\"model\":\"pplx-embed-v1-0.6b\",\"input\":\"hello world\","
- "\"encoding_format\":\"float\"}";
- TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", NULL, ok_req, NULL,
- NULL) == 401);
- TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", "wrong-key", ok_req,
- NULL, NULL) == 401);
+ const char *ok_req = "{\"model\":\"pplx-embed-v1-0.6b\",\"input\":\"hello world\","
+ "\"encoding_format\":\"float\"}";
+ TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", NULL, ok_req, NULL, NULL) == 401);
+ TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", "wrong-key", ok_req, NULL, NULL) == 401);
/* Malformed HTTP (400) and malformed JSON (422 detail). */
- TEST_ASSERT(http_req(port, NULL, NULL, NULL, NULL,
- "GARBAGE\r\n\r\n", NULL) == 400);
- TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY,
- "{nope", NULL, NULL) == 422);
+ TEST_ASSERT(http_req(port, NULL, NULL, NULL, NULL, "GARBAGE\r\n\r\n", NULL) == 400);
+ TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY, "{nope", NULL, NULL) == 422);
/* Validation: missing/unknown model, wrong endpoint family, valid but
* unloaded id, bad encoding enum, dimensions out of range; the
* API answers validation errors with 422 detail arrays. */
- TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY,
- "{\"input\":\"hi\"}", NULL, NULL) == 422);
- TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY,
- "{\"model\":\"foo\",\"input\":\"hi\"}", NULL,
+ TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY, "{\"input\":\"hi\"}", NULL,
NULL) == 422);
TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY,
+ "{\"model\":\"foo\",\"input\":\"hi\"}", NULL, NULL) == 422);
+ TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY,
"{\"model\":\"pplx-embed-context-v1-0.6b\","
- "\"input\":\"hi\"}", NULL, NULL) == 422);
+ "\"input\":\"hi\"}",
+ NULL, NULL) == 422);
TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY,
- "{\"model\":\"pplx-embed-v1-4b\",\"input\":\"hi\"}",
- NULL, NULL) == 503);
+ "{\"model\":\"pplx-embed-v1-4b\",\"input\":\"hi\"}", NULL, NULL) == 503);
TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY,
"{\"model\":\"pplx-embed-v1-0.6b\",\"input\":\"hi\","
- "\"encoding_format\":\"yaml\"}", NULL, NULL) == 422);
+ "\"encoding_format\":\"yaml\"}",
+ NULL, NULL) == 422);
TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY,
"{\"model\":\"pplx-embed-v1-0.6b\",\"input\":\"hi\","
- "\"dimensions\":64}", NULL, NULL) == 422);
+ "\"dimensions\":64}",
+ NULL, NULL) == 422);
TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY,
"{\"model\":\"pplx-embed-v1-0.6b\","
- "\"input\":[]}", NULL, NULL) == 422);
+ "\"input\":[]}",
+ NULL, NULL) == 422);
/* Contextual endpoint: standard id is the wrong enum, the 4B contextual
* id is valid but not loaded. */
- TEST_ASSERT(http_req(port, "POST", "/v1/contextualizedembeddings",
- TEST_API_KEY,
+ TEST_ASSERT(http_req(port, "POST", "/v1/contextualizedembeddings", TEST_API_KEY,
"{\"model\":\"pplx-embed-v1-0.6b\","
- "\"input\":[[\"a\"]]}", NULL, NULL) == 422);
- TEST_ASSERT(http_req(port, "POST", "/v1/contextualizedembeddings",
- TEST_API_KEY,
+ "\"input\":[[\"a\"]]}",
+ NULL, NULL) == 422);
+ TEST_ASSERT(http_req(port, "POST", "/v1/contextualizedembeddings", TEST_API_KEY,
"{\"model\":\"pplx-embed-context-v1-4b\","
- "\"input\":[[\"a\"]]}", NULL, NULL) == 503);
+ "\"input\":[[\"a\"]]}",
+ NULL, NULL) == 503);
/* Happy path, float encoding, single string input. */
- TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY, ok_req,
- NULL, &body) == 200);
+ TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY, ok_req, NULL, &body) == 200);
cJSON *root = body ? parse_embeddings_body(body, 1) : NULL;
if (root) {
cJSON *data = cJSON_GetObjectItemCaseSensitive(root, "data");
@@ -456,12 +468,12 @@ static void test_http_embeddings(int port)
cJSON *v;
cJSON_ArrayForEach(v, emb) {
TEST_ASSERT(cJSON_IsNumber(v));
- if (v->valuedouble != 0.0) nonzero = 1;
+ if (v->valuedouble != 0.0)
+ nonzero = 1;
}
TEST_ASSERT(nonzero);
cJSON *usage = cJSON_GetObjectItemCaseSensitive(root, "usage");
- cJSON *total = usage
- ? cJSON_GetObjectItemCaseSensitive(usage, "total_tokens") : NULL;
+ cJSON *total = usage ? cJSON_GetObjectItemCaseSensitive(usage, "total_tokens") : NULL;
TEST_ASSERT(total && cJSON_IsNumber(total) && total->valueint >= 1);
cJSON_Delete(root);
}
@@ -478,19 +490,15 @@ static void test_http_embeddings(int port)
root = body ? parse_embeddings_body(body, 3) : NULL;
if (root) {
cJSON *data = cJSON_GetObjectItemCaseSensitive(root, "data");
- cJSON *e0 = cJSON_GetObjectItemCaseSensitive(
- cJSON_GetArrayItem(data, 0), "embedding");
- cJSON *e1 = cJSON_GetObjectItemCaseSensitive(
- cJSON_GetArrayItem(data, 1), "embedding");
+ cJSON *e0 = cJSON_GetObjectItemCaseSensitive(cJSON_GetArrayItem(data, 0), "embedding");
+ cJSON *e1 = cJSON_GetObjectItemCaseSensitive(cJSON_GetArrayItem(data, 1), "embedding");
for (int i = 0; i < 3; i++) {
- cJSON *idx = cJSON_GetObjectItemCaseSensitive(
- cJSON_GetArrayItem(data, i), "index");
+ cJSON *idx = cJSON_GetObjectItemCaseSensitive(cJSON_GetArrayItem(data, i), "index");
TEST_ASSERT(cJSON_IsNumber(idx) && idx->valueint == i);
}
int differ = 0;
for (int i = 0; i < 1024 && !differ; i++) {
- if (cJSON_GetArrayItem(e0, i)->valuedouble !=
- cJSON_GetArrayItem(e1, i)->valuedouble)
+ if (cJSON_GetArrayItem(e0, i)->valuedouble != cJSON_GetArrayItem(e1, i)->valuedouble)
differ = 1;
}
TEST_ASSERT(differ);
@@ -502,12 +510,12 @@ static void test_http_embeddings(int port)
/* Default encoding is base64_int8: a string of 1024 quantized bytes. */
TEST_ASSERT(http_req(port, "POST", "/v1/embeddings", TEST_API_KEY,
"{\"model\":\"pplx-embed-v1-0.6b\","
- "\"input\":\"hello\"}", NULL, &body) == 200);
+ "\"input\":\"hello\"}",
+ NULL, &body) == 200);
root = body ? parse_embeddings_body(body, 1) : NULL;
if (root) {
cJSON *emb = cJSON_GetObjectItemCaseSensitive(
- cJSON_GetArrayItem(cJSON_GetObjectItemCaseSensitive(root, "data"),
- 0), "embedding");
+ cJSON_GetArrayItem(cJSON_GetObjectItemCaseSensitive(root, "data"), 0), "embedding");
TEST_ASSERT(cJSON_IsString(emb));
TEST_ASSERT(strlen(emb->valuestring) == ((1024 + 2) / 3) * 4);
cJSON_Delete(root);
@@ -524,8 +532,7 @@ static void test_http_embeddings(int port)
root = body ? parse_embeddings_body(body, 1) : NULL;
if (root) {
cJSON *emb = cJSON_GetObjectItemCaseSensitive(
- cJSON_GetArrayItem(cJSON_GetObjectItemCaseSensitive(root, "data"),
- 0), "embedding");
+ cJSON_GetArrayItem(cJSON_GetObjectItemCaseSensitive(root, "data"), 0), "embedding");
TEST_ASSERT(cJSON_IsArray(emb) && cJSON_GetArraySize(emb) == 128);
cJSON_Delete(root);
}
@@ -548,28 +555,24 @@ static void test_http_embeddings(int port)
NULL, &body) == 200);
cJSON *qwen_mrl = body ? parse_embeddings_body(body, 1) : NULL;
if (qwen_full && qwen_mrl) {
- cJSON *full_data =
- cJSON_GetObjectItemCaseSensitive(qwen_full, "data");
- cJSON *mrl_data =
- cJSON_GetObjectItemCaseSensitive(qwen_mrl, "data");
- cJSON *full_emb = cJSON_GetObjectItemCaseSensitive(
- cJSON_GetArrayItem(full_data, 0), "embedding");
- cJSON *mrl_emb = cJSON_GetObjectItemCaseSensitive(
- cJSON_GetArrayItem(mrl_data, 0), "embedding");
- TEST_ASSERT(cJSON_IsArray(full_emb) &&
- cJSON_GetArraySize(full_emb) == 1024);
- TEST_ASSERT(cJSON_IsArray(mrl_emb) &&
- cJSON_GetArraySize(mrl_emb) == 32);
+ cJSON *full_data = cJSON_GetObjectItemCaseSensitive(qwen_full, "data");
+ cJSON *mrl_data = cJSON_GetObjectItemCaseSensitive(qwen_mrl, "data");
+ cJSON *full_emb =
+ cJSON_GetObjectItemCaseSensitive(cJSON_GetArrayItem(full_data, 0), "embedding");
+ cJSON *mrl_emb =
+ cJSON_GetObjectItemCaseSensitive(cJSON_GetArrayItem(mrl_data, 0), "embedding");
+ TEST_ASSERT(cJSON_IsArray(full_emb) && cJSON_GetArraySize(full_emb) == 1024);
+ TEST_ASSERT(cJSON_IsArray(mrl_emb) && cJSON_GetArraySize(mrl_emb) == 32);
double prefix_diff = 0.0;
for (int i = 0; i < 32; i++) {
double d = fabs(cJSON_GetArrayItem(full_emb, i)->valuedouble -
cJSON_GetArrayItem(mrl_emb, i)->valuedouble);
- if (d > prefix_diff) prefix_diff = d;
+ if (d > prefix_diff)
+ prefix_diff = d;
}
TEST_ASSERT(prefix_diff > 0.0);
cJSON *usage = cJSON_GetObjectItemCaseSensitive(qwen_mrl, "usage");
- cJSON *total = usage
- ? cJSON_GetObjectItemCaseSensitive(usage, "total_tokens") : NULL;
+ cJSON *total = usage ? cJSON_GetObjectItemCaseSensitive(usage, "total_tokens") : NULL;
TEST_ASSERT(total && total->valueint == 2);
}
cJSON_Delete(qwen_full);
@@ -585,11 +588,12 @@ static void test_http_embeddings(int port)
fds[i] = tcp_connect(port);
TEST_ASSERT(fds[i] >= 0);
if (fds[i] >= 0)
- TEST_ASSERT(http_send(fds[i], "POST", "/v1/embeddings",
- TEST_API_KEY, ok_req, NULL) == 0);
+ TEST_ASSERT(http_send(fds[i], "POST", "/v1/embeddings", TEST_API_KEY, ok_req, NULL) ==
+ 0);
}
for (int i = 0; i < N_CONC; i++) {
- if (fds[i] < 0) continue;
+ if (fds[i] < 0)
+ continue;
char *resp = NULL;
TEST_ASSERT(http_recv(fds[i], &resp) == 200);
free(resp);
@@ -598,8 +602,7 @@ static void test_http_embeddings(int port)
}
/* data[di].data[ci].embedding from a contextual response. */
-static cJSON *ctx_chunk_emb(cJSON *root, int di, int ci)
-{
+static cJSON *ctx_chunk_emb(cJSON *root, int di, int ci) {
cJSON *docs = cJSON_GetObjectItemCaseSensitive(root, "data");
cJSON *doc = cJSON_GetArrayItem(docs, di);
cJSON *chunks = cJSON_GetObjectItemCaseSensitive(doc, "data");
@@ -607,40 +610,40 @@ static cJSON *ctx_chunk_emb(cJSON *root, int di, int ci)
return cJSON_GetObjectItemCaseSensitive(item, "embedding");
}
-static double emb_max_absdiff(cJSON *a, cJSON *b)
-{
- if (!cJSON_IsArray(a) || !cJSON_IsArray(b)) return 1e9;
+static double emb_max_absdiff(cJSON *a, cJSON *b) {
+ if (!cJSON_IsArray(a) || !cJSON_IsArray(b))
+ return 1e9;
int n = cJSON_GetArraySize(a);
- if (n != cJSON_GetArraySize(b)) return 1e9;
+ if (n != cJSON_GetArraySize(b))
+ return 1e9;
double m = 0.0;
for (int i = 0; i < n; i++) {
- double d = fabs(cJSON_GetArrayItem(a, i)->valuedouble -
- cJSON_GetArrayItem(b, i)->valuedouble);
- if (d > m) m = d;
+ double d =
+ fabs(cJSON_GetArrayItem(a, i)->valuedouble - cJSON_GetArrayItem(b, i)->valuedouble);
+ if (d > m)
+ m = d;
}
return m;
}
-static void test_http_contextual(int port)
-{
+static void test_http_contextual(int port) {
char *body = NULL;
/* Input must be an array of chunk arrays; chunks must be non-empty. */
- TEST_ASSERT(http_req(port, "POST", "/v1/contextualizedembeddings",
- TEST_API_KEY,
+ TEST_ASSERT(http_req(port, "POST", "/v1/contextualizedembeddings", TEST_API_KEY,
"{\"model\":\"pplx-embed-context-v1-0.6b\","
- "\"input\":\"hi\"}", NULL, NULL) == 422);
- TEST_ASSERT(http_req(port, "POST", "/v1/contextualizedembeddings",
- TEST_API_KEY,
+ "\"input\":\"hi\"}",
+ NULL, NULL) == 422);
+ TEST_ASSERT(http_req(port, "POST", "/v1/contextualizedembeddings", TEST_API_KEY,
"{\"model\":\"pplx-embed-context-v1-0.6b\","
- "\"input\":[[\"\"]]}", NULL, NULL) == 422);
+ "\"input\":[[\"\"]]}",
+ NULL, NULL) == 422);
/* Happy path: chunks concatenate per document with one separator token
* between them, one embedding per chunk in document order. Fixture
* token counts: hello=1, world=3, held=2, one separator in doc 0, so
* usage must report 7. */
- TEST_ASSERT(http_req(port, "POST", "/v1/contextualizedembeddings",
- TEST_API_KEY,
+ TEST_ASSERT(http_req(port, "POST", "/v1/contextualizedembeddings", TEST_API_KEY,
"{\"model\":\"pplx-embed-context-v1-0.6b\","
"\"input\":[[\"hello\",\"world\"],[\"held\"]],"
"\"encoding_format\":\"float\"}",
@@ -664,21 +667,18 @@ static void test_http_contextual(int port)
cJSON *cidx = cJSON_GetObjectItemCaseSensitive(item, "index");
cJSON *emb = ctx_chunk_emb(multi, di, ci);
TEST_ASSERT(cidx && cidx->valueint == ci);
- TEST_ASSERT(cJSON_IsArray(emb) &&
- cJSON_GetArraySize(emb) == 1024);
+ TEST_ASSERT(cJSON_IsArray(emb) && cJSON_GetArraySize(emb) == 1024);
}
}
cJSON *usage = cJSON_GetObjectItemCaseSensitive(multi, "usage");
- cJSON *total = usage
- ? cJSON_GetObjectItemCaseSensitive(usage, "total_tokens") : NULL;
+ cJSON *total = usage ? cJSON_GetObjectItemCaseSensitive(usage, "total_tokens") : NULL;
TEST_ASSERT(total && total->valueint == 7);
}
/* A single-chunk document has no separators and its pooling span covers
* the whole sequence, so it must match the standard embedding of the
* same text from the same weights. */
- TEST_ASSERT(http_req(port, "POST", "/v1/contextualizedembeddings",
- TEST_API_KEY,
+ TEST_ASSERT(http_req(port, "POST", "/v1/contextualizedembeddings", TEST_API_KEY,
"{\"model\":\"pplx-embed-context-v1-0.6b\","
"\"input\":[[\"hello\"]],"
"\"encoding_format\":\"float\"}",
@@ -699,8 +699,7 @@ static void test_http_contextual(int port)
if (multi && single && std) {
cJSON *std_emb = cJSON_GetObjectItemCaseSensitive(
- cJSON_GetArrayItem(cJSON_GetObjectItemCaseSensitive(std, "data"),
- 0), "embedding");
+ cJSON_GetArrayItem(cJSON_GetObjectItemCaseSensitive(std, "data"), 0), "embedding");
cJSON *alone = ctx_chunk_emb(single, 0, 0);
cJSON *in_doc = ctx_chunk_emb(multi, 0, 0);
TEST_ASSERT(emb_max_absdiff(std_emb, alone) < 1e-4);
@@ -713,8 +712,7 @@ static void test_http_contextual(int port)
cJSON_Delete(std);
}
-static void test_http_rerank(int port)
-{
+static void test_http_rerank(int port) {
char *body = NULL;
TEST_ASSERT(http_req(port, "POST", "/v1/rerank", TEST_API_KEY,
@@ -735,55 +733,45 @@ static void test_http_rerank(int port)
"\"top_n\":1,\"top_k\":1}",
NULL, NULL) == 422);
- const char *request =
- "{\"model\":\"pplx-embed-v1-late-0.6b\","
- "\"query\":\"hello\",\"documents\":["
- "\"hello!\",\"world\",\"quoted \\\"document\\\"\"],"
- "\"top_n\":2,\"return_documents\":true}";
- TEST_ASSERT(http_req(port, "POST", "/v1/rerank", TEST_API_KEY, request,
- NULL, &body) == 200);
+ const char *request = "{\"model\":\"pplx-embed-v1-late-0.6b\","
+ "\"query\":\"hello\",\"documents\":["
+ "\"hello!\",\"world\",\"quoted \\\"document\\\"\"],"
+ "\"top_n\":2,\"return_documents\":true}";
+ TEST_ASSERT(http_req(port, "POST", "/v1/rerank", TEST_API_KEY, request, NULL, &body) == 200);
cJSON *root = body ? cJSON_Parse(body) : NULL;
TEST_ASSERT(root != NULL);
if (root) {
cJSON *object = cJSON_GetObjectItemCaseSensitive(root, "object");
cJSON *model = cJSON_GetObjectItemCaseSensitive(root, "model");
cJSON *results = cJSON_GetObjectItemCaseSensitive(root, "results");
- TEST_ASSERT(cJSON_IsString(object) &&
- strcmp(object->valuestring, "list") == 0);
+ TEST_ASSERT(cJSON_IsString(object) && strcmp(object->valuestring, "list") == 0);
TEST_ASSERT(cJSON_IsString(model) &&
- strcmp(model->valuestring,
- "pplx-embed-v1-late-0.6b") == 0);
- TEST_ASSERT(cJSON_IsArray(results) &&
- cJSON_GetArraySize(results) == 2);
+ strcmp(model->valuestring, "pplx-embed-v1-late-0.6b") == 0);
+ TEST_ASSERT(cJSON_IsArray(results) && cJSON_GetArraySize(results) == 2);
double previous = INFINITY;
int seen[3] = {0};
cJSON *result;
cJSON_ArrayForEach(result, results) {
- cJSON *index =
- cJSON_GetObjectItemCaseSensitive(result, "index");
- cJSON *score =
- cJSON_GetObjectItemCaseSensitive(result, "relevance_score");
- cJSON *document =
- cJSON_GetObjectItemCaseSensitive(result, "document");
- TEST_ASSERT(cJSON_IsNumber(index) &&
- index->valueint >= 0 && index->valueint < 3);
- TEST_ASSERT(cJSON_IsNumber(score) &&
- score->valuedouble <= previous);
+ cJSON *index = cJSON_GetObjectItemCaseSensitive(result, "index");
+ cJSON *score = cJSON_GetObjectItemCaseSensitive(result, "relevance_score");
+ cJSON *document = cJSON_GetObjectItemCaseSensitive(result, "document");
+ TEST_ASSERT(cJSON_IsNumber(index) && index->valueint >= 0 && index->valueint < 3);
+ TEST_ASSERT(cJSON_IsNumber(score) && score->valuedouble <= previous);
TEST_ASSERT(cJSON_IsString(document));
- if (cJSON_IsNumber(index) && index->valueint >= 0 &&
- index->valueint < 3)
+ if (cJSON_IsNumber(index) && index->valueint >= 0 && index->valueint < 3)
seen[index->valueint]++;
- if (cJSON_IsNumber(score)) previous = score->valuedouble;
+ if (cJSON_IsNumber(score))
+ previous = score->valuedouble;
}
TEST_ASSERT(seen[0] + seen[1] + seen[2] == 2);
cJSON *usage = cJSON_GetObjectItemCaseSensitive(root, "usage");
- cJSON *query_tokens = usage
- ? cJSON_GetObjectItemCaseSensitive(usage, "query_tokens") : NULL;
- cJSON *document_tokens = usage
- ? cJSON_GetObjectItemCaseSensitive(usage, "document_tokens") : NULL;
- cJSON *total_tokens = usage
- ? cJSON_GetObjectItemCaseSensitive(usage, "total_tokens") : NULL;
+ cJSON *query_tokens =
+ usage ? cJSON_GetObjectItemCaseSensitive(usage, "query_tokens") : NULL;
+ cJSON *document_tokens =
+ usage ? cJSON_GetObjectItemCaseSensitive(usage, "document_tokens") : NULL;
+ cJSON *total_tokens =
+ usage ? cJSON_GetObjectItemCaseSensitive(usage, "total_tokens") : NULL;
TEST_ASSERT(query_tokens && document_tokens && total_tokens);
if (query_tokens && document_tokens && total_tokens) {
TEST_ASSERT(cJSON_IsNumber(query_tokens));
@@ -808,18 +796,16 @@ static void test_http_rerank(int port)
TEST_ASSERT(root != NULL);
if (root) {
cJSON *results = cJSON_GetObjectItemCaseSensitive(root, "results");
- TEST_ASSERT(cJSON_IsArray(results) &&
- cJSON_GetArraySize(results) == 1);
- cJSON *document = cJSON_GetObjectItemCaseSensitive(
- cJSON_GetArrayItem(results, 0), "document");
+ TEST_ASSERT(cJSON_IsArray(results) && cJSON_GetArraySize(results) == 1);
+ cJSON *document =
+ cJSON_GetObjectItemCaseSensitive(cJSON_GetArrayItem(results, 0), "document");
TEST_ASSERT(document == NULL);
cJSON_Delete(root);
}
free(body);
}
-static void test_http_server(void)
-{
+static void test_http_server(void) {
char dir[1024], late_dir[1024], qwen_dir[1024];
snprintf(dir, sizeof(dir), "%s/embed-srv-test-XXXXXX",
getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
@@ -845,14 +831,11 @@ static void test_http_server(void)
/* The 0.6b server slot requires hidden_size 1024; everything else stays
* tiny. The model vocab covers every tokenizer fixture id. */
tm_dims_t dims = {1024, 2, 1, 64, 8, TF_VOCAB_SIZE};
- if (tf_write_vocab(dir) != 0 ||
- tm_write_model_dims(dir, "F32", &dims) != 0 ||
- tf_write_vocab(late_dir) != 0 ||
- tm_write_model_dims(late_dir, "F32", &dims) != 0 ||
+ if (tf_write_vocab(dir) != 0 || tm_write_model_dims(dir, "F32", &dims) != 0 ||
+ tf_write_vocab(late_dir) != 0 || tm_write_model_dims(late_dir, "F32", &dims) != 0 ||
tm_write_late_projection(late_dir, "F32", 128, 1024) != 0 ||
tf_write_vocab(qwen_dir) != 0 ||
- tm_write_qwen3_model_dims(qwen_dir, "F32", &dims,
- TF_EOT_ID) != 0) {
+ tm_write_qwen3_model_dims(qwen_dir, "F32", &dims, TF_EOT_ID) != 0) {
fprintf(stderr, "FAIL: fixture write\n");
test_failures++;
return;
@@ -888,9 +871,13 @@ static void test_http_server(void)
}
int up = 0;
- for (int i = 0; i < 200; i++) { /* up to ~10 s */
+ for (int i = 0; i < 200; i++) { /* up to ~10 s */
int fd = tcp_connect(port);
- if (fd >= 0) { close(fd); up = 1; break; }
+ if (fd >= 0) {
+ close(fd);
+ up = 1;
+ break;
+ }
usleep(50 * 1000);
}
TEST_ASSERT(up);
@@ -905,8 +892,7 @@ static void test_http_server(void)
TEST_ASSERT(ctx.rc == 0);
}
-int main(void)
-{
+int main(void) {
test_base64_rfc4648();
test_quantize_int8_tanh();
test_encode_embedding_int8();
diff --git a/tests/test_tokenizer.c b/tests/test_tokenizer.c
index bed5b7d..5920977 100644
--- a/tests/test_tokenizer.c
+++ b/tests/test_tokenizer.c
@@ -16,31 +16,33 @@
static int failures = 0;
int qwen_verbose = 0;
-#define TEST_ASSERT(cond) do { \
- if (!(cond)) { \
- fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
- failures++; \
- } \
-} while (0)
+#define TEST_ASSERT(cond) \
+ do { \
+ if (!(cond)) { \
+ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
+ failures++; \
+ } \
+ } while (0)
static void check_ids(const qwen_tokenizer_t *tok,
- qwen_tokenizer_workspace_t *ws, const char *text,
- const int *want, int n_want, const char *label)
-{
+ qwen_tokenizer_workspace_t *ws,
+ const char *text,
+ const int *want,
+ int n_want,
+ const char *label) {
int n = 0;
- int *ids = qwen_tokenizer_encode_with_workspace(
- (qwen_tokenizer_t *)tok, ws, text, &n);
+ int *ids = qwen_tokenizer_encode_with_workspace((qwen_tokenizer_t *)tok, ws, text, &n);
TEST_ASSERT(ids != NULL);
- if (!ids) return;
+ if (!ids)
+ return;
if (n != n_want) {
- fprintf(stderr, "FAIL %s \"%s\": got %d ids want %d\n",
- label, text, n, n_want);
+ fprintf(stderr, "FAIL %s \"%s\": got %d ids want %d\n", label, text, n, n_want);
failures++;
} else {
for (int i = 0; i < n; i++) {
if (ids[i] != want[i]) {
- fprintf(stderr, "FAIL %s \"%s\": ids[%d]=%d want %d\n",
- label, text, i, ids[i], want[i]);
+ fprintf(stderr, "FAIL %s \"%s\": ids[%d]=%d want %d\n", label, text, i, ids[i],
+ want[i]);
failures++;
}
}
@@ -51,25 +53,30 @@ static void check_ids(const qwen_tokenizer_t *tok,
/* Concatenated decode of an encoding must reproduce the (NFC-normalized)
* input - byte-level BPE is lossless. */
static void check_roundtrip(const qwen_tokenizer_t *tok,
- qwen_tokenizer_workspace_t *ws, const char *text,
- const char *want, const char *label)
-{
+ qwen_tokenizer_workspace_t *ws,
+ const char *text,
+ const char *want,
+ const char *label) {
int n = 0;
- int *ids = qwen_tokenizer_encode_with_workspace(
- (qwen_tokenizer_t *)tok, ws, text, &n);
+ int *ids = qwen_tokenizer_encode_with_workspace((qwen_tokenizer_t *)tok, ws, text, &n);
TEST_ASSERT(ids != NULL && n > 0);
- if (!ids) return;
+ if (!ids)
+ return;
size_t cap = 1;
for (int i = 0; i < n; i++)
cap += strlen(qwen_tokenizer_decode(tok, ids[i]));
char *got = (char *)malloc(cap);
- if (!got) { free(ids); TEST_ASSERT(0); return; }
+ if (!got) {
+ free(ids);
+ TEST_ASSERT(0);
+ return;
+ }
got[0] = '\0';
for (int i = 0; i < n; i++)
strcat(got, qwen_tokenizer_decode(tok, ids[i]));
if (strcmp(got, want) != 0) {
- fprintf(stderr, "FAIL %s roundtrip \"%s\": got \"%s\" want \"%s\"\n",
- label, text, got, want);
+ fprintf(stderr, "FAIL %s roundtrip \"%s\": got \"%s\" want \"%s\"\n", label, text, got,
+ want);
failures++;
}
free(got);
@@ -79,15 +86,21 @@ static void check_roundtrip(const qwen_tokenizer_t *tok,
/* Two spellings of the same text must encode identically. */
static void check_same_ids(const qwen_tokenizer_t *tok,
qwen_tokenizer_workspace_t *ws,
- const char *a, const char *b, const char *label)
-{
+ const char *a,
+ const char *b,
+ const char *label) {
int na = 0, nb = 0;
- int *ia = qwen_tokenizer_encode_with_workspace(
- (qwen_tokenizer_t *)tok, ws, a, &na);
- if (!ia) { TEST_ASSERT(0); return; }
- int *ib = qwen_tokenizer_encode_with_workspace(
- (qwen_tokenizer_t *)tok, ws, b, &nb);
- if (!ib) { free(ia); TEST_ASSERT(0); return; }
+ int *ia = qwen_tokenizer_encode_with_workspace((qwen_tokenizer_t *)tok, ws, a, &na);
+ if (!ia) {
+ TEST_ASSERT(0);
+ return;
+ }
+ int *ib = qwen_tokenizer_encode_with_workspace((qwen_tokenizer_t *)tok, ws, b, &nb);
+ if (!ib) {
+ free(ia);
+ TEST_ASSERT(0);
+ return;
+ }
if (na != nb || memcmp(ia, ib, (size_t)na * sizeof(int)) != 0) {
fprintf(stderr, "FAIL %s same-ids \"%s\" vs \"%s\"\n", label, a, b);
failures++;
@@ -96,9 +109,8 @@ static void check_same_ids(const qwen_tokenizer_t *tok,
free(ia);
}
-static void run_cases(const qwen_tokenizer_t *tok,
- qwen_tokenizer_workspace_t *ws, const char *label)
-{
+static void
+run_cases(const qwen_tokenizer_t *tok, qwen_tokenizer_workspace_t *ws, const char *label) {
/* h e l l o -> he -> he,ll -> hell -> hello */
const int hello[] = {259};
check_ids(tok, ws, "hello", hello, 1, label);
@@ -147,14 +159,14 @@ static void run_cases(const qwen_tokenizer_t *tok,
/* NFC: decomposed base+combining-mark spellings must match the
* precomposed codepoint (one pair per mark in the composition table). */
- check_same_ids(tok, ws, "e\xCC\x81", "\xC3\xA9", label); /* acute */
- check_same_ids(tok, ws, "a\xCC\x80", "\xC3\xA0", label); /* grave */
- check_same_ids(tok, ws, "e\xCC\x82", "\xC3\xAA", label); /* circumflex */
- check_same_ids(tok, ws, "n\xCC\x83", "\xC3\xB1", label); /* tilde */
- check_same_ids(tok, ws, "u\xCC\x88", "\xC3\xBC", label); /* diaeresis */
- check_same_ids(tok, ws, "a\xCC\x8A", "\xC3\xA5", label); /* ring */
- check_same_ids(tok, ws, "c\xCC\xA7", "\xC3\xA7", label); /* cedilla */
- check_same_ids(tok, ws, "A\xCC\x80", "\xC3\x80", label); /* upper */
+ check_same_ids(tok, ws, "e\xCC\x81", "\xC3\xA9", label); /* acute */
+ check_same_ids(tok, ws, "a\xCC\x80", "\xC3\xA0", label); /* grave */
+ check_same_ids(tok, ws, "e\xCC\x82", "\xC3\xAA", label); /* circumflex */
+ check_same_ids(tok, ws, "n\xCC\x83", "\xC3\xB1", label); /* tilde */
+ check_same_ids(tok, ws, "u\xCC\x88", "\xC3\xBC", label); /* diaeresis */
+ check_same_ids(tok, ws, "a\xCC\x8A", "\xC3\xA5", label); /* ring */
+ check_same_ids(tok, ws, "c\xCC\xA7", "\xC3\xA7", label); /* cedilla */
+ check_same_ids(tok, ws, "A\xCC\x80", "\xC3\x80", label); /* upper */
check_roundtrip(tok, ws, "cafe\xCC\x81", "caf\xC3\xA9", label);
/* 'x' + acute has no composition: bytes pass through untouched. */
check_roundtrip(tok, ws, "x\xCC\x81", "x\xCC\x81", label);
@@ -177,30 +189,37 @@ static void run_cases(const qwen_tokenizer_t *tok,
check_roundtrip(tok, ws, "\xF0\x9F\x98\x80", "\xF0\x9F\x98\x80", label);
check_roundtrip(tok, ws, "tab\tand\nnl 42!", "tab\tand\nnl 42!", label);
const int ellipsis[] = {'a', 0xE2, 0x80, 0xA6, 'b'};
- check_ids(tok, ws, "a\xE2\x80\xA6" "b", ellipsis, 5, label);
+ check_ids(tok, ws,
+ "a\xE2\x80\xA6"
+ "b",
+ ellipsis, 5, label);
}
-static void write_file(const char *dir, const char *name, const char *body)
-{
+static void write_file(const char *dir, const char *name, const char *body) {
char path[2048];
snprintf(path, sizeof(path), "%s/%s", dir, name);
FILE *f = fopen(path, "w");
- if (!f) { perror(path); exit(2); }
+ if (!f) {
+ perror(path);
+ exit(2);
+ }
fputs(body, f);
fclose(f);
}
/* Loader rejection paths and vocab.json escape handling, in throwaway
* subdirectories of the main fixture dir. */
-static void test_loader_edges(const char *root)
-{
+static void test_loader_edges(const char *root) {
char dir[1152], path[2048];
TEST_ASSERT(qwen_tokenizer_load("/nonexistent/vocab.json") == NULL);
/* Not a JSON object. */
snprintf(dir, sizeof(dir), "%s/badjson", root);
- if (mkdir(dir, 0755) != 0) { perror(dir); exit(2); }
+ if (mkdir(dir, 0755) != 0) {
+ perror(dir);
+ exit(2);
+ }
write_file(dir, "vocab.json", "[1, 2]\n");
write_file(dir, "merges.txt", "a b\n");
snprintf(path, sizeof(path), "%s/vocab.json", dir);
@@ -209,7 +228,10 @@ static void test_loader_edges(const char *root)
/* Missing or pair-less merges.txt is tolerated: the tokenizer loads
* and encoding falls back to byte/vocab-level (no merges applied). */
snprintf(dir, sizeof(dir), "%s/nomerges", root);
- if (mkdir(dir, 0755) != 0) { perror(dir); exit(2); }
+ if (mkdir(dir, 0755) != 0) {
+ perror(dir);
+ exit(2);
+ }
write_file(dir, "vocab.json", "{\"a\":0,\"b\":1}\n");
snprintf(path, sizeof(path), "%s/vocab.json", dir);
qwen_tokenizer_t *nom = qwen_tokenizer_load(path);
@@ -225,7 +247,10 @@ static void test_loader_edges(const char *root)
}
snprintf(dir, sizeof(dir), "%s/emptymerges", root);
- if (mkdir(dir, 0755) != 0) { perror(dir); exit(2); }
+ if (mkdir(dir, 0755) != 0) {
+ perror(dir);
+ exit(2);
+ }
write_file(dir, "vocab.json", "{\"a\":0,\"b\":1}\n");
write_file(dir, "merges.txt", "#version: 0.2\n\n \n");
snprintf(path, sizeof(path), "%s/vocab.json", dir);
@@ -235,7 +260,10 @@ static void test_loader_edges(const char *root)
/* Escape forms in vocab keys; a malformed merge line is skipped. */
snprintf(dir, sizeof(dir), "%s/escapes", root);
- if (mkdir(dir, 0755) != 0) { perror(dir); exit(2); }
+ if (mkdir(dir, 0755) != 0) {
+ perror(dir);
+ exit(2);
+ }
write_file(dir, "vocab.json",
"{\"a\":0, \"b\":1, \"ab\":2,\n"
" \"\\u20AC\":3, \"x\\ny\":4, \"q\\\"r\":5,\n"
@@ -264,12 +292,14 @@ static void test_loader_edges(const char *root)
qwen_tokenizer_t *rel = qwen_tokenizer_load("vocab.json");
TEST_ASSERT(rel != NULL);
qwen_tokenizer_free(rel);
- if (chdir(saved_cwd) != 0) { perror("chdir back"); exit(2); }
+ if (chdir(saved_cwd) != 0) {
+ perror("chdir back");
+ exit(2);
+ }
}
}
-int main(void)
-{
+int main(void) {
char dir[1024];
snprintf(dir, sizeof(dir), "%s/embed-tok-test-XXXXXX",
getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
@@ -282,7 +312,8 @@ int main(void)
snprintf(vocab_path, sizeof(vocab_path), "%s/vocab.json", dir);
qwen_tokenizer_t *tok = qwen_tokenizer_load(vocab_path);
TEST_ASSERT(tok != NULL);
- if (!tok) return 1;
+ if (!tok)
+ return 1;
/* The complete byte alphabet means the integer-pair tables must exist. */
TEST_ASSERT(tok->int_merges != NULL);
@@ -312,21 +343,17 @@ int main(void)
{
int out[8];
int n = -1;
- TEST_ASSERT(qwen_tokenizer_encode_into(tok, ws, "hello world", out, 8,
- &n) == 0 && n == 3);
+ TEST_ASSERT(qwen_tokenizer_encode_into(tok, ws, "hello world", out, 8, &n) == 0 && n == 3);
TEST_ASSERT(out[0] == 259 && out[1] == 263 && out[2] == 262);
/* On overflow the prefix is written and n reports the total
* required count, so a caller can re-size and retry. */
n = -1;
- TEST_ASSERT(qwen_tokenizer_encode_into(tok, ws, "hello world", out, 2,
- &n) == -2 && n == 3);
+ TEST_ASSERT(qwen_tokenizer_encode_into(tok, ws, "hello world", out, 2, &n) == -2 && n == 3);
TEST_ASSERT(out[0] == 259 && out[1] == 263);
n = -1;
- TEST_ASSERT(qwen_tokenizer_encode_into(tok, ws, "hello world", out, 0,
- &n) == -2 && n == 3);
+ TEST_ASSERT(qwen_tokenizer_encode_into(tok, ws, "hello world", out, 0, &n) == -2 && n == 3);
n = -1;
- TEST_ASSERT(qwen_tokenizer_encode_into(tok, ws, "", out, 8, &n) == 0 &&
- n == 0);
+ TEST_ASSERT(qwen_tokenizer_encode_into(tok, ws, "", out, 8, &n) == 0 && n == 0);
TEST_ASSERT(qwen_tokenizer_encode_into(tok, ws, NULL, out, 8, &n) == -1);
TEST_ASSERT(qwen_tokenizer_encode_into(NULL, ws, "x", out, 8, &n) == -1);
}
diff --git a/tests/test_workspace.c b/tests/test_workspace.c
index 4f83936..a906689 100644
--- a/tests/test_workspace.c
+++ b/tests/test_workspace.c
@@ -14,12 +14,12 @@
#include <string.h>
#include <unistd.h>
-static float max_abs_diff(const float *a, const float *b, int n)
-{
+static float max_abs_diff(const float *a, const float *b, int n) {
float m = 0.0f;
for (int i = 0; i < n; i++) {
float d = fabsf(a[i] - b[i]);
- if (d > m) m = d;
+ if (d > m)
+ m = d;
}
return m;
}
@@ -28,11 +28,11 @@ static float max_abs_diff(const float *a, const float *b, int n)
* results validated in main. reference is the batched row for ids0. */
static int check_alloc_and_pooling_variants(const embed_model_t *model,
embed_workspace_t *ws,
- const int *ids0, int n0,
+ const int *ids0,
+ int n0,
const float *states,
const float *reference,
- const embed_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;
@@ -73,22 +73,19 @@ static int check_alloc_and_pooling_variants(const embed_model_t *model,
/* One span covering the whole sequence pools to the embedding, and
* two halves recombine into it by token-count weighting. */
embed_span_t whole = {0, n0};
- if (embed_model_encode_spans(model, ws, ids0, n0, &whole, 1,
- span_out) != 0 ||
+ 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;
embed_span_t halves[2] = {{0, h}, {h, n0 - h}};
- if (embed_model_encode_spans(model, ws, ids0, n0, halves, 2,
- span_out) != 0) {
+ if (embed_model_encode_spans(model, ws, ids0, n0, halves, 2, span_out) != 0) {
fprintf(stderr, "embed_model_encode_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;
+ 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;
@@ -108,10 +105,10 @@ done:
* doc0 is chunks 0 and 1 joined by a separator token; doc1 is chunk 2. */
static int check_spans_batch_parity(const embed_model_t *model,
embed_workspace_t *ws,
- int *const ids[], const int *ntok,
+ int *const ids[],
+ const int *ntok,
int separator_id,
- const embed_config_t *cfg)
-{
+ const embed_config_t *cfg) {
int rc = 1;
int dim = cfg->hidden_size;
int n0 = ntok[0] + 1 + ntok[1];
@@ -128,10 +125,9 @@ static int check_spans_batch_parity(const embed_model_t *model,
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 ||
+ if (embed_model_encode_spans(model, ws, doc0, n0, spans0, 2, expected) != 0 ||
embed_model_encode_spans(model, ws, ids[2], ntok[2], &span1, 1,
- expected + (size_t)2 * dim) != 0) {
+ expected + (size_t)2 * dim) != 0) {
fprintf(stderr, "singleton contextual embedding failed\n");
goto done;
}
@@ -164,8 +160,7 @@ done:
return rc;
}
-int main(int argc, char **argv)
-{
+int main(int argc, char **argv) {
char fixture_dir[1024];
const char *model_dir;
if (argc == 2) {
@@ -214,12 +209,10 @@ int main(int argc, char **argv)
return 1;
}
- const char *texts[] = {
- "query: what is the capital of France?",
- "document: Paris is the capital of France.",
- "document: Berlin is the capital of Germany.",
- "document: Istanbul is a major city in Turkey."
- };
+ const char *texts[] = {"query: what is the capital of France?",
+ "document: Paris is the capital of France.",
+ "document: Berlin is the capital of Germany.",
+ "document: Istanbul is a major city in Turkey."};
const int batch = (int)(sizeof(texts) / sizeof(texts[0]));
/* Initialized before the first `goto fail` so the cleanup loop never
* sees indeterminate pointers. */
@@ -291,8 +284,10 @@ int main(int argc, char **argv)
const float *row = batched + (size_t)i * dim;
float diff = max_abs_diff(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;
+ if (diff > worst_diff)
+ worst_diff = diff;
+ if (cos < worst_cos)
+ worst_cos = cos;
}
memcpy(normalized, batched, (size_t)dim * sizeof(float));
@@ -308,8 +303,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, "embed_l2_normalize produced norm_sq=%g\n",
- normalized_norm_sq);
+ fprintf(stderr, "embed_l2_normalize produced norm_sq=%g\n", normalized_norm_sq);
free(states);
free(normalized);
free(single);
@@ -326,8 +320,8 @@ 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) {
+ if (check_alloc_and_pooling_variants(model, batch_ws, ids[0], ntok[0], states, batched, cfg) !=
+ 0) {
free(states);
free(normalized);
free(single);
@@ -338,9 +332,9 @@ 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 = EMBED_CONTEXT_SEPARATOR_TOKEN_ID;
- if (check_spans_batch_parity(model, batch_ws, ids, ntok, sep_id,
- cfg) != 0) {
+ 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);
free(normalized);
free(single);
@@ -353,24 +347,26 @@ int main(int argc, char **argv)
free(single);
free(batched);
- for (int i = 0; i < batch; i++) free(ids[i]);
+ for (int i = 0; i < batch; i++)
+ free(ids[i]);
qwen_tokenizer_free(tok);
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",
- worst_diff, worst_cos);
+ fprintf(stderr, "workspace API parity failed: max_abs_diff=%g cosine=%g\n", worst_diff,
+ worst_cos);
return 1;
}
- printf("ok: workspace API dim=%d batch=%d workspace=%zu bytes max_abs_diff=%g cosine=%g\n",
- dim, batch, batch_ws_bytes, worst_diff, worst_cos);
+ printf("ok: workspace API dim=%d batch=%d workspace=%zu bytes max_abs_diff=%g cosine=%g\n", dim,
+ batch, batch_ws_bytes, worst_diff, worst_cos);
return 0;
fail:
- for (int i = 0; i < batch; i++) free(ids[i]);
+ for (int i = 0; i < batch; i++)
+ free(ids[i]);
qwen_tokenizer_free(tok);
embed_workspace_free(single_ws);
embed_workspace_free(batch_ws);
diff --git a/tests/tiny_model.h b/tests/tiny_model.h
index d4ae431..42542c1 100644
--- a/tests/tiny_model.h
+++ b/tests/tiny_model.h
@@ -14,15 +14,13 @@
#include <sys/stat.h>
#include <sys/types.h>
-static uint16_t tm_f32_to_bf16(float x)
-{
+static uint16_t tm_f32_to_bf16(float x) {
uint32_t u;
memcpy(&u, &x, sizeof(u));
return (uint16_t)(u >> 16);
}
-static float tm_bf16_to_f32(uint16_t b)
-{
+static float tm_bf16_to_f32(uint16_t b) {
uint32_t u = ((uint32_t)b) << 16;
float f;
memcpy(&f, &u, sizeof(f));
@@ -30,8 +28,7 @@ static float tm_bf16_to_f32(uint16_t b)
}
/* Deterministic, BF16-exact value for element i of tensor `name`. */
-static float tm_value(const char *name, size_t i)
-{
+static float tm_value(const char *name, size_t i) {
float v;
size_t len = strlen(name);
if ((len >= 11 && strcmp(name + len - 11, "norm.weight") == 0) ||
@@ -46,7 +43,7 @@ static float tm_value(const char *name, size_t i)
typedef struct {
const char *name;
- int rows, cols; /* cols == 0 means a 1-D tensor of `rows` values */
+ int rows, cols; /* cols == 0 means a 1-D tensor of `rows` values */
} tm_spec_t;
/* Always one layer; everything else is free so a test can match a real
@@ -57,22 +54,21 @@ typedef struct {
enum { TM_N_SPECS = 13 };
-static int tm_write_config(const char *dir, const tm_dims_t *d,
- const char *model_type, int eos_token_id)
-{
+static int
+tm_write_config(const char *dir, const tm_dims_t *d, const char *model_type, int eos_token_id) {
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,"
+ 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);
+ d->hidden, d->heads, d->kv_heads, d->head_dim, d->intermediate, d->vocab);
if (model_type)
- fprintf(f, ",\"model_type\":\"%s\",\"eos_token_id\":%d",
- model_type, eos_token_id);
+ fprintf(f, ",\"model_type\":\"%s\",\"eos_token_id\":%d", model_type, eos_token_id);
fputs("}", f);
fclose(f);
return 0;
@@ -80,9 +76,8 @@ static int tm_write_config(const char *dir, const tm_dims_t *d,
/* Write a safetensors file holding `specs` with tm_value() contents.
* dtype: "F32" or "BF16". Returns 0 on success. */
-static int tm_write_safetensors(const char *path, const char *dtype,
- const tm_spec_t *specs, int n_specs)
-{
+static int
+tm_write_safetensors(const char *path, const char *dtype, const tm_spec_t *specs, int n_specs) {
int bf16 = strcmp(dtype, "BF16") == 0;
size_t esize = bf16 ? 2 : 4;
@@ -95,23 +90,26 @@ static int tm_write_safetensors(const char *path, const char *dtype,
size_t end = doff + n * esize;
if (s->cols)
hoff += (size_t)snprintf(header + hoff, sizeof(header) - hoff,
- "%s\"%s\":{\"dtype\":\"%s\",\"shape\":[%d,%d],"
- "\"data_offsets\":[%zu,%zu]}",
- t ? "," : "", s->name, dtype, s->rows, s->cols, doff, end);
+ "%s\"%s\":{\"dtype\":\"%s\",\"shape\":[%d,%d],"
+ "\"data_offsets\":[%zu,%zu]}",
+ t ? "," : "", s->name, dtype, s->rows, s->cols, doff, end);
else
hoff += (size_t)snprintf(header + hoff, sizeof(header) - hoff,
- "%s\"%s\":{\"dtype\":\"%s\",\"shape\":[%d],"
- "\"data_offsets\":[%zu,%zu]}",
- t ? "," : "", s->name, dtype, s->rows, doff, end);
+ "%s\"%s\":{\"dtype\":\"%s\",\"shape\":[%d],"
+ "\"data_offsets\":[%zu,%zu]}",
+ t ? "," : "", s->name, dtype, s->rows, doff, end);
doff = end;
}
hoff += (size_t)snprintf(header + hoff, sizeof(header) - hoff, "}");
- if (hoff >= sizeof(header) - 1) return -1;
+ if (hoff >= sizeof(header) - 1)
+ return -1;
FILE *f = fopen(path, "wb");
- if (!f) return -1;
+ if (!f)
+ return -1;
unsigned char len8[8];
- for (int i = 0; i < 8; i++) len8[i] = (unsigned char)(hoff >> (8 * i));
+ 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 < n_specs; t++) {
@@ -133,9 +131,7 @@ static int tm_write_safetensors(const char *path, const char *dtype,
/* 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)
-{
+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},
@@ -154,37 +150,37 @@ static int tm_write_model_dims(const char *dir, const char *dtype,
};
char path[2048];
- if (tm_write_config(dir, d, NULL, -1) != 0) return -1;
+ if (tm_write_config(dir, d, NULL, -1) != 0)
+ return -1;
snprintf(path, sizeof(path), "%s/model.safetensors", dir);
return tm_write_safetensors(path, dtype, specs, TM_N_SPECS);
}
-static inline int tm_write_qwen3_model_dims(const char *dir, const char *dtype,
+static inline int tm_write_qwen3_model_dims(const char *dir,
+ const char *dtype,
const tm_dims_t *d,
- int eos_token_id)
-{
- if (tm_write_model_dims(dir, dtype, d) != 0) return -1;
+ int eos_token_id) {
+ if (tm_write_model_dims(dir, dtype, d) != 0)
+ return -1;
return tm_write_config(dir, d, "qwen3", eos_token_id);
}
/* 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)
-{
+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;
+ 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);
+ 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};
@@ -192,16 +188,13 @@ static inline int tm_write_late_projection_named(const char *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");
+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)
-{
+static inline int tm_write_model(const char *dir, const char *dtype) {
static const tm_dims_t d = {4, 2, 1, 2, 8, 16};
return tm_write_model_dims(dir, dtype, &d);
}
diff --git a/tests/tok_fixture.h b/tests/tok_fixture.h
index 7890927..24e1563 100644
--- a/tests/tok_fixture.h
+++ b/tests/tok_fixture.h
@@ -10,15 +10,13 @@
#include <string.h>
/* GPT-2 byte -> unicode codepoint mapping (mirrors the tokenizer's rules). */
-static int tf_byte_to_cp(int b)
-{
+static int tf_byte_to_cp(int b) {
static int table[256];
static int init = 0;
if (!init) {
int n = 0;
for (int i = 0; i < 256; i++) {
- int normal = (i >= 33 && i <= 126) || (i >= 161 && i <= 172) ||
- (i >= 174 && i <= 255);
+ int normal = (i >= 33 && i <= 126) || (i >= 161 && i <= 172) || (i >= 174 && i <= 255);
table[i] = normal ? i : 256 + n++;
}
init = 1;
@@ -26,9 +24,12 @@ static int tf_byte_to_cp(int b)
return table[b];
}
-static int tf_cp_to_utf8(int cp, char *out)
-{
- if (cp < 0x80) { out[0] = (char)cp; out[1] = '\0'; return 1; }
+static int tf_cp_to_utf8(int cp, char *out) {
+ if (cp < 0x80) {
+ out[0] = (char)cp;
+ out[1] = '\0';
+ return 1;
+ }
out[0] = (char)(0xC0 | (cp >> 6));
out[1] = (char)(0x80 | (cp & 0x3F));
out[2] = '\0';
@@ -36,11 +37,11 @@ static int tf_cp_to_utf8(int cp, char *out)
}
/* JSON-escape a byte-unicode token (the byte alphabet needs only \" and \\). */
-static const char *tf_json_escape(const char *s, char *buf, size_t cap)
-{
+static const char *tf_json_escape(const char *s, char *buf, size_t cap) {
size_t o = 0;
for (; *s && o + 2 < cap; s++) {
- if (*s == '"' || *s == '\\') buf[o++] = '\\';
+ if (*s == '"' || *s == '\\')
+ buf[o++] = '\\';
buf[o++] = *s;
}
buf[o] = '\0';
@@ -52,8 +53,7 @@ static const char *TF_MERGED[] = {
"he", "ll", "hell", "hello", "\xC4\xA0w", "or", "ld", "\xC4\xA0wor",
};
static const char *TF_MERGES[] = {
- "h e", "l l", "he ll", "hell o", "\xC4\xA0 w", "o r", "l d",
- "\xC4\xA0w or",
+ "h e", "l l", "he ll", "hell o", "\xC4\xA0 w", "o r", "l d", "\xC4\xA0w or",
};
enum { TF_N_MERGED = sizeof(TF_MERGED) / sizeof(TF_MERGED[0]) };
/* Special tokens go last so contextual and late-interaction ids resolve
@@ -65,17 +65,16 @@ enum { TF_DOCUMENT_ID = TF_QUERY_ID + 1 };
enum { TF_VOCAB_SIZE = TF_DOCUMENT_ID + 1 };
/* Write vocab.json + merges.txt into dir. Returns 0 on success. */
-static int tf_write_vocab(const char *dir)
-{
+static int tf_write_vocab(const char *dir) {
char path[2048], tok[4], esc[16];
snprintf(path, sizeof(path), "%s/vocab.json", dir);
FILE *f = fopen(path, "w");
- if (!f) return -1;
+ if (!f)
+ return -1;
fputs("{", f);
for (int b = 0; b < 256; b++) {
tf_cp_to_utf8(tf_byte_to_cp(b), tok);
- fprintf(f, "%s\"%s\": %d", b ? ", " : "",
- tf_json_escape(tok, esc, sizeof(esc)), b);
+ fprintf(f, "%s\"%s\": %d", b ? ", " : "", tf_json_escape(tok, esc, sizeof(esc)), b);
}
for (int i = 0; i < TF_N_MERGED; i++)
fprintf(f, ", \"%s\": %d", TF_MERGED[i], 256 + i);
@@ -88,7 +87,8 @@ static int tf_write_vocab(const char *dir)
snprintf(path, sizeof(path), "%s/merges.txt", dir);
f = fopen(path, "w");
- if (!f) return -1;
+ if (!f)
+ return -1;
for (size_t i = 0; i < sizeof(TF_MERGES) / sizeof(TF_MERGES[0]); i++)
fprintf(f, "%s\n", TF_MERGES[i]);
fclose(f);