Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
fix(cli): reject numeric arguments we can only read half of (#1295)
Vincenzo Fornaro084cfa5f5c8c
diff --git a/c/Makefile b/c/Makefileindex 5dd1b757..141b4260 100644--- a/c/Makefile+++ b/c/Makefile@@ -716,7 +716,7 @@ $(file >.build-config,$(BUILD_CONFIG))endif.build-config: ;-colibri$(EXE): colibri.c st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h quant.h sample.h kv_persist.h telemetry.h route_trace.h omp_tune.h kv_fp8.h kv_tq.h $(CUDA_OBJ) $(METAL_OBJ) $(VK_OBJ) $(VK_SPV) .build-config+colibri$(EXE): colibri.c cli_args.h st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h quant.h sample.h kv_persist.h telemetry.h route_trace.h omp_tune.h kv_fp8.h kv_tq.h $(CUDA_OBJ) $(METAL_OBJ) $(VK_OBJ) $(VK_SPV) .build-config$(CC) $(CFLAGS) colibri.c $(CUDA_OBJ) $(METAL_OBJ) $(VK_OBJ) -o colibri$(EXE) $(LDFLAGS)# Vulkan backend object (plain C + vulkan headers) and its SPIR-V shaders.@@ -997,7 +997,7 @@ fp8-bench: fp8_bench$(EXE)# the GPU sits idle. Same guard #783 put on kimi_k3, which since gaining an# MXFP4 expert path no longer needs it. tests/test_makefile_cuda_scope.py# asserts this shape.-olmoe$(EXE): olmoe.c st.h json.h compat.h sample.h tok.h tok_unicode.h tok_unicode_o200k.h omp_tune.h route_trace.h serve_codec.h+olmoe$(EXE): olmoe.c cli_args.h st.h json.h compat.h sample.h tok.h tok_unicode.h tok_unicode_o200k.h omp_tune.h route_trace.h serve_codec.h$(CC) $(NOCUDA_CFLAGS) olmoe.c -o olmoe$(EXE) $(NOCUDA_LDFLAGS)# Qwen3.6-35B-A3B engine (hybrid Gated Attention + Gated DeltaNet + streaming@@ -1019,13 +1019,13 @@ QWEN36_TIER_SRC =QWEN36_CFLAGS = $(NOCUDA_CFLAGS)QWEN36_LDFLAGS = $(NOCUDA_LDFLAGS)endif-qwen36$(EXE): qwen36.c qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)+qwen36$(EXE): qwen36.c cli_args.h qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)$(CC) $(QWEN36_CFLAGS) qwen36.c $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o qwen36$(EXE) $(QWEN36_LDFLAGS)# Qwen3.8-Flash-Next text-only sibling. This target is intentionally CPU-only;# qwen38.c owns the direct checkpoint loader and SERVE=1 protocol, with no# CUDA/Metal tier advertised by the control plane.-qwen38$(EXE): qwen38.c qwen38_core.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h quant.h route_trace.h tok.h tok_unicode.h tok_unicode_o200k.h serve_codec.h+qwen38$(EXE): qwen38.c cli_args.h qwen38_core.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h quant.h route_trace.h tok.h tok_unicode.h tok_unicode_o200k.h serve_codec.h$(CC) $(NOCUDA_CFLAGS) qwen38.c -o qwen38$(EXE) $(NOCUDA_LDFLAGS).PHONY: qwen38-tiny-generate qwen38-tiny-check@@ -1071,7 +1071,7 @@ tests/test_qwen36_json_escape$(EXE): tests/test_qwen36_json_escape.c qwen36.c qwtests/bench_qwen36_dense_batch$(EXE): tests/bench_qwen36_dense_batch.c qwen36.c qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)$(CC) $(QWEN36_CFLAGS) $< $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o $@ $(QWEN36_LDFLAGS)-inkling$(EXE): inkling.c st.h json.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h omp_tune.h route_trace.h kv_prefix.h serve_codec.h $(INK_CUDA_OBJ) $(METAL_OBJ)+inkling$(EXE): inkling.c cli_args.h st.h json.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h omp_tune.h route_trace.h kv_prefix.h serve_codec.h $(INK_CUDA_OBJ) $(METAL_OBJ)$(CC) $(CFLAGS) inkling.c $(INK_CUDA_OBJ) $(METAL_OBJ) -o inkling$(EXE) $(LDFLAGS)# ENGINES WITHOUT A CUDA BACKEND (#783).@@ -1089,10 +1089,10 @@ NOCUDA_LDFLAGS = $(filter-out -lcudart -lstdc++ -lcuda -L$(CUDA_HOME)/lib64 \# 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 $(VK_OBJ) $(VK_SPV)+glm53$(EXE): glm53.c cli_args.h 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 $(VK_OBJ) $(VK_SPV)$(CC) $(CFLAGS) glm53.c $(VK_OBJ) -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)+kimi_k3$(EXE): kimi_k3.c cli_args.h 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)# Use a baseline that matches the compiler target. macOS already targets a@@ -1634,6 +1634,9 @@ endiftests/test_route_trace$(EXE): tests/test_route_trace.c route_trace.h$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)+tests/test_cli_args$(EXE): tests/test_cli_args.c cli_args.h+ $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)+tests/test_798_guards$(EXE): tests/test_798_guards.c st.h json.h compat.h route_trace.h$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)diff --git a/c/cli_args.h b/c/cli_args.hnew file mode 100644index 00000000..c550af1f--- /dev/null+++ b/c/cli_args.h@@ -0,0 +1,68 @@+/* Lettura degli argomenti numerici dalla riga di comando.+ *+ * atoi() non ha modo di dire "non e' un numero": restituisce 0 per una stringa+ * che non lo e', e per una che lo e' a meta' restituisce la meta' che ha letto+ * senza dire niente. Il caso che si vede davvero e' un refuso nella capienza:+ *+ * ./qwen38 3x2 -> atoi = 3 -> cache=3/layer invece di 32+ *+ * Nessun errore. Il motore parte con una cache dieci volte piu' piccola, legge+ * dal disco molto piu' del dovuto, e chi l'ha lanciato conclude che il motore+ * e' lento invece che di aver sbagliato a digitare. E' il modo peggiore in cui+ * un programma puo' sbagliare: fa una cosa diversa da quella chiesta e non lo+ * dice.+ *+ * Due funzioni e non una: coli_parse_int decide e riporta, coli_arg_int e' il+ * guscio che stampa ed esce. La divisione non e' estetica, e' quello che rende+ * la decisione verificabile senza un processo figlio -- e quindi su Windows,+ * che non ha fork(). */+#ifndef COLI_CLI_ARGS_H+#define COLI_CLI_ARGS_H++#include <errno.h>+#include <limits.h>+#include <stdio.h>+#include <stdlib.h>++/* 1 se `arg` e' un intero scritto per intero, 0 altrimenti. *out tocco solo+ * quando accetto. */+static int coli_parse_int(const char *arg, int *out)+{+ char *end = NULL;+ long v;++ if (!arg) return 0;+ errno = 0;+ v = strtol(arg, &end, 10);+ /* Prima cosa: end == arg vuol dire che non ha letto nemmeno una cifra.+ * Va controllato PRIMA di saltare gli spazi finali, se no un argomento+ * fatto di soli spazi passerebbe valendo zero -- lo skip sposterebbe end+ * oltre lo spazio e la stringa sembrerebbe consumata per intero. */+ if (end == arg) return 0;+ /* strtol salta gli spazi iniziali da solo; quelli finali li salto qui, se+ * no un "32\r" arrivato da uno script salvato con le terminazioni di riga+ * di Windows verrebbe rifiutato con un messaggio incomprensibile, e i+ * binari Windows li spediamo. */+ while (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n') end++;+ /* *end != 0: ne ha letta una parte e poi ha trovato altro, il caso "3x2". */+ if (*end != '\0' || errno == ERANGE ||+ v < (long)INT_MIN || v > (long)INT_MAX) return 0;+ *out = (int)v;+ return 1;+}++/* `what` nomina l'argomento come lo chiama chi lo scrive sulla riga di comando+ * ("cache/layer", non "cap"): il messaggio serve a chi ha sbagliato a digitare,+ * non a chi legge il sorgente. */+static int coli_arg_int(const char *arg, const char *what)+{+ int v = 0;+ if (!coli_parse_int(arg, &v)) {+ fprintf(stderr, "%s: expected a whole number, got \"%s\"\n",+ what, arg ? arg : "");+ exit(2);+ }+ return v;+}++#endif /* COLI_CLI_ARGS_H */diff --git a/c/colibri.c b/c/colibri.cindex fb69983c..f3118872 100644--- a/c/colibri.c+++ b/c/colibri.c@@ -57,6 +57,7 @@#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))#include <cpuid.h> /* hwinfo_emit: CPU brand string senza /proc */#endif+#include "cli_args.h"#include "st.h"#ifdef __linux__#include "uring.h"@@ -10861,10 +10862,10 @@ int main(int argc, char **argv){/* cap itself is resolved below, once g_metal_enabled and the SSD probe (both* needed for the platform default) are known -- see coli_resolve_cap(). */int cap_given = argc>1;- int cap_arg = cap_given?atoi(argv[1]):0;+ int cap_arg = cap_given?coli_arg_int(argv[1],"cache/layer"):0;int cap_env = getenv("CAP")?atoi(getenv("CAP")):0;- int ebits= argc>2?atoi(argv[2]):8;- int dbits= argc>3?atoi(argv[3]):ebits;+ int ebits= argc>2?coli_arg_int(argv[2],"expert bits"):8;+ int dbits= argc>3?coli_arg_int(argv[3],"dense bits"):ebits;#if !defined(_WIN32)if(getenv("EXPERT_WORKER")){int port=getenv("CLUSTER_WORKER_PORT")?atoi(getenv("CLUSTER_WORKER_PORT")):9100;diff --git a/c/glm53.c b/c/glm53.cindex 262be7be..7de8c553 100644--- a/c/glm53.c+++ b/c/glm53.c@@ -65,6 +65,7 @@#include <stdint.h>#include <stdarg.h>+#include "cli_args.h"#include "json.h"#include "st.h"#include "quant.h"@@ -2578,7 +2579,7 @@ int main(int argc, char **argv) {for (int i = 1; i < argc; i++) {if (!strcmp(argv[i], "--model") && i + 1 < argc) dir = argv[++i];else if (!strcmp(argv[i], "--ids") && i + 1 < argc) ids = argv[++i];- else if (!strcmp(argv[i], "--greedy") && i + 1 < argc) greedy = atoi(argv[++i]);+ else if (!strcmp(argv[i], "--greedy") && i + 1 < argc) greedy = coli_arg_int(argv[++i], "--greedy");else if (!strcmp(argv[i], "--logits")) show_logits = 1;else if (!strcmp(argv[i], "--prompt") && i + 1 < argc) prompt_text = argv[++i];else if (!strcmp(argv[i], "--patches") && i + 1 < argc) patch_file = argv[++i];@@ -2588,10 +2589,10 @@ int main(int argc, char **argv) {/* Capienza posizionale: e' cosi' che il gateway lancia ogni motore* (openai_server.py, Engine.__init__). Zero vuol dire "decidila* tu", che qui e' il budget misurato dalla RAM disponibile. */- const int cap = atoi(argv[i]);+ const int cap = coli_arg_int(argv[i], "cache/layer");if (cap > 0) g_cap_override = cap;}- else { fprintf(stderr, "argomento sconosciuto: %s\n", argv[i]); return 2; }+ else { fprintf(stderr, "unknown argument: %s\n", argv[i]); return 2; }}/* SERVE=1 e SNAP=<dir>: il motore non e' piu' una CLI ma il capo di una* pipa, e il modello arriva dall'ambiente perche' e' cosi' che lo lanciadiff --git a/c/inkling.c b/c/inkling.cindex 4c0b7d45..ccc8dc07 100644--- a/c/inkling.c+++ b/c/inkling.c@@ -32,6 +32,7 @@#include <sys/resource.h>#include <sys/select.h> /* serve-loop stdin poll (POSIX); inkling serves on Linux */#endif+#include "cli_args.h"#include "st.h"#include "tok.h"#ifdef _OPENMP@@ -2411,11 +2412,11 @@ int main(int argc, char **argv) {for (int i = 1; i < argc; i++) {if (!strcmp(argv[i], "-p") && i+1 < argc) prompt = argv[++i];else if (!strcmp(argv[i], "-f") && i+1 < argc) pfile = argv[++i];- else if (!strcmp(argv[i], "-n") && i+1 < argc) n_new = atoi(argv[++i]);+ else if (!strcmp(argv[i], "-n") && i+1 < argc) n_new = coli_arg_int(argv[++i], "-n");else if (!strcmp(argv[i], "--chat")) chat = 1;else if (!strcmp(argv[i], "--audio") && i+1 < argc) audiopath = argv[++i];- else if (npos == 0) { cap = atoi(argv[i]); npos++; }- else if (npos == 1) { bits = atoi(argv[i]); npos++; }+ else if (npos == 0) { cap = coli_arg_int(argv[i], "cache/layer"); npos++; }+ else if (npos == 1) { bits = coli_arg_int(argv[i], "expert bits"); npos++; }else refpath = argv[i];}/* --audio <file>: raw u8 DMel frames, [n_frames, mel_bins] row-major —diff --git a/c/kimi_k3.c b/c/kimi_k3.cindex b9633dfa..354198ba 100644--- a/c/kimi_k3.c+++ b/c/kimi_k3.c@@ -104,6 +104,7 @@#include <sys/sysctl.h>#include <mach/mach.h>#endif+#include "cli_args.h"#include "st.h"#include "tok.h"#include "quant.h"@@ -3047,7 +3048,7 @@ int main(int argc, char **argv){if(!snap||!*snap){ fprintf(stderr,"set SNAP=<Kimi K3 snapshot directory>\n"); return 1; }int ngen=32, chat=0;for(int i=serving?1:2;i<argc;i++){- if(!strcmp(argv[i],"--ngen")&&i+1<argc) ngen=atoi(argv[++i]);+ if(!strcmp(argv[i],"--ngen")&&i+1<argc) ngen=coli_arg_int(argv[++i], "--ngen");else if(!strcmp(argv[i],"--ids")&&i+1<argc) idstr=argv[++i];else if(!strcmp(argv[i],"--chat")) chat=1;else if(!strcmp(argv[i],"--system")&&i+1<argc) sysmsg=argv[++i];diff --git a/c/olmoe.c b/c/olmoe.cindex 65500fdf..f12ce01f 100644--- a/c/olmoe.c+++ b/c/olmoe.c@@ -31,6 +31,7 @@#include <sys/resource.h>#include <unistd.h>#endif+#include "cli_args.h"#include "st.h"#ifdef _OPENMP#include <omp.h> /* omp_set_num_threads/omp_get_max_threads per omp_tune.h */@@ -1476,8 +1477,8 @@ int main(int argc, char **argv) {if (g_wide < 1) g_wide = 1;if (g_wide > 4) g_wide = 4;int hot_n = getenv("HOT") ? atoi(getenv("HOT")) : 0;- int cap = argc > 1 ? atoi(argv[1]) : 16;- int bits = argc > 2 ? atoi(argv[2]) : 8;+ int cap = argc > 1 ? coli_arg_int(argv[1], "cache/layer") : 16;+ int bits = argc > 2 ? coli_arg_int(argv[2], "expert bits") : 8;if (bits < 2 || bits > 8) {fprintf(stderr, "quant_bits must be 2..8 (got %d)\n", bits);return 1;diff --git a/c/qwen36.c b/c/qwen36.cindex 3f693d57..6ee29bf2 100644--- a/c/qwen36.c+++ b/c/qwen36.c@@ -57,6 +57,7 @@ static int qwen36_max_ctx(void) {#include <sys/resource.h>#include <unistd.h>#endif+#include "cli_args.h"#include "st.h"#include "json.h" /* tokenizer.json parsing (reuse minimal parser) */#include "qwen36_tier.h" /* optional transparent Vulkan compute backend for MoE experts */@@ -2615,8 +2616,8 @@ int main(int argc, char **argv) {if (getenv("OPENAI")) g_openai = 1; /* OpenAI-compatible output */const char *mv = getenv("MODEL"); if (mv && *mv) g_model = mv;int hot_n = getenv("HOT") ? atoi(getenv("HOT")) : 0;- int cap = argc > 1 ? atoi(argv[1]) : 16;- int bits = argc > 2 ? atoi(argv[2]) : 4;+ int cap = argc > 1 ? coli_arg_int(argv[1], "cache/layer") : 16;+ int bits = argc > 2 ? coli_arg_int(argv[2], "expert bits") : 4;/* cap < 1 leaves every layer cache empty, so expert_get finds no slot to* evict and waits for a publish that can never come. The old lru=0 fallback* turned that into a heap OOB instead; neither is a failure mode to ship. */diff --git a/c/qwen38.c b/c/qwen38.cindex 991ff129..d7cdd67a 100644--- a/c/qwen38.c+++ b/c/qwen38.c@@ -59,6 +59,7 @@ static int qwen38_max_ctx(void) {#include <sys/resource.h>#include <unistd.h>#endif+#include "cli_args.h"#include "st.h"#include "qwen38_vision.h"#include "json.h" /* tokenizer.json parsing (reuse minimal parser) */@@ -1629,8 +1630,8 @@ int main(int argc, char **argv) {if (getenv("OPENAI")) g_openai = 1; /* OpenAI-compatible output */const char *mv = getenv("MODEL");if (mv && *mv) snprintf(g_model, sizeof g_model, "%s", mv);- int cap = argc > 1 ? atoi(argv[1]) : 1;- int bits = argc > 2 ? atoi(argv[2]) : 8;+ int cap = argc > 1 ? coli_arg_int(argv[1], "cache/layer") : 1;+ int bits = argc > 2 ? coli_arg_int(argv[2], "expert bits") : 8;/* cap < 1 leaves every layer cache empty, so expert_get finds no slot to* evict and waits for a publish that can never come. The old lru=0 fallback* turned that into a heap OOB instead; neither is a failure mode to ship. */diff --git a/c/tests/test_cli_args.c b/c/tests/test_cli_args.cnew file mode 100644index 00000000..e0e1c70b--- /dev/null+++ b/c/tests/test_cli_args.c@@ -0,0 +1,71 @@+/* coli_parse_int: cio' che atoi accettava a meta' ora e' un rifiuto.+ *+ * Il caso che ha motivato tutto questo e' "3x2" scritto al posto di "32": atoi+ * restituiva 3, il motore partiva con una cache dieci volte piu' piccola, e+ * l'unico sintomo era che andava piano. Nessun messaggio.+ *+ * Si prova il parser puro e non il guscio che esce: cosi' il test e' un+ * confronto fra valori, senza fork() -- che su Windows non c'e'. */+#include <stdio.h>+#include <string.h>++#include "../cli_args.h"++static int fails = 0;++static void accepts(const char *arg, int expect)+{+ int got = -12345;+ if (!coli_parse_int(arg, &got)) {+ printf(" FAIL \"%s\": rifiutato, doveva valere %d\n", arg, expect);+ fails++;+ } else if (got != expect) {+ printf(" FAIL \"%s\": vale %d invece di %d\n", arg, got, expect);+ fails++;+ }+}++static void rejects(const char *arg)+{+ int got = -12345;+ if (coli_parse_int(arg, &got)) {+ printf(" FAIL \"%s\": accettato come %d -- e' il bug originale, un "+ "argomento non valido letto a meta'\n", arg, got);+ fails++;+ } else if (got != -12345) {+ printf(" FAIL \"%s\": rifiutato ma ha scritto %d nell'uscita\n",+ arg, got);+ fails++;+ }+}++int main(void)+{+ /* Quello che deve continuare a funzionare. */+ accepts("32", 32);+ accepts("0", 0); /* il limite cap >= 1 e' del chiamante, non di qui */+ accepts("-5", -5);+ accepts("+32", 32);+ accepts(" 32", 32); /* strtol salta gli spazi iniziali */+ accepts("32 ", 32); /* e noi quelli finali */+ accepts("32\r", 32); /* uno script salvato con le terminazioni Windows */+ accepts("32\n", 32);+ accepts("2147483647", 2147483647);++ /* Quello che prima passava in silenzio. */+ rejects("3x2"); /* il refuso vero: atoi diceva 3 */+ rejects("32abc");+ rejects("abc");+ rejects("");+ rejects(" ");+ rejects("0x20"); /* base 16 non e' quello che intendeva chi scrive */+ rejects("3.5");+ rejects("99999999999999999999"); /* ERANGE */+ rejects("-99999999999999999999");+ rejects("2147483648"); /* un piu' di INT_MAX: non entra in un int */+ rejects(NULL);++ if (fails) { printf("test_cli_args: %d fallimenti\n", fails); return 1; }+ printf("test_cli_args: ok\n");+ return 0;+}