Run frontier MoE models on hardware you already own β pure C, zero deps, experts streamed from disk. Tiny engine, immense model. π¦
C
fix(loader): a missing core tensor explains itself on every fatal path (#1318)
Vincenzo Fornaroeccfb69e4d33
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 0d3f41e6..c8165b98 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -780,7 +780,7 @@ jobs:# rieseguiva un binario non strumentato. Uno di quei test era rosso da# settimane senza che nessuno potesse vederlo.- name: Build the engines those tests exercise- run: make -C c kimi_k3 inkling+ run: make -C c kimi_k3 inkling colibri- name: Python test suiterun: cd c && python3 -m unittest discover -s tests -p 'test_*.py'diff --git a/c/deepseek_v4.c b/c/deepseek_v4.cindex ddc670bc..1c458f32 100644--- a/c/deepseek_v4.c+++ b/c/deepseek_v4.c@@ -8397,6 +8397,15 @@ static int lookup_hot(ColiExpertStore *store, ColiExpertKey key,state->eheat[expert_index]++;}rt_count(key.layer, &key.expert, 1); /* selection history, shared format (#700) */+ /* ESPERIMENTO LOCALE (replay policy cache): sequenza ordinata delle+ * richieste come le vede la cache, hit e miss. V4_REPLAY_TRACE=<path>. */+ {+ static FILE *replay_fp; static int replay_init;+ if (!replay_init) { replay_init = 1;+ const char *rp = getenv("V4_REPLAY_TRACE");+ if (rp) replay_fp = fopen(rp, "w"); }+ if (replay_fp) fprintf(replay_fp, "%d %d\n", key.layer, key.expert);+ }uint64_t layer_requests = ++policy->layer_requests[key.layer];if (policy->repin_interval &&layer_requests % policy->repin_interval == 0)diff --git a/c/st.h b/c/st.hindex 9f6a55f3..76987ada 100644--- a/c/st.h+++ b/c/st.h@@ -792,7 +792,7 @@ static void st_prefetch_rep(shards *S, const char *name, int rep) {* drop=1 -> consiglia al kernel di scartare le pagine (per gli expert in streaming). */static int64_t st_read_f32(shards *S, const char *name, float *out, int drop) {st_tensor *t = st_find(S, name);- if (!t) { fprintf(stderr, "missing tensor: %s\n", name); exit(1); }+ if (!t) st_die_missing(S, name); /* #1317: la riga nuda esisteva gia' spiegata *//* SEC: numel viene dallo shape, nbytes dagli offset β due campi indipendenti* del file. Se non concordano, la memcpy F32 (nbytes) o i loop BF16/F16* (numel elementi da un raw di soli nbytes) sforano il buffer del chiamante,@@ -832,7 +832,7 @@ static int64_t st_read_f32(shards *S, const char *name, float *out, int drop) {* self-consistent and may keep using st_read_f32. */static int64_t st_read_f32_cap(shards *S, const char *name, float *out, int64_t cap, int drop) {st_tensor *t = st_find(S, name);- if (!t) { fprintf(stderr, "missing tensor: %s\n", name); exit(1); }+ if (!t) st_die_missing(S, name); /* #1317: la riga nuda esisteva gia' spiegata */if (t->numel < 0 || t->numel > cap) {fprintf(stderr, "tensor %s: numel %lld exceeds destination capacity %lld\n",name, (long long)t->numel, (long long)cap); exit(1); }@@ -883,7 +883,7 @@ static inline float ue8m0_to_f32(uint8_t v) {* che il chiamante ha allocato, come in st_read_f32_cap. */static int64_t st_read_scale_f32(shards *S, const char *name, float *out, int64_t cap, int drop) {st_tensor *t = st_find(S, name);- if (!t) { fprintf(stderr, "missing tensor: %s\n", name); exit(1); }+ if (!t) st_die_missing(S, name); /* #1317: la riga nuda esisteva gia' spiegata */if (t->numel < 0 || t->numel > cap) {fprintf(stderr, "scale %s: numel %lld exceeds destination capacity %lld\n",name, (long long)t->numel, (long long)cap); exit(1); }@@ -920,7 +920,7 @@ static int64_t st_read_scale_f32(shards *S, const char *name, float *out, int64_* A new caller with none of those wants st_read_raw_cap below. */static void st_read_raw(shards *S, const char *name, void *out, int drop) {st_tensor *t = st_find(S, name);- if (!t) { fprintf(stderr, "missing tensor: %s\n", name); exit(1); }+ if (!t) st_die_missing(S, name); /* #1317: la riga nuda esisteva gia' spiegata */st_pread_full(t->fd, out, t->nbytes, t->off, "pread raw");if (drop) posix_fadvise(t->fd, t->off, t->nbytes, POSIX_FADV_DONTNEED);}@@ -931,7 +931,7 @@ static void st_read_raw(shards *S, const char *name, void *out, int drop) {* check -- and one that has none cannot silently do the wrong thing. */static void st_read_raw_cap(shards *S, const char *name, void *out, int64_t cap, int drop) {st_tensor *t = st_find(S, name);- if (!t) { fprintf(stderr, "missing tensor: %s\n", name); exit(1); }+ if (!t) st_die_missing(S, name); /* #1317: la riga nuda esisteva gia' spiegata */if (t->nbytes < 0 || t->nbytes > cap) {fprintf(stderr, "%s: tensor declares %lld bytes, destination holds %lld β refusing ""(untrusted container)\n", name, (long long)t->nbytes, (long long)cap); exit(1); }@@ -1000,7 +1000,7 @@ static void st_unmap_raw(st_mapped_raw *mapped) {static void st_read_slice_raw_cap(shards *S, const char *name, int64_t byte_off,int64_t nbytes, void *out, int64_t cap, int drop) {st_tensor *t = st_find(S, name);- if (!t) { fprintf(stderr, "missing tensor: %s\n", name); exit(1); }+ if (!t) st_die_missing(S, name); /* #1317: la riga nuda esisteva gia' spiegata */if (byte_off < 0 || nbytes < 0) {fprintf(stderr, "slice %s [%lld,+%lld) has negative offset/length\n",name, (long long)byte_off, (long long)nbytes); exit(1);@@ -1036,7 +1036,7 @@ static void st_read_slice_raw_cap(shards *S, const char *name, int64_t byte_off,* solo expert richiesto via pread del sotto-range, niente lettura dell'intero blocco. */static void st_read_slice_f32(shards *S, const char *name, int64_t elem_off, int64_t n_elems, float *out, int drop) {st_tensor *t = st_find(S, name);- if (!t) { fprintf(stderr, "missing tensor: %s\n", name); exit(1); }+ if (!t) st_die_missing(S, name); /* #1317: la riga nuda esisteva gia' spiegata */if (t->dtype >= 3) { /* stesso motivo di st_read_f32 sopra */fprintf(stderr, "slice %s: tensor is %s β not a float tensor\n",name, st_dtype_name(t->dtype)); exit(1); }diff --git a/c/tests/test_missing_tensor_diagnostics.py b/c/tests/test_missing_tensor_diagnostics.pynew file mode 100644index 00000000..66ffa4e5--- /dev/null+++ b/c/tests/test_missing_tensor_diagnostics.py@@ -0,0 +1,77 @@+"""Un tensore core mancante deve spiegarsi, non solo nominarsi (#1317).++Il reporter aveva 141 file su 142 e ha ricevuto una riga sola:+"missing tensor: model.embed_tokens.weight". La diagnostica che conta gli+shard, vede i buchi e suggerisce il resume ESISTEVA gia' (st_die_missing,+dal 2026-07-25) ma sette punti fatali di st.h uscivano con la riga nuda+senza chiamarla -- incluso il percorso dell'embed, cioe' il primo tensore+che ogni caricamento tocca: il caso piu' comune al mondo era l'unico senza+spiegazione.++Il container qui e' costruito a mano, byte per byte: niente numpy, niente+torch, nessun fixture esterno. Un config GLM tiny valido piu' uno shard che+NON contiene l'embed: il motore deve morire spiegando, non nominando.+"""+import json+import os+import struct+import subprocess+import sys+import tempfile+import unittest+from pathlib import Path++HERE = Path(__file__).resolve().parent.parent+ENGINE = HERE / ("colibri.exe" if sys.platform == "win32" else "colibri")++TINY_GLM_CONFIG = json.loads('{"transformers_version":"5.12.1","architectures":null,"output_hidden_states":false,"return_dict":true,"dtype":null,"chunk_size_feed_forward":0,"is_encoder_decoder":false,"id2label":{"0":"LABEL_0","1":"LABEL_1"},"label2id":{"LABEL_0":0,"LABEL_1":1},"problem_type":null,"vocab_size":8192,"hidden_size":1024,"intermediate_size":2048,"moe_intermediate_size":512,"num_hidden_layers":8,"num_attention_heads":16,"num_key_value_heads":16,"n_shared_experts":1,"n_routed_experts":32,"routed_scaling_factor":2.5,"kv_lora_rank":128,"q_lora_rank":256,"qk_rope_head_dim":32,"v_head_dim":64,"qk_nope_head_dim":64,"n_group":1,"topk_group":1,"num_experts_per_tok":8,"norm_topk_prob":true,"hidden_act":"silu","max_position_embeddings":4096,"initializer_range":0.02,"rms_norm_eps":1e-05,"use_cache":true,"pad_token_id":null,"bos_token_id":0,"eos_token_id":1,"tie_word_embeddings":false,"rope_parameters":{"rope_type":"default","rope_theta":10000.0},"mlp_layer_types":["dense","dense","dense","sparse","sparse","sparse","sparse","sparse"],"attention_bias":false,"attention_dropout":0.0,"index_topk":4096,"index_head_dim":32,"index_n_heads":4,"mlp_bias":false,"num_experts":256,"head_dim":32,"first_k_dense_replace":3,"layer_types":["deepseek_sparse_attention","deepseek_sparse_attention","deepseek_sparse_attention","deepseek_sparse_attention","deepseek_sparse_attention","deepseek_sparse_attention","deepseek_sparse_attention","deepseek_sparse_attention"],"indexer_types":["full","full","full","full","full","full","full","full"],"qk_head_dim":96,"_name_or_path":"","model_type":"glm_moe_dsa","output_attentions":false}')+++def write_safetensors(path, tensors):+ """Scrive un safetensors minimale: 8 byte di lunghezza header + JSON + dati."""+ header, blobs, off = {}, [], 0+ for name, values in tensors.items():+ blob = struct.pack(f"<{len(values)}f", *values)+ header[name] = {"dtype": "F32", "shape": [len(values)],+ "data_offsets": [off, off + len(blob)]}+ blobs.append(blob)+ off += len(blob)+ hdr = json.dumps(header).encode()+ with open(path, "wb") as fh:+ fh.write(struct.pack("<Q", len(hdr)))+ fh.write(hdr)+ for blob in blobs:+ fh.write(blob)++[email protected](ENGINE.exists(), "colibri is not built")+class MissingTensorDiagnosticsTest(unittest.TestCase):+ def test_missing_embed_explains_instead_of_naming(self):+ with tempfile.TemporaryDirectory() as tmp:+ root = Path(tmp)+ (root / "config.json").write_text(json.dumps(TINY_GLM_CONFIG))+ # uno shard c'e', ma l'embed no: lo scenario esatto di #1317+ write_safetensors(root / "out-00001.safetensors",+ {"padding.dummy": [0.0, 0.0, 0.0, 0.0]})+ r = subprocess.run([str(ENGINE), "4"], cwd=HERE, text=True,+ capture_output=True, timeout=120,+ env=dict(os.environ, SNAP=str(root),+ OMP_NUM_THREADS="2"))+ out = r.stdout + r.stderr+ self.assertNotEqual(r.returncode, 0)+ self.assertIn("model.embed_tokens.weight", out)+ for fragment, why in (+ ("shards numbered", "non conta gli shard presenti"),+ ("core tensor", "non dice che il tensore e' irrinunciabile"),+ ("HF_HUB_DISABLE_XET", "non suggerisce il resume del download"),+ ):+ self.assertIn(fragment, out,+ f"#1317 di nuovo: la morte per tensore mancante "+ f"{why}. Output:\n{out[-800:]}")+ self.assertNotIn("missing tensor:", out,+ "la riga nuda e' tornata: un sito fatale di st.h "+ "non passa piu' da st_die_missing")+++if __name__ == "__main__":+ unittest.main()