Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
qwen36: decode added tokenizer tokens
GenericRikka9342b473a2d8
diff --git a/c/qwen36.c b/c/qwen36.cindex 670ac1d0..49fd7286 100644--- a/c/qwen36.c+++ b/c/qwen36.c@@ -317,8 +317,8 @@ static void encode_text(const char *text,int **out_ids,int *out_n){*out_ids=ids; *out_n=n;}-/* Load Qwen tokenizer.json and build an id->piece table. Only needs the- * "model.vocab" map (piece string -> id); merges are irrelevant for decoding. */+/* Load Qwen tokenizer.json and build an id->piece table from model.vocab+ * and added_tokens. Merges are irrelevant for decoding. */static void load_tokenizer(const char *path){FILE *f = fopen(path, "rb");if (!f) { fprintf(stderr, "[tok] cannot open %s\n", path); return; }@@ -332,18 +332,37 @@ static void load_tokenizer(const char *path){jval *vocab = json_get(model, "vocab");if (!vocab) vocab = json_get(model, "tokens");if (!vocab) { fprintf(stderr, "[tok] no model.vocab/tokens in %s\n", path); free(buf); return; }+ jval *adds = json_get(root, "added_tokens");+int mx = 0;if (vocab->t == J_OBJ){for (int i=0;i<vocab->len;i++){ int id=(int)vocab->kids[i]->num; if(id>mx)mx=id; }} else {mx = vocab->len - 1;}+ if (adds && adds->t==J_ARR){+ for (int k=0;k<adds->len;k++){+ jval *t = adds->kids[k];+ int id = (int)jnum(t,"id");+ if (id > mx) mx = id;+ }+ }+g_tok = calloc((size_t)mx+1, sizeof(char*));if (vocab->t == J_OBJ){for (int i=0;i<vocab->len;i++){ int id=(int)vocab->kids[i]->num; if(id>=0 && id<=mx) g_tok[id]=strdup(vocab->keys[i]); }} else {for (int i=0;i<vocab->len;i++){ if(vocab->kids[i] && vocab->kids[i]->t==J_STR) g_tok[i]=strdup(vocab->kids[i]->str); }}+ if (adds && adds->t==J_ARR){+ for (int k=0;k<adds->len;k++){+ jval *t = adds->kids[k];+ const char *c = jstr(t,"content");+ int id = (int)jnum(t,"id");+ if (c && id>=0 && id<=mx && !g_tok[id])+ g_tok[id]=strdup(c);+ }+ }g_tok_n = mx+1;/* ---- encoder tables (text -> ids) ---- */@@ -380,7 +399,6 @@ static void load_tokenizer(const char *path){smap_put(&g_merge, key, r);}}- jval *adds = json_get(root, "added_tokens");if (adds && adds->t==J_ARR && g_nspecial==0){g_nspecial = adds->len;g_sp_str = malloc(g_nspecial*sizeof(char*));@@ -486,7 +504,7 @@ static void sse_chunk(const char *json){* <0xXX> byte-fallback tokens emit the raw byte directly. */static void decode_id_to_bytes(int id, unsigned char *out, int *outn){*outn = 0;- if (!g_tok || id<0 || id>=g_tok_n) return;+ if (!g_tok || id<0 || id>=g_tok_n || !g_tok[id]) return;const unsigned char *pc = (const unsigned char*)g_tok[id];/* byte-fallback token: <0xXX> -> raw byte */if (pc[0]=='<' && pc[1]=='0' && pc[2]=='x' && pc[5]=='>'){diff --git a/c/tests/test_edge_adapters_real.c b/c/tests/test_edge_adapters_real.cindex 65cec44f..ac45247c 100644--- a/c/tests/test_edge_adapters_real.c+++ b/c/tests/test_edge_adapters_real.c@@ -154,6 +154,61 @@ int main(int argc, char **argv) {"tokenizer round-trip differs");free(probe_text); probe_text = NULL;free(probe_ids); probe_ids = NULL;++ /* Qwen3.6 stores protocol markers outside model.vocab in tokenizer.json's+ * added_tokens array. They must participate in both tokenization and+ * detokenization just like ordinary vocabulary entries. */+ if (!strcmp(family, "qwen36")) {+ static const struct {+ const char *text;+ int32_t id;+ } added_token_cases[] = {+ {"<tool_call>", 248058},+ {"</tool_call>", 248059},+ {"<tool_response>", 248066},+ {"</tool_response>", 248067},+ {"<think>", 248068},+ {"</think>", 248069},+ };++ for (size_t token = 0;+ token < sizeof(added_token_cases) / sizeof(added_token_cases[0]);+ token++) {+ const char *expected = added_token_cases[token].text;+ size_t expected_bytes = strlen(expected);+ int32_t encoded = -1;+ size_t encoded_count = 0;++ REQUIRE(coli_edge_tokenize(edge, expected, expected_bytes,+ &encoded, 1, &encoded_count,+ error, sizeof(error)) == 0,+ "Qwen3.6 added token encode failed");+ REQUIRE(encoded_count == 1 &&+ encoded == added_token_cases[token].id,+ "Qwen3.6 added token encoded to the wrong ID");++ size_t decoded_bytes = 0;+ REQUIRE(coli_edge_detokenize(edge, &encoded, 1,+ NULL, 0, &decoded_bytes,+ error, sizeof(error)) == 0,+ "Qwen3.6 added token decode sizing failed");+ REQUIRE(decoded_bytes == expected_bytes,+ "Qwen3.6 added token decoded to the wrong size");++ char decoded[64];+ REQUIRE(decoded_bytes + 1u <= sizeof(decoded),+ "Qwen3.6 added token exceeds test buffer");+ REQUIRE(coli_edge_detokenize(edge, &encoded, 1,+ decoded, sizeof(decoded),+ &decoded_bytes,+ error, sizeof(error)) == 0,+ "Qwen3.6 added token decode failed");+ REQUIRE(decoded_bytes == expected_bytes &&+ !memcmp(decoded, expected, expected_bytes),+ "Qwen3.6 added token round-trip differs");+ }+ }+if (!strcmp(family, "qwen38")) {int32_t invalid_ids[] = {-1, (int32_t)edge_cap.vocab_size};for (size_t invalid = 0;