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

fp8: wire qt_scale_bytes() into qt_wire_mmap/qt_unwire_mmap

monotophic5528589558d3
diff --git a/c/colibri.c b/c/colibri.c
index 6eaa3f18..fda24ab6 100644
--- a/c/colibri.c
+++ b/c/colibri.c
@@ -205,9 +205,9 @@ static int64_t qt_bytes(const QT *t){ /* byte residenti del tensore */
* qt_wire_mmap/qt_unwire_mmap mlock the weight and scale ranges as TWO SEPARATE
* regions (separate allocations, not one contiguous buffer: qt_from_disk always
* qalloc's t->q8/t->q4 and t->s independently) and need the scale byte count on
- * its own to split qt_bytes(t) into a weight half and a scale half. The two call
- * sites used to hardcode scale_b=O*4 -- i.e. assume PER-ROW scale for every
- * format -- which is right for fmt 0/1/2/3 but wrong for fmt=4 (grouped,
+ * its own to split qt_bytes(t) into a weight half and a scale half. Hardcoding
+ * scale_b=O*4 at those two call sites -- i.e. assuming PER-ROW scale for every
+ * format -- is right for fmt 0/1/2/3 but wrong for fmt=4 (grouped,
* O*ceil(I/gs) scales), fmt=5 (int3-g64, O*ceil(I/64) scales), and fmt=7
* (per-128x128-block, ceil(O/128)*ceil(I/128) scales): any tensor in one of
* those three formats reaching qt_wire_mmap/qt_unwire_mmap would mlock/munlock
@@ -217,14 +217,21 @@ static int64_t qt_bytes(const QT *t){ /* byte residenti del tensore */
* qt_bytes' `+4` literal above), and t->fmt==6 tensors never reach
* qt_wire_mmap/qt_unwire_mmap's mlock path in this build (int3-g64/E8 stay
* CPU-side, see qt_cuda_upload -- MMAP wiring is a CUDA/Metal-adjacent memory
- * concern this build doesn't extend to them). fixed generically since the same
- * bug shape applies to fmt=4/5, not just the format that surfaced it -- fmt=4
- * is the routed-expert "int4-g64" format (qt_resolve_fmt assigns it to ESlot
- * g/u/d the same as any other tensor, see expert_load_impl), so this is not
- * purely a dormant fmt=7-only fix: any COLI_MMAP + mem_should_wire() session
- * pinning fmt=4 experts was already mlocking the wrong ranges before this
- * branch existed. UNVERIFIED at runtime (no model run performed -- static/
- * arithmetic fix only, see the report). */
+ * concern this build doesn't extend to them). This is not purely a dormant
+ * fmt=7-only fix: fmt=4 is the routed-expert "int4-g64" format (qt_resolve_fmt
+ * assigns it to ESlot g/u/d the same as any other tensor, see
+ * expert_load_impl), so any COLI_MMAP + mem_should_wire() session pinning
+ * fmt=4 experts mlocks the wrong ranges without this. UNVERIFIED at runtime
+ * (no model run performed -- static/arithmetic fix only, see the report).
+ *
+ * REVIEW FINDING (maintainer, #528): this function existed correctly (the
+ * fmt=7 branch below is right) but neither qt_wire_mmap nor qt_unwire_mmap
+ * actually called it -- both independently hardcoded scale_b=(int64_t)t->O*4
+ * inline, so the "fix" this comment describes was never applied at either
+ * call site, and the resulting -Wunused-function warning on this then-dead
+ * function was suppressed by -Wno-unused-function in CFLAGS rather than
+ * caught. qt_wire_split() below is now the ONE place both call sites get
+ * weight_b/scale_b from -- see it and its call sites for the actual fix. */
static int64_t qt_scale_bytes(const QT *t){
if(t->fmt==4){ int ng=(t->I+t->gs-1)/t->gs; return (int64_t)t->O*ng*4; }
if(t->fmt==5){ int64_t ng=((int64_t)t->I+63)/64; return (int64_t)t->O*ng*4; }
@@ -233,6 +240,16 @@ static int64_t qt_scale_bytes(const QT *t){
* fmt=6, the 4-byte tag -- callers of this function never see fmt=6,
* see the comment above; the fallback is harmless dead code for it). */
}
+/* qt_wire_mmap/qt_unwire_mmap's shared weight/scale byte-range split -- the ONE
+ * place that computes it, so the two sites can never independently drift back
+ * to a hardcoded per-row-only formula (that drift -- qt_scale_bytes() existing
+ * but unused at both sites -- was the maintainer's #528 finding, see the
+ * comment above qt_scale_bytes()). Directly unit-tested (test_fp8_load.c) so a
+ * regression here, not just at a call site, fails the suite. */
+static void qt_wire_split(const QT *t, int64_t *weight_b, int64_t *scale_b){
+ *scale_b = qt_scale_bytes(t);
+ *weight_b = qt_bytes(t) - *scale_b;
+}
typedef struct {
float *in_ln, *post_ln;
@@ -7268,8 +7285,7 @@ static int mem_wire(void *addr, size_t len){
static void qt_unwire_mmap(QT *t){
if(!g_mmap || !mem_should_wire()) return;
if(!t->q8 && !t->q4) return;
- int64_t scale_b=(int64_t)t->O*4;
- int64_t weight_b=qt_bytes(t)-scale_b;
+ int64_t weight_b, scale_b; qt_wire_split(t,&weight_b,&scale_b);
void *wp=t->q8?(void*)t->q8:(void*)t->q4;
#if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
if(weight_b>0 && !munlock(wp,(size_t)weight_b)) g_mmap_wired-=weight_b;
@@ -7282,8 +7298,7 @@ static void qt_unwire_mmap(QT *t){
static void qt_wire_mmap(QT *t, int64_t *wired, long *failed){
if(!t->q8 && !t->q4) return;
if(t->cuda_eligible) return; /* resident in VRAM; host range is dead weight */
- int64_t scale_b=(int64_t)t->O*4;
- int64_t weight_b=qt_bytes(t)-scale_b;
+ int64_t weight_b, scale_b; qt_wire_split(t,&weight_b,&scale_b);
void *wp=t->q8?(void*)t->q8:(void*)t->q4;
if(weight_b>0){ if(mem_wire(wp,(size_t)weight_b)==0) *wired+=weight_b; else (*failed)++; }
if(t->s && scale_b>0){ if(mem_wire(t->s,(size_t)scale_b)==0) *wired+=scale_b; else (*failed)++; }
diff --git a/c/tests/test_fp8_load.c b/c/tests/test_fp8_load.c
index 49ca2e67..10d84074 100644
--- a/c/tests/test_fp8_load.c
+++ b/c/tests/test_fp8_load.c
@@ -399,6 +399,40 @@ static void check_fp8_bytes(int O, int I, const char *tag){
if(!(O==1 && I==1)) CHECK(got_total != old_wrong);
}
+/* qt_wire_mmap()/qt_unwire_mmap() (colibri.c) both compute weight_b/scale_b via
+ * qt_wire_split(t,&weight_b,&scale_b) -- this calls that EXACT shared function
+ * directly (no mlock syscall: same reasoning as check_fp8_bytes above, mem_wire's
+ * actual RLIMIT_MEMLOCK behavior is environment-dependent and not what changed)
+ * so a regression at qt_wire_split() itself -- e.g. reverting to the
+ * scale_b=(int64_t)t->O*4 hardcode the maintainer's #528 review found dead-coded
+ * behind an unused qt_scale_bytes() (compiling clean only because
+ * -Wno-unused-function suppressed the warning that should have caught it) --
+ * fails here. Covers every format qt_wire_split's callers can see in practice
+ * (fmt=1 per-row unaffected; fmt=4/5 grouped-scale formats qt_scale_bytes'
+ * own comment names as previously-broken too; fmt=7 nblk>O, the shape this
+ * review round is about). */
+static void check_wire_split(int fmt, int O, int I, int gs, const char *tag){
+ QT t; memset(&t,0,sizeof t); t.fmt=fmt; t.O=O; t.I=I; t.gs=gs;
+ int64_t want_scale = qt_scale_bytes(&t);
+ int64_t want_weight = qt_bytes(&t) - want_scale;
+ int64_t got_weight=-1, got_scale=-1;
+ qt_wire_split(&t,&got_weight,&got_scale);
+ if(got_scale != want_scale)
+ printf("FAIL %s: qt_wire_split scale_b=%lld want=%lld\n", tag, (long long)got_scale, (long long)want_scale);
+ CHECK(got_scale == want_scale);
+ if(got_weight != want_weight)
+ printf("FAIL %s: qt_wire_split weight_b=%lld want=%lld\n", tag, (long long)got_weight, (long long)want_weight);
+ CHECK(got_weight == want_weight);
+ /* the regression this test exists to catch: a per-row-only scale_b==O*4
+ * must NOT be what qt_wire_split returns for a format whose real scale
+ * cardinality differs from O (fmt=4/5/7 here) -- confirm the value has
+ * actually moved off that old constant, not just matched qt_scale_bytes()
+ * by coincidence at a degenerate shape. */
+ int64_t old_wrong_scale = (int64_t)O*4;
+ if((fmt==4 || fmt==5 || fmt==7) && want_scale != old_wrong_scale)
+ CHECK(got_scale != old_wrong_scale);
+}
+
int main(void){
test_disambiguation();
test_fmt6_fp8_collision();
@@ -408,6 +442,11 @@ int main(void){
check_fp8_bytes(6144,2048, "qt_bytes fmt=7 down-shaped O=6144 I=2048");
check_fp8_bytes(130,200, "qt_bytes fmt=7 block edges O,I both non-mult-128");
check_fp8_bytes(1,1, "qt_bytes fmt=7 degenerate 1x1");
+ check_wire_split(1, 4096,4096, 0, "qt_wire_split fmt=1 plain int8 (per-row scale, unaffected by the fix)");
+ check_wire_split(4, 2048,6144, 64, "qt_wire_split fmt=4 grouped int4 (O*ceil(I/gs) scale, not O*4)");
+ check_wire_split(5, 2048,6144, 0, "qt_wire_split fmt=5 int3-g64 (O*ceil(I/64) scale, not O*4)");
+ check_wire_split(7, 2,16384, 0, "qt_wire_split fmt=7 nblk(128) >> O(2): scale=512B, NOT O*4=8B");
+ check_wire_split(7, 2048,6144, 0, "qt_wire_split fmt=7 spec example: scale=3072B, NOT O*4=8192B");
if(fails){ printf("fp8 loader-seam tests: %d FAILED\n", fails); return 1; }
printf("fp8 loader-seam tests: ok\n");
return 0;