Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

Merge pull request #1889 into integ/release-batch-5

JustVugg35da98ca02ac
diff --cc c/kv_persist.h
index e2137a6d,a965579f..fa0db325
--- a/c/kv_persist.h
+++ b/c/kv_persist.h
@@@ -33,24 -47,24 +49,29 @@@ static void kv_disk_write_failed(KVStat
static int kv_disk_open(Model *m){
KVState *k=m->kv;
if(k->disk_fp) return 1;
- k->disk_fp=fopen(k->disk_path,"r+b");
+ k->disk_fp=coli_own_fopen(k->disk_path,"r+b");
- if(k->disk_fp){ char mg[8]; /* formato del file != formato attivo -> riscrivi */
- if(fread(mg,1,8,k->disk_fp)!=8 || memcmp(mg,kv_active_magic(),8)){
+ if(k->disk_fp){ char mg[8]; int32_t h[8], want[8]; kv_hdr(m,want,0);
+ /* A failed initial header write may leave the magic but no valid
+ * dimensions. Do not append rows to that incomplete file. */
+ if(fread(mg,1,8,k->disk_fp)!=8 || memcmp(mg,kv_active_magic(),8) ||
+ fread(h,4,8,k->disk_fp)!=8 || memcmp(h,want,6*sizeof(int32_t)) || h[7]!=want[7]){
fclose(k->disk_fp); k->disk_fp=NULL; k->disk_nrec=0; }
}
if(!k->disk_fp){
- k->disk_fp=fopen(k->disk_path,"wb");
- if(!k->disk_fp) return 0;
+ k->disk_fp=coli_own_fopen(k->disk_path,"wb");
+ if(!k->disk_fp){
+ static int said;
+ if(!said++) fprintf(stderr,"[KV] cannot write %s (%s): conversations will not reopen warm\n",
+ k->disk_path,strerror(errno));
+ return 0;
+ }
+ k->disk_nrec=0; /* a newly created file contains no committed rows */
int32_t h[8]; kv_hdr(m,h,0);
- fwrite(kv_active_magic(),1,8,k->disk_fp); fwrite(h,4,8,k->disk_fp);
- fflush(k->disk_fp);
+ if(fwrite(kv_active_magic(),1,8,k->disk_fp)!=8 ||
+ fwrite(h,4,8,k->disk_fp)!=8 || !kv_disk_sync(k->disk_fp)){
+ kv_disk_write_failed(k); return 0; }
fclose(k->disk_fp);
- k->disk_fp=fopen(k->disk_path,"r+b");
+ k->disk_fp=coli_own_fopen(k->disk_path,"r+b");
if(!k->disk_fp) return 0;
}
return 1;
@@@ -65,11 -79,18 +86,18 @@@ static void kv_disk_truncate(Model *m,
* record MAI scritti — il load successivo li leggerebbe come spazzatura. */
if(nrec>k->disk_nrec) nrec=k->disk_nrec;
if(k->disk_fp){ fclose(k->disk_fp); k->disk_fp=NULL; }
- FILE *f=fopen(k->disk_path,"r+b");
+ FILE *f=coli_own_fopen(k->disk_path,"r+b");
if(!f){ k->disk_nrec=0; return; }
- k->disk_nrec=nrec;
- int32_t nr=nrec; fseek(f,8+6*4,SEEK_SET); fwrite(&nr,4,1,f);
- fflush(f); fclose(f);
+ int32_t nr=nrec;
+ int ok=fseek(f,8+6*4,SEEK_SET)==0 && fwrite(&nr,4,1,f)==1 && kv_disk_sync(f);
+ if(fclose(f)!=0) ok=0;
+ if(ok) k->disk_nrec=nrec;
+ else {
+ /* The desired prefix is shorter than the saved one: retry every live
+ * row next time, rather than letting the old count suppress a save. */
+ k->disk_nrec=0;
+ fprintf(stderr,"[KV] could not persist the conversation's shorter prefix\n");
+ }
}
static void kv_disk_reset(Model *m){ kv_disk_truncate(m,0); }