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 | |
| parent | b4343d6ac1c6cc7a7d228f954220def2f6e6ed67 (diff) | |
| download | ffwd-e00becb643b3a99de16462b3a032717a93b983d9.tar.gz ffwd-e00becb643b3a99de16462b3a032717a93b983d9.zip | |
all: accept F16 safetensors weights (widen to F32 at load)
| -rw-r--r-- | src/dtype.h | 36 | ||||
| -rw-r--r-- | src/internal.h | 4 | ||||
| -rw-r--r-- | src/late.c | 10 | ||||
| -rw-r--r-- | src/mlx.c | 22 | ||||
| -rw-r--r-- | src/model.c | 51 | ||||
| -rw-r--r-- | src/model_internal.h | 9 | ||||
| -rw-r--r-- | src/safetensors.c | 6 | ||||
| -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 |
11 files changed, 283 insertions, 45 deletions
diff --git a/src/dtype.h b/src/dtype.h index 5c1ce8f..2a25cae 100644 --- a/src/dtype.h +++ b/src/dtype.h @@ -16,4 +16,40 @@ static inline float embed_bf16_to_f32(uint16_t bf16) { return f; } +/* IEEE-754 binary16 -> binary32. Unlike BF16, F16 has a 5-bit exponent and a + * 10-bit mantissa, so widening is a field re-pack, not a shift: rebias the + * exponent (15 -> 127), shift the mantissa up by 13, and special-case zeros, + * subnormals, and inf/NaN. Used by the loaders to accept F16 safetensors + * snapshots (e.g. the public multilingual-e5 weights); there is no fused F16 + * kernel, so weights are widened to F32 once at load. */ +static inline float embed_f16_to_f32(uint16_t f16) { + uint32_t sign = (uint32_t)(f16 & 0x8000u) << 16; + uint32_t exp = (f16 >> 10) & 0x1fu; + uint32_t mant = f16 & 0x3ffu; + uint32_t bits; + if (exp == 0) { + if (mant == 0) { + bits = sign; /* signed zero */ + } else { + /* Subnormal half: renormalize into a normal float. Shift the + * mantissa left until its leading 1 reaches the hidden-bit + * position, decrementing the exponent for each shift. */ + exp = 127 - 15 + 1; + while ((mant & 0x400u) == 0) { + mant <<= 1; + exp--; + } + mant &= 0x3ffu; + bits = sign | (exp << 23) | (mant << 13); + } + } else if (exp == 0x1fu) { + bits = sign | 0x7f800000u | (mant << 13); /* inf or NaN */ + } else { + bits = sign | ((exp + (127 - 15)) << 23) | (mant << 13); + } + float f; + memcpy(&f, &bits, sizeof(f)); + return f; +} + #endif /* EMBED_DTYPE_H */ diff --git a/src/internal.h b/src/internal.h index b3c2aa0..c1429de 100644 --- a/src/internal.h +++ b/src/internal.h @@ -7,6 +7,10 @@ typedef struct { safetensor_dtype_t dtype; const void *data; + /* 0: data is a borrowed view into the safetensors mmap (F32/BF16, the common + * case). 1: data is a heap buffer owned by the model and freed on unload. + * Set when an F16 tensor is widened to F32 at load (no fused F16 kernel). */ + int owned; } embed_weight_ref_t; typedef struct { @@ -96,8 +96,10 @@ embed_late_model_t *embed_late_model_load(const char *model_dir) { if (!tensor_has_supported_shape(late->dense_sf, t, "linear.weight", shape, 2, 1)) goto fail; - late->projection.dtype = t->dtype; - late->projection.data = safetensors_data(late->dense_sf, t); + /* Same F32/BF16-borrow, F16-widen contract as the backbone weights. */ + late->projection = weight_ref_from_tensor(late->dense_sf, t); + if (!late->projection.data) + goto fail; late->token_dim = (int)t->shape[0]; return late; @@ -110,6 +112,10 @@ void embed_late_model_free(embed_late_model_t *model) { if (!model) return; embed_model_free(model->base); + /* Free the projection before the mmap: only an F16-widened ref owns its + * buffer; a borrowed F32/BF16 ref points into dense_sf and must not be freed. */ + if (model->projection.owned) + free((void *)model->projection.data); safetensors_close(model->dense_sf); free(model); } @@ -1,5 +1,6 @@ #include "mlx.h" #include "config.h" +#include "dtype.h" #include "embed.h" #include "safetensors.h" @@ -88,6 +89,7 @@ static size_t safetensor_dtype_size(safetensor_dtype_t dtype) { case DTYPE_F32: return sizeof(float); case DTYPE_BF16: + case DTYPE_F16: return sizeof(uint16_t); default: return 0; @@ -101,7 +103,7 @@ static int mlx_tensor_has_supported_shape(const safetensors_file_t *sf, int ndim) { size_t elem_size = safetensor_dtype_size(t->dtype); if (elem_size == 0) { - fprintf(stderr, "mlx: expected F32 or BF16 for %s\n", name); + fprintf(stderr, "mlx: expected F32, BF16, or F16 for %s\n", name); return 0; } if (t->ndim != ndim) { @@ -156,13 +158,29 @@ load_tensor(multi_safetensors_t *ms, const char *name, const int *shape, int ndi } if (!mlx_tensor_has_supported_shape(sf, t, name, shape, ndim)) return (mlx_array){0}; + if (t->dtype == DTYPE_F16) { + /* No F16 forward path on MLX (the matmul fast path keys on BF16). Widen + * to F32 on the host at load so the whole forward runs in F32, mirroring + * the CPU loader. mlx_array_new_data copies its buffer, so the temporary + * is freed immediately. Public F16 snapshots load through here. */ + int64_t n = safetensor_numel(t); + float *tmp = (float *)malloc((size_t)n * sizeof(float)); + if (!tmp) + return (mlx_array){0}; + const uint16_t *src = (const uint16_t *)safetensors_data(sf, t); + for (int64_t i = 0; i < n; i++) + tmp[i] = embed_f16_to_f32(src[i]); + mlx_array a = mlx_array_new_data(tmp, shape, ndim, MLX_FLOAT32); + free(tmp); + return a; + } mlx_dtype dtype; if (t->dtype == DTYPE_F32) { dtype = MLX_FLOAT32; } else if (t->dtype == DTYPE_BF16) { dtype = MLX_BFLOAT16; } else { - fprintf(stderr, "mlx: expected F32 or BF16 for %s\n", name); + fprintf(stderr, "mlx: expected F32, BF16, or F16 for %s\n", name); return (mlx_array){0}; } return mlx_array_new_data(safetensors_data(sf, t), shape, ndim, dtype); diff --git a/src/model.c b/src/model.c index 2956d7f..c39c526 100644 --- a/src/model.c +++ b/src/model.c @@ -45,6 +45,7 @@ static size_t dtype_size(safetensor_dtype_t dtype) { case DTYPE_F32: return sizeof(float); case DTYPE_BF16: + case DTYPE_F16: return sizeof(uint16_t); default: return 0; @@ -76,9 +77,12 @@ int tensor_has_supported_shape(const safetensors_file_t *sf, const int64_t *shape, int ndim, int bf16_ok) { - if (t->dtype != DTYPE_F32 && !(bf16_ok && t->dtype == DTYPE_BF16)) { + /* bf16_ok admits either 16-bit float format (BF16 or F16); both are widened + * to F32 by the caller (BF16 lazily per matmul, F16 once at load). */ + int sixteen_ok = bf16_ok && (t->dtype == DTYPE_BF16 || t->dtype == DTYPE_F16); + if (t->dtype != DTYPE_F32 && !sixteen_ok) { fprintf(stderr, "embed: unsupported dtype for %s: got %s, expected %s\n", name, - dtype_name(t->dtype), bf16_ok ? "F32 or BF16" : "F32"); + dtype_name(t->dtype), bf16_ok ? "F32, BF16, or F16" : "F32"); return 0; } if (t->ndim != ndim) { @@ -128,9 +132,29 @@ int tensor_has_supported_shape(const safetensors_file_t *sf, return 1; } +embed_weight_ref_t weight_ref_from_tensor(const safetensors_file_t *sf, const safetensor_t *t) { + embed_weight_ref_t ref = {DTYPE_UNKNOWN, NULL, 0}; + /* F32/BF16 are consumed directly by the forward pass, so the ref borrows the + * mmap (zero copy; BF16 is widened lazily per matmul). F16 has neither a + * fused kernel nor a zero-copy F32 view, so widen it once here into an owned + * buffer the model frees on unload. DTYPE_F16 never reaches the forward. */ + if (t->dtype == DTYPE_F16) { + float *buf = safetensors_get_f32(sf, t); + if (!buf) + return ref; + ref.dtype = DTYPE_F32; + ref.data = buf; + ref.owned = 1; + } else { + ref.dtype = t->dtype; + ref.data = safetensors_data(sf, t); + } + return ref; +} + static embed_weight_ref_t load_weight_direct(multi_safetensors_t *ms, const char *name, const int64_t *shape, int ndim) { - embed_weight_ref_t ref = {DTYPE_UNKNOWN, NULL}; + embed_weight_ref_t ref = {DTYPE_UNKNOWN, NULL, 0}; safetensors_file_t *sf = NULL; const safetensor_t *t = multi_safetensors_find(ms, name, &sf); if (!t) { @@ -140,9 +164,7 @@ load_weight_direct(multi_safetensors_t *ms, const char *name, const int64_t *sha if (!tensor_has_supported_shape(sf, t, name, shape, ndim, 1)) return ref; - ref.dtype = t->dtype; - ref.data = safetensors_data(sf, t); - return ref; + return weight_ref_from_tensor(sf, t); } static float * @@ -414,12 +436,28 @@ embed_model_t *embed_model_load(const char *model_dir) { return model_load_range_ex(model_dir, 0, -1, 0); } +/* Release a weight ref's backing store. Only F16-widened refs own their buffer; + * borrowed F32/BF16 views into the mmap are a no-op (see weight_ref_from_tensor). */ +static void free_weight_ref(embed_weight_ref_t *w) { + if (w->owned) + free((void *)w->data); + w->data = NULL; + w->owned = 0; +} + void embed_model_free(embed_model_t *model) { if (!model) return; if (model->weights.layers) { for (int i = 0; i < model->config.n_layers; i++) { embed_layer_t *l = &model->weights.layers[i]; + free_weight_ref(&l->wq); + free_weight_ref(&l->wk); + free_weight_ref(&l->wv); + free_weight_ref(&l->wo); + free_weight_ref(&l->gate_proj); + free_weight_ref(&l->up_proj); + free_weight_ref(&l->down_proj); free((void *)l->q_bias); free((void *)l->k_bias); free((void *)l->v_bias); @@ -434,6 +472,7 @@ void embed_model_free(embed_model_t *model) { free((void *)l->ffn_ln_bias); } } + free_weight_ref(&model->weights.embed_tokens); free((void *)model->weights.norm); free((void *)model->weights.position_embeddings); free((void *)model->weights.token_type_embeddings); diff --git a/src/model_internal.h b/src/model_internal.h index f61b069..acbba07 100644 --- a/src/model_internal.h +++ b/src/model_internal.h @@ -12,7 +12,8 @@ * file. The frontends and the GPU backends do not include this header. */ /* Validate that a safetensor matches an expected dtype/rank/shape and that its - * bytes lie within the mapped file. bf16_ok permits BF16 in addition to F32. */ + * bytes lie within the mapped file. bf16_ok permits either 16-bit float format + * (BF16 or F16) in addition to F32. */ int tensor_has_supported_shape(const safetensors_file_t *sf, const safetensor_t *t, const char *name, @@ -20,6 +21,12 @@ int tensor_has_supported_shape(const safetensors_file_t *sf, int ndim, int bf16_ok); +/* Build a weight ref from a located tensor: F32/BF16 borrow the mmap, F16 is + * widened to an owned F32 buffer (no fused F16 kernel). Shared by the model + * loader and the late-interaction projection loader; the owner frees ref.data + * when ref.owned is set. Returns ref.data == NULL on allocation failure. */ +embed_weight_ref_t weight_ref_from_tensor(const safetensors_file_t *sf, const safetensor_t *t); + /* True if model_dir carries a 1_Dense projection (a late-interaction model). */ int model_dir_has_late_projection(const char *model_dir); diff --git a/src/safetensors.c b/src/safetensors.c index a7c567f..65d0d7c 100644 --- a/src/safetensors.c +++ b/src/safetensors.c @@ -364,6 +364,12 @@ float *safetensors_get_f32(const safetensors_file_t *sf, const safetensor_t *t) out[i] = embed_bf16_to_f32(src[i]); break; } + case DTYPE_F16: { + const uint16_t *src = (const uint16_t *)data; + for (int64_t i = 0; i < n; i++) + out[i] = embed_f16_to_f32(src[i]); + break; + } default: free(out); return NULL; 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); |
