// Pre-tokenization: cut text into the pieces that are byte-pair encoded separately. // The release does it with one regular expression (tokenization_kimi.py, run by // tiktoken). There is no regex library here, so the pattern is written out by hand, // alternative by alternative, with the same first-match and backtracking behaviour. // tools/check_tokenizer.py compares this scanner with tiktoken on random text. #include #include #include #include "tokenizer/tokenizer.h" namespace hk { // The eight alternatives, joined by "|" in the release. U and W below are the // two letter classes: both exclude Han, which has an alternative of its own. const char* const kKimiPatternParts[8] = { R"([\p{Han}]+)", R"([^\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"([^\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"(\p{N}{1,3})", R"( ?[^\s\p{L}\p{N}]+[\r\n]*)", R"(\s*[\r\n]+)", R"(\s+(?!\S))", R"(\s+)", }; namespace { #include "tokenizer/unicode_tables.inc" inline bool in_table(const uint32_t (*t)[2], int n, uint32_t c) { int lo = 0, hi = n - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (c < t[mid][0]) hi = mid - 1; else if (c > t[mid][1]) lo = mid + 1; else return true; } return false; } inline bool is_han(uint32_t c) { return c >= 0x2E80 && in_table(kHAN, kHAN_N, c); } inline bool is_letter(uint32_t c) { if (c < 0x80) return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); return in_table(kLETTER, kLETTER_N, c); } inline bool is_number(uint32_t c) { if (c < 0x80) return c >= '0' && c <= '9'; return in_table(kNUMBER, kNUMBER_N, c); } inline bool is_space(uint32_t c) { if (c < 0x80) return (c >= 0x09 && c <= 0x0D) || c == 0x20; return in_table(kSPACE, kSPACE_N, c); } inline bool is_newline(uint32_t c) { return c == '\r' || c == '\n'; } // "Upper" class of the pattern: Lu Lt Lm Lo M, without Han. inline bool is_U(uint32_t c) { if (c < 0x80) return c >= 'A' && c <= 'Z'; if (is_han(c)) return false; return in_table(kUPPER, kUPPER_N, c) || in_table(kBOTH, kBOTH_N, c); } // "Lower" class: Ll Lm Lo M, without Han. inline bool is_W(uint32_t c) { if (c < 0x80) return c >= 'a' && c <= 'z'; if (is_han(c)) return false; return in_table(kLOWER, kLOWER_N, c) || in_table(kBOTH, kBOTH_N, c); } // [^\r\n\p{L}\p{N}]: what may stand in front of a word. inline bool is_prefix(uint32_t c) { return !is_newline(c) && !is_letter(c) && !is_number(c); } // [^\s\p{L}\p{N}]: punctuation, symbols, marks, anything else. inline bool is_other(uint32_t c) { return !is_space(c) && !is_letter(c) && !is_number(c); } // One code point. A malformed byte comes back alone, as a value above the Unicode range // (no class matches it). inline uint32_t cp_at(const uint8_t* s, size_t n, size_t* len) { uint8_t b = s[0]; if (b < 0x80) { *len = 1; return b; } int need = (b >= 0xF0 && b <= 0xF4) ? 3 : (b >= 0xE0) ? 2 : (b >= 0xC2) ? 1 : -1; if (b > 0xF4) need = -1; if (need < 0 || (size_t)need >= n) { *len = 1; return 0x110000u + b; } uint32_t c = need == 1 ? (b & 0x1Fu) : need == 2 ? (b & 0x0Fu) : (b & 0x07u); for (int i = 1; i <= need; ++i) { if ((s[i] & 0xC0) != 0x80) { *len = 1; return 0x110000u + b; } c = (c << 6) | (s[i] & 0x3Fu); } *len = (size_t)need + 1; return c; } // (?i:'s|'t|'re|'ve|'m|'ll|'d)? at position p. Returns the suffix length or 0. inline size_t suffix_at(const uint8_t* s, size_t n, size_t p) { if (p >= n || s[p] != '\'') return 0; auto lower = [&](size_t i) -> int { return i < n ? (s[i] >= 'A' && s[i] <= 'Z' ? s[i] + 32 : s[i]) : -1; }; int a = lower(p + 1), b = lower(p + 2); if (a == 's') return 2; // Unicode case folding makes the long s (U+017F) match 's' as well. if (p + 2 < n && s[p + 1] == 0xC5 && s[p + 2] == 0xBF) return 3; if (a == 't') return 2; if (a == 'r' && b == 'e') return 3; if (a == 'v' && b == 'e') return 3; if (a == 'm') return 2; if (a == 'l' && b == 'l') return 3; if (a == 'd') return 2; return 0; } // Alternatives 2 and 3 after the optional prefix character. // lower_required = true : U* W+ (alternative 2) // lower_required = false : U+ W* (alternative 3) // Returns the matched length from p (suffix included), 0 if no match. size_t match_word(const uint8_t* s, size_t n, size_t p, bool lower_required) { // greedy U*: remember each boundary to replay the regex's backtracking static thread_local std::vector bounds; bounds.clear(); size_t u_end = p; bounds.push_back(p); while (u_end < n) { size_t len; uint32_t c = cp_at(s + u_end, n - u_end, &len); if (!is_U(c)) break; u_end += len; bounds.push_back(u_end); } auto w_run = [&](size_t from) { size_t e = from; while (e < n) { size_t len; uint32_t c = cp_at(s + e, n - e, &len); if (!is_W(c)) break; e += len; } return e; }; if (!lower_required) { if (u_end == p) return 0; size_t e = w_run(u_end); return e + suffix_at(s, n, e) - p; } // W+ must match at or before u_end: the greedy position first, then give back one // character of U* at a time for (size_t i = bounds.size(); i-- > 0;) { size_t q = bounds[i]; size_t e = w_run(q); if (e > q) return e + suffix_at(s, n, e) - p; } return 0; } } // namespace size_t pretok_next(const uint8_t* s, size_t n) { size_t len0; const uint32_t c0 = cp_at(s, n, &len0); // 1. [\p{Han}]+ if (is_han(c0)) { size_t e = len0; while (e < n) { size_t len; if (!is_han(cp_at(s + e, n - e, &len))) break; e += len; } return e; } // 2 and 3: an optional non-letter, non-digit character, then a word. for (int alt = 0; alt < 2; ++alt) { const bool lower_required = alt == 0; if (is_prefix(c0) && len0 < n) { size_t m = match_word(s, n, len0, lower_required); if (m) return len0 + m; } size_t m = match_word(s, n, 0, lower_required); if (m) return m; } // 4. \p{N}{1,3} if (is_number(c0)) { size_t e = len0; for (int k = 1; k < 3 && e < n; ++k) { size_t len; if (!is_number(cp_at(s + e, n - e, &len))) break; e += len; } return e; } // 5. ?[^\s\p{L}\p{N}]+[\r\n]* { size_t start = 0; bool ok = false; if (c0 == ' ' && len0 < n) { size_t len; if (is_other(cp_at(s + len0, n - len0, &len))) { start = len0; ok = true; } } if (!ok && is_other(c0)) ok = true; if (ok) { size_t e = start; while (e < n) { size_t len; if (!is_other(cp_at(s + e, n - e, &len))) break; e += len; } while (e < n && (s[e] == '\r' || s[e] == '\n')) ++e; return e; } } // 6, 7, 8: whitespace. if (is_space(c0)) { size_t e = 0, last_nl_end = 0, last_start = 0; int count = 0; while (e < n) { size_t len; uint32_t c = cp_at(s + e, n - e, &len); if (!is_space(c)) break; last_start = e; e += len; ++count; if (is_newline(c)) last_nl_end = e; } if (last_nl_end > 0) return last_nl_end; // \s*[\r\n]+ : up to the last newline of the run if (e == n) return e; // \s+(?!\S) at the end of the text if (count >= 2) return last_start; // \s+(?!\S): all but the last whitespace character return e; // \s+ : a single whitespace character before text } return len0; // cannot happen: every character belongs to one of the classes above } } // namespace hk