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(kimi): freeze serve wire framing

Mario Gutierrez6a33455bbe78
diff --git a/c/.gitignore b/c/.gitignore
index 98497fee..faf8f236 100644
--- a/c/.gitignore
+++ b/c/.gitignore
@@ -36,6 +36,7 @@ tests/test_int3_load
tests/test_tok_o200k
tests/test_pipe_block
tests/test_kimi_request_state
+tests/test_kimi_serve_framing
*.o
*.dll
cublasLt64*.dll
diff --git a/c/Makefile b/c/Makefile
index ee941025..d7e714cb 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -995,6 +995,9 @@ tests/test_decode_batch$(EXE): tests/test_decode_batch.c decode_batch.h
tests/test_serve_codec$(EXE): tests/test_serve_codec.c serve_codec.h compat.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
+tests/test_kimi_serve_framing$(EXE): tests/test_kimi_serve_framing.c kimi_k3.c kv_prefix.h st.h json.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h quant.h omp_tune.h route_trace.h
+ $(CC) $(NOCUDA_CFLAGS) $< $(VK_OBJ) -o $@ $(NOCUDA_LDFLAGS)
+
# olmoe's matmul_q, not colibri's: compares the IDOT path against FP32
# ACTIVATIONS rather than an integer reference. NOCUDA_* for the same reason
# the olmoe target uses it -- olmoe.c contains no COLI_CUDA code.
diff --git a/c/kimi_k3.c b/c/kimi_k3.c
index 21303bef..a6453e18 100644
--- a/c/kimi_k3.c
+++ b/c/kimi_k3.c
@@ -1905,9 +1905,9 @@ static int serve_stdin_readable(void){
return coli_stdin_readable();
}
-static int serve_read_req(ServeReq *q, const char *active){
+static int serve_read_req(FILE *in, FILE *out, ServeReq *q, const char *active){
char line[512], cmd[16], id[64];
- if(!fgets(line,sizeof(line),stdin)) return -1;
+ if(!fgets(line,sizeof(line),in)) return -1;
if(sscanf(line,"%15s %63s",cmd,id)<2) return 0;
if(!strcmp(cmd,"CANCEL")||!strcmp(cmd,"STOP")) return active&&!strcmp(active,id);
if(strcmp(cmd,"SUBMIT")) return 0;
@@ -1918,13 +1918,13 @@ static int serve_read_req(ServeReq *q, const char *active){
* signed np+max_tok context check below and made the kv_alloc size
* negative, so kv_alloc's early-return kept the previous request's small
* KV buffers and the generation loop wrote past them (heap OOB write). */
- printf("ERROR %s bad submit header\n",id); fflush(stdout); return 0;
+ fprintf(out,"ERROR %s bad submit header\n",id); fflush(out); return 0;
}
(void)slot;
char *payload=malloc((size_t)plen+1);
- if(!payload){ printf("ERROR %s out of memory\n",id); fflush(stdout); return 0; }
- if(fread(payload,1,(size_t)plen,stdin)!=(size_t)plen){ free(payload); return -1; }
- (void)fgetc(stdin); payload[plen]=0;
+ if(!payload){ fprintf(out,"ERROR %s out of memory\n",id); fflush(out); return 0; }
+ if(fread(payload,1,(size_t)plen,in)!=(size_t)plen){ free(payload); return -1; }
+ (void)fgetc(in); payload[plen]=0;
snprintf(q->id,sizeof(q->id),"%s",id);
q->max_tok=max_tok; q->temp=temp; q->top_p=top_p;
q->payload=payload; q->plen=plen;
@@ -1937,7 +1937,7 @@ static int k3_serve_poll_cancel(void *context){
const char *active=context;
int cancelled=0;
while(serve_stdin_readable()){
- ServeReq queued={0}; int r=serve_read_req(&queued,active);
+ ServeReq queued={0}; int r=serve_read_req(stdin,stdout,&queued,active);
if(r<0||r==1) cancelled=1;
if(r==2){
printf("ERROR %s engine busy\n",queued.id); fflush(stdout);
@@ -2063,7 +2063,7 @@ static void serve_one(Model *m, Tok *T, ServeReq *q){
if(!eos) gen++;
while(serve_stdin_readable()){
ServeReq queued={0};
- int r=serve_read_req(&queued,q->id);
+ int r=serve_read_req(stdin,stdout,&queued,q->id);
if(r<0){ cancelled=1; break; }
if(r==1) cancelled=1;
if(r==2){
@@ -2101,7 +2101,7 @@ static void serve_loop(Model *m, Tok *T){
fflush(stdout);
for(;;){
ServeReq q={0}; int r;
- do r=serve_read_req(&q,NULL); while(r==0);
+ do r=serve_read_req(stdin,stdout,&q,NULL); while(r==0);
if(r<0) return;
if(r==2){ serve_one(m,T,&q); free(q.payload); }
}
diff --git a/c/tests/test_kimi_serve_framing.c b/c/tests/test_kimi_serve_framing.c
new file mode 100644
index 00000000..4f521fd0
--- /dev/null
+++ b/c/tests/test_kimi_serve_framing.c
@@ -0,0 +1,103 @@
+/* Freeze Kimi K3's existing request framing before adopting serve_codec.h. */
+#define main kimi_k3_main_unused
+#include "../kimi_k3.c"
+#undef main
+
+#include <assert.h>
+
+static void binary_stream(FILE *stream)
+{
+#ifdef _WIN32
+ assert(_setmode(_fileno(stream), _O_BINARY) != -1);
+#else
+ (void)stream;
+#endif
+}
+
+static FILE *bytes_input(const void *data, size_t size)
+{
+ FILE *stream=tmpfile(); assert(stream); binary_stream(stream);
+ assert(fwrite(data,1,size,stream)==size); rewind(stream); return stream;
+}
+
+static size_t read_output(FILE *stream, char *output, size_t capacity)
+{
+ assert(fflush(stream)==0); rewind(stream);
+ return fread(output,1,capacity,stream);
+}
+
+static void test_submit_preserves_k3_wire_bytes(void)
+{
+ static const char payload[]=
+ "K3CHAT1\nM system 11\nBe precise."
+ "M user 11\n你好\nKimi"
+ "A 7 9\nbecause你好。"
+ "M user 8\nContinueG 1\n";
+ char frame[512];
+ int header=snprintf(frame,sizeof(frame),"SUBMIT req-7 0 %zu 4 0.7 0.95\n",
+ sizeof(payload)-1);
+ assert(header>0);
+ memcpy(frame+header,payload,sizeof(payload)-1);
+ frame[header+sizeof(payload)-1]='\n';
+ FILE *in=bytes_input(frame,(size_t)header+sizeof(payload));
+ FILE *out=tmpfile(); assert(out); binary_stream(out);
+ ServeReq request={0};
+
+ assert(serve_read_req(in,out,&request,NULL)==2);
+ assert(strcmp(request.id,"req-7")==0);
+ assert(request.plen==(int)sizeof(payload)-1);
+ assert(memcmp(request.payload,payload,sizeof(payload)-1)==0);
+ assert(request.max_tok==4);
+ assert(request.temp>.69f&&request.top_p>.94f);
+ assert(fgetc(in)==EOF);
+ char output[16]; assert(read_output(out,output,sizeof(output))==0);
+ free(request.payload); fclose(in); fclose(out);
+}
+
+static void test_controls_match_only_active_request(void)
+{
+ static const char *commands[]={"STOP req-7\n","CANCEL req-7\n"};
+ FILE *out=tmpfile(); assert(out); binary_stream(out);
+ for(size_t i=0;i<2;i++){
+ ServeReq request={0};
+ FILE *match=bytes_input(commands[i],strlen(commands[i]));
+ assert(serve_read_req(match,out,&request,"req-7")==1); fclose(match);
+ FILE *other=bytes_input(commands[i],strlen(commands[i]));
+ assert(serve_read_req(other,out,&request,"req-8")==0); fclose(other);
+ }
+ fclose(out);
+}
+
+static void test_errors_and_busy_payload_are_byte_exact(void)
+{
+ static const char bad[]="SUBMIT bad 0 -1 4 0.7 0.95\n";
+ FILE *in=bytes_input(bad,sizeof(bad)-1);
+ FILE *out=tmpfile(); assert(out); binary_stream(out);
+ ServeReq request={0};
+ assert(serve_read_req(in,out,&request,NULL)==0);
+ char output[128]; size_t count=read_output(out,output,sizeof(output));
+ static const char want[]="ERROR bad bad submit header\n";
+ assert(count==sizeof(want)-1&&memcmp(output,want,count)==0);
+ fclose(in); fclose(out);
+
+ static const char busy[]="SUBMIT busy 0 3 1 0 1\nx\ny\n";
+ in=bytes_input(busy,sizeof(busy)-1); out=tmpfile(); assert(out); binary_stream(out);
+ assert(serve_read_req(in,out,&request,"live")==2);
+ assert(request.plen==3&&memcmp(request.payload,"x\ny",3)==0);
+ fprintf(out,"ERROR %s engine busy\n",request.id); fflush(out);
+ free(request.payload);
+ count=read_output(out,output,sizeof(output));
+ static const char busy_want[]="ERROR busy engine busy\n";
+ assert(count==sizeof(busy_want)-1&&memcmp(output,busy_want,count)==0);
+ assert(fgetc(in)==EOF);
+ fclose(in); fclose(out);
+}
+
+int main(void)
+{
+ test_submit_preserves_k3_wire_bytes();
+ test_controls_match_only_active_request();
+ test_errors_and_busy_payload_are_byte_exact();
+ puts("kimi serve framing baseline: ok");
+ return 0;
+}
diff --git a/c/tests/test_openai_server.py b/c/tests/test_openai_server.py
index 53560487..c7127cb0 100644
--- a/c/tests/test_openai_server.py
+++ b/c/tests/test_openai_server.py
@@ -472,6 +472,39 @@ class FakeProcess:
class DispatcherTest(unittest.TestCase):
+ def test_kimi_request_and_response_transcript_is_byte_exact(self):
+ prompt = render_chat_kimi([
+ {"role": "system", "content": "Be precise."},
+ {"role": "user", "content": "你好\nKimi"},
+ {"role": "assistant", "reasoning_content": "because",
+ "content": "你好。"},
+ {"role": "user", "content": "Continue"},
+ ], enable_thinking=True)
+ payload = prompt.encode("utf-8")
+ expected = (f"SUBMIT 1 0 {len(payload)} 4 0.25 0.9\n".encode() +
+ payload + b"\n")
+
+ def respond(process, frame):
+ self.assertEqual(frame, expected)
+ process.stdout.feed(
+ b"ACCEPT 1 42\n"
+ b"DATA 1 4\nA\n\xc3\xa9\n"
+ b"DONE 1 STAT 1 2.500 50.0 1.25 42 0\n"
+ )
+
+ process = FakeProcess(respond)
+ with patch("openai_server.ARCH", "kimi"), \
+ patch("openai_server.subprocess.Popen", return_value=process):
+ engine = Engine("kimi_k3", "model")
+ chunks = []
+ stats = engine.generate(prompt, 4, 0.25, 0.9, chunks.append)
+ engine.close()
+
+ self.assertEqual(process.writes, [expected])
+ self.assertEqual(chunks, ["A\né"])
+ self.assertEqual(stats["completion_tokens"], 1)
+ self.assertEqual(stats["prompt_tokens"], 42)
+
def test_olmoe_request_and_response_transcript_is_byte_exact(self):
expected = b"SUBMIT 1 0 5 3 0.25 0.9\nH\xc3\xa9\nx\n"