Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
feat(glm53): register the family, so coli run, chat, serve and web reach it
JustVuggb887c505bd52
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 2f1e5c54..9742e195 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -190,7 +190,7 @@ jobs:run: |cd crc=0- ENGINES="colibri inkling kimi_k3 olmoe qwen36"+ ENGINES="colibri glm53 inkling kimi_k3 olmoe qwen36"if [ "${{ matrix.v4 }}" = "1" ]; then ENGINES="$ENGINES deepseek-v4"; fifor t in $ENGINES; doecho "::group::$t"diff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex 0c1579ed..d83d107e 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -114,6 +114,7 @@ jobs:# "engine is not built. Run: coli build" -- with the engine sitting right there.# The version lives in the ARCHIVE name, which is where a downloader looks for it.cp c/colibri${{ matrix.ext }} dist/colibri${{ matrix.ext }}+ cp c/glm53${{ matrix.ext }} dist/glm53${{ matrix.ext }}# The sibling engines go in under their plain names too: `coli` resolves the# engine from the model's config.json and looks for kimi_k3/inkling next to# itself (c/coli, engine_for()). ~830 KB for all four.diff --git a/c/Makefile b/c/Makefileindex 798ce387..068a7235 100644--- a/c/Makefile+++ b/c/Makefile@@ -1022,6 +1022,12 @@ NOCUDA_CFLAGS = $(filter-out -DCOLI_CUDA -DCOLI_ANS,$(CFLAGS))NOCUDA_LDFLAGS = $(filter-out -lcudart -lstdc++ -lcuda -L$(CUDA_HOME)/lib64 \-Wl$(comma)-rpath$(comma)$(CUDA_HOME)/lib64,$(LDFLAGS))+# GLM-5.3-Flash: CPU puro, esperti in streaming dal contenitore int4 gs64.+# Nessun oggetto CUDA/Vulkan/Metal fra le dipendenze perche' il motore non ne+# ha ancora: quando li avra', questa riga somigliera' a quella di kimi_k3.+glm53$(EXE): glm53.c st.h json.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h quant.h hyper_connections.h delta_attention.h sparse_index.h vision_tower.h+ $(CC) $(CFLAGS) glm53.c -o glm53$(EXE) $(LDFLAGS)+kimi_k3$(EXE): kimi_k3.c st.h json.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h quant.h omp_tune.h route_trace.h kv_prefix.h serve_codec.h backend_cuda.h $(CUDA_OBJ) $(VK_OBJ) $(VK_SPV) $(METAL_OBJ)$(CC) $(CFLAGS) kimi_k3.c $(CUDA_OBJ) $(VK_OBJ) $(METAL_OBJ) -o kimi_k3$(EXE) $(LDFLAGS)@@ -1499,13 +1505,14 @@ check:$(MAKE) portable$(MAKE) test-install: colibri$(EXE) inkling$(EXE) kimi_k3$(EXE) olmoe$(EXE) qwen36$(EXE) \+install: colibri$(EXE) glm53$(EXE) inkling$(EXE) kimi_k3$(EXE) olmoe$(EXE) qwen36$(EXE) \$(if $(filter 1,$(COLI_V4_SUPPORTED)),deepseek-v4,)$(INSTALL) -d $(DESTDIR)$(BINDIR)$(INSTALL) -d $(DESTDIR)$(LIBEXECDIR)$(INSTALL) -d $(DESTDIR)$(LIBEXECDIR)/tools$(INSTALL) -m 755 coli $(DESTDIR)$(BINDIR)/coli$(INSTALL) -m 755 colibri$(EXE) $(DESTDIR)$(LIBEXECDIR)/colibri$(EXE)+ $(INSTALL) -m 755 glm53$(EXE) $(DESTDIR)$(LIBEXECDIR)/glm53$(EXE)$(INSTALL) -m 755 inkling$(EXE) $(DESTDIR)$(LIBEXECDIR)/inkling$(EXE)$(INSTALL) -m 755 kimi_k3$(EXE) $(DESTDIR)$(LIBEXECDIR)/kimi_k3$(EXE)$(INSTALL) -m 755 olmoe$(EXE) $(DESTDIR)$(LIBEXECDIR)/olmoe$(EXE)diff --git a/c/coli b/c/coliindex 804d6098..c6691e4a 100755--- a/c/coli+++ b/c/coli@@ -455,8 +455,21 @@ def env_for_engine(a, arch, plan=None):# #855; before that `grep -c RAM_GB c/kimi_k3.c` returned 0, so `coli chat# --ram 242` on Kimi K3 set an environment variable nobody looked at and the# user's session ran itself out of memory with the flag apparently set.- if arch in ("deepseek_v4", "kimi"):+ if arch in ("deepseek_v4", "kimi", "glm53"):if a.ram: env["RAM_GB"] = str(a.ram)+ if arch == "glm53":+ # I densi vanno a int4 di default: sono 9,7 B parametri su 321, e in+ # BF16 sono 19 GB che nessuno recupera. Chi vuole la precisione piena+ # mette GLM53_BITS=32, e questo setdefault gli lascia la scelta.+ env.setdefault("GLM53_BITS", "4")+ # Budget della cache degli esperti. Se l'utente ha dato --ram, si lascia+ # spazio a densi, stato e sistema invece di prendersi tutto.+ if a.ram:+ try:+ spare = max(2.0, float(a.ram) - 8.0)+ env.setdefault("GLM53_EXPERT_GB", f"{spare:.1f}")+ except (TypeError, ValueError):+ passif a.ctx:env[family_by_id(arch).limits.context_env] = str(a.ctx)if arch == "deepseek_v4":@@ -1208,6 +1221,11 @@ def cmd_run(a):try: os.unlink(prompt_path)except OSError: passsys.exit(result)+ if arch=="glm53":+ e=env_for_engine(a,arch)+ cmd=[engine,"--model",os.path.abspath(a.model),"--prompt",prompt,+ "--greedy",str(ngen_for(a,interactive=True,family=family))]+ sys.exit(subprocess.call(cmd,env=e))if arch=="olmoe":# olmoe.c now speaks the gateway's SERVE protocol too (`coli chat`/# `coli web`/`coli serve` use it), but `coli run` predates that anddiff --git a/c/family_registry.py b/c/family_registry.pyindex ca9d3a1f..2241c003 100644--- a/c/family_registry.py+++ b/c/family_registry.py@@ -227,6 +227,75 @@ def _kimi_geometry(config, context, _model_dir):+# Il default di GLM53_PREFILL_CHUNK in glm53.c. Se cambia li', cambia qui:+# e' una costante con due consumatori.+_GLM53_PREFILL_CHUNK = 128+++def _glm53_geometry(config, context, _model_dir):+ """GLM-5.3-Flash: 34 layer KDA ricorrenti piu' 11 layer MLA con DSA.++ Solo i layer DSA crescono col contesto, e crescono meno di quanto sembri:+ il motore assorbe kv_b_proj (glm53.c, absorb_kvb), quindi in cache finisce+ il latente da kv_lora e non chiavi e valori espansi. Con 64 teste a 256 la+ differenza e' fra 33 KB e 1,39 MB per token. Ai latenti si aggiungono le+ chiavi e i gate dell'indexer, due vettori da index_head_dim per posizione.++ I layer KDA portano uno stato ricorrente che NON dipende dal contesto, piu'+ la finestra della convoluzione corta: entrambi vanno nel fisso.++ Lo spazio di lavoro NON scala col contesto: il prefill va a pezzi da+ GLM53_PREFILL_CHUNK (glm53.c, forward_prefill), quindi i temporanei costano+ quanto un pezzo. Il costo che domina dentro al pezzo non e' l'attenzione ma+ i flussi delle hyper-connections, hc_mult copie del residuo per posizione,+ tenute due volte perche' il passaggio le scambia.+ """+ layers = _required_int(config, "num_hidden_layers", "glm5_next")+ experts = _required_int(config, "n_routed_experts", "glm5_next")+ hidden = _required_int(config, "hidden_size", "glm5_next")+ heads = _required_int(config, "num_attention_heads", "glm5_next")+ q_lora = _required_int(config, "q_lora_rank", "glm5_next")+ kv_lora = _required_int(config, "kv_lora_rank", "glm5_next")+ qk_nope = _required_int(config, "qk_nope_head_dim", "glm5_next")+ v_head = _required_int(config, "v_head_dim", "glm5_next")+ index_hd = _required_int(config, "index_head_dim", "glm5_next")+ hc_mult = _required_int(config, "hc_mult", "glm5_next")++ la = config.get("linear_attn_config")+ if not isinstance(la, dict):+ raise ValueError("glm5_next: missing or invalid planning key 'linear_attn_config'")+ kda_heads = _required_int(la, "num_heads", "glm5_next")+ kda_hd = _required_int(la, "head_dim", "glm5_next")+ kernel = _required_int(la, "short_conv_kernel_size", "glm5_next")++ # Quali layer sono ad attenzione piena: il checkpoint lo dice in due modi e+ # devono concordare, altrimenti non si sa quale credere.+ types = config.get("layer_types")+ full_list = la.get("full_attn_layers")+ if isinstance(types, list) and types:+ n_full = sum(1 for t in types if isinstance(t, str) and "linear" not in t)+ elif isinstance(full_list, list):+ n_full = len(full_list)+ else:+ raise ValueError("glm5_next: missing 'layer_types' and 'linear_attn_config.full_attn_layers'")+ n_kda = layers - n_full+ if n_kda < 0:+ n_kda = 0++ state = n_full * context * (kv_lora + 2 * index_hd) * 4+ kda_proj = kda_heads * kda_hd+ fixed = n_kda * (kda_heads * kda_hd * kda_hd + 3 * kda_proj * kernel) * 4++ # prefill: due banchi di flussi residui, piu' i temporanei del layer piu'+ # caro fra KDA e MLA (un passaggio ne tocca uno solo per volta)+ chunk = _GLM53_PREFILL_CHUNK+ ws_streams = 2 * chunk * hc_mult * hidden * 4+ ws_kda = chunk * (6 * kda_proj + kda_hd + hidden) * 4+ ws_mla = chunk * (q_lora + heads * (qk_nope + kv_lora) + 2 * heads * v_head) * 4+ workspace = ws_streams + max(ws_kda, ws_mla)+ return PlannerGeometry(state, fixed, workspace, experts)++def _inkling_local_layers(config, layers):"""Which Inkling layers are sliding-window (1-based rule from inkling.c:557-568).@@ -401,6 +470,9 @@ _KIMI_EXPERT = re.compile(r"experts\.(\d+)\.")_V4_EXPERT = re.compile(r"^layers\.(\d+)\.ffn\.experts\.(\d+)\.")+_GLM53_EXPERT = re.compile(+ r"^model\.(?:language_model\.)?layers\.(\d+)\.mlp\.experts\.(\d+)\."+)_INKLING_EXPERT = re.compile(r"^model\.layers\.(\d+)\.mlp\.experts\."r"(?:gate_up_proj|down_proj)(?:\.|$)"@@ -432,6 +504,35 @@ def _inkling_expert_inventory(name, size, config):COMMON_CAP = FamilyCapabilities(False, False, False, True)FAMILIES = (+ FamilyDescriptor(+ id="glm53",+ # "glm5_next" e' il tipo della radice, che e' il wrapper vision; un+ # export di solo testo dichiara "glm5_next_text" al primo livello.+ model_types=("glm5_next", "glm5_next_text"),+ display_name="GLM-5.3-Flash",+ display_scale="321B",+ engine_artifact="glm53",+ engine_aliases=(),+ engine_group="glm53",+ internal_arch="glm53",+ build_target="glm53",+ process_names=("glm53",),+ default_model_id="glm-5.3-flash-colibri",+ cli_adapter="glm53",+ gateway_adapter="glm53",+ planner_id="glm53_hybrid",+ planner_geometry=_glm53_geometry,+ planner_unsupported_reason="",+ expert_inventory=_individual_expert_inventory(_GLM53_EXPERT),+ config_section="text_config",+ limits=FamilyLimits(8192, 1048576, 1024, 1024, 1, 8, "GLM53_MAXT"),+ capabilities=COMMON_CAP,+ has_gateway_adapter=True,+ has_cli_adapter=True,+ # Dal chat_template.jinja del checkpoint: nessun a capo, e <think>+ # subito dopo <|assistant|>, che e' quello che apre il ragionamento.+ tune_prompt_template="[gMASK]<sop><|user|>{prompt}<|assistant|><think>",+ ),FamilyDescriptor(id="glm",model_types=("glm_moe_dsa", "glm5_moe", "glm"),diff --git a/c/glm53 b/c/glm53new file mode 100644index 00000000..c331efa0Binary files /dev/null and b/c/glm53 differdiff --git a/c/glm53.c b/c/glm53.cindex ee71e3c1..034da6eb 100644--- a/c/glm53.c+++ b/c/glm53.c@@ -1713,6 +1713,73 @@ static float *forward_span(GModel *m, GSession *s, const int *tokens, int n,return logits;}+/* Prefill a pezzi.+ *+ * Un prefill in un colpo solo tiene in memoria i temporanei di tutto il+ * prompt: due banchi di flussi residui per hc_mult, piu' le proiezioni di+ * ogni layer. Su GLM-5.3-Flash sono quasi quattro GB per ottomila token, che+ * e' assurdo per un motore che esiste per stare stretto. A pezzi il lavoro e'+ * lo stesso e i temporanei costano quanto il pezzo.+ *+ * Il pezzo resta grande abbastanza da non buttare via il vantaggio del+ * prefill: gli esperti si leggono da disco una volta per pezzo e per layer,+ * quindi pezzi minuscoli li rileggerebbero in continuazione.+ *+ * Con `keep_all` si tengono i logit di ogni posizione, che serve solo al+ * confronto con l'oracolo; altrimenti si tiene l'ultima riga, che e' l'unica+ * che decide il token successivo. */+static float *forward_prefill(GModel *m, GSession *s, const int *tokens, int n,+ const float *vision, int n_vision, int keep_all) {+ const Cfg *c = &m->c;+ const char *setting = getenv("GLM53_PREFILL_CHUNK");+ int chunk = setting ? atoi(setting) : 128;+ if (chunk < 1) chunk = 1;+ if (chunk > n) chunk = n;++ float *all = keep_all ? malloc((size_t)n * c->vocab * sizeof(float)) : NULL;+ if (keep_all && !all) { fprintf(stderr, "OOM sui logit del prefill\n"); exit(1); }+ float *last = NULL;+ int used_vision = 0;++ for (int at = 0; at < n; at += chunk) {+ const int here = at + chunk <= n ? chunk : n - at;+ /* Gli embedding dell'immagine vanno divisi come i token: a ogni pezzo+ * quelli dei segnaposto che contiene, altrimenti il conto non torna e+ * il motore si ferma -- che e' quello che deve fare. */+ int mine = 0;+ if (vision && c->image_token >= 0)+ for (int i = 0; i < here; i++)+ if (tokens[at + i] == c->image_token) mine++;+ float *part = forward_span(m, s, tokens + at, here,+ vision ? vision + (size_t)used_vision * c->hidden : NULL,+ mine);+ used_vision += mine;+ if (keep_all) {+ memcpy(all + (size_t)at * c->vocab, part,+ (size_t)here * c->vocab * sizeof(float));+ free(part);+ } else {+ free(last);+ last = part;+ if (here > 1) {+ /* si tiene solo l'ultima riga */+ float *tail = malloc((size_t)c->vocab * sizeof(float));+ if (!tail) { fprintf(stderr, "OOM sui logit\n"); exit(1); }+ memcpy(tail, last + (size_t)(here - 1) * c->vocab,+ (size_t)c->vocab * sizeof(float));+ free(last);+ last = tail;+ }+ }+ }+ if (vision && used_vision != n_vision) {+ fprintf(stderr, "%d embedding vision forniti ma %d segnaposto nel prompt\n",+ n_vision, used_vision);+ exit(1);+ }+ return keep_all ? all : last;+}+/* Il passaggio senza sessione: apre, macina tutto, chiude. E' quello che usano* gli oracoli, dove il punto e' il risultato e non il tempo. */static float *forward(GModel *m, const int *tokens, int n,@@ -1949,8 +2016,8 @@ static void serve_one(GModel *m, Tok *tokenizer, ServeReq *q) {* E' lavoro sprecato, non un errore, e il giorno che gli slot esistono* davvero il protocollo qui non cambia. */GSession *session = session_open(m, total + budget + 1);- float *logits = forward_span(m, session, sequence, total, NULL, 0);- int rows = total;+ float *logits = forward_prefill(m, session, sequence, total, NULL, 0, 0);+ int rows = 1;for (int step = 0; step < budget; step++) {if (total >= room) { limited = 1; break; }int next = sample_token(logits + (size_t)(rows - 1) * m->c.vocab, m->c.vocab);@@ -2108,7 +2175,7 @@ int main(int argc, char **argv) {/* Una sola sessione per tutta la generazione: il prompt si prefilla una* volta e ogni token dopo costa un token, non tutto il prefisso. */GSession *session = session_open(&model, count + (greedy > 0 ? greedy : 0) + 1);- float *logits = forward_span(&model, session, tokens, count, vision, n_vision);+ float *logits = forward_prefill(&model, session, tokens, count, vision, n_vision, 1);printf("teacher_forcing");for (int t = 0; t < count; t++)printf(" %d", argmax(logits + (size_t)t * model.c.vocab, model.c.vocab));diff --git a/c/openai_server.py b/c/openai_server.pyindex 588b0d79..3768cab2 100644--- a/c/openai_server.py+++ b/c/openai_server.py@@ -1365,13 +1365,74 @@ def render_chat(messages, enable_thinking=False, reasoning_effort=None, tools=Noreturn "".join(prompt)+def render_chat_glm53(messages, enable_thinking=False, reasoning_effort=None, tools=None,+ tool_choice=None):+ """Render the text-only subset of the official GLM-5.3-Flash chat template.++ Not a variant of the GLM-5.2 renderer above, and the differences are not+ cosmetic. GLM-5.3 emits the reasoning-effort system line ALWAYS, because its+ template defaults the effort to Max rather than leaving it unset; it accepts+ only low, high and max, not the six-level ladder; and its generation prompt+ OPENS the reasoning block with a bare `<think>` where 5.2 closed it+ immediately with `<think></think>`. A past assistant turn still closes it.++ Tools are refused rather than rendered. GLM-5.3 declares them inside a+ `<tool_response><tools>` wrapper with its own JSON serialization, which is+ not the block 5.2 uses, and emitting 5.2's shape here would put a prompt in+ front of the model that it was never trained on -- an answer that looks+ fine and is quietly wrong is worse than an error that says what is missing.+ """+ if not isinstance(messages, list) or not messages:+ raise APIError(400, "`messages` must be a non-empty array.", "messages")+ if tools and tool_choice != "none":+ raise APIError(400, "Tool calling is not wired for GLM-5.3-Flash yet; "+ "its declaration block differs from GLM-5.2's.", "tools")++ # low/high passano, tutto il resto e' Max: e' la scala del template, non la+ # nostra. `none` non arriva qui, spegne il ragionamento a monte.+ effort = {"minimal": "Low", "low": "Low", "medium": "High",+ "high": "High", "xhigh": "Max"}.get(reasoning_effort, "Max")+ prompt = ["[gMASK]<sop>", f"<|system|>Reasoning Effort: {effort}"]++ for message in messages:+ if not isinstance(message, dict):+ raise APIError(400, "each message must be an object.", "messages")+ role = message.get("role")+ content = message.get("content")+ if isinstance(content, list): # parti multimodali: solo il testo+ content = "".join(part.get("text", "") for part in content+ if isinstance(part, dict) and part.get("type") == "text")+ content = content or ""+ if role == "user":+ prompt.append(f"<|user|>{content}")+ elif role == "system":+ prompt.append(f"<|system|>{content}")+ elif role == "assistant":+ reasoning = message.get("reasoning_content")+ if not isinstance(reasoning, str) and "</think>" in content:+ reasoning = content.split("</think>")[0].split("<think>")[-1]+ content = content.split("</think>")[-1]+ opened = f"<think>{reasoning}</think>" if isinstance(reasoning, str) else "<think></think>"+ prompt.append(f"<|assistant|>{opened}{content.strip()}")+ elif role == "tool":+ raise APIError(400, "Tool results are not wired for GLM-5.3-Flash yet.",+ "messages")+ else:+ raise APIError(400, f"unsupported message role {role!r}.", "messages")++ # Il prompt di generazione APRE il blocco di ragionamento e non lo chiude.+ prompt.append("<|assistant|><think>")+ return "".join(prompt)++def render_chat_for_arch(messages, enable_thinking=False, reasoning_effort=None, tools=None,tool_choice=None, audio_out=None):"""Render a chat request with the active engine's native prompt contract."""if ARCH == "inkling":return render_chat_inkling(messages, enable_thinking, reasoning_effort, tools,tool_choice, audio_out=audio_out)- renderer = (render_chat_kimi if ARCH == "kimi" else+ renderer = (render_chat_glm53 if ARCH == "glm53" else+ render_chat_kimi if ARCH == "kimi" elserender_chat_qwen if ARCH == "qwen36" elserender_chat_v4 if ARCH == "deepseek_v4" elserender_chat_olmoe if ARCH == "olmoe" else render_chat)diff --git a/c/tests/test_family_registry.py b/c/tests/test_family_registry.pyindex 19caa9b6..acdb314b 100644--- a/c/tests/test_family_registry.py+++ b/c/tests/test_family_registry.py@@ -833,6 +833,10 @@ class FamilyRegistryTest(unittest.TestCase):def test_tuning_replay_prompts_are_registry_owned(self):prompt = "hello {world}"expected = {+ # GLM-5.3 apre il ragionamento e non lo chiude: il suo+ # chat_template.jinja mette <think> dopo <|assistant|> e basta,+ # dove GLM-5.2 metteva <think></think>.+ "glm53": "[gMASK]<sop><|user|>hello {world}<|assistant|><think>","glm": "[gMASK]<sop><|user|>hello {world}<|assistant|><think></think>","inkling": "<|user|>hello {world}<|assistant|>","kimi": "K3CHAT1\nM user 13\nhello {world}G 0\n\n",