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

qwen36: split added tokens before the pre-tokenizer, and the three whitespace rules (#1653)

JustVugg682f62dc6c02
diff --git a/c/Makefile b/c/Makefile
index edc703f6..4958e1a0 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -1197,6 +1197,10 @@ tests/test_qwen36_tok_truncated$(EXE): tests/test_qwen36_tok_truncated.c qwen36.
tests/test_qwen36_encode_oom$(EXE): tests/test_qwen36_encode_oom.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)
+# #1653: added tokens are split out before the regex pre-tokenizer, HF-style.
+tests/test_qwen36_tokenizer$(EXE): tests/test_qwen36_tokenizer.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.c
index bcf91996..001d5e92 100644
--- a/c/qwen36.c
+++ b/c/qwen36.c
@@ -235,7 +235,22 @@ static int pretok_end(const char *s,int i,int n){
if(s[i]==' '&&i+1<n){ int a1; unsigned c1=utf8_decode(s,i+1,n,&a1); if(uclass(c1)!=U_W&&uclass(c1)!=U_L&&uclass(c1)!=U_N&&c1!='\r'&&c1!='\n'){ k=i+1; while(k<n){int a;unsigned cc=utf8_decode(s,k,n,&a); if(uclass(cc)!=U_W&&uclass(cc)!=U_L&&uclass(cc)!=U_N&&cc!='\r'&&cc!='\n')k+=a; else break;} while(k<n&&(s[k]=='\r'||s[k]=='\n'))k++; return k; } }
if(uclass(c0)!=U_W&&uclass(c0)!=U_L&&uclass(c0)!=U_N&&c0!='\r'&&c0!='\n'){ int k2=i; while(k2<n){int a;unsigned cc=utf8_decode(s,k2,n,&a); if(uclass(cc)!=U_W&&uclass(cc)!=U_L&&uclass(cc)!=U_N&&cc!='\r'&&cc!='\n')k2+=a; else break;} while(k2<n&&(s[k2]=='\r'||s[k2]=='\n'))k2++; return k2; }
}
- if(uclass(c0)==U_W){ int k=i; while(k<n){int a;unsigned cc=utf8_decode(s,k,n,&a); if(uclass(cc)==U_W)k+=a; else break;} return k; }
+ if(uclass(c0)==U_W){
+ /* The three whitespace rules, in the regex's order. `last` is where the
+ * final whitespace char of the run starts, `nl_end` where the last CR/LF
+ * inside the run ends. */
+ int k=i,last=i,nl_end=-1;
+ while(k<n){int a;unsigned cc=utf8_decode(s,k,n,&a); if(uclass(cc)!=U_W) break; last=k; k+=a; if(cc=='\r'||cc=='\n') nl_end=k;}
+ /* rule5: \s*[\r\n]+ -- greedy up to the LAST newline; spaces after it
+ * belong to the next piece ("\n x" is "\n" then " " then " x"). */
+ if(nl_end>0) return nl_end;
+ /* rule6: \s+(?!\S) -- a run followed by a non-space keeps its last char
+ * for the next piece, which then takes it as " x" or " ." (HF: " <" is
+ * " " then " <", not " " then "<"). A single char cannot back off and
+ * falls to rule7, \s+, which takes it whole. */
+ if(k<n && last>i) return last;
+ return k;
+ }
return i+adv;
}
static void bpe_piece(const char *piece,int len,int **ids,int *n,int *cap){
@@ -270,16 +285,33 @@ static void bpe_piece(const char *piece,int len,int **ids,int *n,int *cap){
for(int k=0;k<sc;k++){ int id=smap_get(&g_rev,syms[k]); if(id<0) id=0; push_id(ids,n,cap,id); free(syms[k]); }
free(syms);
}
+/* The next added token at or after i, or n when there is none. HF splits the
+ * added tokens out FIRST and pre-tokenizes only the ordinary text between them.
+ * Looking for the special at the start of each piece is not the same thing: the
+ * punctuation rule ` ?[^\s\p{L}\p{M}\p{N}]+` swallows the `<|` of `<|im_end|>`
+ * together with the `.` before it, so the special was never at a piece start and
+ * got encoded as text (#1653: `X.<|im_end|>` was 7 tokens instead of 3, and
+ * every chat turn ending in punctuation paid +4). qwen38.c already splits this
+ * way; this is the same shape. */
+static int next_special(const char *s,int i,int n){
+ for(int k=i;k<n;k++){ int sid; if(try_special(s,k,n,&sid)>0) return k; }
+ return n;
+}
static void encode_text(const char *text,int **out_ids,int *out_n){
int cap=1024,n=0; int *ids=malloc(cap*sizeof(int));
int tlen=(int)strlen(text); int i=0;
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,tlen);
- if(j>tlen) j=tlen;
- bpe_piece(text+i,j-i,&ids,&n,&cap);
- i=j;
+ /* Ordinary text runs to the next added token, and the pre-tokenizer
+ * sees that boundary as the end of its input, exactly as HF's does. */
+ int end=next_special(text,i+1,tlen);
+ while(i<end){
+ int j=pretok_end(text,i,end); if(j<=i) j=i+utf8_adv(text,i,end);
+ if(j>end) j=end;
+ bpe_piece(text+i,j-i,&ids,&n,&cap);
+ i=j;
+ }
}
*out_ids=ids; *out_n=n;
}
diff --git a/c/tests/test_qwen36_tokenizer.c b/c/tests/test_qwen36_tokenizer.c
new file mode 100644
index 00000000..1eba3032
--- /dev/null
+++ b/c/tests/test_qwen36_tokenizer.c
@@ -0,0 +1,81 @@
+/* #1653: an added token that directly follows punctuation (`X.<|im_end|>`) must
+ * still be one token. HF's tokenizers split added tokens out first and run the
+ * regex pre-tokenizer on the ordinary text between them; encode_text() used to
+ * look for the special only at the start of each piece, and the punctuation
+ * rule had already swallowed the `<|`. Pins the split on a tiny tokenizer.json
+ * so it needs no model: every added token boundary, the whitespace lookahead
+ * at that boundary, and back-to-back specials. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define main qwen36_main_unused
+#include "../qwen36.c"
+#undef main
+
+static int g_fails = 0;
+static void expect(const char *text, const int *want, int nwant){
+ int *ids = NULL, n = 0;
+ encode_text(text, &ids, &n);
+ int ok = (n == nwant);
+ for (int i = 0; ok && i < n; i++) ok = (ids[i] == want[i]);
+ if (!ok) {
+ g_fails++;
+ printf("FAIL: %-24s got [", text);
+ for (int i = 0; i < n; i++) printf("%s%d", i ? " " : "", ids[i]);
+ printf("] want [");
+ for (int i = 0; i < nwant; i++) printf("%s%d", i ? " " : "", want[i]);
+ printf("]\n");
+ }
+ free(ids);
+}
+#define EXPECT(text, ...) do { int w[] = {__VA_ARGS__}; expect(text, w, (int)(sizeof w / sizeof w[0])); } while (0)
+
+int main(void){
+ /* Byte-level BPE vocabulary: printable ASCII maps to itself, U+0120 (\xC4\xA0)
+ * is the space, U+010A (\xC4\x8A) the newline. One merge, the repeated space,
+ * as in the real Qwen table. Id 7 is the only added token. */
+ const char *path = "test_qwen36_tokenizer.json";
+ const char *json =
+ "{\"model\":{\"vocab\":{\"X\":0,\".\":1,\"<\":2,\"|\":3,\"i\":4,\"m\":5,\"Y\":6,"
+ "\"}\":8,\"\\\"\":9,\"\xC4\x8A\":10,\"\xC4\xA0\":11,\"\xC4\xA0\xC4\xA0\":12,"
+ "\"_\":13,\"e\":14,\"n\":15,\"d\":16,\">\":17,\"!\":18,\"\xC4\xA0Y\":19},"
+ "\"merges\":[\"\xC4\xA0 \xC4\xA0\",\"\xC4\xA0 Y\"]},"
+ "\"added_tokens\":[{\"id\":7,\"content\":\"<|im_end|>\",\"special\":true}]}";
+ FILE *f = fopen(path, "wb");
+ if (!f || fwrite(json, 1, strlen(json), f) != strlen(json) || fclose(f)) { printf("FAIL: cannot write %s\n", path); return 1; }
+ load_tokenizer(path);
+ remove(path);
+
+ EXPECT("X", 0);
+ EXPECT("X<|im_end|>", 0, 7);
+ /* the reporter's cases: punctuation right before the special, +4 tokens before the fix */
+ EXPECT("X.", 0, 1);
+ EXPECT("X.<|im_end|>", 0, 1, 7);
+ EXPECT("X}<|im_end|>", 0, 8, 7);
+ EXPECT("X\"<|im_end|>", 0, 9, 7);
+ EXPECT("X!<|im_end|>", 0, 18, 7);
+ EXPECT("X.\n<|im_end|>", 0, 1, 10, 7);
+ /* the special ends the ordinary text: `\s+(?!\S)` must see end-of-input there,
+ * so two spaces stay one piece (and one merged token), as in HF */
+ EXPECT("X <|im_end|>", 0, 11, 7);
+ EXPECT("X <|im_end|>", 0, 12, 7);
+ /* the whitespace rules themselves, with and without a special after them:
+ * `\s+(?!\S)` leaves the last space to the next piece, `\s*[\r\n]+` stops at
+ * the last newline, a lone space before text is that text's prefix */
+ EXPECT(" Y", 11, 19);
+ EXPECT(" Y<|im_end|>", 11, 19, 7);
+ EXPECT("X ", 0, 12);
+ EXPECT("\n Y", 10, 11, 19);
+ EXPECT("X\n\n<|im_end|>", 0, 10, 10, 7);
+ /* back to back, and text on both sides */
+ EXPECT("<|im_end|><|im_end|>", 7, 7);
+ EXPECT("X.<|im_end|>Y", 0, 1, 7, 6);
+ EXPECT("<|im_end|>.X", 7, 1, 0);
+ /* the special spelled out as ordinary text is NOT the special: it must stay text */
+ EXPECT("<|im_en", 2, 3, 4, 5, 13, 14, 15);
+
+ if (g_fails) { printf("test_qwen36_tokenizer: %d failure(s)\n", g_fails); return 1; }
+ printf("OK test_qwen36_tokenizer: added tokens split before the pre-tokenizer (#1653)\n");
+ return 0;
+}