From c4a1c97a09da6d8d88c2ac834bf6234506fc41bc Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Mon, 15 Jun 2026 08:40:11 +0200 Subject: chore: run clang-format --- tests/tiny_model.h | 97 +++++++++++++++++++++++++----------------------------- 1 file changed, 45 insertions(+), 52 deletions(-) (limited to 'tests/tiny_model.h') diff --git a/tests/tiny_model.h b/tests/tiny_model.h index d4ae431..42542c1 100644 --- a/tests/tiny_model.h +++ b/tests/tiny_model.h @@ -14,15 +14,13 @@ #include #include -static uint16_t tm_f32_to_bf16(float x) -{ +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) -{ +static float tm_bf16_to_f32(uint16_t b) { uint32_t u = ((uint32_t)b) << 16; float f; memcpy(&f, &u, sizeof(f)); @@ -30,8 +28,7 @@ static float tm_bf16_to_f32(uint16_t b) } /* Deterministic, BF16-exact value for element i of tensor `name`. */ -static float tm_value(const char *name, size_t i) -{ +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) || @@ -46,7 +43,7 @@ static float tm_value(const char *name, size_t i) typedef struct { const char *name; - int rows, cols; /* cols == 0 means a 1-D tensor of `rows` values */ + int rows, cols; /* cols == 0 means a 1-D tensor of `rows` values */ } tm_spec_t; /* Always one layer; everything else is free so a test can match a real @@ -57,22 +54,21 @@ typedef struct { enum { TM_N_SPECS = 13 }; -static int tm_write_config(const char *dir, const tm_dims_t *d, - const char *model_type, int eos_token_id) -{ +static int +tm_write_config(const char *dir, const tm_dims_t *d, const char *model_type, int eos_token_id) { char path[2048]; snprintf(path, sizeof(path), "%s/config.json", dir); FILE *f = fopen(path, "w"); - if (!f) return -1; - fprintf(f, "{\"hidden_size\":%d,\"num_hidden_layers\":1," + if (!f) + return -1; + fprintf(f, + "{\"hidden_size\":%d,\"num_hidden_layers\":1," "\"num_attention_heads\":%d,\"num_key_value_heads\":%d," "\"head_dim\":%d,\"intermediate_size\":%d,\"vocab_size\":%d," "\"rms_norm_eps\":1e-6,\"rope_theta\":10000.0", - d->hidden, d->heads, d->kv_heads, d->head_dim, - d->intermediate, d->vocab); + d->hidden, d->heads, d->kv_heads, d->head_dim, d->intermediate, d->vocab); if (model_type) - fprintf(f, ",\"model_type\":\"%s\",\"eos_token_id\":%d", - model_type, eos_token_id); + fprintf(f, ",\"model_type\":\"%s\",\"eos_token_id\":%d", model_type, eos_token_id); fputs("}", f); fclose(f); return 0; @@ -80,9 +76,8 @@ static int tm_write_config(const char *dir, const tm_dims_t *d, /* Write a safetensors file holding `specs` with tm_value() contents. * dtype: "F32" or "BF16". Returns 0 on success. */ -static int tm_write_safetensors(const char *path, const char *dtype, - const tm_spec_t *specs, int n_specs) -{ +static int +tm_write_safetensors(const char *path, const char *dtype, const tm_spec_t *specs, int n_specs) { int bf16 = strcmp(dtype, "BF16") == 0; size_t esize = bf16 ? 2 : 4; @@ -95,23 +90,26 @@ static int tm_write_safetensors(const char *path, const char *dtype, 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); + "%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); + "%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; + if (hoff >= sizeof(header) - 1) + return -1; FILE *f = fopen(path, "wb"); - if (!f) return -1; + if (!f) + return -1; unsigned char len8[8]; - for (int i = 0; i < 8; i++) len8[i] = (unsigned char)(hoff >> (8 * i)); + 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 < n_specs; t++) { @@ -133,9 +131,7 @@ static int tm_write_safetensors(const char *path, const char *dtype, /* Write config.json + model.safetensors with the given dimensions into dir. * dtype: "F32" or "BF16". Returns 0 on success. */ -static int tm_write_model_dims(const char *dir, const char *dtype, - const tm_dims_t *d) -{ +static int tm_write_model_dims(const char *dir, const char *dtype, const tm_dims_t *d) { int q = d->heads * d->head_dim, kv = d->kv_heads * d->head_dim; const tm_spec_t specs[TM_N_SPECS] = { {"embed_tokens.weight", d->vocab, d->hidden}, @@ -154,37 +150,37 @@ static int tm_write_model_dims(const char *dir, const char *dtype, }; char path[2048]; - if (tm_write_config(dir, d, NULL, -1) != 0) return -1; + if (tm_write_config(dir, d, NULL, -1) != 0) + return -1; snprintf(path, sizeof(path), "%s/model.safetensors", dir); return tm_write_safetensors(path, dtype, specs, TM_N_SPECS); } -static inline int tm_write_qwen3_model_dims(const char *dir, const char *dtype, +static inline int tm_write_qwen3_model_dims(const char *dir, + const char *dtype, const tm_dims_t *d, - int eos_token_id) -{ - if (tm_write_model_dims(dir, dtype, d) != 0) return -1; + int eos_token_id) { + if (tm_write_model_dims(dir, dtype, d) != 0) + return -1; return tm_write_config(dir, d, "qwen3", eos_token_id); } /* Add the 1_Dense per-token projection head that turns a base model dir into * a late-interaction snapshot. weight_name is "linear.weight" for a valid * head; pass another name to synthesize a malformed head. */ -static inline int tm_write_late_projection_named(const char *dir, - const char *dtype, - int token_dim, int hidden, - const char *weight_name) -{ +static inline int tm_write_late_projection_named( + const char *dir, const char *dtype, int token_dim, int hidden, const char *weight_name) { char path[2048]; snprintf(path, sizeof(path), "%s/1_Dense", dir); - if (mkdir(path, 0755) != 0 && errno != EEXIST) return -1; + if (mkdir(path, 0755) != 0 && errno != EEXIST) + return -1; snprintf(path, sizeof(path), "%s/1_Dense/config.json", dir); FILE *f = fopen(path, "w"); - if (!f) return -1; - fprintf(f, "{\"in_features\":%d,\"out_features\":%d,\"bias\":false}", - hidden, token_dim); + if (!f) + return -1; + fprintf(f, "{\"in_features\":%d,\"out_features\":%d,\"bias\":false}", hidden, token_dim); fclose(f); const tm_spec_t spec = {weight_name, token_dim, hidden}; @@ -192,16 +188,13 @@ static inline int tm_write_late_projection_named(const char *dir, return tm_write_safetensors(path, dtype, &spec, 1); } -static inline int tm_write_late_projection(const char *dir, const char *dtype, - int token_dim, int hidden) -{ - return tm_write_late_projection_named(dir, dtype, token_dim, hidden, - "linear.weight"); +static inline int +tm_write_late_projection(const char *dir, const char *dtype, int token_dim, int hidden) { + return tm_write_late_projection_named(dir, dtype, token_dim, hidden, "linear.weight"); } /* The historical default fixture: 4-dim hidden, 16-token vocab. */ -static inline int tm_write_model(const char *dir, const char *dtype) -{ +static inline int tm_write_model(const char *dir, const char *dtype) { static const tm_dims_t d = {4, 2, 1, 2, 8, 16}; return tm_write_model_dims(dir, dtype, &d); } -- cgit v1.3.1