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

colibri.c: veto under-allocated shards; name the veto honestly; largest shard (#379)

monotophic56ab8890a039
diff --git a/c/colibri.c b/c/colibri.c
index 0c330871..448419a0 100644
--- a/c/colibri.c
+++ b/c/colibri.c
@@ -7597,6 +7597,29 @@ static void prof_config(Model *m, double ram_env, int est_ctx){
#define COLI_SSD_PROBE_COLD_FLOOR (64ll*1024*1024) /* fewer cold bytes than this = contaminated */
#define COLI_SSD_CACHE_MAX 64 /* longest well-formed .coli_ssd, in bytes */
+/* Why a measurement was refused (contaminated-class: nothing cached, one
+ * honest stderr line naming the ACTUAL condition, retry on a later start).
+ * The reasons differ in what the user should do about them -- "wait for a
+ * cold start" vs "finish the download" vs "nothing, this shard can never
+ * host a trustworthy probe" -- so the message must not lump them. */
+enum {
+ COLI_SSD_VETO_NONE = 0,
+ COLI_SSD_VETO_RESIDENT, /* too few cold pages: page cache would answer */
+ COLI_SSD_VETO_SMALL, /* shard offers < COLD_FLOOR of whole windows, ever */
+ COLI_SSD_VETO_SPARSE /* under-allocated: holes read as VFS zero-fill */
+};
+
+/* Under-allocation gate (#386 round 2, F1): steering prefers never-resident
+ * pages, and in a sparse or still-downloading file those are HOLES -- pread
+ * of a hole is VFS zero-fill at RAM speed (a 10 GB shard with 100 MB of real
+ * data measured 73 GB/s and latched it as disk bandwidth). st_blocks*512 is
+ * what actually sits on disk; require it to cover the file size within 12.5%
+ * slack (HFS+/APFS metadata rounding, tail blocks). Same conservative
+ * polarity as the residency veto: not fully allocated -> not measurable. */
+static int coli_ssd_probe_underallocated(long long allocated, long long size){
+ return allocated < size - size/8;
+}
+
/* digits [ "." digits ] -- returns chars consumed, 0 = no match. The ONLY
* number shape the cache grammar admits (see coli_ssd_cache_parse). */
static size_t coli_ssd_scan_number(const char *p, size_t len){
@@ -7702,30 +7725,42 @@ static int coli_ssd_probe_vetoed(long long cold_bytes){
}
#ifdef __APPLE__
-/* Path of the first *.safetensors shard in snap_dir into out, or out[0]=0 if
- * none. readdir order, unsorted (unlike st_init): any shard is representative
- * for a bandwidth probe, so sorting would buy nothing. */
+/* Path of the LARGEST *.safetensors shard in snap_dir into out, or out[0]=0
+ * if none. Any shard's bandwidth is representative, but the biggest one
+ * offers the most cold 4 MB windows -- a stray small shard picked by readdir
+ * order could starve the probe below the 64 MB floor forever (#386 round 2,
+ * F4) even though a full-size neighbor sits right next to it. */
static void coli_ssd_probe_pick_shard(const char *snap_dir, char *out, size_t outsz){
out[0]=0;
DIR *d=opendir(snap_dir);
if(!d) return;
struct dirent *e;
+ long long best=-1;
while((e=readdir(d))){
const char *dot=strrchr(e->d_name,'.');
- if(dot && !strcmp(dot,".safetensors")){ snprintf(out,outsz,"%s/%s",snap_dir,e->d_name); break; }
+ if(!dot || strcmp(dot,".safetensors")) continue;
+ char cand[1280];
+ snprintf(cand,sizeof(cand),"%s/%s",snap_dir,e->d_name);
+ struct stat st;
+ if(stat(cand,&st)==0 && (long long)st.st_size>best){
+ best=(long long)st.st_size;
+ snprintf(out,outsz,"%s",cand);
+ }
}
closedir(d);
}
/* F_NOCACHE random-read probe on one real shard, steered to verified-cold
* ranges. Returns measured GB/s, or -1 if the probe could not run (no shard,
- * open/map failure, file too small) or was vetoed as contaminated --
- * callers treat -1 as "not fast", never as an error to surface;
- * *contaminated_out distinguishes the veto so the caller can say why and
- * skip the cache write. The mmap is address space only (nothing is faulted
- * in): it exists so mincore can fill the residency vector. */
-static double coli_ssd_probe_raw(const char *snap_dir, int *contaminated_out){
- if(contaminated_out) *contaminated_out=0;
+ * open/map failure, file too small) or was vetoed as contaminated -- callers
+ * treat -1 as "not fast", never as an error to surface; *veto_out gets the
+ * COLI_SSD_VETO_* reason so the caller can say honestly why and skip the
+ * cache write. Veto order: SPARSE (holes would measure as zero-fill) before
+ * SMALL (can never offer the floor) before RESIDENT (cold today, maybe).
+ * The mmap is address space only (nothing is faulted in): it exists so
+ * mincore can fill the residency vector. */
+static double coli_ssd_probe_raw(const char *snap_dir, int *veto_out){
+ if(veto_out) *veto_out=COLI_SSD_VETO_NONE;
char path[1280];
coli_ssd_probe_pick_shard(snap_dir,path,sizeof(path));
if(!path[0]) return -1;
@@ -7735,6 +7770,16 @@ static double coli_ssd_probe_raw(const char *snap_dir, int *contaminated_out){
fcntl(fd,F_RDAHEAD,0); /* random access, not sequential streaming */
struct stat st;
if(fstat(fd,&st)!=0 || st.st_size<(off_t)(COLI_SSD_PROBE_BLK*2)){ close(fd); return -1; }
+ if(coli_ssd_probe_underallocated((long long)st.st_blocks*512,(long long)st.st_size)){
+ if(veto_out) *veto_out=COLI_SSD_VETO_SPARSE;
+ close(fd);
+ return -1;
+ }
+ if(((long long)st.st_size/COLI_SSD_PROBE_BLK)*COLI_SSD_PROBE_BLK<COLI_SSD_PROBE_COLD_FLOOR){
+ if(veto_out) *veto_out=COLI_SSD_VETO_SMALL; /* even fully cold it cannot reach the floor */
+ close(fd);
+ return -1;
+ }
size_t page_sz=(size_t)getpagesize();
size_t npages=((size_t)st.st_size+page_sz-1)/page_sz;
unsigned char *vec=malloc(npages);
@@ -7744,8 +7789,10 @@ static double coli_ssd_probe_raw(const char *snap_dir, int *contaminated_out){
if(map!=MAP_FAILED && mincore(map,(size_t)st.st_size,(char*)vec)==0){
long long cold_bytes=0;
size_t ntiles=coli_ssd_probe_cold_tiles(vec,npages,page_sz,tiles,&cold_bytes);
- if(coli_ssd_probe_vetoed(cold_bytes)){
- if(contaminated_out) *contaminated_out=1;
+ if(ntiles==0 || coli_ssd_probe_vetoed(cold_bytes)){
+ /* ntiles==0 is implied by the floor today, but the modulo below
+ * must NEVER be reachable with 0 on its own merits (#386 r2, F7) */
+ if(veto_out) *veto_out=COLI_SSD_VETO_RESIDENT;
}else{
void *buf=NULL;
if(posix_memalign(&buf,16384,COLI_SSD_PROBE_BLK)!=0) buf=NULL;
@@ -7794,17 +7841,34 @@ static double coli_ssd_probe_cached(const char *snap_dir){
return cached;
/* v2 from another volume, legacy, or garbage: fall through, re-measure */
}
- int contaminated=0;
- double gbs=coli_ssd_probe_raw(snap_dir,&contaminated);
- if(contaminated){
- fprintf(stderr,"METAL: storage probe deferred -- the shard is already page-cache resident "
- "(<%lld MB cold), a read now would measure RAM, not the disk; keeping the "
- "conservative cache default, will re-measure on a cold start\n",
- COLI_SSD_PROBE_COLD_FLOOR/(1024*1024));
+ int veto=COLI_SSD_VETO_NONE;
+ double gbs=coli_ssd_probe_raw(snap_dir,&veto);
+ if(veto!=COLI_SSD_VETO_NONE){
+ /* one honest line per deferral, naming the condition that actually
+ * fired (#386 r2, F4): "resident" advice (wait for a cold start) is
+ * wrong advice for a partial download or a permanently tiny shard. */
+ const char *why =
+ veto==COLI_SSD_VETO_SPARSE ? "the shard is not fully allocated on disk (partial "
+ "download?), holes would measure as RAM-speed zero-fill" :
+ veto==COLI_SSD_VETO_SMALL ? "the largest shard offers under 64 MB of probe windows, "
+ "too small for a trustworthy measurement" :
+ "the shard is already page-cache resident (<64 MB cold), "
+ "a read now would measure RAM, not the disk";
+ fprintf(stderr,"METAL: storage probe deferred -- %s; keeping the conservative cache "
+ "default, will re-check on a later start\n",why);
+ return -1;
+ }
+ if(gbs<0) return -1; /* could not run at all: no shard, open failure */
+ if(!(gbs>0 && gbs<1000)){
+ /* a number the cache grammar itself would refuse (0, >=1000 GB/s,
+ * non-finite) is no more trustworthy for THIS run than for the next
+ * one (#386 r2, F6): treat it as contaminated, not as "fast". */
+ fprintf(stderr,"METAL: storage probe measured an implausible %g GB/s -- ignoring the "
+ "measurement, keeping the conservative cache default\n",gbs);
return -1;
}
char line[COLI_SSD_CACHE_MAX+1];
- if(gbs>0 && gbs<1000 && coli_ssd_cache_format(line,sizeof(line),gbs,cur_dev)>0){
+ if(coli_ssd_cache_format(line,sizeof(line),gbs,cur_dev)>0){
/* mkstemp (unique name, O_EXCL) + rename: the model dir is shared (a
* concurrent serve + run pair may both probe a virgin dir) so a torn
* write must never survive, and a fixed, predictable temp name could
@@ -7813,14 +7877,25 @@ static double coli_ssd_probe_cached(const char *snap_dir){
* the safe direction (the platform default just stays off). */
char tpath[1310];
snprintf(tpath,sizeof(tpath),"%s.XXXXXX",cpath);
+ int cache_errno=0;
int tfd=mkstemp(tpath);
- if(tfd>=0){
+ if(tfd<0) cache_errno=errno;
+ else{
size_t len=strlen(line);
+ mode_t um=umask(0); umask(um); /* read it back; umask() has no getter */
int ok = write(tfd,line,len)==(ssize_t)len;
- ok = fchmod(tfd,0644)==0 && ok; /* mkstemp's 0600 would hide it from group readers */
+ ok = fchmod(tfd,(mode_t)(0644&~um))==0 && ok; /* mkstemp forces 0600; honor the umask instead */
+ if(!ok) cache_errno=errno;
close(tfd);
- if(!ok || rename(tpath,cpath)!=0) unlink(tpath);
- }
+ if(ok && rename(tpath,cpath)!=0){ cache_errno=errno; ok=0; }
+ if(!ok) unlink(tpath);
+ }
+ if(cache_errno)
+ /* the measurement still governs THIS run; only persistence failed
+ * (read-only model dir, quota, ...) -- say so once (#386 r2, F5) */
+ fprintf(stderr,"METAL: could not cache the storage probe in %s (%s) -- "
+ "will re-measure on every start until it is writable\n",
+ cpath,strerror(cache_errno));
}
return gbs;
}
diff --git a/c/tests/test_ssd_probe.c b/c/tests/test_ssd_probe.c
index df78c68a..e30d8ee5 100644
--- a/c/tests/test_ssd_probe.c
+++ b/c/tests/test_ssd_probe.c
@@ -18,9 +18,33 @@
* Portable: the pure layer builds and runs on every platform, same as
* test_cap_precedence.c. */
#define COLI_SSD_PROBE_TEST 1
+
+#ifdef __APPLE__
+/* pread recorder (#386 r2, FR2-2): pins that probe_raw actually READS the
+ * offsets the steering selected -- the decision function alone proved
+ * mutable out from under the suite (a probe_raw that ignores tiles[] and
+ * reads r11-style random offsets passed every round-1 test). unistd.h is
+ * included BEFORE the macro below so its include guard keeps the real
+ * prototype; every pread in the engine TU then routes through the recorder,
+ * which delegates to the real call. */
+#include <unistd.h>
+#include <sys/types.h>
+static long long g_pread_rec[4096];
+static int g_pread_nrec;
+static int g_pread_rec_on;
+static ssize_t coli_test_pread(int fd, void *buf, size_t n, off_t off){
+ if(g_pread_rec_on && g_pread_nrec<4096) g_pread_rec[g_pread_nrec++]=(long long)off;
+ return pread(fd,buf,n,off);
+}
+#define pread coli_test_pread
+#endif /* __APPLE__ */
+
#define main coli_glm_main_unused
#include "../colibri.c"
#undef main
+#ifdef __APPLE__
+#undef pread
+#endif
static int failures = 0;
@@ -250,15 +274,17 @@ static void run_cached_decisions(void){
free(mb); fclose(f);
char cp[1400]; snprintf(cp,sizeof(cp),"%s/.coli_ssd",dirbuf);
unlink(cp); /* start with no cache at all */
- int contaminated=0;
- double raw=coli_ssd_probe_raw(dirbuf,&contaminated);
- if(!contaminated){
+ int veto=COLI_SSD_VETO_NONE;
+ double raw=coli_ssd_probe_raw(dirbuf,&veto);
+ if(veto==COLI_SSD_VETO_NONE){
/* the OS evicted a fresh 80MB write already? environment too
* unusual to assert on -- report and move on, the pure veto
* tests above still hold the line */
fprintf(stderr,"note: fresh shard not resident (raw=%.3f), skipping veto e2e\n",raw);
}else{
CHECK(raw==-1,"vetoed raw probe must report -1: got %.3f",raw);
+ CHECK(veto==COLI_SSD_VETO_RESIDENT,
+ "fully-allocated warm shard must veto as RESIDENT, not %d",veto);
got=coli_ssd_probe_cached(dirbuf);
CHECK(got==-1,"vetoed cached probe must report -1: got %.3f",got);
CHECK(read_file(dirbuf,".coli_ssd",back,sizeof(back))==-1,
@@ -271,14 +297,176 @@ static void run_cached_decisions(void){
char cp[1400]; snprintf(cp,sizeof(cp),"%s/.coli_ssd",dirbuf);
unlink(cp); rmdir(dirbuf);
}
+
+/* ---- round 2: sparse / small / steering-pin arms ------------------------ */
+
+static char *make_scratch_dir(char *dirbuf, size_t sz){
+ const char *tmp=getenv("TMPDIR");
+ snprintf(dirbuf,sz,"%s/coli_ssd_probe_r2.XXXXXX",(tmp && strlen(tmp)<1100)?tmp:"/tmp");
+ if(!mkdtemp(dirbuf)){ perror("mkdtemp"); failures++; return NULL; }
+ return dirbuf;
+}
+
+/* write `mb` MB through F_NOCACHE so the pages land on disk but NOT in the
+ * page cache (the confirmation battery's cold-file technique) */
+static int write_cold_mb(const char *path, int mb){
+ int fd=open(path,O_WRONLY|O_CREAT|O_TRUNC,0644);
+ if(fd<0) return -1;
+ fcntl(fd,F_NOCACHE,1);
+ char *buf=malloc(1u<<20);
+ if(!buf){ close(fd); return -1; }
+ memset(buf,0x5A,1u<<20);
+ int ok=1;
+ for(int i=0;i<mb && ok;i++) ok = write(fd,buf,1u<<20)==(ssize_t)(1u<<20);
+ free(buf); close(fd);
+ return ok?0:-1;
+}
+
+/* fraction of the byte range [lo,hi) currently page-cache resident */
+static double resident_share(const char *path, long long lo, long long hi){
+ int fd=open(path,O_RDONLY);
+ if(fd<0) return -1;
+ struct stat st;
+ if(fstat(fd,&st)!=0){ close(fd); return -1; }
+ size_t page_sz=(size_t)getpagesize();
+ size_t npages=((size_t)st.st_size+page_sz-1)/page_sz;
+ unsigned char *vec=malloc(npages);
+ void *map=vec?mmap(NULL,(size_t)st.st_size,PROT_READ,MAP_SHARED,fd,0):MAP_FAILED;
+ double share=-1;
+ if(map!=MAP_FAILED && mincore(map,(size_t)st.st_size,(char*)vec)==0){
+ size_t p0=(size_t)(lo/(long long)page_sz), p1=(size_t)(hi/(long long)page_sz);
+ if(p1>npages) p1=npages;
+ size_t res=0;
+ for(size_t i=p0;i<p1;i++) res+=(size_t)(vec[i]&1);
+ share = p1>p0 ? (double)res/(double)(p1-p0) : -1;
+ }
+ if(map!=MAP_FAILED) munmap(map,(size_t)st.st_size);
+ free(vec); close(fd);
+ return share;
+}
+
+/* F1: an under-allocated (sparse / still-downloading) shard must be vetoed
+ * BEFORE any read: its never-resident pages are holes, and pread of a hole
+ * is VFS zero-fill at RAM speed (auditor repro: 73.3 GB/s latched). */
+static void run_sparse_veto(void){
+ char dirbuf[1200];
+ if(!make_scratch_dir(dirbuf,sizeof(dirbuf))) return;
+ char p[1400]; snprintf(p,sizeof(p),"%s/out-00000.safetensors",dirbuf);
+ int fd=open(p,O_WRONLY|O_CREAT|O_TRUNC,0644);
+ if(fd<0){ perror(p); failures++; rmdir(dirbuf); return; }
+ char *mb=malloc(1u<<20); memset(mb,0xA5,1u<<20);
+ for(int i=0;i<100;i++) write(fd,mb,1u<<20); /* 100 MB real data */
+ free(mb);
+ ftruncate(fd,(off_t)400*1024*1024); /* + 300 MB of holes */
+ close(fd);
+ struct stat st;
+ if(stat(p,&st)==0 && !coli_ssd_probe_underallocated((long long)st.st_blocks*512,(long long)st.st_size)){
+ fprintf(stderr,"note: filesystem allocated the ftruncate hole (blocks*512=%lld); skipping sparse e2e\n",
+ (long long)st.st_blocks*512);
+ }else{
+ int veto=COLI_SSD_VETO_NONE;
+ double raw=coli_ssd_probe_raw(dirbuf,&veto);
+ CHECK(raw==-1,"sparse shard must not yield a measurement: got %.3f",raw);
+ CHECK(veto==COLI_SSD_VETO_SPARSE,"sparse shard must veto as SPARSE, got %d",veto);
+ double got=coli_ssd_probe_cached(dirbuf);
+ char back[160];
+ CHECK(got==-1,"sparse cached probe must report -1: got %.3f",got);
+ CHECK(read_file(dirbuf,".coli_ssd",back,sizeof(back))==-1,
+ "sparse probe must not write .coli_ssd (found: %s)",back);
+ }
+ unlink(p);
+ char cp[1400]; snprintf(cp,sizeof(cp),"%s/.coli_ssd",dirbuf);
+ unlink(cp); rmdir(dirbuf);
+}
+
+/* F4 honesty: a shard that can never offer 64 MB of probe windows is SMALL,
+ * not "page-cache resident" -- even when it is stone cold. */
+static void run_small_veto(void){
+ char dirbuf[1200];
+ if(!make_scratch_dir(dirbuf,sizeof(dirbuf))) return;
+ char p[1400]; snprintf(p,sizeof(p),"%s/out-00000.safetensors",dirbuf);
+ if(write_cold_mb(p,30)!=0){ perror(p); failures++; rmdir(dirbuf); return; }
+ int veto=COLI_SSD_VETO_NONE;
+ double raw=coli_ssd_probe_raw(dirbuf,&veto);
+ CHECK(raw==-1,"30MB shard must not yield a measurement: got %.3f",raw);
+ CHECK(veto==COLI_SSD_VETO_SMALL,"cold 30MB shard must veto as SMALL, got %d",veto);
+ unlink(p); rmdir(dirbuf);
+}
+
+/* FR2-2: the steering must be USED, not merely computed. Half the shard is
+ * cold (F_NOCACHE-written), half deliberately warmed; every offset probe_raw
+ * actually preads (recorded by the coli_test_pread shim) must fall in the
+ * cold half. A probe_raw that ignores tiles[] and reads r11-style random
+ * offsets lands ~half its reads in the warm half and fails here. */
+static void run_steering_pin(void){
+ enum { MB_TOTAL=144, MB_COLD=72 }; /* 36 tiles: 18 cold + 18 warm */
+ char dirbuf[1200];
+ if(!make_scratch_dir(dirbuf,sizeof(dirbuf))) return;
+ char p[1400]; snprintf(p,sizeof(p),"%s/out-00000.safetensors",dirbuf);
+ if(write_cold_mb(p,MB_TOTAL)!=0){ perror(p); failures++; rmdir(dirbuf); return; }
+ long long warm_lo=(long long)MB_COLD*1024*1024;
+ long long warm_hi=(long long)MB_TOTAL*1024*1024;
+ /* warm the second half with plain buffered reads */
+ int fd=open(p,O_RDONLY);
+ if(fd>=0){
+ char *buf=malloc(1u<<20);
+ for(long long off=warm_lo; off<warm_hi; off+=1u<<20)
+ if(pread(fd,buf,1u<<20,(off_t)off)<=0) break;
+ free(buf); close(fd);
+ }
+ double cold_res=resident_share(p,0,warm_lo);
+ double warm_res=resident_share(p,warm_lo,warm_hi);
+ if(!(cold_res>=0 && cold_res<0.05 && warm_res>0.90)){
+ /* residency preconditions not met on this box: report, don't flake */
+ fprintf(stderr,"note: residency prep off (cold %.0f%%, warm %.0f%%), skipping steering pin\n",
+ cold_res*100,warm_res*100);
+ }else{
+ g_pread_nrec=0; g_pread_rec_on=1;
+ int veto=COLI_SSD_VETO_NONE;
+ double gbs=coli_ssd_probe_raw(dirbuf,&veto);
+ g_pread_rec_on=0;
+ CHECK(veto==COLI_SSD_VETO_NONE,"half-cold shard must not veto (72MB cold): got %d",veto);
+ CHECK(gbs>0,"half-cold shard must measure: got %.3f",gbs);
+ CHECK(g_pread_nrec>0,"probe made no preads");
+ for(int i=0;i<g_pread_nrec;i++){
+ CHECK(g_pread_rec[i]>=0 && g_pread_rec[i]<warm_lo,
+ "pread %d at %lld outside the cold half [0,%lld)",i,g_pread_rec[i],warm_lo);
+ CHECK(g_pread_rec[i]%COLI_SSD_PROBE_BLK==0,
+ "pread %d at %lld is not tile-aligned",i,g_pread_rec[i]);
+ }
+ fprintf(stderr,"steering pin: %d preads, all in the cold half\n",g_pread_nrec);
+ }
+ unlink(p);
+ char cp[1400]; snprintf(cp,sizeof(cp),"%s/.coli_ssd",dirbuf);
+ unlink(cp); rmdir(dirbuf);
+}
#endif /* __APPLE__ */
+/* F1's decision layer, portable: st_blocks*512 must cover st_size within
+ * 12.5% slack; the boundary sits exactly at size - size/8. */
+static void run_underallocated(void){
+ const long long GBl=1024ll*1024*1024;
+ CHECK(coli_ssd_probe_underallocated(100ll*1024*1024,10*GBl),
+ "auditor repro: 100MB real of 10GB must read as under-allocated");
+ CHECK(!coli_ssd_probe_underallocated(10*GBl,10*GBl),"fully allocated must pass");
+ CHECK(!coli_ssd_probe_underallocated(10*GBl+4096,10*GBl),
+ "metadata rounding above size must pass");
+ long long size=800ll*1024*1024;
+ CHECK(!coli_ssd_probe_underallocated(size-size/8,size),"exactly at the slack edge must pass");
+ CHECK(coli_ssd_probe_underallocated(size-size/8-1,size),"one byte past the slack must veto");
+ CHECK(coli_ssd_probe_underallocated(0,size),"zero allocation must veto");
+}
+
int main(void){
run_vectors();
run_roundtrip();
run_steering();
+ run_underallocated();
#ifdef __APPLE__
run_cached_decisions();
+ run_sparse_veto();
+ run_small_veto();
+ run_steering_pin();
#endif
if(failures){
fprintf(stderr,"ssd probe tests: %d FAILURE(S)\n",failures);