Commit a43d06ef9 for llama.cpp

commit a43d06ef90598b075210208f4f21a2a6f00e05d4
Author: IsNot <209144869@qq.com>
Date:   Sun Nov 29 05:28:55 2026 +0800

    unicode : speed up tokenization (#33164)

    * unicode : speed up tokenization

    * unicode : remove always-true asserts, pass byte_encode explicitly in test

    * unicode : remove always-true asserts, pass byte_encode explicitly in test

diff --git a/src/unicode.cpp b/src/unicode.cpp
index ea5957514..91f8b8b9d 100644
--- a/src/unicode.cpp
+++ b/src/unicode.cpp
@@ -2,6 +2,7 @@
 #include "unicode-data.h"

 #include <algorithm>
+#include <array>
 #include <cassert>
 #include <cstddef>
 #include <cstdint>
@@ -159,23 +160,20 @@ static std::vector<unicode_cpt_flags> unicode_cpt_flags_array() {
     return cpt_flags;
 }

-static std::unordered_map<uint8_t, std::string> unicode_byte_to_utf8_map() {
-    std::unordered_map<uint8_t, std::string> map;
+static std::array<std::string, 256> unicode_byte_to_utf8_map() {
+    std::array<std::string, 256> map;
     for (int ch = 0x21; ch <= 0x7E; ++ch) {  // u'!' to u'~'
-        assert(0 <= ch && ch < 256);
         map[ch] = unicode_cpt_to_utf8(ch);
     }
     for (int ch = 0xA1; ch <= 0xAC; ++ch) {  // u'¡' to u'¬'
-        assert(0 <= ch && ch < 256);
         map[ch] = unicode_cpt_to_utf8(ch);
     }
     for (int ch = 0xAE; ch <= 0xFF; ++ch) {  // u'®' to u'ÿ'
-        assert(0 <= ch && ch < 256);
         map[ch] = unicode_cpt_to_utf8(ch);
     }
     auto n = 0;
     for (int ch = 0; ch < 256; ++ch) {
-        if (map.find(ch) == map.end()) {
+        if (map[ch].empty()) {
             map[ch] = unicode_cpt_to_utf8(256 + n);
             ++n;
         }
@@ -186,15 +184,12 @@ static std::unordered_map<uint8_t, std::string> unicode_byte_to_utf8_map() {
 static std::unordered_map<std::string, uint8_t> unicode_utf8_to_byte_map() {
     std::unordered_map<std::string, uint8_t> map;
     for (int ch = 0x21; ch <= 0x7E; ++ch) {  // u'!' to u'~'
-        assert(0 <= ch && ch < 256);
         map[unicode_cpt_to_utf8(ch)] = ch;
     }
     for (int ch = 0xA1; ch <= 0xAC; ++ch) {  // u'¡' to u'¬'
-        assert(0 <= ch && ch < 256);
         map[unicode_cpt_to_utf8(ch)] = ch;
     }
     for (int ch = 0xAE; ch <= 0xFF; ++ch) {  // u'®' to u'ÿ'
-        assert(0 <= ch && ch < 256);
         map[unicode_cpt_to_utf8(ch)] = ch;
     }
     auto n = 0;
@@ -222,18 +217,13 @@ static std::vector<int> unicode_cpt_to_byte_map() {

 static std::vector<std::string> unicode_byte_encoding_process(const std::vector<std::string> & bpe_words) {
     std::vector<std::string> bpe_encoded_words;
+    bpe_encoded_words.reserve(bpe_words.size());
     for (const auto & word : bpe_words) {
-        std::string text_utf;
-        auto utf_word =  unicode_cpts_from_utf8(word);
-        for (size_t i = 0; i < utf_word.size(); ++i) {
-            text_utf += unicode_cpt_to_utf8(utf_word[i]);
-        }
-
-        std::string encoded_token;
-        for (char & c : text_utf) {
+        auto & encoded_token = bpe_encoded_words.emplace_back();
+        encoded_token.reserve(word.size() * 2);
+        for (const char & c : word) {
             encoded_token += unicode_byte_to_utf8(c);
         }
-        bpe_encoded_words.emplace_back(encoded_token);
     }
     return bpe_encoded_words;
 }
@@ -1439,33 +1429,30 @@ static std::vector<size_t> unicode_regex_split_custom(const std::string & text,
 // interface
 //

-std::string unicode_cpt_to_utf8(uint32_t cpt) {
-    std::string result;
-
+static void unicode_cpt_to_utf8(uint32_t cpt, std::string & out) {
     if (/* 0x00 <= cpt && */ cpt <= 0x7f) {
-        result.push_back(cpt);
-        return result;
-    }
-    if (0x80 <= cpt && cpt <= 0x7ff) {
-        result.push_back(0xc0 | ((cpt >> 6) & 0x1f));
-        result.push_back(0x80 | (cpt & 0x3f));
-        return result;
-    }
-    if (0x800 <= cpt && cpt <= 0xffff) {
-        result.push_back(0xe0 | ((cpt >> 12) & 0x0f));
-        result.push_back(0x80 | ((cpt >> 6) & 0x3f));
-        result.push_back(0x80 | (cpt & 0x3f));
-        return result;
-    }
-    if (0x10000 <= cpt && cpt <= 0x10ffff) {
-        result.push_back(0xf0 | ((cpt >> 18) & 0x07));
-        result.push_back(0x80 | ((cpt >> 12) & 0x3f));
-        result.push_back(0x80 | ((cpt >> 6) & 0x3f));
-        result.push_back(0x80 | (cpt & 0x3f));
-        return result;
+        out.push_back(cpt);
+    } else if (cpt <= 0x7ff) {
+        out.push_back(0xc0 | ((cpt >> 6) & 0x1f));
+        out.push_back(0x80 | (cpt & 0x3f));
+    } else if (cpt <= 0xffff) {
+        out.push_back(0xe0 | ((cpt >> 12) & 0x0f));
+        out.push_back(0x80 | ((cpt >> 6) & 0x3f));
+        out.push_back(0x80 | (cpt & 0x3f));
+    } else if (cpt <= 0x10ffff) {
+        out.push_back(0xf0 | ((cpt >> 18) & 0x07));
+        out.push_back(0x80 | ((cpt >> 12) & 0x3f));
+        out.push_back(0x80 | ((cpt >> 6) & 0x3f));
+        out.push_back(0x80 | (cpt & 0x3f));
+    } else {
+        throw std::invalid_argument("invalid codepoint");
     }
+}

-    throw std::invalid_argument("invalid codepoint");
+std::string unicode_cpt_to_utf8(uint32_t cpt) {
+    std::string result;
+    unicode_cpt_to_utf8(cpt, result);
+    return result;
 }

 std::vector<uint32_t> unicode_cpts_normalize_nfd(const std::vector<uint32_t> & cpts) {
@@ -1599,9 +1586,9 @@ unicode_cpt_flags unicode_cpt_flags_from_utf8(const std::string & utf8) {
     return unicode_cpt_flags_from_cpt(unicode_cpt_from_utf8(utf8, offset));
 }

-std::string unicode_byte_to_utf8(uint8_t byte) {
-    static std::unordered_map<uint8_t, std::string> map = unicode_byte_to_utf8_map();
-    return map.at(byte);
+const std::string & unicode_byte_to_utf8(uint8_t byte) {
+    static const std::array<std::string, 256> map = unicode_byte_to_utf8_map();
+    return map[byte];
 }

 uint8_t unicode_utf8_to_byte(const std::string & utf8) {
@@ -1845,9 +1832,9 @@ std::vector<std::string> unicode_regex_split(const std::string & text, const std

     size_t start = 0;
     for (size_t & offset : bpe_offsets) {
-        bpe_words.emplace_back();
+        auto & word = bpe_words.emplace_back();
         for (size_t i = start; i < start + offset; ++i) {
-            bpe_words.back() += unicode_cpt_to_utf8(cpts[i]);
+            unicode_cpt_to_utf8(cpts[i], word);
         }
         start += offset;
     }
diff --git a/src/unicode.h b/src/unicode.h
index 29dbfc709..6352179e3 100644
--- a/src/unicode.h
+++ b/src/unicode.h
@@ -102,9 +102,9 @@ std::vector<uint32_t> unicode_cpts_normalize_nfc(const std::vector<uint32_t> & c
 unicode_cpt_flags unicode_cpt_flags_from_cpt (uint32_t cpt);
 unicode_cpt_flags unicode_cpt_flags_from_utf8(const std::string & utf8);

-std::string unicode_byte_to_utf8(uint8_t byte);
-uint8_t     unicode_utf8_to_byte(const std::string & utf8);
-uint8_t     unicode_cpt_to_byte (uint32_t cpt);
+const std::string & unicode_byte_to_utf8(uint8_t byte);
+uint8_t             unicode_utf8_to_byte(const std::string & utf8);
+uint8_t             unicode_cpt_to_byte (uint32_t cpt);

 uint32_t unicode_tolower(uint32_t cpt);

diff --git a/tests/test-unicode.cpp b/tests/test-unicode.cpp
index 5ced7a28a..93d74298f 100644
--- a/tests/test-unicode.cpp
+++ b/tests/test-unicode.cpp
@@ -82,5 +82,27 @@ int main() {
         success = false;
     }

+    const std::vector<std::string> regex_exprs_bytes    = { "\\p{L}+", "\\p{N}+", "\\s+" };
+    const std::string              byte_encode_text     = "\xc4\x80\xc4\xb1\xe0\xa4\x95\xf0\x9f\x98\x80";
+    const auto                     actual_byte_encode   = unicode_regex_split(byte_encode_text, regex_exprs_bytes, true);
+    // GPT-2 byte-level encoding of byte_encode_text, independent of unicode_byte_to_utf8().
+    const std::vector<std::string> expected_byte_encode = {
+        "\xc3\x84\xc4\xa2\xc3\x84\xc2\xb1\xc3\xa0\xc2\xa4\xc4\xb7",
+        "\xc3\xb0\xc5\x81\xc4\xba\xc4\xa2",
+    };
+
+    if (actual_byte_encode != expected_byte_encode) {
+        fprintf(stderr, "unexpected byte-encoded split:");
+        for (const auto & piece : actual_byte_encode) {
+            fprintf(stderr, " [");
+            for (const unsigned char byte : piece) {
+                fprintf(stderr, "%02x", byte);
+            }
+            fprintf(stderr, "]");
+        }
+        fprintf(stderr, "\n");
+        success = false;
+    }
+
     return success ? 0 : 1;
 }