Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

tokenizer: a word longer than 64 symbols merges with a heap and a linked list, not a rescan per merge (#268)

Niko122197362183ef58
diff --git a/serve/test_detok.py b/serve/test_detok.py
index 1a1f1ec60..b1c614247 100644
--- a/serve/test_detok.py
+++ b/serve/test_detok.py
@@ -103,5 +103,150 @@ class Detok(unittest.TestCase):
self.assertLess(late, early * 5 + 0.002)
+# ------------------------------------------------------------------ #268: the heap BPE for long words
+_REAL = []
+
+
+def real_tokenizer():
+ """The pack's tokenizer, loaded once for every class that needs it (None when there is no pack here)."""
+ if not _REAL:
+ t = find_tokenizer()
+ if t is None:
+ _REAL.append(None)
+ else:
+ import json
+ import strata_tokenizer as ST
+ vocab = json.loads((t / "vocab.json").read_text(encoding="utf-8"))
+ toks = [None] * len(vocab)
+ for s, i in vocab.items():
+ toks[i] = s
+ _REAL.append(ST.Tokenizer(toks, (t / "merges.txt").read_text(encoding="utf-8").split("\n"),
+ json.loads((t / "token_type.json").read_text())))
+ return _REAL[0]
+
+
+def random_text(rng, n_pieces=40):
+ """Strings shaped to make one pre-token piece long or awkward: CJK and Hangul runs (syllables and conjoining
+ jamo), letters under stacks of combining marks, URLs, minified code, digits, emoji, whitespace, specials."""
+ def run(lo, hi, k):
+ return "".join(chr(rng.randint(lo, hi)) for _ in range(k))
+
+ kinds = [
+ lambda: run(0x4E00, 0x9FFF, rng.randint(1, 400)), # CJK
+ lambda: run(0xAC00, 0xD7A3, rng.randint(1, 200)), # Hangul syllables
+ lambda: "".join(chr(0x1100 + rng.randrange(19)) + chr(0x1161 + rng.randrange(21))
+ for _ in range(rng.randint(1, 60))), # conjoining jamo
+ lambda: "".join(rng.choice("aeiouxyz") + run(0x0300, 0x036F, rng.randint(0, 4))
+ for _ in range(rng.randint(1, 80))), # combining marks
+ lambda: "https://example.com/" + "/".join(run(0x61, 0x7A, rng.randint(1, 12))
+ for _ in range(rng.randint(1, 20))) + "?q=" + run(0x30, 0x39, 8),
+ lambda: "".join(rng.choice(["var ", "a=", "b", "(", ")", "{", "}", ";", "function", "return", "x", "+",
+ "!0", "void 0", ",", ".", "[", "]", "=>", "&&"]) for _ in range(rng.randint(1, 300))),
+ lambda: run(0x30, 0x39, rng.randint(1, 50)),
+ lambda: rng.choice([" ", " ", "\n", "\n\n", "\t", "\r\n", " \n "]) * rng.randint(1, 30),
+ lambda: rng.choice(["\U0001f600", "\U0001f30a", "\U0001f469‍\U0001f4bb", "é", "ß"]) * rng.randint(1, 30),
+ lambda: rng.choice(["<|im_end|>", "<|im_start|>", "<think>", "</think>", "<tool_call>", "<|im_sta"]),
+ lambda: "a" * rng.randint(60, 300),
+ lambda: run(0x20, 0x7E, rng.randint(1, 200)),
+ ]
+ return "".join(rng.choice(kinds)() for _ in range(rng.randint(1, n_pieces)))
+
+
+def encode_with(tok, text, parse_special, heap_min):
+ tok.HEAP_MIN = heap_min
+ try:
+ return tok.encode(text, parse_special=parse_special)
+ finally:
+ del tok.HEAP_MIN
+
+
+def synthetic_tokenizer(seed=268, n_merges=1500):
+ """A small byte-level BPE vocabulary with merges learned (randomly) from random_text, and a few specials of
+ both kinds - so the parity test runs without a pack."""
+ import strata_tokenizer as ST
+ rng = random.Random(seed)
+ tokens = sorted(set(ST.BYTE_TO_UNICODE.values()))
+ have, merges = set(tokens), []
+ words = []
+ for _ in range(60):
+ for piece in ST.regex.compile(ST.QWEN35_PATTERN).findall(random_text(rng, 10)):
+ words.append([ST.BYTE_TO_UNICODE[b] for b in piece.encode("utf-8")])
+ words = [w for w in words if len(w) > 1]
+ while len(merges) < n_merges:
+ w = rng.choice(words)
+ if len(w) < 2:
+ continue
+ i = rng.randrange(len(w) - 1)
+ a, b = w[i], w[i + 1]
+ w[i:i + 2] = [a + b] # learn in place, so later merges build on earlier ones
+ if a + b in have or f"{a} {b}" in merges:
+ continue
+ merges.append(f"{a} {b}")
+ tokens.append(a + b)
+ have.add(a + b)
+ types = [1] * len(tokens)
+ for lit, ty in [("<|im_end|>", 3), ("<|im_start|>", 3), ("<think>", 4), ("</think>", 4), ("<tool_call>", 4)]:
+ tokens.append(lit)
+ types.append(ty)
+ return ST.Tokenizer(tokens, merges, types)
+
+
+class HeapBpe(unittest.TestCase):
+ """#268: words longer than HEAP_MIN symbols merge with a heap; the ids must be exactly the scan's."""
+
+ def parity(self, tok, texts):
+ for k, text in enumerate(texts):
+ for ps in (False, True):
+ old = encode_with(tok, text, ps, 10 ** 9) # the scan for every word
+ self.assertEqual(encode_with(tok, text, ps, 0), old, f"text {k} parse_special={ps}") # the heap
+ self.assertEqual(tok.encode(text, parse_special=ps), old, f"text {k} parse_special={ps}")
+
+ def test_words_directly(self):
+ tok = synthetic_tokenizer()
+ rng = random.Random(1)
+ import strata_tokenizer as ST
+ for _ in range(300):
+ for piece in ST.regex.compile(ST.QWEN35_PATTERN).findall(random_text(rng, 8)):
+ word = "".join(ST.BYTE_TO_UNICODE[b] for b in piece.encode("utf-8"))
+ tok.HEAP_MIN = 10 ** 9
+ try:
+ old = tok._bpe(word)
+ finally:
+ del tok.HEAP_MIN
+ self.assertEqual(tok._bpe_heap(word), old, repr(piece[:40]))
+
+ def test_synthetic_vocab(self):
+ tok = synthetic_tokenizer()
+ rng = random.Random(2)
+ self.parity(tok, [random_text(rng) for _ in range(60)] + ["a" * 5000, "ab" * 3000, ""])
+
+ @unittest.skipIf(find_tokenizer() is None, "no pack tokenizer here")
+ def test_pack_vocab(self):
+ tok = real_tokenizer()
+ rng = random.Random(3)
+ fixed = ["你好,世界" * 150, "한국어 텍스트" * 100,
+ "é́̂" * 200, "x" * 5000,
+ "https://example.com/" + "a/b-c_d.e?f=g&h=i#j" * 60,
+ "!function(e){var t={};function n(r){if(t[r])return t[r].exports}}" * 30,
+ "<|im_start|>user\n日本語" * 40 + "<|im_end|><think>" + "漢" * 300]
+ self.parity(tok, fixed + [random_text(rng) for _ in range(40)])
+
+ def check_speed(self, tok):
+ rng = random.Random(4)
+ text = "".join(chr(rng.randint(0x4E00, 0x9FFF)) for _ in range(8192)) # one 24K-byte piece
+ t0 = time.perf_counter()
+ ids = tok.encode(text)
+ took = time.perf_counter() - t0
+ self.assertEqual(tok.decode(ids), text)
+ self.assertLess(took, 1.0, f"an 8K-character CJK run took {took:.2f} s")
+
+ def test_speed_synthetic(self):
+ self.check_speed(synthetic_tokenizer())
+
+ @unittest.skipIf(find_tokenizer() is None, "no pack tokenizer here")
+ def test_speed_pack(self):
+ self.check_speed(real_tokenizer())
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/tools/strata_tokenizer.py b/tools/strata_tokenizer.py
index 92a71f879..c14492cb9 100644
--- a/tools/strata_tokenizer.py
+++ b/tools/strata_tokenizer.py
@@ -142,7 +142,13 @@ class Tokenizer:
Applying merges in list order rather than rank order is the classic BPE bug: it produces a different
segmentation and a plausible token count.
+
+ A word longer than HEAP_MIN symbols (a long CJK run, a minified blob: one pre-token piece of thousands of
+ bytes) goes to _bpe_heap, which applies the same merges in the same order without rescanning every pair
+ after each merge (#268: that rescan is O(n^2) and took seconds on an 8K-character CJK run).
"""
+ if len(word) > self.HEAP_MIN:
+ return self._bpe_heap(word)
parts = list(word)
while len(parts) > 1:
best, best_rank = None, None
@@ -155,6 +161,47 @@ class Tokenizer:
parts[best:best + 2] = [parts[best] + parts[best + 1]]
return parts
+ HEAP_MIN = 64 # words up to this many symbols keep the scan above, unchanged
+
+ def _bpe_heap(self, word: str) -> list[str]:
+ """_bpe's result in O(n log n): the symbols as a linked list, every adjacent pair with a rank in a heap
+ keyed (rank, position). Popping the lowest rank and then the lowest position is exactly the order the
+ scan picks (its strict `<` keeps the leftmost of equal ranks), and a symbol's position is where it starts
+ in the word, which only its left neighbour's merge can change - so a popped pair whose two symbols are
+ no longer the ones it was pushed with is stale and skipped."""
+ import heapq
+ parts = list(word)
+ n = len(parts)
+ nxt = list(range(1, n)) + [-1]
+ prv = list(range(-1, n - 1))
+ ranks = self.ranks
+ heap = []
+ for i in range(n - 1):
+ r = ranks.get((parts[i], parts[i + 1]))
+ if r is not None:
+ heap.append((r, i, parts[i], parts[i + 1]))
+ heapq.heapify(heap)
+ while heap:
+ r, i, left, right = heapq.heappop(heap)
+ j = nxt[i]
+ if parts[i] != left or j < 0 or parts[j] != right:
+ continue # stale: one of its symbols has merged since
+ parts[i] = left + right
+ parts[j] = None # j is gone: i takes its place in the list
+ k = nxt[j]
+ nxt[i] = k
+ if k >= 0:
+ prv[k] = i
+ r2 = ranks.get((parts[i], parts[k]))
+ if r2 is not None:
+ heapq.heappush(heap, (r2, i, parts[i], parts[k]))
+ p = prv[i]
+ if p >= 0:
+ r2 = ranks.get((parts[p], parts[i]))
+ if r2 is not None:
+ heapq.heappush(heap, (r2, p, parts[p], parts[i]))
+ return [s for s in parts if s is not None]
+
def _encode_plain(self, text: str) -> list[int]:
out: list[int] = []
for piece in self._re.findall(text):