Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
st.h: a missing core tensor is diagnosed through the checkpoint's index
JustVugg88082cc3622a
diff --git a/c/Makefile b/c/Makefileindex 63d0865e..ba136a48 100644--- a/c/Makefile+++ b/c/Makefile@@ -1236,6 +1236,9 @@ tests/test_st_mirror$(EXE): tests/test_st_mirror.c st.h json.h compat.htests/test_st_overlay$(EXE): tests/test_st_overlay.c st.h json.h compat.h$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)+tests/test_st_missing$(EXE): tests/test_st_missing.c st.h json.h compat.h+ $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)+tests/test_tier$(EXE): tests/test_tier.c tier.h$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)diff --git a/c/st.h b/c/st.hindex 7fadfd37..dd6939bf 100644--- a/c/st.h+++ b/c/st.h@@ -835,6 +835,52 @@ static void st_die_missing(shards *S, const char *name) {else if (numbered > 0)fprintf(stderr, " shards numbered %05d..%05d, %d file(s)%s\n", lo, hi, numbered,numbered == hi - lo + 1 ? " (contiguous: a gap would be at the tail)" : " (GAPS in the numbering)");+ /* The checkpoint's own index says more than a file count can: which shard+ * should hold this name, whether that shard is here, and whether the name+ * exists in this checkpoint at all. The last case is the one a file count+ * gets exactly wrong: every shard present, and the tensor never existed,+ * because the engine running here is not this checkpoint's engine (a+ * directory copied without config.json, and the GLM default looking for+ * model.embed_tokens.weight in a Qwen3.8 that stores it under+ * model.language_model). */+ {+ char dir[1200]; snprintf(dir, sizeof dir, "%s", S->paths[0]);+ char *slash = strrchr(dir, '/');+#ifdef _WIN32+ char *bs = strrchr(dir, '\\'); if (bs && (!slash || bs > slash)) slash = bs;+#endif+ if (slash) *slash = 0; else snprintf(dir, sizeof dir, ".");+ st_index ix; memset(&ix, 0, sizeof ix);+ st_index_load(&ix, dir);+ if (ix.map) {+ const char *auth = st_index_shard(&ix, name);+ if (!auth) {+ fprintf(stderr,+ "\n model.safetensors.index.json does not list '%s': this checkpoint never had a\n"+ " tensor by that name. The engine running here expects one, so it is not this\n"+ " checkpoint's engine. coli picks the engine from config.json -- run\n"+ " `coli info --model <dir>` and check the family it names; without a config.json\n"+ " there is no family, and the files to copy next to the shards are config.json,\n"+ " tokenizer.json and model.safetensors.index.json from the model repo.\n", name);+ } else {+ int present = 0;+ for (int i = 0; i < S->nfd && !present; i++) present = !strcmp(st_basename(S->paths[i]), auth);+ if (!present)+ fprintf(stderr,+ "\n model.safetensors.index.json maps '%s' to %s, which is not in this\n"+ " directory: that shard is MISSING. Fetch just that file:\n"+ " hf download <repo> %s --local-dir <model-dir>\n", name, auth, auth);+ else+ fprintf(stderr,+ "\n model.safetensors.index.json maps '%s' to %s, which is here but whose\n"+ " header does not declare it: the file is not the one the index describes (an\n"+ " interrupted transfer that kept the name, or a shard from another revision of\n"+ " the repo). Re-download that one shard and compare its size with the repo's.\n", name, auth);+ }+ st_index_free(&ix);+ exit(1);+ }+ }/* Follow the evidence: telling someone who already has every declared shard to* re-download sends them round a loop that cannot help them. */if (complete) {diff --git a/c/tests/test_st_missing.c b/c/tests/test_st_missing.cnew file mode 100644index 00000000..5d2adc88--- /dev/null+++ b/c/tests/test_st_missing.c@@ -0,0 +1,88 @@+/* st_die_missing: with model.safetensors.index.json present, the message says+ * which of three things happened -- the name is not in this checkpoint at all+ * (wrong engine for the directory), the shard the index maps it to is absent,+ * or that shard is here but does not declare the tensor (bad copy). Without+ * an index the historical file-count wording stands. POSIX only: the function+ * exits, so each case runs in a forked child whose stderr is captured. */+#include <stdio.h>+#include <stdint.h>+#include <string.h>+#include <stdlib.h>+#ifdef _WIN32+int main(void) { puts("test_st_missing: skipped on Windows (fork)"); return 0; }+#else+#include <sys/stat.h>+#include <sys/wait.h>+#include <unistd.h>+#include "../st.h"++#define CHECK(c) do { if (!(c)) { fprintf(stderr, "%s:%d: check failed: %s\n", __FILE__, __LINE__, #c); return 1; } } while (0)+static int write_shard(const char *dir, const char *file, const char *name) {+ char hdr[256], path[512];+ int hl = snprintf(hdr, sizeof hdr, "{\"%s\":{\"dtype\":\"F32\",\"shape\":[8],\"data_offsets\":[0,32]}}", name);+ snprintf(path, sizeof path, "%s/%s", dir, file);+ FILE *f = fopen(path, "wb"); if (!f) return -1;+ uint64_t n = (uint64_t)hl; fwrite(&n, 8, 1, f); fwrite(hdr, 1, (size_t)hl, f);+ float d[8] = {0}; fwrite(d, 4, 8, f); fclose(f); return 0;+}+static int put(const char *dir, const char *file, const char *text) {+ char p[512]; snprintf(p, sizeof p, "%s/%s", dir, file);+ FILE *f = fopen(p, "wb"); if (!f) return -1; fputs(text, f); fclose(f); return 0;+}+static void wipe(const char *dir) {+ const char *names[] = {"model-00001-of-00002.safetensors", "model-00002-of-00002.safetensors", "model.safetensors.index.json"};+ for (int i = 0; i < 3; i++) { char p[512]; snprintf(p, sizeof p, "%s/%s", dir, names[i]); remove(p); }+ rmdir(dir);+}+/* run st_init + st_read_f32(name) in a child; return its stderr in buf, its exit code */+static int run_case(const char *dir, const char *name, char *buf, size_t cap) {+ int pfd[2]; if (pipe(pfd)) return -1;+ fflush(stdout); fflush(stderr);+ pid_t pid = fork(); if (pid < 0) return -1;+ if (pid == 0) {+ close(pfd[0]); dup2(pfd[1], 2); close(pfd[1]);+ shards S; memset(&S, 0, sizeof S); st_init(&S, dir);+ float v[8]; st_read_f32(&S, name, v, 0);+ _exit(0);+ }+ close(pfd[1]); size_t got = 0; ssize_t r;+ while (got + 1 < cap && (r = read(pfd[0], buf + got, cap - 1 - got)) > 0) got += (size_t)r;+ buf[got] = 0; close(pfd[0]);+ int status = 0; waitpid(pid, &status, 0);+ return WIFEXITED(status) ? WEXITSTATUS(status) : -1;+}++int main(void) {+ const char *D = "tmp_st_missing"; char err[8192];+ wipe(D); CHECK(mkdir(D, 0755) == 0);+ CHECK(write_shard(D, "model-00001-of-00002.safetensors", "a") == 0);+ CHECK(write_shard(D, "model-00002-of-00002.safetensors", "b") == 0);++ /* 1. no index: the file-count wording, "complete" branch */+ CHECK(run_case(D, "model.embed_tokens.weight", err, sizeof err) == 1);+ CHECK(strstr(err, "NOT the usual truncated download") != NULL);+ CHECK(strstr(err, "model.safetensors.index.json") == NULL);++ /* 2. index present, name absent from it: wrong engine for this checkpoint */+ CHECK(put(D, "model.safetensors.index.json", "{\"weight_map\":{\"a\":\"model-00001-of-00002.safetensors\",\"b\":\"model-00002-of-00002.safetensors\"}}") == 0);+ CHECK(run_case(D, "model.embed_tokens.weight", err, sizeof err) == 1);+ CHECK(strstr(err, "never had a") != NULL && strstr(err, "not this") != NULL);+ CHECK(strstr(err, "coli info --model") != NULL);++ /* 3. index maps the name to a shard that is not here */+ CHECK(put(D, "model.safetensors.index.json", "{\"weight_map\":{\"a\":\"model-00001-of-00002.safetensors\",\"c\":\"model-00003-of-00003.safetensors\"}}") == 0);+ CHECK(run_case(D, "c", err, sizeof err) == 1);+ CHECK(strstr(err, "MISSING") != NULL && strstr(err, "model-00003-of-00003.safetensors") != NULL);++ /* 4. index maps the name to a shard that is here but does not declare it */+ CHECK(put(D, "model.safetensors.index.json", "{\"weight_map\":{\"a\":\"model-00001-of-00002.safetensors\",\"c\":\"model-00002-of-00002.safetensors\"}}") == 0);+ CHECK(run_case(D, "c", err, sizeof err) == 1);+ CHECK(strstr(err, "header does not declare it") != NULL);++ /* and a present tensor still reads fine with the index around */+ { shards S; memset(&S, 0, sizeof S); st_init(&S, D); float v[8]; CHECK(st_read_f32(&S, "a", v, 0) == 8); st_destroy(&S); }+ wipe(D);+ puts("st_die_missing diagnosis tests: ok");+ return 0;+}+#endif