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 dev into security branch: resolve #391/#413-era conflicts + fix fmt=5 scale cap

JustVuggcc7461f68bd1
diff --cc c/Makefile
index a9315012,f40c21c0..1799b6b5
--- a/c/Makefile
+++ b/c/Makefile
@@@ -166,10 -155,22 +171,22 @@@ ifneq ($(IS_WIN),
# '/Wextra'"), which made `make cuda-dll` unbuildable as shipped. -W3 is
# the MSVC warning level (dash form, NOT /W3: MSYS make would mangle the
# slash-form into a filesystem path).
-NVCCFLAGS ?= -O3 -std=c++17 -arch=$(CUDA_ARCH) -Xcompiler=-W3
+NVCCFLAGS ?= -O3 -std=c++17 $(CUDA_GENCODE) -Xcompiler=-W3
else
-NVCCFLAGS ?= -O3 -std=c++17 -arch=$(CUDA_ARCH) -Xcompiler=-Wall,-Wextra
+NVCCFLAGS ?= -O3 -std=c++17 $(CUDA_GENCODE) -Xcompiler=-Wall,-Wextra
endif
+ # HIP=1 builds the SAME backend for AMD GPUs via ROCm: backend_cuda.cu is
+ # compiled unchanged through backend_gpu_compat.h (one source, two vendors,
+ # like compat.h does for Windows). HIP_ARCH=native targets the GPU in this
+ # machine; set an explicit arch (e.g. HIP_ARCH=gfx1201) when distributing.
+ HIP ?= 0
+ ROCM_HOME ?= /opt/rocm
+ HIPCC ?= $(ROCM_HOME)/bin/hipcc
+ HIP_ARCH ?= native
+ HIPCCFLAGS ?= -O3 -std=c++17 -x hip --offload-arch=$(HIP_ARCH) -Wall -Wextra
+ GPUCC = $(NVCC)
+ GPUFLAGS = $(NVCCFLAGS)
+ GPUCC_NAME = nvcc (set CUDA_HOME or NVCC)
# PYTHON is a HOST tool (clean/test-c/test-python run it on the build machine),
# so key it off the host, NOT $(IS_WIN) — which is derived from the *target*
# triple ($(CC) -dumpmachine). Otherwise a cross build (make CC=x86_64-w64-
diff --cc c/colibri.c
index f1308260,5adf95b1..ec4c2173
--- a/c/colibri.c
+++ b/c/colibri.c
@@@ -1332,25 -819,17 +819,32 @@@ static void rope_interleave(float *v, i
}
/* ---------- config ---------- */
+/* SEC-9: bounded slurp for untrusted config/oracle JSON. config.json arrives from
+ * unverified mirrors (see qt_check_fmt threat model); an unbounded ftell->malloc
+ * gave a hostile file a load-time OOM or, on malloc failure, a NULL deref via
+ * b[got]=0. Cap the size, NULL-check the alloc, require a full read. Returns a
+ * malloc'd NUL-terminated buffer, or NULL on any failure. Mirrors tok.h tk_read_file. */
+#define CFG_MAX_BYTES (256ll<<20) /* config/oracle JSON is KB-MB in practice */
+static char* cfg_slurp(const char *path){
+ FILE *f=fopen(path,"rb"); if(!f) return NULL;
+ fseek(f,0,SEEK_END); long n=ftell(f); fseek(f,0,SEEK_SET);
+ if(n<0 || (long long)n>CFG_MAX_BYTES){ fclose(f); return NULL; }
+ char *b=malloc((size_t)n+1); if(!b){ fclose(f); return NULL; }
+ size_t got=fread(b,1,(size_t)n,f); fclose(f);
+ if((long)got!=n){ free(b); return NULL; }
+ b[got]=0; return b;
+}
static jval* cfg_root(const char *snap, char **arena){
char p[2048]; snprintf(p,sizeof(p),"%s/config.json",snap);
- char *b=cfg_slurp(p);
- if(!b){ fprintf(stderr,"%s: cannot read config.json (missing, unreadable, short, or > %lld bytes)\n",p,(long long)CFG_MAX_BYTES); exit(1); }
+ FILE *f=fopen(p,"rb"); if(!f){perror(p);exit(1);}
+ fseek(f,0,SEEK_END); long n=ftell(f); fseek(f,0,SEEK_SET);
+ /* SEC: config.json arriva dalla dir modello non fidata. Limita la dimensione
+ * (un file ostile enorme = OOM al load) e controlla la malloc: senza il NULL
+ * check, b[got]=0 su malloc fallita era un NULL-deref. */
+ if(n<0 || n>(256L<<20)){ fprintf(stderr,"%s: size %ld out of range (0..256 MB)\n",p,n); exit(1); }
+ char *b=malloc((size_t)n+1); if(!b){ fprintf(stderr,"OOM reading %s (%ld bytes)\n",p,n); exit(1); }
+ size_t got=fread(b,1,(size_t)n,f); b[got]=0; fclose(f);
+ if((long)got!=n) fprintf(stderr,"warning: short read on %s (%ld of %ld)\n",p,(long)got,n);
return json_parse(b,arena);
}
static int gi(jval*r,const char*k){ jval*v=json_get(r,k); return v?(int)v->num:0; }
@@@ -1497,12 -983,15 +998,22 @@@ static void qt_from_disk(Model *m, cons
else if(fmt==4){ int ng=(I+gs-1)/gs;
if(t->fmt!=4||!t->q4){ t->fmt=4; t->O=O; t->I=I; t->gs=gs; t->q4=qalloc(nb); t->s=falloc((int64_t)O*ng); }
st_read_raw(&m->S,name,t->q4,drop); }
+ else if(fmt==5){ int64_t ng=i3_groups(I); /* int3-g64: 24B/group weights + O*ng group scales */
+ if(t->fmt!=5||!t->q4){ t->fmt=5; t->O=O; t->I=I; t->gs=0; t->q4=qalloc(nb); t->s=falloc((int64_t)O*ng); }
+ st_read_raw(&m->S,name,t->q4,drop); }
else { if(t->fmt!=fmt||!t->q4){ t->fmt=fmt; t->O=O; t->I=I; t->gs=0; t->q4=qalloc(nb); t->s=qsalloc(O); } st_read_raw(&m->S,name,t->q4,drop); }
- st_read_f32_cap(&m->S,sn,t->s, fmt==4?(int64_t)O*((I+gs-1)/gs):(int64_t)O, drop);
- st_read_f32(&m->S,sn,t->s,drop);
++ /* cap MUST match the scale cardinality qt_resolve_fmt already validated and
++ * the falloc above actually reserved, per format: grouped-int4 (fmt=4) keeps
++ * O*ceil(I/gs) scales and int3-g64 (fmt=5) keeps O*i3_groups(I); everything
++ * else is per-row O. Using the per-row bound for a grouped format would
++ * reject a legitimate container (fmt=5 regressed exactly that way). */
++ st_read_f32_cap(&m->S,sn,t->s,
++ fmt==4 ? (int64_t)O*((I+gs-1)/gs) :
++ fmt==5 ? (int64_t)O*i3_groups(I) : (int64_t)O, drop);
} else {
if(!t->qf && !t->q8 && !t->q4) qt_alloc(t,O,I,bits);
- if(t->fmt==0) st_read_f32(&m->S,name,t->qf,drop);
- else { float *tmp=falloc((int64_t)O*I); st_read_f32(&m->S,name,tmp,drop); qt_fill(t,tmp,bits); free(tmp); }
+ if(t->fmt==0) st_read_f32_cap(&m->S,name,t->qf,(int64_t)O*I,drop);
+ else { float *tmp=falloc((int64_t)O*I); st_read_f32_cap(&m->S,name,tmp,(int64_t)O*I,drop); qt_fill(t,tmp,bits); free(tmp); }
}
}
static QT qt_load(Model *m, const char *name, int O, int I, int bits){
@@@ -1711,8 -1213,15 +1235,16 @@@ static void model_init(Model *m, const
/* embed: dequantizza la riga del token (scala per-riga) in x[hidden] */
static void embed_row(Model *m, int tok, float *x){
int D=m->c.hidden; QT *e=&m->embed;
+ if(tok<0 || tok>=e->O){ memset(x,0,(size_t)D*sizeof(float)); return; } /* #SEC-5: out-of-range token id -> zero row, never OOB */
if(e->fmt==0){ memcpy(x, e->qf+(int64_t)tok*D, D*sizeof(float)); return; }
+ if(e->fmt==4){ /* grouped int4: per-group scale (embed/lm_head at io_bits, usually fmt 0/1) */
+ const uint8_t *q=e->q4+(int64_t)tok*((D+1)/2); int gs=e->gs,ng=(D+gs-1)/gs;
+ const float *scl=e->s+(int64_t)tok*ng;
+ for(int g=0;g*gs<D;g++){ int base=g*gs,glen=gs; if(base+glen>D)glen=D-base; float s=scl[g];
+ for(int i=base;i+1<base+glen;i+=2){ uint8_t byte=q[i>>1]; x[i]=(float)((int)(byte&0xF)-8)*s;
+ x[i+1]=(float)((int)(byte>>4)-8)*s; }
+ if(glen&1){ uint8_t byte=q[(base+glen-1)>>1]; x[base+glen-1]=(float)((int)(byte&0xF)-8)*s; } }
+ return; }
if(e->fmt==1){ const int8_t *q=e->q8+(int64_t)tok*D; float s=e->s[tok];
for(int i=0;i<D;i++) x[i]=(float)q[i]*s; return; }
if(e->fmt==2){ const uint8_t *q=e->q4+(int64_t)tok*((D+1)/2); float s=e->s[tok]; /* int4 */
diff --cc c/tests/test_dsa_select
index 00000000,00000000..777f9918
new file mode 100644
Binary files differ
diff --cc c/tests/test_e8_kernel
index 00000000,00000000..8ee07b36
new file mode 100644
Binary files differ
diff --cc c/tests/test_i4_grouped
index 00000000,00000000..bf3e1ce8
new file mode 100644
Binary files differ
diff --cc c/tests/test_int3
index 00000000,00000000..e2745b28
new file mode 100644
Binary files differ
diff --cc c/tests/test_int3_load
index 00000000,00000000..afd3b135
new file mode 100644
Binary files differ
diff --cc c/tests/test_kv_alloc
index 00000000,00000000..3625b61a
new file mode 100644
Binary files differ
diff --cc c/tests/test_logit_nan
index 00000000,00000000..77b34c55
new file mode 100644
Binary files differ
diff --cc c/tests/test_pipe_block
index 00000000,00000000..8aac3044
new file mode 100644
Binary files differ
diff --cc c/tests/test_sample_nan
index 00000000,00000000..04a0f699
new file mode 100644
Binary files differ
diff --cc c/tests/test_stops
index 00000000,00000000..966f729f
new file mode 100644
Binary files differ
diff --cc c/tests/test_tok_o200k
index 00000000,00000000..56771a5a
new file mode 100644
Binary files differ
diff --cc c/tests/test_topp
index 00000000,00000000..9902b5fb
new file mode 100644
Binary files differ
diff --cc web/src/Brain.tsx
index b5635e74,6b4f70ed..e67310b3
--- a/web/src/Brain.tsx
+++ b/web/src/Brain.tsx
@@@ -164,13 -159,13 +162,13 @@@ export function Brain({ baseUrl, apiKey
const realLayer = isMtp ? 78 : tip.row + 3
const entry = atlas?.[`${realLayer}:${tip.col}`]
return (
- <div className="brain-tip" style={{ left: tip.x + 14, top: tip.y + 14 }}>
+ <div className="brain-tip" style={{ left: Math.min(tip.x + 14, window.innerWidth - 260), top: Math.min(tip.y + 14, window.innerHeight - 170) }}>
<div className="brain-tip-title"><Layers className="size-3" /> Layer {realLayer}{isMtp ? " (MTP)" : ""} · Expert {tip.col}</div>
- <div>Tier: <strong style={{ color: ["#8b9aa3", "#5a9bd8", "#4ed6a5"][tip.tier] }}>{TIER_NAME[tip.tier]}</strong></div>
- <div>Heat: <strong>{tip.heat === 0 ? "never routed" : `~2^${tip.heat} selections`}</strong></div>
+ <div>Tier: <strong style={{ color: ["#8b9aa3", "#5a9bd8", "#4ed6a5"][tip.tier] }}>{t(TIER_KEYS[tip.tier])}</strong></div>
+ <div>Heat: <strong>{tip.heat === 0 ? t("brain.neverRouted") : t("brain.selections", { heat: tip.heat })}</strong></div>
{entry ? <>
<div className={entry.label.startsWith("specialist") ? "brain-tip-spec" : undefined}>
- {entry.label.startsWith("specialist") ? `⭐ Specialist: ${entry.top}` : "Generalist"}
+ {entry.label.startsWith("specialist") ? t("brain.specialist", { top: entry.top }) : t("brain.generalist")}
<small> (entropy {entry.entropy})</small>
</div>
<div className="brain-tip-aff">{Object.entries(entry.affinity).sort((a, b) => b[1] - a[1]).slice(0, 3)