aboutsummaryrefslogtreecommitdiff
path: root/tests/test_safetensors.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_safetensors.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_safetensors.c')
-rw-r--r--tests/test_safetensors.c51
1 files changed, 50 insertions, 1 deletions
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;
}