Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
fix(qwen36): a truncated multibyte tail made the tokenizer read past the prompt
Qutibah Ananzeh3b459d079048
diff --git a/c/Makefile b/c/Makefileindex 2e66298a..78c97fc8 100644--- a/c/Makefile+++ b/c/Makefile@@ -1150,6 +1150,11 @@ tests/test_qwen36_json_escape$(EXE): tests/test_qwen36_json_escape.c qwen36.c extests/test_qwen36_tok_merges$(EXE): tests/test_qwen36_tok_merges.c qwen36.c expert_ffn.h 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)+# A byte-counted serving payload may end mid-character; the pre-tokenizer must+# not read past it. qwen38 already gates this (tests/test_qwen38_tokenizer.c).+tests/test_qwen36_tok_truncated$(EXE): tests/test_qwen36_tok_truncated.c qwen36.c expert_ffn.h 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)+# Reproducible local timing evidence; intentionally not a noisy CI perf gate.tests/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)diff --git a/c/qwen36.c b/c/qwen36.cindex 3f793268..a85903e5 100644--- a/c/qwen36.c+++ b/c/qwen36.c@@ -149,17 +149,24 @@ static int uclass(unsigned cp){if (cp>=0x2030&&cp<=0x205E) return U_P;return U_O;}+/* A serving payload is byte-counted and may end in a truncated multibyte+ * sequence. Treat that byte as one invalid unit without reading past it: this+ * is qwen38's utf8_decode, which the two engines share a pre-tokenizer with. */static int utf8_decode(const char *s,int i,int n,int *adv){+ if(!s||i<0||i>=n){if(adv)*adv=0;return 0xfffd;}unsigned char c=(unsigned char)s[i]; int cp,a;if(c<0x80){cp=c;a=1;}else if((c>>5)==6){cp=c&0x1F;a=2;}else if((c>>4)==14){cp=c&0x0F;a=3;}else if((c>>3)==30){cp=c&0x07;a=4;}else {cp=c;a=1;}- for(int k=1;k<a;k++){ if(i+k<n && ((unsigned char)s[i+k]&0xC0)==0x80) cp=(cp<<6)|((unsigned char)s[i+k]&0x3F); }+ for(int k=1;k<a;k++){+ if(i+k>=n||((unsigned char)s[i+k]&0xC0)!=0x80){if(adv)*adv=1;return c;}+ cp=(cp<<6)|((unsigned char)s[i+k]&0x3F);+ }if(adv)*adv=a; return cp;}-static int utf8_adv(const char *s,int i){ int a; utf8_decode(s,i,0x7fffffff,&a); return a; }+static int utf8_adv(const char *s,int i,int n){ int a; utf8_decode(s,i,n,&a); return a?a:1; }static void build_byte_sym(void){for(int i=0;i<512;i++) g_unmap[i]=-1;@@ -252,7 +259,8 @@ static void encode_text(const char *text,int **out_ids,int *out_n){while(i<tlen){int sid; int L=try_special(text,i,tlen,&sid);if(L>0){ push_id(&ids,&n,&cap,sid); i+=L; continue; }- int j=pretok_end(text,i,tlen); if(j<=i) j=i+utf8_adv(text,i);+ int j=pretok_end(text,i,tlen); if(j<=i) j=i+utf8_adv(text,i,tlen);+ if(j>tlen) j=tlen;bpe_piece(text+i,j-i,&ids,&n,&cap);i=j;}diff --git a/c/tests/test_qwen36_tok_truncated.c b/c/tests/test_qwen36_tok_truncated.cnew file mode 100644index 00000000..0c341c01--- /dev/null+++ b/c/tests/test_qwen36_tok_truncated.c@@ -0,0 +1,73 @@+/* A serving payload is byte-counted, not NUL-terminated text: serve_read_req+ * reads exactly plen bytes into malloc(plen+1) and serve_one hands them to+ * encode_text as raw prompt text. A prompt whose tail is a truncated multibyte+ * sequence therefore reaches the pre-tokenizer with a lead byte whose+ * continuation bytes are not there.+ *+ * qwen36 and qwen38 carry the same pre-tokenizer. qwen38's copy treats that+ * byte as one invalid unit and is gated in tests/test_qwen38_tokenizer.c;+ * qwen36's copy read the continuation bytes anyway and reported the lead byte's+ * full length, so pretok_end returned a piece end past the prompt. These are+ * the sibling's assertions, run against this engine's copy. */+#define _GNU_SOURCE+#define main qwen36_main_unused+#include "../qwen36.c"+#undef main++#define CHECK(condition) do { \+ if (!(condition)) { \+ fprintf(stderr, "%s:%d: check failed: %s\n", \+ __FILE__, __LINE__, #condition); \+ return 1; \+ } \+} while (0)++/* the buffer serve_read_req builds: exactly n bytes plus the NUL */+static char *payload(const char *bytes, int n) {+ char *p = (char *)malloc((size_t)n + 1);+ if (!p) return NULL;+ memcpy(p, bytes, (size_t)n);+ p[n] = 0;+ return p;+}++int main(void) {+ /* the gate tests/test_qwen38_tokenizer.c already applies to the sibling */+ const char lead3[] = { (char)0xe2, 0 };+ int adv = 0;+ CHECK(utf8_decode(lead3, 0, 1, &adv) == 0xe2);+ CHECK(adv == 1);+ CHECK(pretok_end(lead3, 0, 1) == 1);++ /* a truncated 2-byte lead: pretok_end must not walk past n */+ {+ char *p = payload("a\xc3", 2);+ CHECK(p != NULL);+ CHECK(pretok_end(p, 1, 2) == 2);+ free(p);+ }++ /* a truncated 4-byte lead: utf8_adv must report one byte, not four */+ {+ char *p = payload("hi \xf0\x9f", 5);+ CHECK(p != NULL);+ CHECK(utf8_adv(p, 3, 5) == 1);+ CHECK(pretok_end(p, 3, 5) <= 5);+ free(p);+ }++ /* an out-of-range index answers U+FFFD with advance 0, as qwen38 does */+ CHECK(utf8_decode(lead3, 1, 1, &adv) == 0xfffd);+ CHECK(adv == 0);++ /* well-formed input is untouched: the decode is still one whole character */+ {+ const char *ok = "\xe4\xbd\xa0"; /* U+4F60 */+ CHECK(utf8_decode(ok, 0, 3, &adv) == 0x4f60);+ CHECK(adv == 3);+ CHECK(utf8_adv(ok, 0, 3) == 3);+ }++ puts("test_qwen36_tok_truncated: ok");+ return 0;+}