Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
score: validate complete input rows before scoring
Rudy Celeklic8a48a7fd8cb
diff --git a/c/colibri.c b/c/colibri.cindex 0c19116b..7a684380 100644--- a/c/colibri.c+++ b/c/colibri.c@@ -8262,6 +8262,35 @@ static int forward_all(Model *m, const int *ids, int S, int *pred, const int *re* input: file con righe "<ctxlen> <contlen> <id0> .. <id_{T-1}>" (T=ctxlen+contlen)* output: riga "<logprob_continuazione> <contlen> <greedy 0/1>" per richiesta.* Un solo forward per richiesta (teacher-forcing): niente generazione -> fattibile a bassa velocita'. */+/* SCORE rows are a whitespace-separated pair of lengths and exactly that+ * many vocabulary ids. Check the entire file before allocating inference+ * buffers or emitting any score; a missing id must not become token zero. */+static int score_int(char **cursor, int *value){+ char *p=*cursor, *end;+ while(isspace((unsigned char)*p)) p++;+ if(!*p) return 0;+ errno=0;+ long n=strtol(p,&end,10);+ if(p==end || errno==ERANGE || n<0 || n>INT_MAX ||+ (*end && !isspace((unsigned char)*end))) return 0;+ *value=(int)n; *cursor=end; return 1;+}+static int score_row(char *line, size_t length, int vocab, int reserve,+ int *ctx, int *cont, int *ids, int capacity){+ if(memchr(line,0,length)) return 0;+ char *p=line;+ if(!score_int(&p,ctx) || !score_int(&p,cont) || *ctx<1 ||+ *ctx>INT_MAX-reserve || *cont>INT_MAX-reserve-*ctx) return 0;+ int total=*ctx+*cont;+ if(ids && total>capacity-reserve) return 0;+ for(int i=0;i<total;i++){+ int id;+ if(!score_int(&p,&id) || id>=vocab) return 0;+ if(ids) ids[i]=id;+ }+ while(isspace((unsigned char)*p)) p++;+ return !*p;+}static void run_score(Model *m, const char *snap, const char *path){Cfg *c=&m->c; int D=c->hidden;/* prefisso GLM (#108): il modello vede [gMASK]<sop> in testa a OGNI sequenza di training —@@ -8285,18 +8314,29 @@ static void run_score(Model *m, const char *snap, const char *path){free(ar);}FILE *f=fopen(path,"rb"); if(!f){perror(path);exit(1);}- int maxT=1; { char *ln=NULL; size_t cp=0;- while(getline(&ln,&cp,f)>0){ int a,b; if(sscanf(ln,"%d %d",&a,&b)==2 && a+b>maxT) maxT=a+b; }+ int maxT=1, reserve=pfx_on?2:0; { char *ln=NULL; size_t cp=0; ssize_t length; size_t lineno=0;+ while((length=getline(&ln,&cp,f))>0){+ int ctx,cont; lineno++;+ if(!score_row(ln,(size_t)length,c->vocab,reserve,&ctx,&cont,NULL,0)){+ fprintf(stderr,"[SCORE] invalid request at line %zu\n",lineno); exit(1);+ }+ if(ctx+cont>maxT) maxT=ctx+cont;+ }+ if(ferror(f)){ perror(path); exit(1); }free(ln); }if(pfx_on) maxT+=2; /* le richieste senza prefisso crescono di 2 token */kv_alloc(m,maxT);float *x=falloc((int64_t)maxT*D), *lo=falloc(c->vocab), *row=falloc(D);- int *ids=malloc(maxT*sizeof(int));- rewind(f); char *ln=NULL; size_t cp=0; int nreq=0; double t0=now_s();- while(getline(&ln,&cp,f)>0){- char *p=ln; int ctxlen=strtol(p,&p,10), contlen=strtol(p,&p,10), T=ctxlen+contlen;- if(T<=0||ctxlen<1){ printf("0 0 0\n"); fflush(stdout); continue; }- for(int i=0;i<T;i++) ids[i]=strtol(p,&p,10);+ int *ids=malloc((size_t)maxT*sizeof(int));+ if(!ids){ perror("SCORE token buffer"); exit(1); }+ rewind(f); char *ln=NULL; size_t cp=0; ssize_t length; size_t lineno=0;+ int nreq=0; double t0=now_s();+ while((length=getline(&ln,&cp,f))>0){+ int ctxlen,contlen; lineno++;+ if(!score_row(ln,(size_t)length,c->vocab,reserve,&ctxlen,&contlen,ids,maxT)){+ fprintf(stderr,"[SCORE] invalid request at line %zu\n",lineno); exit(1);+ }+ int T=ctxlen+contlen;if(pfx_on && !(T>=2 && ids[0]==pfx[0] && ids[1]==pfx[1])){ /* gia' prefissato -> intatto */memmove(ids+2,ids,(size_t)T*sizeof(int));ids[0]=pfx[0]; ids[1]=pfx[1]; ctxlen+=2; T+=2;@@ -8313,6 +8353,7 @@ static void run_score(Model *m, const char *snap, const char *path){if(++nreq%5==0) fprintf(stderr,"[score %d req | %.1fs | RSS %.2f GB | hit %.0f%%]\n",nreq, now_s()-t0, rss_gb(), (m->hits+m->miss)?100.0*m->hits/(m->hits+m->miss):0.0);}+ if(ferror(f)){ perror(path); exit(1); }free(ln); free(ids); free(x); free(lo); free(row); fclose(f);}diff --git a/c/tests/test_score_rows.py b/c/tests/test_score_rows.pynew file mode 100644index 00000000..7d71a07f--- /dev/null+++ b/c/tests/test_score_rows.py@@ -0,0 +1,61 @@+"""Real-process SCORE rows must not silently invent or ignore token ids."""+import os+from pathlib import Path+import subprocess+import tempfile+import unittest++C_DIR = Path(__file__).resolve().parents[1]+ENGINE = C_DIR / ("colibri.exe" if os.name == "nt" else "colibri")+SNAP = C_DIR / "glm_tiny"+++class ScoreRowsTest(unittest.TestCase):+ @classmethod+ def setUpClass(cls):+ if not ENGINE.is_file() or not (SNAP / "model.safetensors").is_file():+ raise unittest.SkipTest("build colibri and generate the GLM tiny fixture first")++ def run_score(self, rows):+ keep = {"PATH", "HOME", "SYSTEMROOT", "WINDIR", "TEMP", "TMP", "USERPROFILE",+ "LD_LIBRARY_PATH", "DYLD_LIBRARY_PATH"}+ env = {k: v for k, v in os.environ.items() if k in keep}+ with tempfile.TemporaryDirectory() as tmp:+ path = Path(tmp) / "rows.txt"+ path.write_bytes(rows.encode() if isinstance(rows, str) else rows)+ env.update(SNAP=str(SNAP), SCORE=str(path), SCORE_PREFIX="0", OMP_NUM_THREADS="4")+ return subprocess.run([str(ENGINE), "64", "16", "16"], cwd=C_DIR,+ env=env, capture_output=True, text=True, timeout=30)++ def test_valid_rows_keep_scores_and_empty_continuations(self):+ result = self.run_score("1 1 3 14\n1 0 3\n")+ self.assertEqual(result.returncode, 0, result.stderr)+ rows = result.stdout.splitlines()[-2:]+ self.assertEqual(rows[0].split()[1:], ["1", "0"])+ self.assertAlmostEqual(float(rows[0].split()[0]), -5.152055, places=4)+ self.assertEqual(rows[1], "0.000000 0 1")++ def test_missing_negative_out_of_vocabulary_and_extra_ids(self):+ for row in ("1 1 3", "1 1 3 -1", "1 1 3 10000000", "1 1 3 14 7"):+ with self.subTest(row=row):+ result = self.run_score(row + "\n")+ self.assertEqual(result.returncode, 1, result.stderr)+ self.assertIn("[SCORE] invalid request at line 1", result.stderr)++ def test_invalid_lengths_and_noninteger_fields(self):+ for row in ("0 1 3", "1 -1", "1.0 1 3 14", "1 1 3 14junk",+ "2147483647 1", "999999999999999999999 0", "1 1 3 14\0ignored"):+ with self.subTest(row=row):+ result = self.run_score(row + "\n")+ self.assertEqual(result.returncode, 1, result.stderr)+ self.assertIn("[SCORE] invalid request at line 1", result.stderr)++ def test_whole_file_validation_precedes_any_score(self):+ result = self.run_score("1 1 3 14\n1 1 3\n")+ self.assertEqual(result.returncode, 1, result.stderr)+ self.assertIn("[SCORE] invalid request at line 2", result.stderr)+ self.assertNotIn("-5.152055 1 0", result.stdout)+++if __name__ == "__main__":+ unittest.main()