Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
engines: publish the lazy HITS table under a lock (the parallel warmstart segfaulted on first touch)
kreuzzelgfc6e7bbe23c6
diff --git a/c/glm53.c b/c/glm53.cindex 560f092c..14854358 100644--- a/c/glm53.c+++ b/c/glm53.c@@ -59,6 +59,7 @@*/#define _GNU_SOURCE#include <stdio.h>+#include <pthread.h> /* ehit_mark publishes the lazy HITS table under a lock */#include <stdlib.h>#include <string.h>#include <math.h>@@ -2720,13 +2721,27 @@ static int serve_one(GModel *m, Tok *tokenizer, ServeReq *q) {* matmul esperti (ffn_layer meno il disco), attention (mla/kda), testa* (mv su lm_head). L'attesa asincrona non esiste qui: glm53 legge in modo* sincrono, quindi expert_wait_s e' 0 per costruzione, non per omissione. */+static pthread_mutex_t g_ehit_mx = PTHREAD_MUTEX_INITIALIZER;static void ehit_mark(GModel *m, int layer, int eid) {const Cfg *c = &m->c;- if (!m->ehit) {- m->ehit = calloc((size_t)c->n_layers, sizeof(uint8_t *));- for (int i = 0; i < c->n_layers; i++) m->ehit[i] = calloc((size_t)c->n_experts, 1);+ /* The first touch can come from a parallel region (qwen36: the tier+ * warmstart's omp loop calls expert_get from twelve threads at once):+ * one thread published m->ehit while it was still filling the rows and+ * a sibling dereferenced m->ehit[layer] == NULL -- SIGSEGV in about one+ * run in twelve on a CUDA warmstart. Build the table privately, publish+ * it once under a lock (double-checked), and read it with acquire order. */+ uint8_t **ehit = __atomic_load_n(&m->ehit, __ATOMIC_ACQUIRE);+ if (!ehit) {+ pthread_mutex_lock(&g_ehit_mx);+ ehit = m->ehit;+ if (!ehit) {+ ehit = calloc((size_t)c->n_layers, sizeof(uint8_t *));+ for (int i = 0; i < c->n_layers; i++) ehit[i] = calloc((size_t)c->n_experts, 1);+ __atomic_store_n(&m->ehit, ehit, __ATOMIC_RELEASE);+ }+ pthread_mutex_unlock(&g_ehit_mx);}- if (layer >= 0 && layer < c->n_layers && eid >= 0 && eid < c->n_experts) m->ehit[layer][eid] = 1;+ if (layer >= 0 && layer < c->n_layers && eid >= 0 && eid < c->n_experts) ehit[layer][eid] = 1;}static int dash_rows(const GModel *m) {int rows = 0;diff --git a/c/kimi_k3.c b/c/kimi_k3.cindex f3329441..7b847f63 100644--- a/c/kimi_k3.c+++ b/c/kimi_k3.c@@ -1511,13 +1511,27 @@ static uint64_t g_slot_index_probes;#endif/* One byte per expert: routed in this turn or not. The dashboard's Brain tab* reads it as the HITS bitmap after every turn (serve_hits), cleared there. */+static pthread_mutex_t g_ehit_mx = PTHREAD_MUTEX_INITIALIZER;static void ehit_mark(Model *m, int li, int eid){Cfg *c=&m->c;- if(!m->ehit){- m->ehit=calloc((size_t)c->n_layers,sizeof(uint8_t*));- for(int i=0;i<c->n_layers;i++) m->ehit[i]=calloc((size_t)c->n_experts,1);- }- if(li>=0&&li<c->n_layers&&eid>=0&&eid<c->n_experts) m->ehit[li][eid]=1;+ /* The first touch can come from a parallel region (qwen36: the tier+ * warmstart's omp loop calls expert_get from twelve threads at once):+ * one thread published m->ehit while it was still filling the rows and+ * a sibling dereferenced m->ehit[layer] == NULL -- SIGSEGV in about one+ * run in twelve on a CUDA warmstart. Build the table privately, publish+ * it once under a lock (double-checked), and read it with acquire order. */+ uint8_t **ehit=__atomic_load_n(&m->ehit,__ATOMIC_ACQUIRE);+ if(!ehit){+ pthread_mutex_lock(&g_ehit_mx);+ ehit=m->ehit;+ if(!ehit){+ ehit=calloc((size_t)c->n_layers,sizeof(uint8_t*));+ for(int i=0;i<c->n_layers;i++) ehit[i]=calloc((size_t)c->n_experts,1);+ __atomic_store_n(&m->ehit,ehit,__ATOMIC_RELEASE);+ }+ pthread_mutex_unlock(&g_ehit_mx);+ }+ if(li>=0&&li<c->n_layers&&eid>=0&&eid<c->n_experts) ehit[li][eid]=1;}static Slot *slot_indexed(Model *m, int li, int eid){LCache *lc=&m->ecache[li];diff --git a/c/qwen36.c b/c/qwen36.cindex eda6747b..1b0ad6e3 100644--- a/c/qwen36.c+++ b/c/qwen36.c@@ -1595,13 +1595,27 @@ static void slot_ensure_int8(Model *m, Slot *s) {/* Segna l'esperto instradato per la bitmap HITS della dashboard. Vive qui,* fuori dalla regione QWEN36_NO_MAIN: expert_get la chiama anche nel build* del segment adapter, dove il resto della telemetria serve non esiste. */+static pthread_mutex_t g_ehit_mx = PTHREAD_MUTEX_INITIALIZER;static void ehit_mark(Model *m, int layer, int eid){const Cfg *c=&m->c;- if(!m->ehit){- m->ehit=calloc((size_t)c->n_layers,sizeof(uint8_t*));- for(int i=0;i<c->n_layers;i++) m->ehit[i]=calloc((size_t)c->n_experts,1);- }- if(layer>=0&&layer<c->n_layers&&eid>=0&&eid<c->n_experts) m->ehit[layer][eid]=1;+ /* The first touch can come from a parallel region (qwen36: the tier+ * warmstart's omp loop calls expert_get from twelve threads at once):+ * one thread published m->ehit while it was still filling the rows and+ * a sibling dereferenced m->ehit[layer] == NULL -- SIGSEGV in about one+ * run in twelve on a CUDA warmstart. Build the table privately, publish+ * it once under a lock (double-checked), and read it with acquire order. */+ uint8_t **ehit=__atomic_load_n(&m->ehit,__ATOMIC_ACQUIRE);+ if(!ehit){+ pthread_mutex_lock(&g_ehit_mx);+ ehit=m->ehit;+ if(!ehit){+ ehit=calloc((size_t)c->n_layers,sizeof(uint8_t*));+ for(int i=0;i<c->n_layers;i++) ehit[i]=calloc((size_t)c->n_experts,1);+ __atomic_store_n(&m->ehit,ehit,__ATOMIC_RELEASE);+ }+ pthread_mutex_unlock(&g_ehit_mx);+ }+ if(layer>=0&&layer<c->n_layers&&eid>=0&&eid<c->n_experts) ehit[layer][eid]=1;}static void expert_get(Model *m, int layer, int eid, Slot **out) {ehit_mark(m, layer, eid); /* tocca solo m->ehit[layer][eid] */diff --git a/c/qwen38_core.h b/c/qwen38_core.hindex db3702fe..4b2bf891 100644--- a/c/qwen38_core.h+++ b/c/qwen38_core.h@@ -8,6 +8,7 @@*/#ifndef COLI_QWEN38_CORE_H#define COLI_QWEN38_CORE_H+#include <pthread.h> /* q38_ehit_mark publishes the lazy HITS table under a lock */#define Q38_MAX_LAYERS 512#define Q38_MAX_EXPERTS 1024@@ -1242,13 +1243,27 @@ static void q38_load_expert(Model *m,int layer,int eid,Slot *s) {/* One byte per expert: routed in this turn or not. The dashboard's Brain tab* reads it as the HITS bitmap after every turn (serve_hits in qwen38.c),* cleared there. Marked on both lookup paths, single and batched. */+static pthread_mutex_t g_q38_ehit_mx=PTHREAD_MUTEX_INITIALIZER;static void q38_ehit_mark(Model *m,int layer,int eid) {const Cfg *c=&m->c;- if(!m->ehit){- m->ehit=(uint8_t**)calloc((size_t)c->layers,sizeof(uint8_t*));- for(int i=0;i<c->layers;i++)m->ehit[i]=(uint8_t*)calloc((size_t)c->experts,1);+ /* The first touch can come from a parallel region (qwen36: the tier+ * warmstart's omp loop calls expert_get from twelve threads at once):+ * one thread published m->ehit while it was still filling the rows and+ * a sibling dereferenced m->ehit[layer] == NULL -- SIGSEGV in about one+ * run in twelve on a CUDA warmstart. Build the table privately, publish+ * it once under a lock (double-checked), and read it with acquire order. */+ uint8_t **ehit=__atomic_load_n(&m->ehit,__ATOMIC_ACQUIRE);+ if(!ehit){+ pthread_mutex_lock(&g_q38_ehit_mx);+ ehit=m->ehit;+ if(!ehit){+ ehit=(uint8_t**)calloc((size_t)c->layers,sizeof(uint8_t*));+ for(int i=0;i<c->layers;i++)ehit[i]=(uint8_t*)calloc((size_t)c->experts,1);+ __atomic_store_n(&m->ehit,ehit,__ATOMIC_RELEASE);+ }+ pthread_mutex_unlock(&g_q38_ehit_mx);}- if(layer>=0&&layer<c->layers&&eid>=0&&eid<c->experts)m->ehit[layer][eid]=1;+ if(layer>=0&&layer<c->layers&&eid>=0&&eid<c->experts)ehit[layer][eid]=1;}static Slot *q38_expert_get(Model *m,int layer,int eid) {q38_ehit_mark(m,layer,eid);