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

mirror: N-way multi-SSD read replicas (COLI_MODEL_MIRROR dir list)

Steve Markgraf2737ee6e7d4f
diff --git a/c/colibri.c b/c/colibri.c
index f2ed4496..ba81ff7c 100644
--- a/c/colibri.c
+++ b/c/colibri.c
@@ -1461,17 +1461,20 @@ static void *map_of_fd(int fd){
return base;
}
-/* ==================== DUAL-SSD: two model copies, two drives ====================
- * COLI_MODEL_MIRROR=<dir> registers a SECOND (read-only) copy of the model on
- * another drive; expert reads are split between the two according to
- * COLI_DISK_WEIGHTS=<primary>,<mirror> (relative bandwidth; without the env it
- * is measured at startup with the engine's own access pattern). Cold decode is
- * disk-bound (~11 GB/token): two NVMe drives reading in parallel add up. */
-static const char *g_mirror_dir=NULL; /* COLI_MODEL_MIRROR / SNAP_MIRROR */
+/* ==================== MULTI-SSD: N model copies, N drives ====================
+ * COLI_MODEL_MIRROR=<dir>[;<dir>...] registers additional read-only copies of
+ * the model on other drives; expert reads split across all copies according to
+ * COLI_DISK_WEIGHTS=<primary>,<mirror>[,<mirror2>...] (relative bandwidth;
+ * without the env it is measured at startup with the engine's own access
+ * pattern). Cold decode is disk-bound (~11 GB/token): N NVMe drives reading in
+ * parallel add up. */
+#define MIR_REPS (1+ST_MAX_MIR) /* replicas incl. the primary */
+static const char *g_mirror_dir=NULL; /* COLI_MODEL_MIRROR / SNAP_MIRROR (dir list) */
static int g_mirror=0; /* 1 = mirror active (at least one shard accepted) */
-static int g_mir_share=64; /* expert share routed to the mirror, out of 256 */
-static _Atomic int64_t g_mir_bytes[2]; /* bytes served per drive: [0] primary, [1] mirror */
-static _Atomic int64_t g_mir_nread[2];
+static int g_mir_nrep=1; /* replicas incl. the primary */
+static int g_mir_cut[MIR_REPS]={256}; /* cumulative hash cuts of 256: replica r serves h in [cut[r-1],cut[r]) */
+static _Atomic int64_t g_mir_bytes[MIR_REPS]; /* bytes served per drive: [0] primary, [r] mirror r */
+static _Atomic int64_t g_mir_nread[MIR_REPS];
/* replica of one expert: DETERMINISTIC hash of (layer,eid). Determinism is a
* requirement, not a style choice: the readahead/PILOT WILLNEED and the demand
@@ -1481,7 +1484,9 @@ static inline int expert_route(int layer,int eid){
if(!g_mirror) return 0;
uint32_t h=(uint32_t)layer*2654435761u ^ (uint32_t)eid*0x9E3779B9u;
h^=h>>16; h*=0x45d9f3bu; h^=h>>16;
- return (int)(h&255) < g_mir_share;
+ int hv=(int)(h&255), r=0;
+ while(hv>=g_mir_cut[r]) r++; /* cut[nrep-1]==256 terminates the scan */
+ return r;
}
/* buffered fd of the replica, falling back to the primary if the file is not mirrored */
@@ -1498,7 +1503,7 @@ static int pread_full(int fd, void *buf, int64_t n, int64_t off, const char *tag
* Accounts bytes per drive. Returns 0 = ok, -1 = real error/EOF (like pread_full). */
static ssize_t mir_pread(shards *S,int fd,int rep,void *buf,int64_t n,int64_t off,const char *tag){
int rfd = st_fd_rep(S,fd,rep);
- int used = rep && rfd>=0;
+ int used = (rep && rfd>=0) ? rep : 0;
if(rfd<0) rfd=fd;
int rc=pread_full(rfd,buf,n,off,tag);
if(rc && used){
@@ -1623,7 +1628,7 @@ static int expert_load_impl(Model *m, int layer, int eid, ESlot *s, int fatal, i
__atomic_add_fetch(&m->bytes_main,(uint64_t)tb,__ATOMIC_RELAXED); }
}
int rep=expert_route(layer,eid); /* DUAL-SSD: this expert's replica */
- if(rep && st_fd_rep(&m->S,tw[0]->fd,1)<0) rep=0; /* shard not in the mirror (partial) */
+ if(rep && st_fd_rep(&m->S,tw[0]->fd,rep)<0) rep=0; /* shard not in that mirror (partial) */
if(g_mmap){
void *bw[3],*bq[3]; int okm=1;
for(int k=0;k<3;k++){
@@ -5420,12 +5425,14 @@ static void profile_print(Model *m, double elapsed){
g_vkb_cls,g_vkb_issue,g_vkb_acc,m->t_egpu,m->t_ecpu,m->t_ewait);
#endif
if(g_mirror){
- double b0=atomic_load_explicit(&g_mir_bytes[0],memory_order_relaxed)/1e9;
- double b1=atomic_load_explicit(&g_mir_bytes[1],memory_order_relaxed)/1e9;
- printf("MIRROR: primary %.2f GB (%lld reads) | mirror %.2f GB (%lld reads) β€” %.0f%% of expert bytes from the mirror\n",
- b0,(long long)atomic_load_explicit(&g_mir_nread[0],memory_order_relaxed),
- b1,(long long)atomic_load_explicit(&g_mir_nread[1],memory_order_relaxed),
- b0+b1>0?100.0*b1/(b0+b1):0.0);
+ double br[MIR_REPS]={0}, bt=0;
+ for(int r=0;r<g_mir_nrep;r++){ br[r]=atomic_load_explicit(&g_mir_bytes[r],memory_order_relaxed)/1e9; bt+=br[r]; }
+ printf("MIRROR: primary %.2f GB (%lld reads)",
+ br[0],(long long)atomic_load_explicit(&g_mir_nread[0],memory_order_relaxed));
+ for(int r=1;r<g_mir_nrep;r++)
+ printf(" | mirror%d %.2f GB (%lld reads)",
+ r,br[r],(long long)atomic_load_explicit(&g_mir_nread[r],memory_order_relaxed));
+ printf(" β€” %.0f%% of expert bytes from the mirrors\n", bt>0?100.0*(bt-br[0])/bt:0.0);
}
#ifdef COLI_METAL
if(g_metal_enabled){ uint64_t ok=0,fb=0,ex=0; double su=0,gp=0,sc=0;
@@ -5573,8 +5580,7 @@ static void run_replay(Model *m, const int *full, int nfull, int np){
m->hits=m->miss=m->ereq=m->gpu_expert_calls=0; m->hit_pin=m->hit_ecache=0; m->hit_vk=0;
profile_reset(m);
ProfBase pb; prof_base(m,&pb);
- atomic_store(&g_mir_bytes[0],0); atomic_store(&g_mir_bytes[1],0);
- atomic_store(&g_mir_nread[0],0); atomic_store(&g_mir_nread[1],0);
+ for(int r=0;r<MIR_REPS;r++){ atomic_store(&g_mir_bytes[r],0); atomic_store(&g_mir_nread[r],0); }
double t0=now_s(); int steps=0;
for(int i=np-1;i<nfull-1;i++){
double tf0=g_prof?now_s():0;
@@ -6762,16 +6768,16 @@ static void pin_wire(Model *m){
static double mirror_probe_bw(shards *S,int rep){
int big=-1; int64_t bsz=0;
for(int i=0;i<S->nfd;i++){
- if(rep && S->mfds[i]<0) continue;
- int64_t sz=lseek(rep?S->mfds[i]:S->fds[i],0,SEEK_END);
+ if(rep && S->mfds[rep-1][i]<0) continue;
+ int64_t sz=lseek(rep?S->mfds[rep-1][i]:S->fds[i],0,SEEK_END);
if(sz>bsz){ bsz=sz; big=i; }
}
const int64_t blk=19ll<<20; const int NB=8;
if(big<0 || bsz<blk*(NB+1)) return 0;
- int dfd = rep? S->mdfds[big] : S->dfds[big];
- int fd = dfd>=0? dfd : (rep? S->mfds[big] : S->fds[big]);
- if(dfd<0) fprintf(stderr,"[MIRROR] no O_DIRECT on %s: the probe may read the page cache β€” "
- "set COLI_DISK_WEIGHTS for an accurate split\n", rep?"the mirror":"the primary");
+ int dfd = rep? S->mdfds[rep-1][big] : S->dfds[big];
+ int fd = dfd>=0? dfd : (rep? S->mfds[rep-1][big] : S->fds[big]);
+ if(dfd<0) fprintf(stderr,"[MIRROR] no O_DIRECT on replica %d: the probe may read the page cache β€” "
+ "set COLI_DISK_WEIGHTS for an accurate split\n", rep);
double t0=now_s(); int64_t tot=0;
#pragma omp parallel for schedule(dynamic,1) reduction(+:tot)
for(int i=0;i<NB;i++){
@@ -6787,37 +6793,82 @@ static double mirror_probe_bw(shards *S,int rep){
return (dt>0 && tot>0)? tot/1e9/dt : 0;
}
-/* DUAL-SSD setup: register the mirror copy, derive the read split from
- * COLI_DISK_WEIGHTS=<primary>,<mirror> or from a startup bandwidth probe.
- * Runs after model_init (needs the shard index) and BEFORE any pin/autopin
- * load, so the OMP-parallel pin warmup already streams from both drives. */
+/* MULTI-SSD setup: register every mirror copy listed in COLI_MODEL_MIRROR
+ * (';' or ',' separated dirs), derive the read split from
+ * COLI_DISK_WEIGHTS=<primary>,<mirror>[,<mirror2>...] or from a startup
+ * bandwidth probe. Runs after model_init (needs the shard index) and BEFORE
+ * any pin/autopin load, so the OMP-parallel pin warmup streams from all drives. */
static void mirror_setup(Model *m){
if(!g_mirror_dir) return;
- const char *snap=getenv("SNAP");
- if(snap && !strcmp(snap,g_mirror_dir)){
- fprintf(stderr,"[MIRROR] mirror dir equals the model dir β€” ignored\n"); return;
- }
- int nf=st_mirror_init(&m->S,g_mirror_dir);
- if(nf<=0){
- fprintf(stderr,"[MIRROR] %s: no usable shard (missing or divergent copy) β€” "
- "running on the primary drive only\n",g_mirror_dir);
+ const char *snap=getenv("SNAP"); if(!snap||!*snap) snap=getenv("COLI_MODEL");
+ st_mirror_reset(&m->S);
+ int nrep=1;
+ { char buf[4096]; snprintf(buf,sizeof(buf),"%s",g_mirror_dir);
+ char *p=buf;
+ while(p && *p){
+ char *sep=p; while(*sep && *sep!=';' && *sep!=',') sep++;
+ int last=(*sep==0); *sep=0;
+ while(*p==' ') p++;
+ size_t plen=strlen(p); while(plen>0 && p[plen-1]==' ') p[--plen]=0;
+ if(*p){
+ if(snap && !strcmp(snap,p))
+ fprintf(stderr,"[MIRROR] %s equals the model dir β€” ignored\n",p);
+ else if(nrep>=MIR_REPS)
+ fprintf(stderr,"[MIRROR] %s: too many mirrors (max %d) β€” ignored\n",p,ST_MAX_MIR);
+ else {
+ int nf=st_mirror_add(&m->S,p);
+ if(nf<=0)
+ fprintf(stderr,"[MIRROR] %s: no usable shard (missing or divergent copy) β€” skipped\n",p);
+ else {
+ fprintf(stderr,"[MIRROR] %s: %d/%d shards (replica %d)\n",p,nf,m->S.nfd,nrep);
+ nrep++;
+ }
+ }
+ }
+ p=last?NULL:sep+1;
+ }
+ }
+ if(nrep<2){
+ fprintf(stderr,"[MIRROR] no usable mirror β€” running on the primary drive only\n");
return;
}
- g_mirror=1;
- double wp=0,wm=0; const char *w=getenv("COLI_DISK_WEIGHTS"); const char *how="COLI_DISK_WEIGHTS";
- if(w && (sscanf(w," %lf , %lf",&wp,&wm)!=2 || wp<=0 || wm<=0)){
- fprintf(stderr,"[MIRROR] invalid COLI_DISK_WEIGHTS '%s' (want e.g. 9,3) β€” probing instead\n",w);
- wp=wm=0;
- }
- if(wp<=0||wm<=0){
- wp=mirror_probe_bw(&m->S,0); wm=mirror_probe_bw(&m->S,1); how="measured";
- if(wp>0&&wm>0) fprintf(stderr,"[MIRROR] probe: primary %.2f GB/s, mirror %.2f GB/s\n",wp,wm);
- else { wp=wm=1; how="fallback 1:1 (probe failed)"; }
- }
- int share=(int)(256.0*wm/(wp+wm)+0.5);
- g_mir_share = share<1?1 : share>255?255 : share;
- fprintf(stderr,"[MIRROR] %s: %d/%d shards | read split primary %.0f%% / mirror %.0f%% (%s)\n",
- g_mirror_dir,nf,m->S.nfd,100.0*(256-g_mir_share)/256,100.0*g_mir_share/256,how);
+ g_mirror=1; g_mir_nrep=nrep;
+ double wt[MIR_REPS]; int have=0;
+ const char *w=getenv("COLI_DISK_WEIGHTS"); const char *how="COLI_DISK_WEIGHTS";
+ if(w && *w){
+ char wb[256]; snprintf(wb,sizeof(wb),"%s",w); int wn=0, bad=0;
+ for(char *tok=strtok(wb,", "); tok; tok=strtok(NULL,", ")){
+ double v=atof(tok);
+ if(v<=0 || wn>=MIR_REPS){ bad=1; break; }
+ wt[wn++]=v;
+ }
+ if(!bad && wn==nrep) have=1;
+ else fprintf(stderr,"[MIRROR] invalid COLI_DISK_WEIGHTS '%s' (want %d positive comma-separated "
+ "weights, e.g. 9,3) β€” probing instead\n",w,nrep);
+ }
+ if(!have){
+ have=1; how="measured";
+ for(int r=0;r<nrep;r++){ wt[r]=mirror_probe_bw(&m->S,r); if(wt[r]<=0) have=0; }
+ if(have){
+ fprintf(stderr,"[MIRROR] probe:");
+ for(int r=0;r<nrep;r++) fprintf(stderr,"%s %s %.2f GB/s",r?" |":"",r?"mirror":"primary",wt[r]);
+ fprintf(stderr,"\n");
+ } else { for(int r=0;r<nrep;r++) wt[r]=1; how="fallback 1:1 (probe failed)"; }
+ }
+ double W=0; for(int r=0;r<nrep;r++) W+=wt[r];
+ int acc=0; double cum=0;
+ for(int r=0;r<nrep;r++){
+ cum+=wt[r];
+ int c=(int)(256.0*cum/W+0.5);
+ if(c<=acc) c=acc+1; /* every replica keeps a non-empty slice */
+ if(c>256) c=256;
+ g_mir_cut[r]=c; acc=c;
+ }
+ g_mir_cut[nrep-1]=256; /* the last replica absorbs rounding */
+ fprintf(stderr,"[MIRROR] %d drives | read split",nrep);
+ for(int r=0;r<nrep;r++){ int lo=r?g_mir_cut[r-1]:0;
+ fprintf(stderr,"%s %.0f%%",r?" /":"",100.0*(g_mir_cut[r]-lo)/256); }
+ fprintf(stderr," (%s)\n",how);
}
typedef struct { int l,e; uint32_t c; } PinRec;
diff --git a/c/st.h b/c/st.h
index 244ecda6..2b87f920 100644
--- a/c/st.h
+++ b/c/st.h
@@ -40,9 +40,11 @@ typedef struct {
int dfds[512]; /* gemelli O_DIRECT (aperti pigramente): -2 = non ancora provato */
char *paths[512];
int nfd;
- int mfds[512]; /* MIRROR: fds of the second model copy (dual-SSD), -1 = absent */
- int mdfds[512]; /* O_DIRECT twins of the second copy, -1 = absent */
- int nmirror; /* files accepted into the mirror (0 = mirror inactive) */
+#define ST_MAX_MIR 4 /* extra read replicas beyond the primary (multi-SSD) */
+ int mfds[ST_MAX_MIR][512]; /* MIRROR: fds of replica copy r+1 (multi-SSD), -1 = absent */
+ int mdfds[ST_MAX_MIR][512]; /* O_DIRECT twins of the replica copies, -1 = absent */
+ int nmirror[ST_MAX_MIR]; /* files accepted into replica r+1 */
+ int nrep; /* registered replica copies (0 = mirror inactive) */
int *hidx; /* hash map nome->indice (open addressing): con ~120k tensori
* (GLM: 256 expert x 78 layer x 3 x 2) la scansione lineare
* costava decine di secondi/token (misurato sul primo run reale) */
@@ -110,34 +112,41 @@ static int st_direct_fd(shards *S, int fd) {
int i = st_fidx(S, fd); return i < 0 ? -1 : S->dfds[i];
}
-/* ---- MIRROR (dual-SSD): second read-only copy of the model on another drive ----
- * st_fd_rep/st_direct_fd_rep: fd of replica `rep` (0 = primary, 1 = mirror) for
- * the SAME file identified by its primary fd. -1 if that replica is absent. */
+/* ---- MIRROR (multi-SSD): read-only copies of the model on other drives ----
+ * st_fd_rep/st_direct_fd_rep: fd of replica `rep` (0 = primary, 1..nrep =
+ * mirrors) for the SAME file identified by its primary fd. -1 if absent. */
static int st_fd_rep(shards *S, int fd, int rep) {
if (!rep) return fd;
- if (!S->nmirror) return -1;
- int i = st_fidx(S, fd); return i < 0 ? -1 : S->mfds[i];
+ if (rep > S->nrep) return -1;
+ int i = st_fidx(S, fd); return i < 0 ? -1 : S->mfds[rep-1][i];
}
static int st_direct_fd_rep(shards *S, int fd, int rep) {
if (!rep) return st_direct_fd(S, fd);
- if (!S->nmirror) return -1;
- int i = st_fidx(S, fd); return i < 0 ? -1 : S->mdfds[i];
+ if (rep > S->nrep) return -1;
+ int i = st_fidx(S, fd); return i < 0 ? -1 : S->mdfds[rep-1][i];
}
-/* Registers <dir>/<basename> as a read replica of every already-indexed shard.
- * A file is accepted ONLY if its size and safetensors header are byte-identical
- * to the primary: the data_offsets then match by construction, so every pread
- * is valid on either copy. Missing or divergent files simply stay on the
- * primary (the mirror may be partial, e.g. a smaller SSD holding only the
- * expert shards). Returns the number of accepted files. The mirror is NEVER
- * written to: .coli_usage/.coli_kv keep deriving from the primary alone. */
-static int st_mirror_init(shards *S, const char *dir) {
- if (S->nmirror) for (int i = 0; i < S->nfd; i++) { /* re-init: drop the old replica */
- if (S->mfds[i] >= 0) close(S->mfds[i]);
- if (S->mdfds[i] >= 0) close(S->mdfds[i]);
+/* Registers <dir>/<basename> as read replica S->nrep+1 of every already-indexed
+ * shard. A file is accepted ONLY if its size and safetensors header are
+ * byte-identical to the primary: the data_offsets then match by construction,
+ * so every pread is valid on any copy. Missing or divergent files simply stay
+ * on the primary (a mirror may be partial, e.g. a smaller SSD holding only the
+ * expert shards). Returns the number of accepted files; a dir contributing 0
+ * files claims no replica slot. Mirrors are NEVER written to: .coli_usage /
+ * .coli_kv keep deriving from the primary alone. */
+static void st_mirror_reset(shards *S) { /* re-init: drop every replica */
+ for (int r = 0; r < S->nrep; r++) for (int i = 0; i < S->nfd; i++) {
+ if (S->mfds[r][i] >= 0) close(S->mfds[r][i]);
+ if (S->mdfds[r][i] >= 0) close(S->mdfds[r][i]);
}
- for (int i = 0; i < ST_MAX_SHARDS; i++) { S->mfds[i] = -1; S->mdfds[i] = -1; }
- S->nmirror = 0;
+ memset(S->nmirror, 0, sizeof(S->nmirror));
+ S->nrep = 0;
+}
+static int st_mirror_add(shards *S, const char *dir) {
+ if (S->nrep >= ST_MAX_MIR) return 0;
+ int r = S->nrep;
+ for (int i = 0; i < ST_MAX_SHARDS; i++) { S->mfds[r][i] = -1; S->mdfds[r][i] = -1; }
+ S->nmirror[r] = 0;
for (int i = 0; i < S->nfd; i++) {
const char *base = strrchr(S->paths[i], '/');
#ifdef _WIN32
@@ -166,15 +175,21 @@ static int st_mirror_init(shards *S, const char *dir) {
fprintf(stderr, "[MIRROR] %s: header differs from the primary copy β€” file skipped\n", mp);
close(mfd); continue;
}
- S->mfds[i] = mfd;
+ S->mfds[r][i] = mfd;
#ifdef O_DIRECT
- S->mdfds[i] = open(mp, COMPAT_O_RDONLY | O_DIRECT);
+ S->mdfds[r][i] = open(mp, COMPAT_O_RDONLY | O_DIRECT);
#elif defined(__APPLE__) || defined(_WIN32)
- S->mdfds[i] = compat_open_direct(mp);
+ S->mdfds[r][i] = compat_open_direct(mp);
#endif
- S->nmirror++;
+ S->nmirror[r]++;
}
- return S->nmirror;
+ if (S->nmirror[r] > 0) { S->nrep++; return S->nmirror[r]; }
+ return 0;
+}
+/* backward-compatible single-mirror entry point */
+static int st_mirror_init(shards *S, const char *dir) {
+ st_mirror_reset(S);
+ return st_mirror_add(S, dir);
}
/* indicizza tutti i model-*.safetensors in snap_dir */
diff --git a/c/tests/test_st_mirror.c b/c/tests/test_st_mirror.c
index f6bbdd33..83909ba5 100644
--- a/c/tests/test_st_mirror.c
+++ b/c/tests/test_st_mirror.c
@@ -1,6 +1,6 @@
-/* Dual-SSD mirror (st_mirror_init & friends): a second read-only model copy is
+/* Multi-SSD mirror (st_mirror_add & friends): read-only model copies are
* accepted only when byte-identical in size and safetensors header, reads on
- * either replica return the same bytes, and divergent/missing copies degrade
+ * any replica return the same bytes, and divergent/missing copies degrade
* to the primary instead of being trusted. Fixture dirs are created in the
* current working directory and removed on exit. */
#include <stdint.h>
@@ -32,6 +32,7 @@
#define DIR_C "tmp_mirror_c" /* same size, divergent header */
#define DIR_D "tmp_mirror_d" /* different size */
#define DIR_E "tmp_mirror_e" /* empty (missing file) */
+#define DIR_F "tmp_mirror_f" /* second identical copy (multi-SSD) */
/* one-tensor safetensors file; flip lets us corrupt one header byte and
* pad lets us grow the payload, both without changing anything else */
@@ -54,8 +55,8 @@ static int write_model(const char *dir, int flip, int pad) {
}
static void cleanup(void) {
- const char *dirs[] = {DIR_A, DIR_B, DIR_C, DIR_D, DIR_E};
- for (int i = 0; i < 5; i++) {
+ const char *dirs[] = {DIR_A, DIR_B, DIR_C, DIR_D, DIR_E, DIR_F};
+ for (int i = 0; i < 6; i++) {
char path[256];
snprintf(path, sizeof(path), "%s/model.safetensors", dirs[i]);
remove(path);
@@ -66,11 +67,12 @@ static void cleanup(void) {
int main(void) {
cleanup();
CHECK(MKDIR(DIR_A) == 0 && MKDIR(DIR_B) == 0 && MKDIR(DIR_C) == 0 &&
- MKDIR(DIR_D) == 0 && MKDIR(DIR_E) == 0);
+ MKDIR(DIR_D) == 0 && MKDIR(DIR_E) == 0 && MKDIR(DIR_F) == 0);
CHECK(write_model(DIR_A, 0, 0) == 0);
CHECK(write_model(DIR_B, 0, 0) == 0);
CHECK(write_model(DIR_C, 1, 0) == 0);
CHECK(write_model(DIR_D, 0, 64) == 0);
+ CHECK(write_model(DIR_F, 0, 0) == 0);
shards S;
st_init(&S, DIR_A);
@@ -109,6 +111,29 @@ int main(void) {
/* unknown fd never maps to a replica */
CHECK(st_fd_rep(&S, 987654, 1) == -1);
+ /* ---- multi-SSD: two identical copies become replicas 1 and 2 ---- */
+ st_mirror_reset(&S);
+ CHECK(st_mirror_add(&S, DIR_B) == 1);
+ CHECK(st_mirror_add(&S, DIR_F) == 1);
+ CHECK(S.nrep == 2);
+ int f1 = st_fd_rep(&S, t->fd, 1), f2 = st_fd_rep(&S, t->fd, 2);
+ CHECK(f1 >= 0 && f2 >= 0 && f1 != f2 && f1 != t->fd && f2 != t->fd);
+ float c2[8];
+ CHECK(pread(f2, c2, t->nbytes, t->off) == t->nbytes);
+ CHECK(memcmp(a, c2, sizeof(a)) == 0);
+ st_prefetch_rep(&S, "t0", 2); /* smoke: WILLNEED on the second mirror */
+
+ /* a divergent dir claims no replica slot; existing replicas survive */
+ CHECK(st_mirror_add(&S, DIR_C) == 0);
+ CHECK(S.nrep == 2);
+ CHECK(st_fd_rep(&S, t->fd, 1) == f1 && st_fd_rep(&S, t->fd, 2) == f2);
+ CHECK(st_fd_rep(&S, t->fd, 3) == -1);
+
+ /* reset drops every replica */
+ st_mirror_reset(&S);
+ CHECK(S.nrep == 0);
+ CHECK(st_fd_rep(&S, t->fd, 1) == -1);
+
cleanup();
puts("safetensors mirror tests: ok");
return 0;
diff --git a/docs/ENVIRONMENT.md b/docs/ENVIRONMENT.md
index 9e97d303..16fb3240 100644
--- a/docs/ENVIRONMENT.md
+++ b/docs/ENVIRONMENT.md
@@ -83,8 +83,8 @@ Format: `VAR` β€” default β€” effect.
| Variable | Default | Effect |
|---|---|---|
| `COLI_MODEL_DIRS` | unset | SPLIT the model across 2+ drives: a `;`/`,`-separated list of extra directories, each holding a **distinct** subset of the `.safetensors` shards (no duplication). Shards act as a search path β€” every shard is read from whichever drive holds it, so concurrent expert loads parallelise across drives and combined capacity is used. Scales to N drives. Metadata (config/tokenizer/`.coli_usage`) stays in the primary `COLI_MODEL` dir. Pairs well with `PIPE=1` (concurrent loaders) + `DIRECT=1`. Distinct from β€” and composable with β€” `COLI_MODEL_MIRROR`: the mirror is matched per-shard by basename against the merged (split) index, so a mirror dir may hold a copy of any subset of the split's shards. |
-| `COLI_MODEL_MIRROR` | unset | Path to a second, byte-identical (read-only) copy of the model on another drive; expert reads are split across both. Partial mirrors work (only the shards present are used). |
-| `COLI_DISK_WEIGHTS` | unset (startup bandwidth probe) | Split ratio `<primary>,<mirror>` (e.g. `1,1` for 50/50, `9,3` for a fast+slow pair). Unset = probe both drives with the engine's own access pattern at startup. |
+| `COLI_MODEL_MIRROR` | unset | `;`/`,`-separated list of directories, each a byte-identical (read-only) copy of the model on another drive; expert reads split across the primary and every mirror. Partial mirrors work (only the shards present are used). |
+| `COLI_DISK_WEIGHTS` | unset (startup bandwidth probe) | Split ratio `<primary>,<mirror>[,<mirror2>...]` β€” one positive weight per drive (e.g. `1,1` for 50/50, `9,3` for a fast+slow pair, `1,1,1` for a 3-way mirror). Unset = probe every drive with the engine's own access pattern at startup. |
Per-drive byte counts are reported in a `MIRROR:` stats line. Combine with `DIRECT=1` so the two copies never compete for page cache.