Gemma's tokenizer.json spells runs of 2-31 spaces as added, non-special tokens in U+2581 (ids 138-167). piece_at returned every user-defined token literally, so indentation and double spaces in a Gemma reply came out as U+2581. HuggingFace runs every token it keeps through the decoder; now a user-defined token does too, and only control and unknown tokens stay literal. decode, the Detokenizer and TokenView change together. tokenizer_test covers it with a Gemma-shaped tokenizer.json. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1055 lines
49 KiB
C++
1055 lines
49 KiB
C++
/* tokenizer_test.cpp -- the encoder against its reference, and the parts under it.
|
||
*
|
||
* The reference is tools/tokref: an encoder built on llama.cpp's structural splitters, std::regex
|
||
* for every other pattern and a string-keyed merge; it shares no code with the engine. The
|
||
* differential tests hold the engine's splitter and encoder (`encode_new`) to it over tens of
|
||
* thousands of random strings. Every allowed difference is a place the reference is wrong about
|
||
* HuggingFace (listed in tokref.h), and each has its own test showing the engine gives
|
||
* HuggingFace's answer.
|
||
*
|
||
* Then the parts: the merge on small vocabularies worked out by hand, the linear and heap merges
|
||
* against each other, special-token priority, the NFC shortcut against the full normaliser, and
|
||
* the segment cache (cold, warm, evicted, across a growing conversation, under concurrent encodes).
|
||
*/
|
||
#include "rad_test.h"
|
||
#include "tokenizer_fixture.h"
|
||
#include "tokref.h"
|
||
|
||
#include "rad_internal.h"
|
||
#include "text/tokenizer_bpe.h"
|
||
#include "text/tokenizer_nfc.h"
|
||
#include "text/token_view.h"
|
||
#include "text/unicode_norm.h"
|
||
#include "sample/gbnf.h"
|
||
#include "llama/src/unicode.h"
|
||
|
||
#include <atomic>
|
||
#include <cstring>
|
||
#include <cstdio>
|
||
#include <map>
|
||
#include <set>
|
||
#include <thread>
|
||
|
||
using namespace rad;
|
||
using namespace tokfix;
|
||
|
||
namespace {
|
||
|
||
/* ------------------------------------------------------------------ helpers */
|
||
|
||
/* Compiled once per pattern: compiling is a load-time cost, and the tests split thousands of
|
||
* strings with each pattern. */
|
||
const TokRegex* compiled(const char* pattern) {
|
||
static std::map<std::string, std::unique_ptr<TokRegex>> cache;
|
||
auto it = cache.find(pattern);
|
||
if (it == cache.end()) {
|
||
std::unique_ptr<TokRegex> re;
|
||
std::string why;
|
||
TokRegex::compile(pattern, &re, &why);
|
||
it = cache.emplace(pattern, std::move(re)).first;
|
||
}
|
||
return it->second.get();
|
||
}
|
||
|
||
std::vector<std::string> new_split(const char* pattern, const std::string& word) {
|
||
const TokRegex* re = compiled(pattern);
|
||
if (!re) return { "<refused>" };
|
||
std::string canon;
|
||
std::string_view v = word;
|
||
if (!utf8_is_canonical(v)) { utf8_canonicalize(v, canon); v = canon; }
|
||
std::vector<TokSpan> out, scratch;
|
||
tok_split(*re, v, TOK_SPLIT_ISOLATED, false, out, scratch);
|
||
std::vector<std::string> pieces;
|
||
for (const TokSpan& s : out) pieces.emplace_back(v.substr(s.begin, s.end - s.begin));
|
||
return pieces;
|
||
}
|
||
|
||
std::string escaped(const std::string& s) {
|
||
std::string o;
|
||
for (unsigned char c : s) {
|
||
if (c >= 0x20 && c < 0x7F && c != '\\') o += (char)c;
|
||
else o += fmt("\\x%02x", c);
|
||
}
|
||
return o;
|
||
}
|
||
|
||
std::string show(const std::vector<std::string>& v) {
|
||
std::string o;
|
||
for (const std::string& p : v) o += "[" + escaped(p) + "]";
|
||
return o;
|
||
}
|
||
|
||
std::string show_ids(const std::vector<int32_t>& v, size_t from = 0, size_t n = 12) {
|
||
std::string o;
|
||
for (size_t i = from; i < v.size() && i < from + n; ++i) o += fmt("%d ", v[i]);
|
||
return o;
|
||
}
|
||
|
||
/* Where llama.cpp's category tables (Unicode 15.1) and the compiler's (Oniguruma, 16.0) put a
|
||
* codepoint in different categories. */
|
||
bool tables_disagree(uint32_t c) {
|
||
const auto f = unicode_cpt_flags_from_cpt(c);
|
||
const uint32_t g = gc_of(c);
|
||
const bool L = g <= ucd::Lo, M = g >= ucd::Mn && g <= ucd::Me, N = g >= ucd::Nd && g <= ucd::No;
|
||
const bool P = g >= ucd::Pc && g <= ucd::Po, S = g >= ucd::Sm && g <= ucd::So;
|
||
return f.is_letter != L || f.is_accent_mark != M || f.is_number != N ||
|
||
f.is_punctuation != P || f.is_symbol != S;
|
||
}
|
||
|
||
std::vector<int32_t> encode_new(const Tokenizer& t, const std::string& s, bool parse_special = true) {
|
||
std::vector<int32_t> ids;
|
||
t.encode(s, ids, false, parse_special);
|
||
return ids;
|
||
}
|
||
|
||
std::vector<int32_t> encode_ref(const Vocab& v, const std::string& s, bool parse_special = true) {
|
||
std::vector<int32_t> ids;
|
||
tokref::encode(v, s, ids, false, parse_special);
|
||
return ids;
|
||
}
|
||
|
||
/* Compare the two encoders over `n` texts from `gen`; report the first few differences. */
|
||
template <class Gen>
|
||
int compare_encoders(const char* what, std::shared_ptr<const Vocab> v, int n, Gen gen,
|
||
bool parse_special = true) {
|
||
Tokenizer t;
|
||
t.init(v);
|
||
t.set_segment_cache(false);
|
||
int bad = 0;
|
||
for (int i = 0; i < n; ++i) {
|
||
const std::string s = gen(i);
|
||
const std::vector<int32_t> a = encode_new(t, s, parse_special), b = encode_ref(*v, s, parse_special);
|
||
if (a == b) continue;
|
||
if (++bad <= 3) {
|
||
size_t k = 0;
|
||
while (k < a.size() && k < b.size() && a[k] == b[k]) ++k;
|
||
std::fprintf(stderr, " %s: differs at id %zu for '%s'\n new %s\n ref %s\n", what, k,
|
||
escaped(s.substr(0, 200)).c_str(), show_ids(a, k > 3 ? k - 3 : 0).c_str(),
|
||
show_ids(b, k > 3 ? k - 3 : 0).c_str());
|
||
}
|
||
}
|
||
return bad;
|
||
}
|
||
|
||
/* The corpus the trained vocabularies learn from: code, prose, every script. */
|
||
std::string training_corpus() {
|
||
TextGen g(7);
|
||
std::string s;
|
||
for (int i = 0; i < 400; ++i) s += g.text(60) + "\n";
|
||
return s;
|
||
}
|
||
|
||
const VocabBuild& trained() {
|
||
static const VocabBuild vb = train_bytelevel(training_corpus(), k_qwen35, 1200);
|
||
return vb;
|
||
}
|
||
|
||
} /* namespace */
|
||
|
||
/* ================================================================== splits against the reference */
|
||
|
||
/* The splitter against the reference on every pattern the reference implements: its structural
|
||
* splitters (GPT-2, Llama-3, Qwen2, Qwen3.5, in both spellings llama.cpp keys them on, and
|
||
* "[^\n]+|[\n]+") and patterns it runs through std::regex (MiniCPM5's two, the GGUF defaults, the
|
||
* gpt-4o adaptation). The text covers every general category, marks and ZWJ sequences, CJK,
|
||
* Hangul and RTL, digit runs of every length, every whitespace kind including CRLF and the
|
||
* Unicode spaces, mixed-case contractions, and invalid UTF-8.
|
||
*
|
||
* Two inputs are held out, each tested below: codepoints the two category tables disagree on,
|
||
* and (for the `(?i)` spelling only) an apostrophe before U+017F. */
|
||
TEST(splits_match_the_reference_splitters) {
|
||
struct P { const char* pattern; bool ci; };
|
||
const P patterns[] = {
|
||
{ "'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)", false },
|
||
{ "(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}{1,3}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+", false },
|
||
{ k_llama3, true },
|
||
{ "(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+", false },
|
||
{ "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+", true },
|
||
{ "(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])|[^\\r\\n\\p{L}\\p{N}]?[\\p{L}\\p{M}]+|\\p{N}| ?[^\\s\\p{L}\\p{M}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+", false },
|
||
{ k_qwen35, true },
|
||
{ "[^\\n]+|[\\n]+", false },
|
||
{ k_minicpm_digits, false },
|
||
{ k_minicpm, true },
|
||
{ "[\\p{P}\\$\\+<=>\\^~\\|]+", false },
|
||
{ "\\p{N}+", false },
|
||
{ "[0-9][0-9][0-9]", false },
|
||
{ "\\S+", false },
|
||
{ "[^\\r\\n\\p{L}\\p{N}]?((?=[\\p{L}])([^a-z]))*((?=[\\p{L}])([^A-Z]))+(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])?|[^\\r\\n\\p{L}\\p{N}]?((?=[\\p{L}])([^a-z]))+((?=[\\p{L}])([^A-Z]))*(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])?|\\p{N}{1,3}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n/]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+", false },
|
||
};
|
||
const std::string long_s = "'\xc5\xbf";
|
||
size_t compared = 0;
|
||
for (const P& p : patterns) {
|
||
TextGen g(1234);
|
||
int bad = 0;
|
||
/* Few long strings rather than many short ones: the reference builds a std::regex per
|
||
* call for patterns it has no splitter for. */
|
||
for (int i = 0; i < 160; ++i) {
|
||
const std::string s = g.text(1 + g.pick(i < 80 ? 40 : 1200), /*junk=*/true, tables_disagree);
|
||
if (p.ci && s.find(long_s) != std::string::npos) continue;
|
||
const std::vector<std::string> a = new_split(p.pattern, s), b = tokref::split(p.pattern, s);
|
||
compared += s.size();
|
||
if (a == b) continue;
|
||
if (++bad <= 3)
|
||
std::fprintf(stderr, " /%.60s/ on '%s'\n new %s\n ref %s\n", p.pattern,
|
||
escaped(s.substr(0, 300)).c_str(), show(a).c_str(), show(b).c_str());
|
||
}
|
||
CHECK_EQ(bad, 0);
|
||
}
|
||
CHECK(compared > (size_t)960 * 1024); /* the run compared ~1 MB, not an empty loop */
|
||
}
|
||
|
||
/* Digit runs of every length and whitespace of every shape, exhaustively, since the structural
|
||
* splitters special-case both. */
|
||
TEST(splits_match_the_reference_on_every_digit_run_and_whitespace_run) {
|
||
const char* patterns[] = { k_llama3, k_qwen35,
|
||
"'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)" };
|
||
const char* ws[] = { " ", "\t", "\n", "\r", "\r\n", "\xc2\xa0", "\xe3\x80\x80", "\x0b", "\xe2\x80\xa8" };
|
||
int bad = 0;
|
||
for (const char* p : patterns) {
|
||
for (int n = 1; n <= 24; ++n) {
|
||
for (const char* before : { "", "a", " ", "x ", "!" }) {
|
||
for (const char* after : { "", "a", " ", "\n", "," }) {
|
||
const std::string s = std::string(before) + std::string((size_t)n, '7') + after;
|
||
if (new_split(p, s) != tokref::split(p, s)) ++bad;
|
||
}
|
||
}
|
||
}
|
||
for (int mask = 1; mask < 4096; mask += 7) {
|
||
std::string s = "w";
|
||
for (int k = 0, m = mask; k < 4; ++k, m /= 9) s += ws[m % 9];
|
||
for (const char* after : { "", "x", "!", "1", "\n" }) {
|
||
const std::string t = s + after;
|
||
if (new_split(p, t) != tokref::split(p, t)) ++bad;
|
||
}
|
||
}
|
||
}
|
||
CHECK_EQ(bad, 0);
|
||
}
|
||
|
||
/* ================================================================== where the reference is wrong */
|
||
|
||
/* The reference rewrites `(?i:'s|...)` as `'[sS]|...`, which loses U+017F: Oniguruma folds LATIN
|
||
* SMALL LETTER LONG S to 's', so "'ſ" is a contraction to HuggingFace. The expected pieces are
|
||
* HuggingFace's. Only the (?i) spelling is affected; llama.cpp's expanded one has no ſ. */
|
||
TEST(long_s_is_a_contraction_under_case_folding_as_in_huggingface) {
|
||
const std::string s = "x'\xc5\xbfy it'S";
|
||
const std::vector<std::string> want = { "x", "'\xc5\xbf", "y", " it", "'S" };
|
||
CHECK(new_split(k_qwen35, s) == want);
|
||
CHECK(tokref::split(k_qwen35, s) != want);
|
||
const char* expanded =
|
||
"(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])|[^\\r\\n\\p{L}\\p{N}]?[\\p{L}\\p{M}]+|\\p{N}| ?[^\\s\\p{L}\\p{M}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+";
|
||
CHECK(new_split(expanded, s) == tokref::split(expanded, s));
|
||
}
|
||
|
||
/* The two category tables differ only by Unicode 16.0: each of the 5185 codepoints they disagree
|
||
* on is unassigned in llama.cpp's and assigned in Oniguruma's. A split follows Oniguruma's
|
||
* category, as HuggingFace does: U+10D40 GARAY DIGIT ZERO is a digit and U+10D6E GARAY HYPHEN is
|
||
* punctuation. */
|
||
TEST(the_category_tables_differ_only_by_what_unicode_16_assigned) {
|
||
size_t n = 0, bad = 0;
|
||
for (uint32_t c = 0; c < 0x110000; ++c) {
|
||
if (c >= 0xD800 && c <= 0xDFFF) continue;
|
||
if (!tables_disagree(c)) continue;
|
||
++n;
|
||
if (!unicode_cpt_flags_from_cpt(c).is_undefined || gc_of(c) == ucd::Cn) ++bad;
|
||
}
|
||
CHECK_EQ(n, (size_t)5185);
|
||
CHECK_EQ(bad, (size_t)0);
|
||
const std::string s = "a\xf0\x90\xb5\x80" "b!\xf0\x90\xb5\xae.";
|
||
const std::vector<std::string> want = { "a", "\xf0\x90\xb5\x80", "b", "!\xf0\x90\xb5\xae." };
|
||
CHECK(new_split(k_qwen35, s) == want);
|
||
CHECK(tokref::split(k_qwen35, s) != want);
|
||
}
|
||
|
||
/* `\p{Han}+` is not Kimi-K2's pattern but llama.cpp's key for a splitter approximating it. The
|
||
* GGUF table carries the real pattern, whose letter classes split camel case. */
|
||
TEST(the_kimi_k2_entry_is_the_real_pattern) {
|
||
const char* k2 =
|
||
"[\\p{Han}]+|[^\\r\\n\\p{L}\\p{N}]?[\\p{Lu}\\p{Lt}\\p{Lm}\\p{Lo}\\p{M}&&[^\\p{Han}]]*[\\p{Ll}\\p{Lm}\\p{Lo}\\p{M}&&[^\\p{Han}]]+(?i:'s|'t|'re|'ve|'m|'ll|'d)?|[^\\r\\n\\p{L}\\p{N}]?[\\p{Lu}\\p{Lt}\\p{Lm}\\p{Lo}\\p{M}&&[^\\p{Han}]]+[\\p{Ll}\\p{Lm}\\p{Lo}\\p{M}&&[^\\p{Han}]]*(?i:'s|'t|'re|'ve|'m|'ll|'d)?|\\p{N}{1,3}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+";
|
||
const std::string s = "HelloWorld \xe4\xb8\xad\xe6\x96\x87" "ABc";
|
||
const std::vector<std::string> want = { "Hello", "World", " ", "\xe4\xb8\xad\xe6\x96\x87", "ABc" };
|
||
CHECK(new_split(k2, s) == want);
|
||
CHECK(tokref::split("\\p{Han}+", s) != want);
|
||
}
|
||
|
||
/* A digit-grouping lookahead is refused rather than run through a rule that means something
|
||
* else: llama.cpp's AFMoE splitter splits "12345abc", the pattern does not. */
|
||
TEST(a_wide_lookahead_is_refused_where_the_reference_approximated_it) {
|
||
std::unique_ptr<TokRegex> re;
|
||
std::string why;
|
||
CHECK_EQ(TokRegex::compile("\\d{1,3}(?=(?:\\d{3})*\\b)", &re, &why), RAD_E_UNSUPPORTED);
|
||
CHECK(why.find("lookaround") != std::string::npos);
|
||
VocabBuild vb = trained();
|
||
vb.steps = { split_step("\\d{1,3}(?=(?:\\d{3})*\\b)"), bytelevel_step() };
|
||
auto v = std::make_shared<Vocab>();
|
||
CHECK_EQ(v->load(std::move(vb)), RAD_E_UNSUPPORTED);
|
||
CHECK(!v->ready());
|
||
}
|
||
|
||
/* ================================================================== BPE by hand */
|
||
|
||
namespace {
|
||
|
||
/* A byte-level vocabulary small enough to work every answer out by hand. "c c" merges to text
|
||
* that is not a token, which blocks "c ab" -> "cab" behind it; "Ġab" is a user-defined token the
|
||
* raw text reaches only through the byte-level mapping. */
|
||
const char* k_tiny = R"JSON({
|
||
"added_tokens": [ {"id": 12, "content": "<|end|>", "special": true},
|
||
{"id": 13, "content": "Ġab", "special": false} ],
|
||
"pre_tokenizer": { "type": "Sequence", "pretokenizers": [
|
||
{"type": "Split", "pattern": {"Regex": " ?\\p{L}+|\\p{N}|\\s+|[^\\s\\p{L}\\p{N}]+"}, "behavior": "Isolated"},
|
||
{"type": "ByteLevel", "add_prefix_space": false, "use_regex": false} ] },
|
||
"decoder": { "type": "ByteLevel" },
|
||
"model": { "type": "BPE",
|
||
"vocab": {"a": 0, "b": 1, "c": 2, "Ġ": 3, "ab": 4, "bc": 5, "abc": 6, "Ġa": 7, "aa": 8, "1": 9,
|
||
"cab": 10, "x": 11},
|
||
"merges": ["a b", "b c", "ab c", "Ġ a", "a a", "c c", "c ab"] }
|
||
})JSON";
|
||
|
||
/* SentencePiece-shaped: Metaspace, codepoint symbols, "<0xNN>" byte fallback. */
|
||
const char* k_spm = R"JSON({
|
||
"pre_tokenizer": { "type": "Metaspace", "replacement": "▁", "prepend_scheme": "always", "split": false },
|
||
"decoder": { "type": "Metaspace", "replacement": "▁", "prepend_scheme": "always" },
|
||
"model": { "type": "BPE", "byte_fallback": true,
|
||
"vocab": {"<unk>": 0, "▁": 1, "h": 2, "i": 3, "▁h": 4, "▁hi": 5, "<0xE2>": 6, "<0x82>": 7, "<0xAC>": 8},
|
||
"merges": ["▁ h", "▁h i"] }
|
||
})JSON";
|
||
|
||
std::shared_ptr<const Vocab> from_json(const char* js, int32_t unk = -1) {
|
||
VocabBuild vb;
|
||
if (parse_tokenizer_json_buf(js, "test.json", vb) < 0) return nullptr;
|
||
if (unk >= 0) vb.unk = unk;
|
||
return load(std::move(vb));
|
||
}
|
||
|
||
} /* namespace */
|
||
|
||
TEST(bpe_merges_as_worked_out_by_hand) {
|
||
auto v = from_json(k_tiny);
|
||
REQUIRE(v != nullptr);
|
||
Tokenizer t;
|
||
CHECK_OK(t.init(v));
|
||
t.set_segment_cache(false);
|
||
const struct { const char* text; bool parse_special; std::vector<int32_t> ids; } cases[] = {
|
||
{ "abc", true, { 6 } }, /* a b -> ab (rank 0), ab c -> abc (2) */
|
||
{ "bcab", true, { 5, 4 } }, /* a b first (0), then b c (1); bc ab has no rank */
|
||
{ "aaa", true, { 8, 0 } }, /* two equal ranks: the leftmost pair merges */
|
||
{ "cc", true, { 2, 2 } }, /* merged into text that is no token: spelled by byte */
|
||
{ "ccab", true, { 2, 2, 4 } }, /* and that merge (5) blocks c ab (6) */
|
||
{ " a", true, { 7 } },
|
||
{ " ab", true, { 13 } }, /* "Ġab" is user-defined: taken whole */
|
||
{ "abx", true, { 4, 11 } },
|
||
{ "1a", true, { 9, 0 } },
|
||
{ "a<|end|>b", true, { 0, 12, 1 } },
|
||
{ "a<|end|>b", false, { 0, 1 } }, /* not parsed: its bytes have no tokens, dropped */
|
||
};
|
||
for (const auto& c : cases) {
|
||
const std::vector<int32_t> got = encode_new(t, c.text, c.parse_special);
|
||
CHECK(got == c.ids);
|
||
CHECK(encode_ref(*v, c.text, c.parse_special) == c.ids);
|
||
if (got != c.ids) std::fprintf(stderr, " '%s': %s\n", c.text, show_ids(got).c_str());
|
||
}
|
||
}
|
||
|
||
/* `ignore_merges`: "abc" is a vocabulary entry the one merge cannot reach, so only the flag
|
||
* produces it. It applies to each pre-token after the declared chain (the Lowercase normalizer
|
||
* and whitespace split run first), on both ByteLevel paths: the deferred one that merges raw
|
||
* bytes, and the materialised one a later Split forces. */
|
||
TEST(bpe_ignore_merges_takes_a_whole_pretoken) {
|
||
const char* chains[] = {
|
||
R"({"type":"WhitespaceSplit"})",
|
||
R"({"type":"Sequence","pretokenizers":[{"type":"WhitespaceSplit"},)"
|
||
R"({"type":"ByteLevel","add_prefix_space":false,"use_regex":false}]})",
|
||
R"({"type":"Sequence","pretokenizers":[{"type":"WhitespaceSplit"},)"
|
||
R"({"type":"ByteLevel","add_prefix_space":false,"use_regex":false},)"
|
||
R"({"type":"Split","pattern":{"String":" "},"behavior":"Removed"}]})",
|
||
};
|
||
for (const char* chain : chains)
|
||
for (const char* flag : { "", "\"ignore_merges\":false,", "\"ignore_merges\":true," }) {
|
||
const bool ignore = std::strstr(flag, "true") != nullptr;
|
||
const std::string js = std::string(R"({"normalizer":{"type":"Lowercase"},"pre_tokenizer":)") +
|
||
chain + R"(,"model":{"type":"BPE",)" + flag +
|
||
R"("vocab":{"a":0,"b":1,"c":2,"ab":3,"abc":4,"abc abc":5},"merges":["a b"]}})";
|
||
auto v = from_json(js.c_str());
|
||
REQUIRE(v != nullptr);
|
||
CHECK_EQ(v->build().ignore_merges, ignore);
|
||
Tokenizer t;
|
||
CHECK_OK(t.init(v));
|
||
const std::vector<int32_t> whole{ 4 }, merged{ 3, 2 };
|
||
for (int repeat = 0; repeat < 2; ++repeat) { /* the second pass reads the caches */
|
||
CHECK(encode_new(t, "ABC") == (ignore ? whole : merged));
|
||
CHECK(encode_new(t, "ABCABC") == (std::vector<int32_t>{ 3, 2, 3, 2 }));
|
||
CHECK(encode_new(t, "ABC ABC") == (ignore ? std::vector<int32_t>{ 4, 4 }
|
||
: std::vector<int32_t>{ 3, 2, 3, 2 }));
|
||
}
|
||
CHECK(encode_ref(*v, "ABC ABC") == encode_new(t, "ABC ABC"));
|
||
}
|
||
}
|
||
|
||
/* A control token read as text stays text: the flag must not turn its spelling back into the
|
||
* marker when specials are not parsed. */
|
||
TEST(bpe_ignore_merges_leaves_unparsed_control_tokens_as_text) {
|
||
auto v = from_json(R"JSON({
|
||
"added_tokens":[{"id":4,"content":"abc","special":true}],
|
||
"pre_tokenizer":{"type":"WhitespaceSplit"},
|
||
"model":{"type":"BPE","ignore_merges":true,
|
||
"vocab":{"a":0,"b":1,"c":2,"ab":3},"merges":["a b"]}
|
||
})JSON");
|
||
REQUIRE(v != nullptr);
|
||
Tokenizer t;
|
||
CHECK_OK(t.init(v));
|
||
CHECK(encode_new(t, "abc", true) == (std::vector<int32_t>{ 4 }));
|
||
CHECK(encode_new(t, "abc", false) == (std::vector<int32_t>{ 3, 2 }));
|
||
CHECK(encode_ref(*v, "abc", false) == (std::vector<int32_t>{ 3, 2 }));
|
||
}
|
||
|
||
TEST(byte_fallback_and_unk_follow_the_declared_chain) {
|
||
auto v = from_json(k_spm);
|
||
REQUIRE(v != nullptr);
|
||
Tokenizer t;
|
||
t.init(v);
|
||
CHECK(encode_new(t, "hi") == (std::vector<int32_t>{ 5 }));
|
||
CHECK(encode_new(t, "hi\xe2\x82\xac") == (std::vector<int32_t>{ 5, 6, 7, 8 })); /* the euro sign, by byte */
|
||
CHECK(encode_new(t, "hix") == (std::vector<int32_t>{ 5 })); /* no <0x78>, no unk: dropped */
|
||
auto u = from_json(k_spm, 0);
|
||
REQUIRE(u != nullptr);
|
||
Tokenizer tu;
|
||
tu.init(u);
|
||
CHECK(encode_new(tu, "hix") == (std::vector<int32_t>{ 5, 0 })); /* no <0x78>: unk */
|
||
for (const char* s : { "hi", "hi\xe2\x82\xac", "hix", "h i hi", "\xe2\x82\xac\xe2\x82\xac" }) {
|
||
CHECK(encode_new(t, s) == encode_ref(*v, s));
|
||
CHECK(encode_new(tu, s) == encode_ref(*u, s));
|
||
}
|
||
}
|
||
|
||
/* The linear and heap merges pick the same pair at every step: lowest rank, then leftmost.
|
||
* Checked on random words over a random merge table dense enough that most words merge many
|
||
* times, with results that are and are not tokens. */
|
||
TEST(linear_and_heap_merges_agree) {
|
||
std::mt19937 rng(99);
|
||
MergeTable mt;
|
||
const int n_ids = 24;
|
||
for (uint32_t r = 0; r < 300; ++r) {
|
||
const int32_t l = (int32_t)(rng() % n_ids), rr = (int32_t)(rng() % n_ids);
|
||
const int32_t merged = rng() % 5 == 0 ? -1 : (int32_t)(rng() % n_ids);
|
||
mt.insert(l, rr, r, merged);
|
||
}
|
||
int bad = 0;
|
||
for (int i = 0; i < 20000; ++i) {
|
||
std::vector<BpeSym> a;
|
||
const size_t n = 1 + rng() % kBpeLinearMax;
|
||
for (size_t k = 0; k < n; ++k) a.push_back({ (uint32_t)k, 1, (int32_t)(rng() % n_ids) });
|
||
std::vector<BpeSym> b = a;
|
||
bpe_merge(a, mt, false);
|
||
bpe_merge(b, mt, true);
|
||
bool same = a.size() == b.size();
|
||
for (size_t k = 0; same && k < a.size(); ++k)
|
||
same = a[k].start == b[k].start && a[k].len == b[k].len && a[k].id == b[k].id;
|
||
if (!same) ++bad;
|
||
}
|
||
CHECK_EQ(bad, 0);
|
||
}
|
||
|
||
/* ================================================================== the whole encoder against the reference */
|
||
|
||
/* Trained vocabularies over every chain shape the encoder routes differently: the byte-level fast
|
||
* path (Split then ByteLevel, merged on raw bytes), two Splits, ByteLevel with its own regex and
|
||
* prefix space, ByteLevel before a Split (the mapped text is split and merged), NFC, special and
|
||
* user-defined tokens including one that prefixes another, SentencePiece Metaspace with byte
|
||
* fallback, WordPiece and Unigram. */
|
||
TEST(the_encoder_matches_the_reference_on_trained_vocabularies) {
|
||
const std::vector<std::pair<std::string, uint8_t>> specials = {
|
||
{ "<|im_start|>", RAD_TT_CONTROL }, { "<|im_end|>", RAD_TT_CONTROL }, { "<|im", RAD_TT_CONTROL },
|
||
{ "XYZ", RAD_TT_CONTROL }, { "YZWV", RAD_TT_CONTROL }, { "<think>", RAD_TT_USER_DEFINED },
|
||
{ "</think>", RAD_TT_USER_DEFINED }, { "Wv", RAD_TT_USER_DEFINED },
|
||
};
|
||
auto with = [&](std::vector<VocabStep> steps, bool add_sp = true) {
|
||
VocabBuild vb = trained();
|
||
vb.steps = std::move(steps);
|
||
if (add_sp) add_specials(vb, specials);
|
||
return load(std::move(vb));
|
||
};
|
||
auto gen_for = [&](uint32_t seed, bool no_long_s) {
|
||
auto g = std::make_shared<TextGen>(seed);
|
||
return [g, no_long_s](int) {
|
||
std::string s;
|
||
const int parts = 1 + g->pick(5);
|
||
for (int k = 0; k < parts; ++k) {
|
||
s += g->text(g->pick(60), true, tables_disagree);
|
||
static const char* inject[] = { "<|im_start|>", "<|im_end|>", "<|im", "XYZWV", "XYZ",
|
||
"YZWV", "<think>", "</think>", "Wv", "WWv" };
|
||
if (g->pick(2)) s += inject[g->pick(10)];
|
||
}
|
||
if (no_long_s) {
|
||
for (size_t p; (p = s.find("'\xc5\xbf")) != std::string::npos; ) s.replace(p, 1, "_");
|
||
}
|
||
return s;
|
||
};
|
||
};
|
||
VocabStep nfc = simple_step(RAD_NORM_NFC);
|
||
struct Chain { const char* name; std::shared_ptr<const Vocab> v; bool ci; };
|
||
const Chain chains[] = {
|
||
{ "split+bytelevel", with({ split_step(k_qwen35), bytelevel_step() }), true },
|
||
{ "nfc+split+bytelevel", with({ nfc, split_step(k_qwen35), bytelevel_step() }), true },
|
||
{ "two splits", with({ split_step(k_minicpm_digits), split_step(k_minicpm), bytelevel_step() }), true },
|
||
{ "bytelevel regex", with({ bytelevel_step(true, true) }), false },
|
||
{ "bytelevel then split", with({ bytelevel_step(), split_step("\\S+|\\s+") }), false },
|
||
{ "gpt2 without \\s+", with({ split_step("'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)"), bytelevel_step() }), false },
|
||
};
|
||
uint32_t seed = 500;
|
||
for (const Chain& c : chains) {
|
||
REQUIRE(c.v != nullptr);
|
||
for (bool ps : { true, false })
|
||
CHECK_EQ(compare_encoders(c.name, c.v, 500, gen_for(seed++, c.ci), ps), 0);
|
||
}
|
||
|
||
/* SentencePiece-shaped: codepoint symbols and byte fallback, no byte-level mapping. */
|
||
{
|
||
VocabBuild vb;
|
||
vb.kind = RAD_TOK_BPE;
|
||
for (int b = 0; b < 256; ++b) vb.tokens.push_back(fmt("<0x%02X>", b));
|
||
vb.types.assign(256, (uint8_t)RAD_TT_BYTE);
|
||
const std::vector<std::string> base = { "\xe2\x96\x81", "a", "b", "c", "d", "e", "h", "i",
|
||
"l", "o", "t", "\xe4\xb8\x96", "\xe7\x95\x8c" };
|
||
for (const std::string& t : base) { vb.tokens.push_back(t); vb.types.push_back(RAD_TT_NORMAL); }
|
||
auto id = [&](const std::string& t) {
|
||
for (size_t i = 0; i < vb.tokens.size(); ++i) if (vb.tokens[i] == t) return (uint32_t)i;
|
||
return UINT32_MAX;
|
||
};
|
||
const std::vector<std::pair<std::string, std::string>> merges = {
|
||
{ "\xe2\x96\x81", "t" }, { "h", "e" }, { "\xe2\x96\x81t", "he" }, { "l", "l" },
|
||
{ "ll", "o" }, { "e", "llo" }, { "h", "ello" }, { "\xe2\x96\x81", "hello" },
|
||
{ "\xe4\xb8\x96", "\xe7\x95\x8c" }, { "a", "b" }, { "ab", "c" }, { "\xe2\x96\x81", "abc" },
|
||
};
|
||
for (const auto& [l, r] : merges) {
|
||
if (id(l + r) == UINT32_MAX) { vb.tokens.push_back(l + r); vb.types.push_back(RAD_TT_NORMAL); }
|
||
vb.merges.push_back({ id(l), id(r) });
|
||
}
|
||
VocabStep ms;
|
||
ms.kind = RAD_PRE_METASPACE;
|
||
ms.arg0 = "\xe2\x96\x81";
|
||
ms.iarg = 2;
|
||
vb.steps = { ms, simple_step(RAD_PRE_BYTE_FALLBACK) };
|
||
add_specials(vb, specials);
|
||
auto v = load(std::move(vb));
|
||
REQUIRE(v != nullptr);
|
||
CHECK_EQ(compare_encoders("spm", v, 800, gen_for(seed++, false)), 0);
|
||
}
|
||
|
||
/* WordPiece and Unigram over the trained vocabulary's pieces. */
|
||
{
|
||
VocabBuild vb;
|
||
vb.kind = RAD_TOK_WORDPIECE;
|
||
vb.tokens = { "[UNK]", "the", "cache", "##s", "##ing", "sh", "##ard", "val", "##ue", "a", "##b",
|
||
"\xe4\xb8\x96", "##\xe7\x95\x8c", ",", "." };
|
||
vb.unk = 0;
|
||
vb.steps = { simple_step(RAD_NORM_LOWERCASE), simple_step(RAD_PRE_WHITESPACE) };
|
||
auto v = load(std::move(vb));
|
||
REQUIRE(v != nullptr);
|
||
CHECK_EQ(compare_encoders("wordpiece", v, 800, gen_for(seed++, false)), 0);
|
||
}
|
||
{
|
||
VocabBuild vb = trained();
|
||
vb.kind = RAD_TOK_UNIGRAM;
|
||
vb.merges.clear();
|
||
vb.scores.resize(vb.tokens.size());
|
||
for (size_t i = 0; i < vb.tokens.size(); ++i) vb.scores[i] = -1.0f - 0.001f * (float)(i % 997);
|
||
vb.tokens.push_back("<unk>");
|
||
vb.types.push_back(RAD_TT_UNKNOWN);
|
||
vb.scores.push_back(0.0f);
|
||
vb.unk = (int32_t)vb.tokens.size() - 1;
|
||
VocabStep ms;
|
||
ms.kind = RAD_PRE_METASPACE;
|
||
ms.arg0 = "\xe2\x96\x81";
|
||
ms.iarg = 1;
|
||
ms.flags = 1;
|
||
vb.steps = { simple_step(RAD_NORM_STRIP), ms };
|
||
auto v = load(std::move(vb));
|
||
REQUIRE(v != nullptr);
|
||
CHECK_EQ(compare_encoders("unigram", v, 400, gen_for(seed++, false)), 0);
|
||
}
|
||
}
|
||
|
||
/* A text that opens with BOS gets no second one: a client rendering its own chat template writes
|
||
* the BOS itself. A BOS spelled anywhere else, or a second one at the start, is kept. */
|
||
TEST(a_text_that_opens_with_bos_gets_no_second_one) {
|
||
VocabBuild vb;
|
||
REQUIRE(parse_tokenizer_json_buf(k_tiny, "test.json", vb) >= 0);
|
||
vb.bos = 12;
|
||
vb.add_bos = true;
|
||
auto v = load(std::move(vb));
|
||
REQUIRE(v != nullptr);
|
||
Tokenizer t;
|
||
CHECK_OK(t.init(v));
|
||
auto enc = [&](const char* s, bool add_special, bool parse_special) {
|
||
std::vector<int32_t> ids;
|
||
t.encode(s, ids, add_special, parse_special);
|
||
return ids;
|
||
};
|
||
CHECK(enc("ab", true, true) == (std::vector<int32_t>{ 12, 4 }));
|
||
CHECK(enc("<|end|>ab", true, true) == (std::vector<int32_t>{ 12, 4 }));
|
||
CHECK(enc("<|end|><|end|>ab", true, true) == (std::vector<int32_t>{ 12, 12, 4 }));
|
||
CHECK(enc("ab<|end|>", true, true) == (std::vector<int32_t>{ 12, 4, 12 }));
|
||
CHECK(enc("<|end|>ab", false, true) == (std::vector<int32_t>{ 12, 4 }));
|
||
/* Not parsed, the spelling is not the token, so the BOS is still owed. */
|
||
CHECK(enc("<|end|>x", true, false) == (std::vector<int32_t>{ 12, 11 }));
|
||
}
|
||
|
||
/* Special tokens are claimed longest first, not leftmost: "bcde" beats "ab" in "abcde" even
|
||
* though "ab" starts earlier, and "<|im_start|>" is never cut by its prefix "<|im". */
|
||
TEST(special_tokens_are_claimed_by_priority) {
|
||
VocabBuild vb = trained();
|
||
vb.steps = { split_step(k_qwen35), bytelevel_step() };
|
||
const int32_t base = (int32_t)vb.tokens.size();
|
||
add_specials(vb, { { "ab", RAD_TT_CONTROL }, { "bcde", RAD_TT_CONTROL },
|
||
{ "<|im", RAD_TT_CONTROL }, { "<|im_start|>", RAD_TT_CONTROL } });
|
||
auto v = load(std::move(vb));
|
||
REQUIRE(v != nullptr);
|
||
Tokenizer t;
|
||
t.init(v);
|
||
const std::vector<int32_t> ids = encode_new(t, "abcde");
|
||
REQUIRE(ids.size() == 2);
|
||
CHECK_EQ(ids[1], base + 1);
|
||
CHECK(ids == encode_ref(*v, "abcde"));
|
||
CHECK(encode_new(t, "x<|im_start|>y<|im") == encode_ref(*v, "x<|im_start|>y<|im"));
|
||
const std::vector<int32_t> im = encode_new(t, "<|im_start|>");
|
||
CHECK(im == (std::vector<int32_t>{ base + 3 }));
|
||
/* Not parsed, a control token is text. */
|
||
CHECK(encode_new(t, "<|im_start|>", false) != im);
|
||
CHECK(encode_new(t, "<|im_start|>", false) == encode_ref(*v, "<|im_start|>", false));
|
||
}
|
||
|
||
/* ================================================================== NFC */
|
||
|
||
/* Normalising only around what can change equals normalising the whole text. Checked on every
|
||
* codepoint alone, then random runs of starters, marks in and out of canonical order, Hangul
|
||
* jamo, composed and decomposed forms, and invalid UTF-8. */
|
||
TEST(nfc_between_boundaries_is_the_whole_text_normalised) {
|
||
size_t bad = 0;
|
||
for (uint32_t c = 0; c < 0x110000; ++c) {
|
||
if (c >= 0xD800 && c <= 0xDFFF) continue;
|
||
std::string s = cp_utf8(c);
|
||
const std::string want = unorm_utf8(s, false, true);
|
||
tok_nfc(s);
|
||
if (s != want) ++bad;
|
||
}
|
||
CHECK_EQ(bad, (size_t)0);
|
||
|
||
static const char* parts[] = {
|
||
"a", "e", "o", "A", " ", "x", "\xcc\x81", "\xcc\x80", "\xcc\x88", "\xcc\xa3", "\xcc\x9b",
|
||
"\xcd\x85", "\xe0\xb9\x88", "\xd6\xb0", "\xe1\x84\x80", "\xe1\x85\xa1", "\xe1\x86\xa8",
|
||
"\xea\xb0\x80", "\xc3\xa9", "\xc3\x85", "\xe2\x84\xab", "\xef\xac\x81", "\xe2\x80\x8d",
|
||
"\xf0\x90\xbd\x8a", "\xe0\xbb\x8b", "\xe0\xbd\xb3", "\xcd\x84", "\xe4\xb8\x96", "\xc3",
|
||
"\x80", "\xed\xa0\x80", "\xc0\x80", "\xf4\x90\x80\x80", "abcdefgh",
|
||
};
|
||
std::mt19937 rng(5);
|
||
for (int i = 0; i < 100000; ++i) {
|
||
std::string s;
|
||
const int n = 1 + rng() % 12;
|
||
for (int k = 0; k < n; ++k) s += parts[rng() % (sizeof(parts) / sizeof(parts[0]))];
|
||
const std::string want = unorm_utf8(s, false, true);
|
||
std::string got = s;
|
||
const bool touched = tok_nfc(got);
|
||
/* Untouched means already normalised; touched text may still come out as it went in. */
|
||
if (got != want || (!touched && want != s)) ++bad;
|
||
}
|
||
CHECK_EQ(bad, (size_t)0);
|
||
}
|
||
|
||
/* ================================================================== the segment cache */
|
||
|
||
namespace {
|
||
|
||
std::shared_ptr<const Vocab> chat_vocab() {
|
||
static std::shared_ptr<const Vocab> v = [] {
|
||
VocabBuild vb = trained();
|
||
vb.steps = { simple_step(RAD_NORM_NFC), split_step(k_qwen35), bytelevel_step() };
|
||
add_specials(vb, { { "<|im_start|>", RAD_TT_CONTROL }, { "<|im_end|>", RAD_TT_CONTROL },
|
||
{ "<think>", RAD_TT_USER_DEFINED } });
|
||
return load(std::move(vb));
|
||
}();
|
||
return v;
|
||
}
|
||
|
||
/* A conversation: every turn is every message so far, framed as a chat template frames them. */
|
||
std::vector<std::string> conversation(uint32_t seed, int turns) {
|
||
TextGen g(seed);
|
||
std::vector<std::string> prompts;
|
||
std::string conv = "<|im_start|>system\nYou are a coding agent.<|im_end|>\n";
|
||
for (int k = 0; k < turns; ++k) {
|
||
const char* role = k % 2 ? "assistant" : "user";
|
||
conv += std::string("<|im_start|>") + role + "\n" + (k % 3 == 1 ? "<think>\n" : "") +
|
||
g.text(50 + g.pick(400)) + "<|im_end|>\n";
|
||
prompts.push_back(conv + "<|im_start|>assistant\n");
|
||
}
|
||
return prompts;
|
||
}
|
||
|
||
} /* namespace */
|
||
|
||
TEST(the_cache_returns_what_encoding_returns_cold_warm_and_evicted) {
|
||
auto v = chat_vocab();
|
||
REQUIRE(v != nullptr);
|
||
Tokenizer plain, cached;
|
||
plain.init(v);
|
||
plain.set_segment_cache(false);
|
||
cached.init(v);
|
||
cached.clear_cache();
|
||
cached.set_cache_limit(Tokenizer::kSegmentCacheDefault);
|
||
|
||
const std::vector<std::string> prompts = conversation(11, 12);
|
||
const std::string& last = prompts.back();
|
||
const std::vector<int32_t> want = encode_new(plain, last);
|
||
CHECK(encode_new(cached, last) == want); /* cold */
|
||
const Tokenizer::CacheStats cold = cached.cache_stats();
|
||
CHECK(cold.entries > 0);
|
||
CHECK(encode_new(cached, last) == want); /* warm */
|
||
const Tokenizer::CacheStats warm = cached.cache_stats();
|
||
CHECK(warm.hits >= cold.entries);
|
||
CHECK(warm.bytes <= warm.limit);
|
||
|
||
/* A bound smaller than the conversation evicts, and what is left still answers exactly. */
|
||
cached.set_cache_limit(cold.bytes / 3);
|
||
const Tokenizer::CacheStats small = cached.cache_stats();
|
||
CHECK(small.bytes <= small.limit);
|
||
CHECK(small.entries < cold.entries);
|
||
for (int k = 0; k < 3; ++k) CHECK(encode_new(cached, last) == want);
|
||
CHECK(cached.cache_stats().bytes <= cold.bytes / 3);
|
||
|
||
/* No room at all: every segment is encoded, nothing is kept. */
|
||
cached.set_cache_limit(0);
|
||
CHECK_EQ(cached.cache_stats().entries, (size_t)0);
|
||
CHECK(encode_new(cached, last) == want);
|
||
CHECK_EQ(cached.cache_stats().entries, (size_t)0);
|
||
cached.set_cache_limit(Tokenizer::kSegmentCacheDefault);
|
||
}
|
||
|
||
/* A growing conversation costs its new turn: every earlier message is a hit, and every turn's
|
||
* ids equal the uncached ids. */
|
||
TEST(the_cache_serves_a_growing_conversation_exactly) {
|
||
auto v = chat_vocab();
|
||
REQUIRE(v != nullptr);
|
||
Tokenizer plain, cached;
|
||
plain.init(v);
|
||
plain.set_segment_cache(false);
|
||
cached.init(v);
|
||
cached.clear_cache();
|
||
const std::vector<std::string> prompts = conversation(21, 30);
|
||
uint64_t prev_hits = 0;
|
||
for (size_t k = 0; k < prompts.size(); ++k) {
|
||
CHECK(encode_new(cached, prompts[k]) == encode_new(plain, prompts[k]));
|
||
const Tokenizer::CacheStats s = cached.cache_stats();
|
||
/* Turn k re-sends k earlier messages; each long enough to cache is a hit. */
|
||
if (k >= 2) CHECK(s.hits - prev_hits >= k / 2);
|
||
prev_hits = s.hits;
|
||
}
|
||
/* A segment under the minimum is never cached. */
|
||
cached.clear_cache();
|
||
encode_new(cached, "<|im_start|>user\nhi<|im_end|>");
|
||
CHECK_EQ(cached.cache_stats().entries, (size_t)0);
|
||
}
|
||
|
||
/* Concurrent encodes that share prefixes and evict each other each get the uncached answer. */
|
||
TEST(the_cache_is_exact_under_concurrent_encodes) {
|
||
auto v = chat_vocab();
|
||
REQUIRE(v != nullptr);
|
||
Tokenizer plain;
|
||
plain.init(v);
|
||
plain.set_segment_cache(false);
|
||
std::vector<std::vector<std::string>> convs;
|
||
std::vector<std::vector<std::vector<int32_t>>> want;
|
||
for (uint32_t c = 0; c < 4; ++c) {
|
||
convs.push_back(conversation(100 + c, 16));
|
||
want.emplace_back();
|
||
for (const std::string& p : convs.back()) want.back().push_back(encode_new(plain, p));
|
||
}
|
||
Tokenizer shared;
|
||
shared.init(v);
|
||
shared.clear_cache();
|
||
/* Small enough that the threads evict each other's segments. */
|
||
shared.set_cache_limit(64 << 10);
|
||
std::atomic<int> bad{ 0 };
|
||
std::vector<std::thread> threads;
|
||
for (int t = 0; t < 8; ++t) {
|
||
threads.emplace_back([&, t] {
|
||
const size_t c = (size_t)t % convs.size();
|
||
for (int round = 0; round < 3; ++round)
|
||
for (size_t k = 0; k < convs[c].size(); ++k)
|
||
if (encode_new(shared, convs[c][k]) != want[c][k]) ++bad;
|
||
});
|
||
}
|
||
for (std::thread& th : threads) th.join();
|
||
CHECK_EQ(bad.load(), 0);
|
||
const Tokenizer::CacheStats s = shared.cache_stats();
|
||
CHECK(s.bytes <= s.limit);
|
||
CHECK(s.hits > 0);
|
||
shared.set_cache_limit(Tokenizer::kSegmentCacheDefault);
|
||
}
|
||
|
||
/* ================================================================== the character-class pre-tokenisers */
|
||
|
||
/* HuggingFace's Whitespace, WhitespaceSplit, BertPreTokenizer, Punctuation under every behaviour
|
||
* and Digits both ways: the engine (their equivalent patterns through the Split machinery)
|
||
* against the reference (each written from HuggingFace's character predicate), on random text
|
||
* over every general category. The WordPiece vocabulary holds every character of the texts alone
|
||
* and as a "##" continuation, so the ids spell out exactly where each pre-token starts. text_test
|
||
* holds both to HuggingFace's own answers on one line. */
|
||
TEST(character_class_pre_tokenizers_match_the_reference) {
|
||
TextGen gen(900);
|
||
std::vector<std::string> texts;
|
||
std::set<uint32_t> cps;
|
||
for (int i = 0; i < 300; ++i) {
|
||
texts.push_back(gen.text(1 + gen.pick(40)));
|
||
for (uint32_t c : unicode_cpts_from_utf8(texts.back())) cps.insert(c);
|
||
}
|
||
VocabBuild base;
|
||
base.kind = RAD_TOK_WORDPIECE;
|
||
base.tokens = { "[UNK]" };
|
||
for (uint32_t c : cps) {
|
||
base.tokens.push_back(cp_utf8(c));
|
||
base.tokens.push_back("##" + cp_utf8(c));
|
||
}
|
||
base.unk = 0;
|
||
auto with = [&](std::vector<VocabStep> steps) {
|
||
VocabBuild vb = base;
|
||
vb.steps = std::move(steps);
|
||
return load(std::move(vb));
|
||
};
|
||
auto step = [](uint32_t kind, uint32_t flags, int64_t iarg) {
|
||
VocabStep s;
|
||
s.kind = kind;
|
||
s.flags = flags;
|
||
s.iarg = iarg;
|
||
return s;
|
||
};
|
||
struct Chain { const char* name; std::shared_ptr<const Vocab> v; };
|
||
std::vector<Chain> chains = {
|
||
{ "Whitespace", with({ step(RAD_PRE_WHITESPACE, kPreWhitespace, 0) }) },
|
||
{ "WhitespaceSplit", with({ step(RAD_PRE_WHITESPACE, kPreWhitespaceSplit, 0) }) },
|
||
{ "Bert", with({ step(RAD_PRE_WHITESPACE, kPreBert, 0) }) },
|
||
{ "Digits", with({ step(RAD_PRE_DIGITS, 0, 0) }) },
|
||
{ "Digits individual", with({ step(RAD_PRE_DIGITS, 1, 0) }) },
|
||
};
|
||
static const char* behaviours[] = { "Punctuation Removed", "Punctuation Isolated",
|
||
"Punctuation MergedWithPrevious",
|
||
"Punctuation MergedWithNext", "Punctuation Contiguous" };
|
||
for (int b = 0; b < 5; ++b)
|
||
chains.push_back({ behaviours[b], with({ step(RAD_PRE_PUNCTUATION, 0, b) }) });
|
||
for (const Chain& c : chains) {
|
||
REQUIRE(c.v != nullptr);
|
||
CHECK_EQ(compare_encoders(c.name, c.v, (int)texts.size(),
|
||
[&](int i) { return texts[(size_t)i]; }, false), 0);
|
||
}
|
||
}
|
||
|
||
/* ================================================================== the streaming decoder */
|
||
|
||
namespace {
|
||
|
||
/* A byte-level vocabulary for the streaming cases: every byte, merges trained on mixed text, and
|
||
* two control tokens. */
|
||
std::shared_ptr<const Vocab> stream_vocab() {
|
||
static std::shared_ptr<const Vocab> v = [] {
|
||
TextGen gen(41);
|
||
std::string corpus;
|
||
for (int i = 0; i < 40; ++i) corpus += gen.text(80) + " </tool_call> Q: ";
|
||
VocabBuild vb = train_bytelevel(corpus, k_qwen35, 300);
|
||
VocabStep dec;
|
||
dec.kind = RAD_DEC_BYTELEVEL;
|
||
vb.steps = { split_step(k_qwen35), bytelevel_step(), dec };
|
||
add_specials(vb, { { "<|im_start|>", RAD_TT_CONTROL }, { "<|im_end|>", RAD_TT_CONTROL } });
|
||
return load(std::move(vb));
|
||
}();
|
||
return v;
|
||
}
|
||
|
||
} /* namespace */
|
||
|
||
/* What a stream emits is final text: for random id streams (single bytes that leave characters
|
||
* unfinished or malformed, merged pieces, control tokens), every push() is well-formed UTF-8, the
|
||
* pushes plus flush() are the whole decode with broken bytes replaced, and with a stop string
|
||
* drawn from that decode what has been emitted at every point is text before the stop's first
|
||
* occurrence, and at the end all of it. Seeded; the failing seed and stream are printed. */
|
||
TEST(a_stream_emits_well_formed_final_text_and_never_a_stop_string) {
|
||
auto v = stream_vocab();
|
||
REQUIRE(v != nullptr);
|
||
Tokenizer t;
|
||
CHECK_OK(t.init(v));
|
||
std::mt19937 rng(2024);
|
||
const int32_t n = v->n_tokens();
|
||
int stopped = 0;
|
||
for (int trial = 0; trial < 2000; ++trial) {
|
||
std::vector<int32_t> ids;
|
||
const int len = 1 + (int)(rng() % 24);
|
||
for (int i = 0; i < len; ++i)
|
||
ids.push_back(rng() % 3 ? (int32_t)(rng() % 256) : (int32_t)(rng() % (uint32_t)n));
|
||
const std::string whole = t.decode(ids);
|
||
|
||
Detokenizer d(v);
|
||
std::string got;
|
||
bool ok = true;
|
||
for (int32_t id : ids) {
|
||
const std::string out = d.push(id);
|
||
ok = ok && utf8_valid_prefix(out) == out.size();
|
||
got += out;
|
||
}
|
||
got += d.flush();
|
||
ok = ok && got == utf8_sanitize(whole);
|
||
|
||
/* A stop string drawn from the decode itself, so it occurs. It is matched against the
|
||
* decoded bytes, so one holding a replacement character, which only sanitising
|
||
* produced, could never occur: draw again. */
|
||
const std::string clean = utf8_sanitize(whole);
|
||
std::string stop;
|
||
for (int attempt = 0; attempt < 16 && stop.empty() && clean.size() >= 3; ++attempt) {
|
||
const size_t at = rng() % (clean.size() - 2), len_s = 2 + rng() % 3;
|
||
stop = clean.substr(at, len_s);
|
||
if (utf8_valid_prefix(stop) != stop.size() ||
|
||
stop.find("\xef\xbf\xbd") != std::string::npos)
|
||
stop.clear();
|
||
}
|
||
if (!stop.empty()) {
|
||
/* Everything before the stop's first occurrence, and nothing of it or after it, at
|
||
* every point of the stream. */
|
||
const size_t at = clean.find(stop);
|
||
const std::string before = clean.substr(0, at);
|
||
Detokenizer s(v, { stop });
|
||
std::string emitted;
|
||
for (int32_t id : ids) {
|
||
emitted += s.push(id);
|
||
ok = ok && before.compare(0, emitted.size(), emitted) == 0;
|
||
if (s.stopped()) break;
|
||
}
|
||
emitted += s.flush();
|
||
ok = ok && s.stopped() == (at != std::string::npos) && emitted == before;
|
||
stopped += s.stopped();
|
||
}
|
||
if (!ok) {
|
||
CHECK(ok);
|
||
fprintf(stderr, " seed 2024 trial %d: ids %s, stop '%s'\n streamed '%s'\n"
|
||
" decoded '%s'\n", trial, show_ids(ids, 0, ids.size()).c_str(),
|
||
escaped(stop).c_str(), escaped(got).c_str(), escaped(clean).c_str());
|
||
break;
|
||
}
|
||
}
|
||
std::fprintf(stderr, " %d of 2000 streams met their stop string\n", stopped);
|
||
CHECK(stopped > 1000);
|
||
}
|
||
|
||
/* A SentencePiece byte-fallback vocabulary spells a character it has no piece for as its bytes,
|
||
* which its ByteFallback decoder turns back into bytes: the stream holds them until the character
|
||
* is whole, and a byte no continuation can complete
|
||
* (0xC1 only starts overlong forms) comes out as U+FFFD at the end rather than as raw invalid
|
||
* UTF-8. Metaspace's prepended space is dropped on the stream's first token only. */
|
||
TEST(a_byte_fallback_stream_holds_bytes_until_the_character_is_whole) {
|
||
auto v = from_json(R"JSON({
|
||
"pre_tokenizer": { "type": "Metaspace", "replacement": "▁", "prepend_scheme": "always", "split": false },
|
||
"decoder": { "type": "Sequence", "decoders": [
|
||
{ "type": "Metaspace", "replacement": "▁", "prepend_scheme": "always" },
|
||
{ "type": "ByteFallback" } ] },
|
||
"model": { "type": "BPE", "byte_fallback": true,
|
||
"vocab": {"<unk>": 0, "▁": 1, "h": 2, "i": 3, "▁h": 4, "▁hi": 5, "<0xE2>": 6, "<0x82>": 7,
|
||
"<0xAC>": 8, "<0xC1>": 9},
|
||
"merges": ["▁ h", "▁h i"] }
|
||
})JSON");
|
||
REQUIRE(v != nullptr);
|
||
Detokenizer d(v);
|
||
CHECK_EQ(d.push(5), std::string("hi"));
|
||
CHECK_EQ(d.push(5), std::string(" hi"));
|
||
CHECK_EQ(d.push(6), std::string());
|
||
CHECK_EQ(d.push(7), std::string());
|
||
CHECK_EQ(d.push(8), std::string("\xe2\x82\xac"));
|
||
CHECK_EQ(d.push(9), std::string());
|
||
CHECK_EQ(d.flush(), std::string("\xef\xbf\xbd"));
|
||
|
||
/* Malformed in the middle: the orphan is replaced as soon as what follows shows it cannot
|
||
* complete. */
|
||
Detokenizer e(v);
|
||
CHECK_EQ(e.push(9), std::string());
|
||
CHECK_EQ(e.push(2), std::string("\xef\xbf\xbdh"));
|
||
}
|
||
|
||
/* ================================================================== the grammar's view */
|
||
|
||
/* TokenView is what grammars walk: each token's decoded output bytes, not the vocabulary's own
|
||
* encoding. Over the byte-level alphabet a space is 'Ġ' and a newline 'Ċ' in the table, and a
|
||
* grammar reading those would refuse every token with a space or newline in it. A byte token is
|
||
* its byte, a control token renders literally (an empty piece would satisfy any grammar
|
||
* position), and special tokens tokenise as themselves. */
|
||
TEST(the_token_view_gives_grammars_the_decoded_bytes) {
|
||
auto v = stream_vocab();
|
||
REQUIRE(v != nullptr);
|
||
TokenView tv;
|
||
CHECK_OK(tv.init(v));
|
||
Tokenizer t;
|
||
CHECK_OK(t.init(v));
|
||
for (int32_t id = 0; id < v->n_tokens(); ++id) CHECK_EQ(tv.token_piece(id), t.piece(id, true));
|
||
CHECK_EQ(tv.token_piece(v->find("\xc4\xa0")), std::string(" "));
|
||
CHECK_EQ(tv.token_piece(v->find("\xc4\x8a")), std::string("\n"));
|
||
const int32_t start = v->find("<|im_start|>");
|
||
CHECK_EQ(tv.token_piece(start), std::string("<|im_start|>"));
|
||
std::vector<int32_t> ids;
|
||
CHECK_OK(tv.tokenize_special("<|im_start|>", &ids));
|
||
CHECK(ids == (std::vector<int32_t>{ start }));
|
||
|
||
auto spm = from_json(R"JSON({
|
||
"pre_tokenizer": { "type": "Metaspace", "replacement": "▁", "prepend_scheme": "always", "split": false },
|
||
"decoder": { "type": "Sequence", "decoders": [
|
||
{ "type": "Metaspace", "replacement": "▁", "prepend_scheme": "always" },
|
||
{ "type": "ByteFallback" } ] },
|
||
"model": { "type": "BPE", "byte_fallback": true,
|
||
"vocab": {"<unk>": 0, "▁": 1, "h": 2, "<0x0A>": 3, "▁h": 4}, "merges": ["▁ h"] }
|
||
})JSON");
|
||
REQUIRE(spm != nullptr);
|
||
TokenView sv;
|
||
CHECK_OK(sv.init(spm));
|
||
CHECK_EQ(sv.token_piece(3), std::string("\n"));
|
||
CHECK_EQ(sv.token_piece(4), std::string(" h"));
|
||
|
||
/* A grammar over the view admits the token that carries the space. */
|
||
std::vector<int32_t> hello, world;
|
||
CHECK_OK(t.encode("hello", hello, false, false));
|
||
CHECK_OK(t.encode(" world", world, false, false));
|
||
std::unique_ptr<GbnfGrammar> g;
|
||
std::string err;
|
||
REQUIRE(GbnfGrammar::create("root ::= \"hello world\"", "root", &tv, false, {}, {}, &g, &err) >= 0);
|
||
for (int32_t id : hello) REQUIRE(g->accept_token(id) >= 0);
|
||
std::vector<uint32_t> mask((size_t)bitmask_words(v->n_tokens()));
|
||
CHECK_OK(g->fill_mask(mask.data(), (int64_t)mask.size()));
|
||
CHECK((mask[(size_t)world[0] >> 5] >> (world[0] & 31)) & 1u);
|
||
CHECK_EQ(tv.token_piece(world[0])[0], ' ');
|
||
}
|
||
|
||
/* Gemma's tokenizer.json: whitespace runs are added tokens, not special, spelled in U+2581, and
|
||
* the decoder replaces U+2581 with a space. HuggingFace decodes them to spaces; returned literally,
|
||
* an indented line came out as "▁▁- web-a" (TranslateGemma, 18. 10. 2026). */
|
||
TEST(an_added_whitespace_run_decodes_to_spaces_as_huggingface_does) {
|
||
auto v = from_json(R"JSON({
|
||
"normalizer": { "type": "Replace", "pattern": { "String": " " }, "content": "▁" },
|
||
"decoder": { "type": "Sequence", "decoders": [
|
||
{ "type": "Replace", "pattern": { "String": "▁" }, "content": " " },
|
||
{ "type": "ByteFallback" }, { "type": "Fuse" } ] },
|
||
"model": { "type": "BPE", "byte_fallback": true,
|
||
"vocab": {"<unk>": 0, "▁": 1, "-": 2, "▁▁": 3, "<eot>": 4, "▁▁▁▁": 5},
|
||
"merges": ["▁ ▁", "▁▁ ▁▁"] },
|
||
"added_tokens": [
|
||
{"id": 3, "content": "▁▁", "special": false, "normalized": false},
|
||
{"id": 5, "content": "▁▁▁▁", "special": false, "normalized": false},
|
||
{"id": 4, "content": "<eot>", "special": true} ]
|
||
})JSON");
|
||
REQUIRE(v != nullptr);
|
||
Tokenizer t;
|
||
CHECK_OK(t.init(v));
|
||
CHECK_EQ(t.piece(3, false), std::string(" "));
|
||
CHECK_EQ(t.piece(5, false), std::string(" "));
|
||
CHECK_EQ(t.decode({ 3, 2, 5, 2 }, false), std::string(" - -"));
|
||
/* A control token still renders as written. */
|
||
CHECK_EQ(t.piece(4, true), std::string("<eot>"));
|
||
|
||
TokenView tv;
|
||
CHECK_OK(tv.init(v));
|
||
CHECK_EQ(tv.token_piece(3), std::string(" "));
|
||
}
|
||
|
||
RAD_TEST_MAIN()
|