aboutsummaryrefslogtreecommitdiff
path: root/tests/test_bf16_model.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-17 01:21:41 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-17 01:21:41 +0200
commite00becb643b3a99de16462b3a032717a93b983d9 (patch)
tree873ad2646b0ef2f07eae5a5a74387d062c77ec0c /tests/test_bf16_model.c
parentb4343d6ac1c6cc7a7d228f954220def2f6e6ed67 (diff)
downloadffwd-e00becb643b3a99de16462b3a032717a93b983d9.tar.gz
ffwd-e00becb643b3a99de16462b3a032717a93b983d9.zip
all: accept F16 safetensors weights (widen to F32 at load)
Diffstat (limited to 'tests/test_bf16_model.c')
-rw-r--r--tests/test_bf16_model.c59
1 files changed, 39 insertions, 20 deletions
diff --git a/tests/test_bf16_model.c b/tests/test_bf16_model.c
index 48e4766..0849f3e 100644
--- a/tests/test_bf16_model.c
+++ b/tests/test_bf16_model.c
@@ -1,6 +1,7 @@
-/* tests/test_bf16_model.c - F32 vs BF16 loader/dispatch parity on a tiny
- * synthetic model. Hermetic: builds its own model dirs. Runs via
- * `make test`. */
+/* tests/test_bf16_model.c - F32 vs BF16 vs F16 loader/dispatch parity on a tiny
+ * synthetic model. Hermetic: builds its own model dirs. Runs via `make test`.
+ * F16 has no fused kernel: the loader widens it to F32 once at load, so an F16
+ * copy must match the F32 copy as closely as the BF16 copy does. */
#include "embed.h"
#include "tiny_model.h"
@@ -12,7 +13,7 @@
#include <unistd.h>
int main(void) {
- char root[1024], f32_dir[1088], bf16_dir[1088], prefixed_dir[1088];
+ char root[1024], f32_dir[1088], bf16_dir[1088], f16_dir[1088], prefixed_dir[1088];
snprintf(root, sizeof(root), "%s/embed-bf16-test-XXXXXX",
getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
if (!mkdtemp(root)) {
@@ -21,10 +22,12 @@ int main(void) {
}
snprintf(f32_dir, sizeof(f32_dir), "%s/f32", root);
snprintf(bf16_dir, sizeof(bf16_dir), "%s/bf16", root);
+ snprintf(f16_dir, sizeof(f16_dir), "%s/f16", root);
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 ||
+ if (mkdir(f32_dir, 0755) != 0 || mkdir(bf16_dir, 0755) != 0 || mkdir(f16_dir, 0755) != 0 ||
+ mkdir(prefixed_dir, 0755) != 0 || tm_write_model(f32_dir, "F32") != 0 ||
+ tm_write_model(bf16_dir, "BF16") != 0 || tm_write_model(f16_dir, "F16") != 0 ||
tm_write_prefixed_model_dims(prefixed_dir, "F32", &dims) != 0) {
fprintf(stderr, "fixture creation failed\n");
return 2;
@@ -33,8 +36,9 @@ 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);
+ embed_model_t *mf16 = embed_model_load(f16_dir);
embed_model_t *mprefixed = embed_model_load(prefixed_dir);
- if (!mf32 || !mbf16 || !mprefixed) {
+ if (!mf32 || !mbf16 || !mf16 || !mprefixed) {
fprintf(stderr, "model load failed\n");
return 1;
}
@@ -44,32 +48,39 @@ int main(void) {
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));
+ float *e = (float *)malloc((size_t)dim * sizeof(float));
embed_workspace_t *wf32 = embed_workspace_new(mf32);
embed_workspace_t *wbf16 = embed_workspace_new(mbf16);
+ embed_workspace_t *wf16 = embed_workspace_new(mf16);
embed_workspace_t *wprefixed = embed_workspace_new(mprefixed);
- if (!a || !b || !c || !wf32 || !wbf16 || !wprefixed) {
+ if (!a || !b || !c || !e || !wf32 || !wbf16 || !wf16 || !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(mf16, wf16, ids, 4, e) != 0 ||
embed_model_encode_into(mprefixed, wprefixed, ids, 4, c) != 0) {
fprintf(stderr, "embedding failed\n");
return 1;
}
float max_diff = 0.0f;
+ float f16_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]) || !isfinite(c[i])) {
+ if (!isfinite(a[i]) || !isfinite(b[i]) || !isfinite(e[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] - e[i]);
+ if (d > f16_diff)
+ f16_diff = d;
d = fabsf(a[i] - c[i]);
if (d > prefix_diff)
prefix_diff = d;
@@ -77,10 +88,11 @@ int main(void) {
}
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 || prefix_diff != 0.0f) {
- fprintf(stderr, "bad parity: norm=%.9g max_diff=%.9g prefix_diff=%.9g\n", norm, max_diff,
- prefix_diff);
+ * magnitude and tight F32-vs-BF16/F16 parity, not unit norm. F16 carries
+ * more mantissa bits than BF16, so the same 2e-5 bound holds. */
+ if (norm <= 1e-6f || max_diff > 2e-5f || f16_diff > 2e-5f || prefix_diff != 0.0f) {
+ fprintf(stderr, "bad parity: norm=%.9g max_diff=%.9g f16_diff=%.9g prefix_diff=%.9g\n",
+ norm, max_diff, f16_diff, prefix_diff);
return 1;
}
@@ -91,36 +103,43 @@ int main(void) {
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) {
+ embed_model_encode_into(mbf16, wbf16, long_ids, LONG_N, b) != 0 ||
+ embed_model_encode_into(mf16, wf16, long_ids, LONG_N, e) != 0) {
fprintf(stderr, "long-sequence embedding failed\n");
return 1;
}
- float long_diff = 0.0f;
+ float long_diff = 0.0f, long_f16_diff = 0.0f;
for (int i = 0; i < dim; i++) {
- if (!isfinite(a[i]) || !isfinite(b[i])) {
+ if (!isfinite(a[i]) || !isfinite(b[i]) || !isfinite(e[i])) {
fprintf(stderr, "non-finite long-sequence value\n");
return 1;
}
float d = fabsf(a[i] - b[i]);
if (d > long_diff)
long_diff = d;
+ d = fabsf(a[i] - e[i]);
+ if (d > long_f16_diff)
+ long_f16_diff = d;
}
- if (long_diff > 2e-5f) {
- fprintf(stderr, "bad widen-path parity: max_diff=%.9g\n", long_diff);
+ if (long_diff > 2e-5f || long_f16_diff > 2e-5f) {
+ fprintf(stderr, "bad widen-path parity: bf16=%.9g f16=%.9g\n", long_diff, long_f16_diff);
return 1;
}
- printf("ok: bf16 and model-prefix parity dim=%d max_abs_diff=%.9g norm=%.9g\n", dim, max_diff,
- norm);
+ printf("ok: bf16/f16 and model-prefix parity dim=%d bf16_diff=%.9g f16_diff=%.9g norm=%.9g\n",
+ dim, max_diff, f16_diff, norm);
embed_workspace_free(wf32);
embed_workspace_free(wbf16);
+ embed_workspace_free(wf16);
embed_workspace_free(wprefixed);
embed_model_free(mf32);
embed_model_free(mbf16);
+ embed_model_free(mf16);
embed_model_free(mprefixed);
free(a);
free(b);
free(c);
+ free(e);
return 0;
}