aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-15 11:45:26 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-15 11:45:26 +0200
commit588feec258dd309e4b947c25bfacad7ad956547f (patch)
tree98eca640f17e9815748358fba41982806259b281 /tests
parentd7b62df9dfae32c62c2d7ec3da79ab3239af8d31 (diff)
downloadffwd-588feec258dd309e4b947c25bfacad7ad956547f.tar.gz
ffwd-588feec258dd309e4b947c25bfacad7ad956547f.zip
api: accept Hugging Face model weight prefix
Diffstat (limited to 'tests')
-rw-r--r--tests/test_bf16_model.c36
-rw-r--r--tests/tiny_model.h38
2 files changed, 54 insertions, 20 deletions
diff --git a/tests/test_bf16_model.c b/tests/test_bf16_model.c
index 9a5d842..48e4766 100644
--- a/tests/test_bf16_model.c
+++ b/tests/test_bf16_model.c
@@ -12,7 +12,7 @@
#include <unistd.h>
int main(void) {
- char root[1024], f32_dir[1088], bf16_dir[1088];
+ char root[1024], f32_dir[1088], bf16_dir[1088], prefixed_dir[1088];
snprintf(root, sizeof(root), "%s/embed-bf16-test-XXXXXX",
getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
if (!mkdtemp(root)) {
@@ -21,8 +21,11 @@ int main(void) {
}
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) {
+ snprintf(prefixed_dir, sizeof(prefixed_dir), "%s/prefixed", root);
+ static const tm_dims_t dims = {4, 2, 1, 2, 8, 16};
+ if (mkdir(f32_dir, 0755) != 0 || mkdir(bf16_dir, 0755) != 0 || mkdir(prefixed_dir, 0755) != 0 ||
+ tm_write_model(f32_dir, "F32") != 0 || tm_write_model(bf16_dir, "BF16") != 0 ||
+ tm_write_prefixed_model_dims(prefixed_dir, "F32", &dims) != 0) {
fprintf(stderr, "fixture creation failed\n");
return 2;
}
@@ -30,7 +33,8 @@ int main(void) {
int ids[] = {1, 2, 3, 4};
embed_model_t *mf32 = embed_model_load(f32_dir);
embed_model_t *mbf16 = embed_model_load(bf16_dir);
- if (!mf32 || !mbf16) {
+ embed_model_t *mprefixed = embed_model_load(prefixed_dir);
+ if (!mf32 || !mbf16 || !mprefixed) {
fprintf(stderr, "model load failed\n");
return 1;
}
@@ -39,36 +43,44 @@ int main(void) {
int dim = cfg->hidden_size;
float *a = (float *)malloc((size_t)dim * sizeof(float));
float *b = (float *)malloc((size_t)dim * sizeof(float));
+ float *c = (float *)malloc((size_t)dim * sizeof(float));
embed_workspace_t *wf32 = embed_workspace_new(mf32);
embed_workspace_t *wbf16 = embed_workspace_new(mbf16);
- if (!a || !b || !wf32 || !wbf16) {
+ embed_workspace_t *wprefixed = embed_workspace_new(mprefixed);
+ if (!a || !b || !c || !wf32 || !wbf16 || !wprefixed) {
fprintf(stderr, "allocation failed\n");
return 1;
}
if (embed_model_encode_into(mf32, wf32, ids, 4, a) != 0 ||
- embed_model_encode_into(mbf16, wbf16, ids, 4, b) != 0) {
+ embed_model_encode_into(mbf16, wbf16, ids, 4, b) != 0 ||
+ embed_model_encode_into(mprefixed, wprefixed, ids, 4, c) != 0) {
fprintf(stderr, "embedding failed\n");
return 1;
}
float max_diff = 0.0f;
+ float prefix_diff = 0.0f;
float norm = 0.0f;
for (int i = 0; i < dim; i++) {
- if (!isfinite(a[i]) || !isfinite(b[i])) {
+ if (!isfinite(a[i]) || !isfinite(b[i]) || !isfinite(c[i])) {
fprintf(stderr, "non-finite embedding value\n");
return 1;
}
float d = fabsf(a[i] - b[i]);
if (d > max_diff)
max_diff = d;
+ d = fabsf(a[i] - c[i]);
+ if (d > prefix_diff)
+ prefix_diff = d;
norm += b[i] * b[i];
}
norm = sqrtf(norm);
/* Embeddings are intentionally unnormalized; require a sane nonzero
* magnitude and tight F32-vs-BF16 parity, not unit norm. */
- if (norm <= 1e-6f || max_diff > 2e-5f) {
- fprintf(stderr, "bad parity: norm=%.9g max_diff=%.9g\n", norm, max_diff);
+ if (norm <= 1e-6f || max_diff > 2e-5f || prefix_diff != 0.0f) {
+ fprintf(stderr, "bad parity: norm=%.9g max_diff=%.9g prefix_diff=%.9g\n", norm, max_diff,
+ prefix_diff);
return 1;
}
@@ -98,13 +110,17 @@ int main(void) {
return 1;
}
- printf("ok: bf16 model parity dim=%d max_abs_diff=%.9g norm=%.9g\n", dim, max_diff, norm);
+ printf("ok: bf16 and model-prefix parity dim=%d max_abs_diff=%.9g norm=%.9g\n", dim, max_diff,
+ norm);
embed_workspace_free(wf32);
embed_workspace_free(wbf16);
+ embed_workspace_free(wprefixed);
embed_model_free(mf32);
embed_model_free(mbf16);
+ embed_model_free(mprefixed);
free(a);
free(b);
+ free(c);
return 0;
}
diff --git a/tests/tiny_model.h b/tests/tiny_model.h
index 42542c1..d2534d2 100644
--- a/tests/tiny_model.h
+++ b/tests/tiny_model.h
@@ -76,8 +76,8 @@ tm_write_config(const char *dir, const tm_dims_t *d, const char *model_type, int
/* 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_with_prefix(
+ const char *path, const char *dtype, const tm_spec_t *specs, int n_specs, const char *prefix) {
int bf16 = strcmp(dtype, "BF16") == 0;
size_t esize = bf16 ? 2 : 4;
@@ -89,15 +89,16 @@ tm_write_safetensors(const char *path, const char *dtype, const tm_spec_t *specs
size_t n = (size_t)s->rows * (s->cols ? (size_t)s->cols : 1);
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);
+ hoff +=
+ (size_t)snprintf(header + hoff, sizeof(header) - hoff,
+ "%s\"%s%s\":{\"dtype\":\"%s\",\"shape\":[%d,%d],"
+ "\"data_offsets\":[%zu,%zu]}",
+ t ? "," : "", prefix, 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],"
+ "%s\"%s%s\":{\"dtype\":\"%s\",\"shape\":[%d],"
"\"data_offsets\":[%zu,%zu]}",
- t ? "," : "", s->name, dtype, s->rows, doff, end);
+ t ? "," : "", prefix, s->name, dtype, s->rows, doff, end);
doff = end;
}
hoff += (size_t)snprintf(header + hoff, sizeof(header) - hoff, "}");
@@ -129,9 +130,17 @@ tm_write_safetensors(const char *path, const char *dtype, const tm_spec_t *specs
return 0;
}
+static int
+tm_write_safetensors(const char *path, const char *dtype, const tm_spec_t *specs, int n_specs) {
+ return tm_write_safetensors_with_prefix(path, dtype, specs, n_specs, "");
+}
+
/* 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_with_prefix(const char *dir,
+ const char *dtype,
+ const tm_dims_t *d,
+ const char *prefix) {
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,7 +163,16 @@ static int tm_write_model_dims(const char *dir, const char *dtype, const tm_dims
return -1;
snprintf(path, sizeof(path), "%s/model.safetensors", dir);
- return tm_write_safetensors(path, dtype, specs, TM_N_SPECS);
+ return tm_write_safetensors_with_prefix(path, dtype, specs, TM_N_SPECS, prefix);
+}
+
+static int tm_write_model_dims(const char *dir, const char *dtype, const tm_dims_t *d) {
+ return tm_write_model_dims_with_prefix(dir, dtype, d, "");
+}
+
+static inline int
+tm_write_prefixed_model_dims(const char *dir, const char *dtype, const tm_dims_t *d) {
+ return tm_write_model_dims_with_prefix(dir, dtype, d, "model.");
}
static inline int tm_write_qwen3_model_dims(const char *dir,