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

test: record sparse routing benchmark

EricSpencer00b8672608004f
diff --git a/c/Makefile b/c/Makefile
index ba136a48..7e679191 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -1809,6 +1809,11 @@ tests/test_ssd_probe$(EXE): tests/test_ssd_probe.c colibri.c st.h uring.h json.h
tests/bench_dsa_select$(EXE): tests/bench_dsa_select.c colibri.c st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h tier.h quant.h sample.h kv_persist.h telemetry.h route_trace.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
+# bench_router_select is a microbenchmark (duplicate-prefix scan vs marked-score scan),
+# not a test gate. Build on demand: make tests/bench_router_select.
+tests/bench_router_select$(EXE): tests/bench_router_select.c colibri.c st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h tier.h quant.h sample.h kv_persist.h telemetry.h route_trace.h
+ $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
+
# bench_indexer_allocations: microbenchmark (DeepSeek V4 indexer malloc vs persistent arena scratch), NOT a test gate.
# Build on demand: make tests/bench_indexer_allocations
tests/bench_indexer_allocations$(EXE): tests/bench_indexer_allocations.c
diff --git a/c/tests/bench_router_select.c b/c/tests/bench_router_select.c
new file mode 100644
index 00000000..6e79af07
--- /dev/null
+++ b/c/tests/bench_router_select.c
@@ -0,0 +1,107 @@
+/* Microbenchmark: old duplicate-prefix router scan vs marked-score selection.
+ *
+ * This measures only top-K expert selection. The input is prepared before each
+ * timed call, and both arms use the same finite score arrays and fallback path.
+ * The full run uses 11 seeds and 2,000 repetitions per cell; correctness is
+ * covered separately by test_router_nan.c.
+ */
+#define main coli_glm_main_unused
+#include "../colibri.c"
+#undef main
+
+#include <stdint.h>
+
+#define N_REPEAT 2000
+#define N_SEEDS 11
+
+static int select_old(const float *choice, int E, int K, int *idx, int layer){
+ for(int kk=0;kk<K;kk++){
+ int best=-1; float bv=-1e30f;
+ for(int e=0;e<E;e++){
+ int taken=0;
+ for(int j=0;j<kk;j++) if(idx[j]==e){ taken=1; break; }
+ if(!taken && choice[e]>bv){ bv=choice[e]; best=e; }
+ }
+ idx[kk]=router_best_or_fallback(best,kk,E,layer);
+ }
+ return idx[0];
+}
+
+static uint32_t rng_state;
+static void seed_rng(uint32_t seed){ rng_state=seed; }
+static float next_score(void){
+ rng_state^=rng_state<<13; rng_state^=rng_state>>17; rng_state^=rng_state<<5;
+ return (float)(rng_state>>8)*(1.f/16777216.f);
+}
+
+static void fill_scores(float *choice, int E, int shape){
+ for(int e=0;e<E;e++) choice[e]=next_score();
+ if(shape==1){
+ for(int e=0;e<E;e++) choice[e]*=0.02f;
+ choice[0]=1.f;
+ if(E>4) choice[E/4]=0.8f;
+ } else if(shape==2){
+ for(int e=0;e<E;e++) choice[e]=(float)(e/4)*0.01f;
+ }
+}
+
+static void sort_doubles(double *v, int n){
+ for(int i=1;i<n;i++){
+ double x=v[i]; int j=i-1;
+ while(j>=0 && v[j]>x){ v[j+1]=v[j]; j--; }
+ v[j+1]=x;
+ }
+}
+
+static double bench_old(const float *choice, int E, int K, int *idx){
+ static double samples[N_REPEAT];
+ volatile int sink=0;
+ for(int r=0;r<N_REPEAT;r++){
+ double t0=now_s();
+ sink^=select_old(choice,E,K,idx,0);
+ samples[r]=(now_s()-t0)*1e9;
+ }
+ if(sink==INT32_MIN) fprintf(stderr,"unexpected benchmark sink\n");
+ sort_doubles(samples,N_REPEAT);
+ return samples[N_REPEAT/2];
+}
+
+static double bench_new(const float *choice, int E, int K, int *idx, float *work){
+ static double samples[N_REPEAT];
+ volatile int sink=0;
+ for(int r=0;r<N_REPEAT;r++){
+ memcpy(work,choice,(size_t)E*sizeof(float));
+ double t0=now_s();
+ router_select_topk(work,E,K,idx,0);
+ sink^=idx[0];
+ samples[r]=(now_s()-t0)*1e9;
+ }
+ if(sink==INT32_MIN) fprintf(stderr,"unexpected benchmark sink\n");
+ sort_doubles(samples,N_REPEAT);
+ return samples[N_REPEAT/2];
+}
+
+int main(void){
+ const int cases[][2]={{64,8},{256,8},{256,32}};
+ const char *shapes[]={"random","peaked","plateau"};
+ printf("bench_router_select: old duplicate scan vs marked-score scan (median of %d seeds x %d reps)\n",
+ N_SEEDS,N_REPEAT);
+ printf("%-9s %4s %3s %14s %14s %9s\n","shape","E","K","old ns/call","new ns/call","speedup");
+ printf("---------------------------------------------------------------------\n");
+ float choice[256], work[256]; int idx[32];
+ double old_seed[N_SEEDS], new_seed[N_SEEDS];
+ for(int shape=0;shape<3;shape++) for(size_t ci=0;ci<sizeof(cases)/sizeof(cases[0]);ci++){
+ int E=cases[ci][0], K=cases[ci][1];
+ for(int seed=0;seed<N_SEEDS;seed++){
+ seed_rng(0xA5A5A5A5u+(uint32_t)(seed*0x9E3779B9u));
+ fill_scores(choice,E,shape);
+ old_seed[seed]=bench_old(choice,E,K,idx);
+ new_seed[seed]=bench_new(choice,E,K,idx,work);
+ }
+ sort_doubles(old_seed,N_SEEDS); sort_doubles(new_seed,N_SEEDS);
+ double old=old_seed[N_SEEDS/2], now=new_seed[N_SEEDS/2];
+ printf("%-9s %4d %3d %14.0f %14.0f %8.2fx\n",shapes[shape],E,K,old,now,old/now);
+ }
+ puts("bench_router_select: done (lower ns is better; speedup = old/new)");
+ return 0;
+}
diff --git a/docs/experiments/sparse-routing-2026-09-13.json b/docs/experiments/sparse-routing-2026-09-13.json
new file mode 100644
index 00000000..c94c7756
--- /dev/null
+++ b/docs/experiments/sparse-routing-2026-09-13.json
@@ -0,0 +1,73 @@
+{
+ "version": 1,
+ "hypothesis": "Marking selected router scores removes the repeated duplicate-prefix scan without changing finite-score ordering or non-finite fallback behavior.",
+ "commit": "0702319e100151eb213cda901e8637bdfd8942e6",
+ "model": "Synthetic router-selection fixture; E/K/score-shape matrix, no checkpoint",
+ "command": "make -C c tests/bench_router_select && ./c/tests/bench_router_select",
+ "prompt_hash": "sha256: no prompt; deterministic score arrays",
+ "hardware": {
+ "cpu": "Apple M1 Max, 10 cores (8 performance, 2 efficiency)",
+ "ram": "64 GB unified memory",
+ "storage": "Internal APFS volume",
+ "os": "macOS Darwin 25.6.0 arm64; Apple clang 17.0.0"
+ },
+ "warmup_runs": 0,
+ "changed_variables": ["top_k_selection"],
+ "baseline": {
+ "config": {
+ "top_k_selection": "duplicate-prefix scan",
+ "matrix": "E/K/shape cells fixed"
+ },
+ "samples": {
+ "tok_s": [
+ 1000000.0,
+ 250000.0,
+ 14084.50704225352,
+ 1000000.0,
+ 200000.0,
+ 14084.50704225352,
+ 1000000.0,
+ 200000.0,
+ 12987.012987012987
+ ]
+ },
+ "median_tok_s": 200000.0,
+ "quality": {
+ "method": "router finite-ordering and non-finite fallback tests",
+ "passed": true
+ },
+ "evidence": {
+ "uri": "docs/experiments/sparse-routing-2026-09-13.txt",
+ "sha256": "350dc67288a496f8eab469a1b4feca5619d8f169ad0cb0bd0a9d37f1e7abc7a2"
+ }
+ },
+ "trial": {
+ "config": {
+ "top_k_selection": "marked-score scan",
+ "matrix": "E/K/shape cells fixed"
+ },
+ "samples": {
+ "tok_s": [
+ 1000000.0,
+ 333333.3333333333,
+ 76923.07692307692,
+ 1000000.0,
+ 250000.0,
+ 71428.57142857143,
+ 1000000.0,
+ 333333.3333333333,
+ 83333.33333333333
+ ]
+ },
+ "median_tok_s": 333333.3333333333,
+ "quality": {
+ "method": "router finite-ordering and non-finite fallback tests",
+ "passed": true
+ },
+ "evidence": {
+ "uri": "docs/experiments/sparse-routing-2026-09-13.txt",
+ "sha256": "350dc67288a496f8eab469a1b4feca5619d8f169ad0cb0bd0a9d37f1e7abc7a2"
+ }
+ },
+ "outcome": "improvement"
+}
diff --git a/docs/experiments/sparse-routing-2026-09-13.txt b/docs/experiments/sparse-routing-2026-09-13.txt
new file mode 100644
index 00000000..81b83aa5
--- /dev/null
+++ b/docs/experiments/sparse-routing-2026-09-13.txt
@@ -0,0 +1,65 @@
+Colibri sparse-routing performance evidence
+============================================
+
+Implementation commit under test:
+0702319e100151eb213cda901e8637bdfd8942e6
+
+Hardware:
+- Apple M1 Max
+- 10 cores (8 performance, 2 efficiency)
+- 64 GB RAM
+- macOS Darwin 25.6.0 arm64
+- Apple clang 17.0.0
+- Internal APFS storage
+
+Correctness gate:
+- ./c/tests/test_router_nan: pass
+- ./c/tests/test_dsa_select: 129 cases, 0 failures
+- Python suite: 861 tests, 0 failures, 78 skipped
+
+Command:
+ make -C c tests/bench_router_select && ./c/tests/bench_router_select
+
+Router benchmark output (11 seeds x 2,000 repetitions per cell):
+
+bench_router_select: old duplicate scan vs marked-score scan (median of 11 seeds x 2000 reps)
+shape E K old ns/call new ns/call speedup
+---------------------------------------------------------------------
+random 64 8 1000 1000 1.00x
+random 256 8 4000 3000 1.33x
+random 256 32 71000 13000 5.46x
+peaked 64 8 1000 1000 1.00x
+peaked 256 8 5000 4000 1.25x
+peaked 256 32 71000 14000 5.07x
+plateau 64 8 1000 1000 1.00x
+plateau 256 8 5000 3000 1.67x
+plateau 256 32 77000 12000 6.42x
+bench_router_select: done (lower ns is better; speedup = old/new)
+
+Full DSA benchmark output (11 seeds x 2,000 repetitions per cell):
+
+bench_dsa_select: DSA top-keep, old (qsort) vs new (partial-select) keep=2048 (median of 11 seeds x 2000 reps)
+shape nk old ns/call new ns/call speedup
+------------------------------------------------------------------------
+realistic 2049 143000 8000 17.87x
+realistic 4096 279000 12000 23.25x
+realistic 8192 637000 20000 31.85x
+realistic 16384 1386000 107000 12.95x
+realistic 32768 3021000 251000 12.04x
+realistic 65536 6392000 309000 20.69x
+
+uniform 2049 112000 6000 18.67x
+uniform 4096 285000 10000 28.50x
+uniform 8192 632000 26000 24.31x
+uniform 16384 1422000 114000 12.47x
+uniform 32768 2994000 223000 13.43x
+uniform 65536 6465000 513000 12.60x
+
+plateau 2049 30000 6000 5.00x
+plateau 4096 66000 7000 9.43x
+plateau 8192 145000 9000 16.11x
+plateau 16384 314000 16000 19.62x
+plateau 32768 678000 30000 22.60x
+plateau 65536 1458000 58000 25.14x
+
+bench_dsa_select: done (lower ns is better; speedup = old/new)