diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-17 01:21:41 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-17 01:21:41 +0200 |
| commit | e00becb643b3a99de16462b3a032717a93b983d9 (patch) | |
| tree | 873ad2646b0ef2f07eae5a5a74387d062c77ec0c /tests | |
| parent | b4343d6ac1c6cc7a7d228f954220def2f6e6ed67 (diff) | |
| download | ffwd-e00becb643b3a99de16462b3a032717a93b983d9.tar.gz ffwd-e00becb643b3a99de16462b3a032717a93b983d9.zip | |
all: accept F16 safetensors weights (widen to F32 at load)
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_bf16_model.c | 59 | ||||
| -rw-r--r-- | tests/test_safetensors.c | 51 | ||||
| -rw-r--r-- | tests/test_sentencepiece.c | 39 | ||||
| -rw-r--r-- | tests/tiny_model.h | 41 |
4 files changed, 156 insertions, 34 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; } diff --git a/tests/test_safetensors.c b/tests/test_safetensors.c index ccfd6fb..607bfc5 100644 --- a/tests/test_safetensors.c +++ b/tests/test_safetensors.c @@ -64,6 +64,53 @@ static const char ONE_TENSOR_B[] = static const char ONE_TENSOR_X[] = "{\"x\":{\"dtype\":\"F32\",\"shape\":[1],\"data_offsets\":[0,4]}}"; +/* safetensors_get_f32 must widen F16 losslessly. Compares the produced F32 bit + * patterns exactly (so signed zero, subnormals, and inf are all checked), across + * normals, both zeros, the smallest and largest subnormals, max normal, inf, and + * a full-mantissa value (~1/3) that catches a wrong mantissa shift. */ +static void check_f16_conversion(void) { + const uint16_t half_bits[] = { + 0x0000, 0x8000, 0x3c00, 0xbc00, 0x3800, 0x4000, 0x3555, 0x0001, 0x03ff, 0x7bff, 0x7c00, + }; + const uint32_t want_bits[] = { + 0x00000000, 0x80000000, 0x3f800000, 0xbf800000, 0x3f000000, 0x40000000, + 0x3eaaa000, 0x33800000, 0x387fc000, 0x477fe000, 0x7f800000, + }; + int n = (int)(sizeof(half_bits) / sizeof(half_bits[0])); + + char header[128]; + snprintf(header, sizeof(header), + "{\"x\":{\"dtype\":\"F16\",\"shape\":[%d],\"data_offsets\":[0,%d]}}", n, n * 2); + const char *path = path_in("f16_convert.safetensors"); + write_st(path, header, half_bits, (size_t)n * sizeof(uint16_t)); + + safetensors_file_t *sf = safetensors_open(path); + if (!sf || sf->num_tensors != 1) { + fprintf(stderr, "f16 conversion: open failed\n"); + failures++; + safetensors_close(sf); + return; + } + float *got = safetensors_get_f32(sf, &sf->tensors[0]); + if (!got) { + fprintf(stderr, "f16 conversion: get_f32 returned NULL\n"); + failures++; + safetensors_close(sf); + return; + } + for (int i = 0; i < n; i++) { + uint32_t got_bits; + memcpy(&got_bits, &got[i], sizeof(got_bits)); + if (got_bits != want_bits[i]) { + fprintf(stderr, "f16[%d]=0x%04x: got 0x%08x want 0x%08x\n", i, half_bits[i], got_bits, + want_bits[i]); + failures++; + } + } + free(got); + safetensors_close(sf); +} + int main(void) { snprintf(tmproot, sizeof(tmproot), "%s/embed-st-test-XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); @@ -137,10 +184,12 @@ int main(void) { write_st(path_in("bad_name/model-foo.safetensors"), ONE_TENSOR_X, zeros, 4); check("multi", path_in("bad_name"), 0); + check_f16_conversion(); + if (failures) { fprintf(stderr, "safetensors tests: %d failure(s)\n", failures); return 1; } - puts("ok: safetensors parser limit checks passed"); + puts("ok: safetensors parser limit + F16 conversion checks passed"); return 0; } diff --git a/tests/test_sentencepiece.c b/tests/test_sentencepiece.c index b597af2..38bcb36 100644 --- a/tests/test_sentencepiece.c +++ b/tests/test_sentencepiece.c @@ -167,14 +167,22 @@ static int run_hermetic(void) { return 2; } + /* A directory with no SentencePiece model must fail cleanly, not crash. */ + char empty[1024]; + snprintf(empty, sizeof(empty), "%s/embed-sp-empty-XXXXXX", tmp); + if (mkdtemp(empty) && sentencepiece_tokenizer_load(empty) != NULL) { + fprintf(stderr, "load of model-less dir should fail\n"); + return 1; + } + sentencepiece_tokenizer_t *tok = sentencepiece_tokenizer_load(dir); sentencepiece_workspace_t *ws = sentencepiece_workspace_new(); if (!tok || !ws) { fprintf(stderr, "load failed\n"); return 1; } - if (tok->piece_count != 12 || tok->vocab_size != 14 || tok->unk_id != 3 || - tok->bos_id != 0 || tok->eos_id != 2 || tok->pad_id != 1 || tok->mask_id != 13) { + if (tok->piece_count != 12 || tok->vocab_size != 14 || tok->unk_id != 3 || tok->bos_id != 0 || + tok->eos_id != 2 || tok->pad_id != 1 || tok->mask_id != 13) { fprintf(stderr, "metadata wrong\n"); return 1; } @@ -201,6 +209,17 @@ static int run_hermetic(void) { fprintf(stderr, "small-buffer handling wrong\n"); return 1; } + + /* The no-workspace encode entry point must agree with the workspace path. */ + int plain_n = -1; + int *plain = sentencepiece_tokenizer_encode(tok, "Hello world!", &plain_n); + if (plain_n != 3 || !plain || plain[0] != 8 || plain[1] != 9 || plain[2] != 7) { + fprintf(stderr, "plain encode mismatch: n=%d\n", plain_n); + free(plain); + return 1; + } + free(plain); + sentencepiece_workspace_free(ws); sentencepiece_tokenizer_free(tok); @@ -232,17 +251,19 @@ static int run_live(const char *model_dir) { int rc = 0; rc |= check_ids(tok, ws, "Hello world! \xe5\x8d\x97\xe7\x93\x9c", (int[]){35378, 8999, 38, 6, 4617, 39613}, 6, "live-mixed"); - rc |= check_ids(tok, ws, "caf\xc3\xa9 d\xc3\xa9j\xc3\xa0 vu", - (int[]){26216, 15154, 13946}, 3, "live-accents"); - rc |= check_ids(tok, ws, "\xef\xbc\xa1\xef\xbc\xa2\xef\xbc\xa3 \xef\xbc\x91\xef\xbc\x92\xef\xbc\x93", + rc |= check_ids(tok, ws, "caf\xc3\xa9 d\xc3\xa9j\xc3\xa0 vu", (int[]){26216, 15154, 13946}, 3, + "live-accents"); + rc |= check_ids(tok, ws, + "\xef\xbc\xa1\xef\xbc\xa2\xef\xbc\xa3 \xef\xbc\x91\xef\xbc\x92\xef\xbc\x93", (int[]){47457, 37638}, 2, "live-nfkc"); rc |= check_ids(tok, ws, "query: what is snowflake?", - (int[]){41, 1294, 12, 2367, 83, 108203, 13034, 350, 32}, 9, - "live-query"); - rc |= check_ids(tok, ws, "\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82 \xd0\xbc\xd0\xb8\xd1\x80", + (int[]){41, 1294, 12, 2367, 83, 108203, 13034, 350, 32}, 9, "live-query"); + rc |= check_ids(tok, ws, + "\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82 \xd0\xbc\xd0\xb8\xd1\x80", (int[]){1813, 18454, 11373}, 3, "live-cyrillic"); rc |= check_ids(tok, ws, - "\xd9\x85\xd8\xb1\xd8\xad\xd8\xa8\xd8\xa7 \xd8\xa8\xd8\xa7\xd9\x84\xd8\xb9\xd8\xa7\xd9\x84\xd9\x85", + "\xd9\x85\xd8\xb1\xd8\xad\xd8\xa8\xd8\xa7 " + "\xd8\xa8\xd8\xa7\xd9\x84\xd8\xb9\xd8\xa7\xd9\x84\xd9\x85", (int[]){665, 193478, 258, 1705, 77796}, 5, "live-arabic"); rc |= check_ids(tok, ws, "", NULL, 0, "live-empty"); rc |= check_ids(tok, ws, " ", NULL, 0, "live-spaces"); diff --git a/tests/tiny_model.h b/tests/tiny_model.h index e0d000b..9aee665 100644 --- a/tests/tiny_model.h +++ b/tests/tiny_model.h @@ -27,6 +27,38 @@ static float tm_bf16_to_f32(uint16_t b) { return f; } +/* Round f32 to IEEE-754 binary16 (round-to-nearest-even) for F16 fixtures. + * Covers normals, subnormals, zero, and overflow-to-inf - enough for the small + * in-range weight values tm_value produces. */ +static uint16_t tm_f32_to_f16(float x) { + uint32_t u; + memcpy(&u, &x, sizeof(u)); + uint32_t sign = (u >> 16) & 0x8000u; + int exp = (int)((u >> 23) & 0xffu) - 127; + uint32_t mant = u & 0x7fffffu; + if (((u >> 23) & 0xffu) == 0xffu) + return (uint16_t)(sign | 0x7c00u | (mant ? 0x200u : 0u)); /* inf/NaN */ + if (exp > 15) + return (uint16_t)(sign | 0x7c00u); /* overflow -> inf */ + if (exp >= -14) { + uint32_t h = ((uint32_t)(exp + 15) << 10) | (mant >> 13); + uint32_t rem = mant & 0x1fffu; + if (rem > 0x1000u || (rem == 0x1000u && (h & 1u))) + h++; /* round to nearest even; a carry into the exponent is correct */ + return (uint16_t)(sign | h); + } + if (exp < -25) + return (uint16_t)sign; /* underflow -> signed zero */ + mant |= 0x800000u; /* implicit leading 1 */ + int shift = -1 - exp; + uint32_t h = mant >> shift; + uint32_t rem = mant & ((1u << shift) - 1u); + uint32_t halfway = 1u << (shift - 1); + if (rem > halfway || (rem == halfway && (h & 1u))) + h++; + return (uint16_t)(sign | h); +} + /* Deterministic, BF16-exact value for element i of tensor `name`. */ static float tm_value(const char *name, size_t i) { float v; @@ -75,7 +107,7 @@ 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. */ + * dtype: "F32", "BF16", or "F16". Returns 0 on success. */ static int tm_write_safetensors_with_prefix_options(const char *path, const char *dtype, const tm_spec_t *specs, @@ -83,7 +115,8 @@ static int tm_write_safetensors_with_prefix_options(const char *path, const char *prefix, int zero_bias) { int bf16 = strcmp(dtype, "BF16") == 0; - size_t esize = bf16 ? 2 : 4; + int f16 = strcmp(dtype, "F16") == 0; + size_t esize = (bf16 || f16) ? 2 : 4; char header[4096]; size_t hoff = 0, doff = 0; @@ -122,8 +155,8 @@ static int tm_write_safetensors_with_prefix_options(const char *path, size_t n = (size_t)s->rows * (s->cols ? (size_t)s->cols : 1); for (size_t i = 0; i < n; i++) { float v = zero_bias && strstr(s->name, ".bias") ? 0.0f : tm_value(s->name, i); - if (bf16) { - uint16_t h = tm_f32_to_bf16(v); + if (bf16 || f16) { + uint16_t h = bf16 ? tm_f32_to_bf16(v) : tm_f32_to_f16(v); fwrite(&h, sizeof(h), 1, f); } else { fwrite(&v, sizeof(v), 1, f); |
