BPE decides token boundaries after seeing the whole string, so where a token starts can depend on bytes that come later. With the Llama 3 tokenizer, _distill becomes [_dist, ill] and _distinct becomes [_distinct], so whether the i after _dist starts a token depends on the next byte. The H-Net paper calls this tokenization bias and uses it to explain why a causal router cannot reproduce BPE boundaries exactly. Measure it. Strings stand in for bytes, one character per byte, with _ standing for a space.
bpe_encode(word, merges)
merges is an ordered list of pairs. Start from the individual characters. For each pair (a, b) in order, go left to right and merge every adjacent a, b into a + b, repeating until no adjacent pair is left, before moving to the next merge.token_starts(word, merges)
True where a token starts.lookahead_table(words, merges)
word and each position t, let be the smallest value in 0, 1, ..., len(word) - 1 - t such that every string in words starting with word[:t + 1 + k] has the same token_starts value at position t. If none settles it, use len(word) - 1 - t.causal_accuracy(counts, merges, k)
counts maps a word to an integer count. A classifier predicts whether position t of a word starts a token, seeing only the context word[:t + 1 + k] (the whole word if that runs past its end).float.causal_accuracy. Include the position in its key: near the end of a word, clipped contexts for different positions can be the same string.