aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_bf16_model.c24
-rw-r--r--tests/test_safetensors.c133
-rw-r--r--tests/tiny_model.h129
3 files changed, 262 insertions, 24 deletions
diff --git a/tests/test_bf16_model.c b/tests/test_bf16_model.c
index 0f3101a..75d3309 100644
--- a/tests/test_bf16_model.c
+++ b/tests/test_bf16_model.c
@@ -1,21 +1,33 @@
/* tests/test_bf16_model.c - F32 vs BF16 loader/dispatch parity on a tiny
- * synthetic model. Runs via `make test`. */
+ * synthetic model. Hermetic: builds its own model dirs. Runs via
+ * `make test`. */
#include "embed.h"
+#include "tiny_model.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
-int main(int argc, char **argv) {
- if (argc != 3) {
- fprintf(stderr, "usage: %s f32_model bf16_model\n", argv[0]);
+int main(void) {
+ char root[1024], f32_dir[1088], bf16_dir[1088];
+ snprintf(root, sizeof(root), "%s/pplx-bf16-test-XXXXXX",
+ getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
+ if (!mkdtemp(root)) { perror("mkdtemp"); return 2; }
+ 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) {
+ fprintf(stderr, "fixture creation failed\n");
return 2;
}
int ids[] = {1, 2, 3, 4};
- pplx_model_t *mf32 = pplx_model_load(argv[1]);
- pplx_model_t *mbf16 = pplx_model_load(argv[2]);
+ pplx_model_t *mf32 = pplx_model_load(f32_dir);
+ pplx_model_t *mbf16 = pplx_model_load(bf16_dir);
if (!mf32 || !mbf16) {
fprintf(stderr, "model load failed\n");
return 1;
diff --git a/tests/test_safetensors.c b/tests/test_safetensors.c
index 42d8f21..401aab3 100644
--- a/tests/test_safetensors.c
+++ b/tests/test_safetensors.c
@@ -1,36 +1,133 @@
/* tests/test_safetensors.c - safetensors parser limit and rejection tests.
- * Runs via `make test`. Fixtures are synthesized in a temp directory. */
+ * Hermetic: synthesizes every fixture in a temp directory. Runs via
+ * `make test`. */
#include "qwen_safetensors.h"
+#include <stdint.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
-int main(int argc, char **argv) {
- if (argc != 4) {
- fprintf(stderr, "usage: %s file|multi path ok|fail\n", argv[0]);
- return 2;
- }
+static int failures = 0;
+static char tmproot[1024];
+
+/* Write u64-LE header length + JSON header + payload (safetensors layout). */
+static void write_st(const char *path, const char *header,
+ const void *payload, size_t payload_len)
+{
+ FILE *f = fopen(path, "wb");
+ if (!f) { perror(path); exit(2); }
+ uint64_t n = strlen(header);
+ unsigned char len8[8];
+ for (int i = 0; i < 8; i++) len8[i] = (unsigned char)(n >> (8 * i));
+ fwrite(len8, 1, 8, f);
+ fwrite(header, 1, n, f);
+ if (payload_len) fwrite(payload, 1, payload_len, f);
+ fclose(f);
+}
- int want_ok = strcmp(argv[3], "ok") == 0;
- int got_ok = 0;
- if (strcmp(argv[1], "file") == 0) {
- safetensors_file_t *sf = safetensors_open(argv[2]);
+static const char *path_in(const char *rel)
+{
+ static char buf[2048];
+ snprintf(buf, sizeof(buf), "%s/%s", tmproot, rel);
+ return buf;
+}
+
+static void check(const char *mode, const char *path, int want_ok)
+{
+ int got_ok;
+ if (strcmp(mode, "file") == 0) {
+ safetensors_file_t *sf = safetensors_open(path);
got_ok = sf != NULL;
safetensors_close(sf);
- } else if (strcmp(argv[1], "multi") == 0) {
- multi_safetensors_t *ms = multi_safetensors_open(argv[2]);
+ } else {
+ multi_safetensors_t *ms = multi_safetensors_open(path);
got_ok = ms != NULL;
multi_safetensors_close(ms);
- } else {
- return 2;
}
-
if (got_ok != want_ok) {
- fprintf(stderr, "%s %s: got %s want %s\n",
- argv[1], argv[2], got_ok ? "ok" : "fail",
- want_ok ? "ok" : "fail");
+ fprintf(stderr, "%s %s: got %s want %s\n", mode, path,
+ got_ok ? "ok" : "fail", want_ok ? "ok" : "fail");
+ failures++;
+ }
+}
+
+static const char ONE_TENSOR_A[] =
+ "{\"a\":{\"dtype\":\"F32\",\"shape\":[1],\"data_offsets\":[0,4]}}";
+static const char ONE_TENSOR_B[] =
+ "{\"b\":{\"dtype\":\"F32\",\"shape\":[1],\"data_offsets\":[0,4]}}";
+static const char ONE_TENSOR_X[] =
+ "{\"x\":{\"dtype\":\"F32\",\"shape\":[1],\"data_offsets\":[0,4]}}";
+
+int main(void)
+{
+ snprintf(tmproot, sizeof(tmproot), "%s/pplx-st-test-XXXXXX",
+ getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
+ if (!mkdtemp(tmproot)) { perror("mkdtemp"); return 2; }
+
+ static const char zeros[4 * 1025] = {0};
+
+ write_st(path_in("valid.safetensors"), ONE_TENSOR_X, zeros, 4);
+ check("file", path_in("valid.safetensors"), 1);
+
+ /* 1025 tensors exceeds the parser's tensor-count limit. */
+ {
+ size_t cap = 1025 * 64 + 16;
+ char *hdr = malloc(cap);
+ if (!hdr) return 2;
+ size_t off = 0;
+ off += (size_t)snprintf(hdr + off, cap - off, "{");
+ for (int i = 0; i < 1025; i++) {
+ off += (size_t)snprintf(hdr + off, cap - off,
+ "%s\"t%d\":{\"dtype\":\"F32\",\"shape\":[1],"
+ "\"data_offsets\":[%d,%d]}",
+ i ? "," : "", i, i * 4, i * 4 + 4);
+ }
+ snprintf(hdr + off, cap - off, "}");
+ write_st(path_in("too_many.safetensors"), hdr, zeros, sizeof(zeros));
+ free(hdr);
+ check("file", path_in("too_many.safetensors"), 0);
+ }
+
+ write_st(path_in("too_many_dims.safetensors"),
+ "{\"x\":{\"dtype\":\"F32\",\"shape\":[1,1,1,1,1,1,1,1,1],"
+ "\"data_offsets\":[0,4]}}", zeros, 4);
+ check("file", path_in("too_many_dims.safetensors"), 0);
+
+ write_st(path_in("bad_offsets.safetensors"),
+ "{\"x\":{\"dtype\":\"F32\",\"shape\":[1],"
+ "\"data_offsets\":[4,0]}}", zeros, 4);
+ check("file", path_in("bad_offsets.safetensors"), 0);
+
+ mkdir(path_in("valid_multi"), 0755);
+ write_st(path_in("valid_multi/model-00001-of-00002.safetensors"),
+ ONE_TENSOR_A, zeros, 4);
+ write_st(path_in("valid_multi/model-00002-of-00002.safetensors"),
+ ONE_TENSOR_B, zeros, 4);
+ check("multi", path_in("valid_multi"), 1);
+
+ mkdir(path_in("missing"), 0755);
+ write_st(path_in("missing/model-00001-of-00002.safetensors"),
+ ONE_TENSOR_X, zeros, 4);
+ check("multi", path_in("missing"), 0);
+
+ mkdir(path_in("too_many_shards"), 0755);
+ write_st(path_in("too_many_shards/model-00001-of-00009.safetensors"),
+ ONE_TENSOR_X, zeros, 4);
+ check("multi", path_in("too_many_shards"), 0);
+
+ mkdir(path_in("bad_name"), 0755);
+ write_st(path_in("bad_name/model-foo.safetensors"),
+ ONE_TENSOR_X, zeros, 4);
+ check("multi", path_in("bad_name"), 0);
+
+ if (failures) {
+ fprintf(stderr, "safetensors tests: %d failure(s)\n", failures);
return 1;
}
+ puts("ok: safetensors parser limit checks passed");
return 0;
}
diff --git a/tests/tiny_model.h b/tests/tiny_model.h
new file mode 100644
index 0000000..fc8cf48
--- /dev/null
+++ b/tests/tiny_model.h
@@ -0,0 +1,129 @@
+/* tests/tiny_model.h - synthesize a tiny 1-layer model directory (config.json
+ * + model.safetensors) for hermetic tests. Values are BF16-representable so an
+ * F32 and a BF16 copy of the same model produce identical embeddings. */
+
+#ifndef TINY_MODEL_H
+#define TINY_MODEL_H
+
+#include <math.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static uint16_t tm_f32_to_bf16(float x)
+{
+ uint32_t u;
+ memcpy(&u, &x, sizeof(u));
+ return (uint16_t)(u >> 16);
+}
+
+static float tm_bf16_to_f32(uint16_t b)
+{
+ uint32_t u = ((uint32_t)b) << 16;
+ float f;
+ memcpy(&f, &u, sizeof(f));
+ return f;
+}
+
+/* Deterministic, BF16-exact value for element i of tensor `name`. */
+static float tm_value(const char *name, size_t i)
+{
+ float v;
+ size_t len = strlen(name);
+ if ((len >= 11 && strcmp(name + len - 11, "norm.weight") == 0) ||
+ strstr(name, "layernorm.weight"))
+ v = 0.75f + 0.03f * (float)((i % 5) + 1);
+ else if (strcmp(name, "embed_tokens.weight") == 0)
+ v = sinf((float)i * 0.17f) * 0.2f;
+ else
+ v = sinf((float)i * 0.11f + (float)len) * 0.08f;
+ return tm_bf16_to_f32(tm_f32_to_bf16(v));
+}
+
+typedef struct {
+ const char *name;
+ int rows, cols; /* cols == 0 means a 1-D tensor of `rows` values */
+} tm_spec_t;
+
+static const tm_spec_t TM_SPECS[] = {
+ {"embed_tokens.weight", 16, 4},
+ {"layers.0.self_attn.q_proj.weight", 4, 4},
+ {"layers.0.self_attn.k_proj.weight", 2, 4},
+ {"layers.0.self_attn.v_proj.weight", 2, 4},
+ {"layers.0.self_attn.o_proj.weight", 4, 4},
+ {"layers.0.self_attn.q_norm.weight", 2, 0},
+ {"layers.0.self_attn.k_norm.weight", 2, 0},
+ {"layers.0.input_layernorm.weight", 4, 0},
+ {"layers.0.post_attention_layernorm.weight", 4, 0},
+ {"layers.0.mlp.gate_proj.weight", 8, 4},
+ {"layers.0.mlp.up_proj.weight", 8, 4},
+ {"layers.0.mlp.down_proj.weight", 4, 8},
+ {"norm.weight", 4, 0},
+};
+enum { TM_N_SPECS = sizeof(TM_SPECS) / sizeof(TM_SPECS[0]) };
+
+/* Write config.json + model.safetensors into dir. dtype: "F32" or "BF16".
+ * Returns 0 on success. */
+static int tm_write_model(const char *dir, const char *dtype)
+{
+ char path[2048];
+ snprintf(path, sizeof(path), "%s/config.json", dir);
+ FILE *f = fopen(path, "w");
+ if (!f) return -1;
+ fputs("{\"hidden_size\":4,\"num_hidden_layers\":1,"
+ "\"num_attention_heads\":2,\"num_key_value_heads\":1,"
+ "\"head_dim\":2,\"intermediate_size\":8,\"vocab_size\":16,"
+ "\"rms_norm_eps\":1e-6,\"rope_theta\":10000.0}", f);
+ fclose(f);
+
+ int bf16 = strcmp(dtype, "BF16") == 0;
+ size_t esize = bf16 ? 2 : 4;
+
+ char header[4096];
+ size_t hoff = 0, doff = 0;
+ hoff += (size_t)snprintf(header + hoff, sizeof(header) - hoff, "{");
+ for (int t = 0; t < TM_N_SPECS; t++) {
+ const tm_spec_t *s = &TM_SPECS[t];
+ 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);
+ else
+ hoff += (size_t)snprintf(header + hoff, sizeof(header) - hoff,
+ "%s\"%s\":{\"dtype\":\"%s\",\"shape\":[%d],"
+ "\"data_offsets\":[%zu,%zu]}",
+ t ? "," : "", s->name, dtype, s->rows, doff, end);
+ doff = end;
+ }
+ hoff += (size_t)snprintf(header + hoff, sizeof(header) - hoff, "}");
+ if (hoff >= sizeof(header) - 1) return -1;
+
+ snprintf(path, sizeof(path), "%s/model.safetensors", dir);
+ f = fopen(path, "wb");
+ if (!f) return -1;
+ unsigned char len8[8];
+ for (int i = 0; i < 8; i++) len8[i] = (unsigned char)(hoff >> (8 * i));
+ fwrite(len8, 1, 8, f);
+ fwrite(header, 1, hoff, f);
+ for (int t = 0; t < TM_N_SPECS; t++) {
+ const tm_spec_t *s = &TM_SPECS[t];
+ size_t n = (size_t)s->rows * (s->cols ? (size_t)s->cols : 1);
+ for (size_t i = 0; i < n; i++) {
+ float v = tm_value(s->name, i);
+ if (bf16) {
+ uint16_t h = tm_f32_to_bf16(v);
+ fwrite(&h, sizeof(h), 1, f);
+ } else {
+ fwrite(&v, sizeof(v), 1, f);
+ }
+ }
+ }
+ fclose(f);
+ return 0;
+}
+
+#endif /* TINY_MODEL_H */