Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
fix(serve): kimi, inkling, olmoe clamp max_tokens to the context
kevin9327162737774747
diff --git a/c/Makefile b/c/Makefileindex fe37e6bb..cc740262 100644--- a/c/Makefile+++ b/c/Makefile@@ -1091,7 +1091,7 @@ tests/bench_cuda_resident_batch$(EXE): tests/bench_cuda_resident_batch.cu backen# 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 cli_args.h st.h json.h compat.h sample.h tok.h tok_unicode.h tok_unicode_o200k.h omp_tune.h kv_prefix.h pin_pool.h route_trace.h serve_codec.h edge_adapters.h edge_runtime.h edge_tok_internal.h fused_simd.h segment_adapter_internal.h segment_adapters.h segment_runtime.h sse41_kernels.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 kv_prefix.h pin_pool.h route_trace.h serve_codec.h serve_budget.h edge_adapters.h edge_runtime.h edge_tok_internal.h fused_simd.h segment_adapter_internal.h segment_adapters.h segment_runtime.h sse41_kernels.h$(CC) $(NOCUDA_CFLAGS) olmoe.c -o olmoe$(EXE) $(NOCUDA_LDFLAGS)# Qwen3.6-35B-A3B engine (hybrid Gated Attention + Gated DeltaNet + streaming@@ -1233,7 +1233,7 @@ tests/test_qwen36_tokenizer$(EXE): tests/test_qwen36_tokenizer.c qwen36.c experttests/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 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 pin_pool.h serve_codec.h backend_cuda_ink.h backend_metal.h edge_adapters.h edge_runtime.h edge_tok_internal.h segment_adapter_internal.h segment_adapters.h segment_runtime.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 pin_pool.h serve_codec.h serve_budget.h backend_cuda_ink.h backend_metal.h edge_adapters.h edge_runtime.h edge_tok_internal.h segment_adapter_internal.h segment_adapters.h segment_runtime.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).@@ -1253,7 +1253,7 @@ NOCUDA_LDFLAGS = $(filter-out -lcudart -lstdc++ -lcuda -L$(CUDA_HOME)/lib64 \glm53$(EXE): glm53.c decode_batch.h pin_pool.h cli_args.h st.h json.h stop_ids.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h omp_tune.h serve_poll.h route_trace.h quant.h idot.h hyper_connections.h delta_attention.h sparse_index.h vision_tower.h backend_metal.h backend_vulkan.h edge_adapter_internal.h edge_adapters.h edge_runtime.h segment_adapter_internal.h segment_adapters.h segment_runtime.h $(METAL_OBJ) $(VK_OBJ) $(VK_SPV)$(CC) $(CFLAGS) glm53.c $(METAL_OBJ) $(VK_OBJ) -o glm53$(EXE) $(LDFLAGS)-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 idot.h omp_tune.h route_trace.h kv_prefix.h pin_pool.h serve_codec.h backend_cuda.h backend_metal.h backend_vulkan.h edge_adapters.h edge_runtime.h edge_tok_internal.h hybrid_split.h segment_adapter_internal.h segment_adapters.h segment_runtime.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 idot.h omp_tune.h route_trace.h kv_prefix.h pin_pool.h serve_codec.h serve_budget.h backend_cuda.h backend_metal.h backend_vulkan.h edge_adapters.h edge_runtime.h edge_tok_internal.h hybrid_split.h segment_adapter_internal.h segment_adapters.h segment_runtime.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@@ -1429,13 +1429,16 @@ tests/test_pin_pool$(EXE): tests/test_pin_pool.c pin_pool.h kv_prefix.htests/test_serve_codec$(EXE): tests/test_serve_codec.c serve_codec.h compat.h$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)+tests/test_serve_budget$(EXE): tests/test_serve_budget.c serve_budget.h+ $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)+tests/test_segment_runtime$(EXE): tests/test_segment_runtime.c segment_runtime.c segment_runtime.h$(CC) $(CFLAGS) tests/test_segment_runtime.c segment_runtime.c -o $@ $(LDFLAGS)tests/test_segment_conformance$(EXE): tests/test_segment_conformance.c tests/segment_conformance_fixtures.c tests/segment_conformance_fixtures.h segment_runtime.c segment_runtime.h$(CC) $(CFLAGS) tests/test_segment_conformance.c tests/segment_conformance_fixtures.c segment_runtime.c -o $@ $(LDFLAGS)-tests/test_inkling_serve_framing$(EXE): tests/test_inkling_serve_framing.c 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)+tests/test_inkling_serve_framing$(EXE): tests/test_inkling_serve_framing.c 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 serve_budget.h $(INK_CUDA_OBJ) $(METAL_OBJ)$(CC) $(CFLAGS) $< $(INK_CUDA_OBJ) $(METAL_OBJ) -o $@ $(LDFLAGS)tests/test_inkling_shared_batch$(EXE): tests/test_inkling_shared_batch.c 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)@@ -1449,7 +1452,7 @@ tests/bench_inkling_shared_batch$(EXE): tests/bench_inkling_shared_batch.c inklitests/test_inkling_cache_index$(EXE): tests/test_inkling_cache_index.c 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)$(CC) $(CFLAGS) $< $(INK_CUDA_OBJ) $(METAL_OBJ) -o $@ $(LDFLAGS)-tests/test_kimi_serve_framing$(EXE): tests/test_kimi_serve_framing.c kimi_k3.c kv_prefix.h serve_codec.h st.h json.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h quant.h idot.h omp_tune.h route_trace.h+tests/test_kimi_serve_framing$(EXE): tests/test_kimi_serve_framing.c kimi_k3.c kv_prefix.h serve_codec.h serve_budget.h st.h json.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h quant.h idot.h omp_tune.h route_trace.h$(CC) $(NOCUDA_CFLAGS) $< $(VK_OBJ) -o $@ $(NOCUDA_LDFLAGS)tests/test_kimi_cache_index$(EXE): tests/test_kimi_cache_index.c kimi_k3.c kv_prefix.h serve_codec.h st.h json.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h quant.h idot.h omp_tune.h route_trace.h@@ -1464,7 +1467,7 @@ tests/test_k3_chat_tools$(EXE): tests/test_k3_chat_tools.c kimi_k3.c kv_prefix.htests/test_olmoe_matmul_q$(EXE): tests/test_olmoe_matmul_q.c 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$(CC) $(NOCUDA_CFLAGS) $< -o $@ $(NOCUDA_LDFLAGS)-tests/test_olmoe_serve_framing$(EXE): tests/test_olmoe_serve_framing.c 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+tests/test_olmoe_serve_framing$(EXE): tests/test_olmoe_serve_framing.c 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 serve_budget.h$(CC) $(NOCUDA_CFLAGS) $< -o $@ $(NOCUDA_LDFLAGS)tests/test_olmoe_cache_index$(EXE): tests/test_olmoe_cache_index.c 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.hdiff --git a/c/inkling.c b/c/inkling.cindex 0ee1740d..b58b6bb5 100644--- a/c/inkling.c+++ b/c/inkling.c@@ -43,6 +43,7 @@#include "kv_prefix.h"#include "pin_pool.h" /* KV prefix reuse (shared) */ /* shared routing telemetry (#700) */#include "serve_codec.h"+#include "serve_budget.h"#ifdef COLI_SEGMENT_ADAPTER#include "segment_runtime.h"#include "segment_adapters.h"@@ -2198,15 +2199,20 @@ static void apply_rep_penalty(float *logit, int n, const int *hist, int nhist, f}}-/* reject a prompt that would overrun the served KV bound (CTX_MAX, default 8192).- * The refusal is the frame the gateway turns into a 400 context_length_exceeded- * (#506, #1381); free text here reached the client as a 500. One request is- * served at a time, so the returned buffer is only read before the next call. */+/* Refuse only a prompt that does not fit the served KV bound (CTX_MAX,+ * default 8192). max_tokens is a ceiling: coli chat's interactive default+ * (16384) used to 400 every turn because 2 + 16384 > 8192. The refusal is+ * the frame the gateway turns into a 400 context_length_exceeded (#506,+ * #1381); free text here reached the client as a 500. One request is served+ * at a time, so the returned buffer is only read before the next call. */+static int ink_ctx_max(void) {+ const char *cm = getenv("CTX_MAX");+ return cm ? atoi(cm) : 8192;+}static const char *prompt_reject(int np, int want) {static char message[96];- const char *cm = getenv("CTX_MAX");- int ctx_max = cm ? atoi(cm) : 8192;- if (np + want <= ctx_max) return NULL;+ int ctx_max = ink_ctx_max();+ if (coli_serve_budget(np, want, ctx_max, 0) >= 0) return NULL;snprintf(message, sizeof(message),"CONTEXT_EXCEEDED prompt_tokens=%d requested=%d capacity=%d",np, want, ctx_max);@@ -2323,8 +2329,19 @@ static int serve_one(Model *m, Tok *T, SReq *q) {int *ids = malloc((size_t)cap * sizeof(int));int np = tok_encode(T, q->payload, q->plen, ids, cap);if (np <= 0) { coli_serve_write_error(stdout,q->id,"empty prompt"); free(ids); return 0; }- const char *bad = prompt_reject(np, q->max_tok);- if (bad) { coli_serve_write_error(stdout,q->id,bad); free(ids); return 0; }+ int ctx_max = ink_ctx_max();+ int budget = coli_serve_budget(np, q->max_tok, ctx_max, q->logprobs > 0);+ if (budget < 0) {+ const char *bad = prompt_reject(np, q->max_tok);+ coli_serve_write_error(stdout,q->id,bad ? bad : "CONTEXT_EXCEEDED");+ free(ids); return 0;+ }+ if (budget < q->max_tok) {+ fprintf(stderr, "[serve] max_tokens %d clamped to %d (context %d - prompt %d); "+ "raise CTX_MAX for longer answers\n",+ q->max_tok, budget, ctx_max, np);+ q->max_tok = budget;+ }/* audio: every <|audio|> placeholder must have exactly one DMel frame */int naud = q->alen / m->c.mel_bins;if (q->alen % m->c.mel_bins != 0 || audio_tok_count(m, ids, np) != naud) {diff --git a/c/kimi_k3.c b/c/kimi_k3.cindex a05f6336..054b0e0d 100644--- a/c/kimi_k3.c+++ b/c/kimi_k3.c@@ -117,6 +117,7 @@#include "pin_pool.h" /* coli_pin_slots_wanted: quanti scatti tenere */#include "hybrid_split.h" /* KV prefix reuse (shared) */#include "serve_codec.h"+#include "serve_budget.h"#ifdef COLI_SEGMENT_ADAPTER#include "segment_runtime.h"#include "segment_adapters.h"@@ -3072,12 +3073,24 @@ static int serve_one(Model *m, Tok *T, ServeReq *q){np+=tok_encode(T,q->payload,q->plen,ids+np,cap-np);}int max_ctx=getenv("K3_MAXT")?atoi(getenv("K3_MAXT")):8192;- if(np<1||(int64_t)np+q->max_tok>max_ctx){ /* SEC (GHSA-gf38): int64 so np+max_tok can't wrap negative */- char message[160];- snprintf(message,sizeof(message),- "CONTEXT_EXCEEDED prompt_tokens=%d requested=%d capacity=%d",- np,q->max_tok,max_ctx);- coli_serve_write_error(stdout,q->id,message); free(ids); return 0;+ int budget=coli_serve_budget(np,q->max_tok,max_ctx,q->logprobs>0);+ if(budget<0){+ if(np<1){+ coli_serve_write_error(stdout,q->id,"EMPTY_PROMPT");+ }else{+ char message[160];+ snprintf(message,sizeof(message),+ "CONTEXT_EXCEEDED prompt_tokens=%d requested=%d capacity=%d",+ np,q->max_tok,max_ctx);+ coli_serve_write_error(stdout,q->id,message);+ }+ free(ids); return 0;+ }+ if(budget<q->max_tok){+ fprintf(stderr,"[serve] max_tokens %d clamped to %d (context %d - prompt %d); "+ "raise K3_MAXT for longer answers\n",+ q->max_tok,budget,max_ctx,np);+ q->max_tok=budget;}coli_serve_write_accept(stdout,q->id,np);/* Declare the structured sideband before any generated DATA. Even andiff --git a/c/olmoe.c b/c/olmoe.cindex 944566c5..d56b606d 100644--- a/c/olmoe.c+++ b/c/olmoe.c@@ -46,6 +46,7 @@#include "kv_prefix.h"#include "pin_pool.h" /* piu scatti annidati */ /* riuso del prefisso tra turni (shared) */#include "serve_codec.h"+#include "serve_budget.h"#ifdef COLI_SEGMENT_ADAPTER#include "segment_runtime.h"#include "segment_adapters.h"@@ -1612,7 +1613,8 @@ static int serve_one(Model *m, Tok *T, SReq *q, int ctx_cap) {int *ids = malloc((size_t)cap * sizeof(int));int np = tok_encode(T, q->payload, q->plen, ids, cap);if (np <= 0) { coli_serve_write_error(stdout, q->id, "empty prompt"); free(ids); return 0; }- if (np + q->max_tok > ctx_cap) {+ int budget = coli_serve_budget(np, q->max_tok, ctx_cap, q->logprobs > 0);+ if (budget < 0) {char message[128];/* The frame the gateway turns into a 400 context_length_exceeded* (#506, #1381). Free text here reached the client as a 500. */@@ -1621,6 +1623,12 @@ static int serve_one(Model *m, Tok *T, SReq *q, int ctx_cap) {np, q->max_tok, ctx_cap);coli_serve_write_error(stdout, q->id, message); free(ids); return 0;}+ if (budget < q->max_tok) {+ fprintf(stderr, "[serve] max_tokens %d clamped to %d (context %d - prompt %d); "+ "raise CTX for longer answers\n",+ q->max_tok, budget, ctx_cap, np);+ q->max_tok = budget;+ }g_temp = q->temp; g_nuc = q->top_p;/* A chat client resends the whole transcript every turn. If this prompt* begins with the ids the cache was built from, those keys and values AREdiff --git a/c/serve_budget.h b/c/serve_budget.hnew file mode 100644index 00000000..36c8ae50--- /dev/null+++ b/c/serve_budget.h@@ -0,0 +1,21 @@+#ifndef COLI_SERVE_BUDGET_H+#define COLI_SERVE_BUDGET_H++/* max_tokens is a ceiling, not a target (#260/#382/#1641).+ * Returns the tokens the request may generate, or -1 when the PROMPT does+ * not fit. Generation needs one free position; a read-only logprobs request+ * may fill the context exactly. Refusing when prompt + budget exceeded the+ * context turned coli chat's interactive default (16384) into a 400 on every+ * Kimi/Inkling message against the default 8192-token window. */++static inline int coli_serve_budget(int prompt, int requested, int context,+ int read_only)+{+ if (prompt < 1) return -1;+ int room = context - prompt;+ if (read_only) return room < 0 ? -1 : (requested < room ? requested : room);+ if (room < 1) return -1;+ return requested > room ? room : requested;+}++#endifdiff --git a/c/tests/test_inkling_serve_framing.c b/c/tests/test_inkling_serve_framing.cindex 9bd1e7cc..d48eabe0 100644--- a/c/tests/test_inkling_serve_framing.c+++ b/c/tests/test_inkling_serve_framing.c@@ -101,14 +101,18 @@ static void test_malformed_submit_cannot_become_control(void)}/* The gateway answers `ERROR <id> CONTEXT_EXCEEDED ...` with a 400- * context_length_exceeded; any other refusal text reaches the client as a 500. */+ * context_length_exceeded; any other refusal text reaches the client as a 500.+ * max_tokens is a ceiling: coli chat's 16384 used to 400 a prompt that fits. */static void test_over_long_prompt_is_refused_with_the_context_frame(void){assert(setenv("CTX_MAX","16",1)==0);assert(prompt_reject(12,4)==NULL);+ assert(prompt_reject(2,16384)==NULL);+ assert(coli_serve_budget(2,16384,16,0)==14);const char *refusal=prompt_reject(30,4);assert(refusal);assert(strcmp(refusal,"CONTEXT_EXCEEDED prompt_tokens=30 requested=4 capacity=16")==0);+ assert(prompt_reject(16,4)!=NULL);}int main(void)diff --git a/c/tests/test_kimi_serve_framing.c b/c/tests/test_kimi_serve_framing.cindex 27fa5b80..4d2b972f 100644--- a/c/tests/test_kimi_serve_framing.c+++ b/c/tests/test_kimi_serve_framing.c@@ -121,12 +121,22 @@ static void test_malformed_submit_cannot_become_a_control(void)fclose(in); fclose(out);}+static void test_max_tokens_is_a_ceiling(void)+{+ /* coli chat sends interactive_max_output=16384 against K3_MAXT=8192. */+ assert(coli_serve_budget(2,16384,8192,0)==8190);+ assert(coli_serve_budget(100,50,8192,0)==50);+ assert(coli_serve_budget(8192,1,8192,0)==-1);+ assert(coli_serve_budget(8192,0,8192,1)==0);+}+int main(void){test_submit_preserves_k3_wire_bytes();test_controls_match_only_active_request();test_errors_and_busy_payload_are_byte_exact();test_malformed_submit_cannot_become_a_control();+ test_max_tokens_is_a_ceiling();puts("kimi serve framing baseline: ok");return 0;}diff --git a/c/tests/test_olmoe_serve_framing.c b/c/tests/test_olmoe_serve_framing.cindex 41858edd..546e9af4 100644--- a/c/tests/test_olmoe_serve_framing.c+++ b/c/tests/test_olmoe_serve_framing.c@@ -175,6 +175,14 @@ static void test_bad_frame_stops_before_the_next_command(void)fclose(output);}+static void test_max_tokens_is_a_ceiling(void)+{+ assert(coli_serve_budget(2, 1024, 4096, 0) == 1024);+ assert(coli_serve_budget(3500, 1024, 4096, 0) == 596);+ assert(coli_serve_budget(4096, 1, 4096, 0) == -1);+ assert(coli_serve_budget(4096, 0, 4096, 1) == 0);+}+int main(void){test_submit_moves_exact_payload_into_queue();@@ -182,6 +190,7 @@ int main(void)test_errors_are_byte_exact_and_frames_are_consumed();test_invalid_submit_does_not_parse_its_payload_as_control();test_bad_frame_stops_before_the_next_command();+ test_max_tokens_is_a_ceiling();puts("olmoe serve framing baseline: ok");return 0;}diff --git a/c/tests/test_serve_budget.c b/c/tests/test_serve_budget.cnew file mode 100644index 00000000..5c6ce511--- /dev/null+++ b/c/tests/test_serve_budget.c@@ -0,0 +1,29 @@+/* max_tokens is a ceiling. The remaining serve engines (kimi, inkling, olmoe)+ * used to refuse when prompt + budget exceeded the context, so coli chat's+ * interactive default (16384) 400'd every Kimi/Inkling turn at the default+ * 8192-token window. Only a prompt that does not fit is refused. */+#include "../serve_budget.h"+#include <limits.h>+#include <stdio.h>++#define CHECK(c) do { if (!(c)) { \+ fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #c); return 1; \+} } while (0)++int main(void) {+ CHECK(coli_serve_budget(2, 16384, 8192, 0) == 8190);+ CHECK(coli_serve_budget(2, 8192, 8192, 0) == 8190);+ CHECK(coli_serve_budget(100, 50, 8192, 0) == 50);+ CHECK(coli_serve_budget(8191, 1, 8192, 0) == 1);+ CHECK(coli_serve_budget(8192, 1, 8192, 0) == -1);+ CHECK(coli_serve_budget(9000, 1, 8192, 0) == -1);+ CHECK(coli_serve_budget(0, 1, 8192, 0) == -1);+ CHECK(coli_serve_budget(8192, 0, 8192, 1) == 0);+ CHECK(coli_serve_budget(8193, 0, 8192, 1) == -1);+ CHECK(coli_serve_budget(20, 0, 8192, 1) == 0);+ CHECK(coli_serve_budget(8191, 100, 8192, 1) == 1);+ CHECK(coli_serve_budget(20, INT_MAX, 8192, 0) == 8172);+ CHECK(coli_serve_budget(3500, 1024, 4096, 0) == 596);+ puts("serve budget ceiling: ok");+ return 0;+}