Team Ai
Apppublic

KBaba7/llama.cpp

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
unicode.cpp850 linesDownload Raw Back to src
1#if defined(_MSC_VER)2#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING3#endif4 5#include "unicode.h"6#include "unicode-data.h"7 8#include <algorithm>9#include <cassert>10#include <codecvt>11#include <cstddef>12#include <cstdint>13#include <locale>14#include <map>15#include <regex>16#include <stdexcept>17#include <string>18#include <unordered_map>19#include <utility>20#include <vector>21 22size_t unicode_len_utf8(char src) {23    const size_t lookup[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4 };24    uint8_t highbits = static_cast<uint8_t>(src) >> 4;25    return lookup[highbits];26}27 28static std::string unicode_cpts_to_utf8(const std::vector<uint32_t> & cps) {29    std::string result;30    for (size_t i = 0; i < cps.size(); ++i) {31        result.append(unicode_cpt_to_utf8(cps[i]));32    }33    return result;34}35 36uint32_t unicode_cpt_from_utf8(const std::string & utf8, size_t & offset) {37    assert(offset < utf8.size());38    if (!(utf8[offset + 0] & 0x80)) {39        auto result = utf8[offset + 0];40        offset += 1;41        return result;42    }43    if (!(utf8[offset + 0] & 0x40)) {44        throw std::invalid_argument("invalid character");45    }46    if (!(utf8[offset + 0] & 0x20)) {47        if (offset + 1 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80)) {48            throw std::invalid_argument("invalid character");49        }50        auto result = ((utf8[offset + 0] & 0x1f) << 6) | (utf8[offset + 1] & 0x3f);51        offset += 2;52        return result;53    }54    if (!(utf8[offset + 0] & 0x10)) {55        if (offset + 2 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80) || ! ((utf8[offset + 2] & 0xc0) == 0x80)) {56            throw std::invalid_argument("invalid character");57        }58        auto result = ((utf8[offset + 0] & 0x0f) << 12) | ((utf8[offset + 1] & 0x3f) << 6) | (utf8[offset + 2] & 0x3f);59        offset += 3;60        return result;61    }62    if (!(utf8[offset + 0] & 0x08)) {63        if (offset + 3 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80) || ! ((utf8[offset + 2] & 0xc0) == 0x80) || !((utf8[offset + 3] & 0xc0) == 0x80)) {64            throw std::invalid_argument("invalid character");65        }66        auto result = ((utf8[offset + 0] & 0x07) << 18) | ((utf8[offset + 1] & 0x3f) << 12) | ((utf8[offset + 2] & 0x3f) << 6) | (utf8[offset + 3] & 0x3f);67        offset += 4;68        return result;69    }70    throw std::invalid_argument("failed to convert utf8 to codepoint");71}72 73//static std::vector<uint16_t> unicode_cpt_to_utf16(uint32_t cpt) {74//    std::vector<uint16_t> result;75//    if (/* 0x0000 <= cpt && */ cpt <= 0xffff) {76//        result.emplace_back(cpt);77//        return result;78//    }79//    if (0x10000 <= cpt && cpt <= 0x10ffff) {80//        result.emplace_back(0xd800 | ((cpt - 0x10000) >> 10));81//        result.emplace_back(0xdc00 | ((cpt - 0x10000) & 0x03ff));82//        return result;83//    }84//    throw std::invalid_argument("failed to convert codepoint to utf16");85//}86 87//static std::vector<uint16_t> unicode_cpts_to_utf16(const std::vector<uint32_t> & cps) {88//    std::vector<uint16_t> result;89//    for (size_t i = 0; i < cps.size(); ++i) {90//        auto temp = unicode_cpt_to_utf16(cps[i]);91//        result.insert(result.end(), temp.begin(), temp.end());92//    }93//    return result;94//}95 96//static uint32_t unicode_cpt_from_utf16(const std::vector<uint16_t> & utf16, size_t & offset) {97//    assert(offset < utf16.size());98//    if (((utf16[0] >> 10) << 10) != 0xd800) {99//        auto result = utf16[offset + 0];100//        offset += 1;101//        return result;102//    }103//104//    if (offset + 1 >= utf16.size() || !((utf16[1] & 0xdc00) == 0xdc00)) {105//        throw std::invalid_argument("invalid character");106//    }107//108//    auto result = 0x10000 + (((utf16[0] & 0x03ff) << 10) | (utf16[1] & 0x03ff));109//    offset += 2;110//    return result;111//}112 113//static std::vector<uint32_t> unicode_cpts_from_utf16(const std::vector<uint16_t> & utf16) {114//    std::vector<uint32_t> result;115//    size_t offset = 0;116//    while (offset < utf16.size()) {117//        result.push_back(unicode_cpt_from_utf16(utf16, offset));118//    }119//    return result;120//}121 122static std::vector<unicode_cpt_flags> unicode_cpt_flags_array() {123    std::vector<unicode_cpt_flags> cpt_flags(MAX_CODEPOINTS, unicode_cpt_flags::UNDEFINED);124 125    assert (unicode_ranges_flags.begin()[0].first == 0);126    assert (unicode_ranges_flags.begin()[unicode_ranges_flags.size()-1].first == MAX_CODEPOINTS);127    for (size_t i = 1; i < unicode_ranges_flags.size(); ++i) {128        const auto range_ini = unicode_ranges_flags.begin()[i-1];  // codepoint_ini, flags129        const auto range_end = unicode_ranges_flags.begin()[i];    // codepoint_end, flags130        for (uint32_t cpt = range_ini.first; cpt < range_end.first; ++cpt) {131            cpt_flags[cpt] = range_ini.second;132        }133    }134 135    for (auto cpt : unicode_set_whitespace) {136        cpt_flags[cpt].is_whitespace = true;137    }138 139    for (auto p : unicode_map_lowercase) {140        cpt_flags[p.second].is_lowercase = true;141    }142 143    for (auto p : unicode_map_uppercase) {144        cpt_flags[p.second].is_uppercase = true;145    }146 147    for (auto &range : unicode_ranges_nfd) {  // start, last, nfd148        cpt_flags[range.nfd].is_nfd = true;149    }150 151    return cpt_flags;152}153 154static std::unordered_map<uint8_t, std::string> unicode_byte_to_utf8_map() {155    std::unordered_map<uint8_t, std::string> map;156    for (int ch = 0x21; ch <= 0x7E; ++ch) {  // u'!' to u'~'157        assert(0 <= ch && ch < 256);158        map[ch] = unicode_cpt_to_utf8(ch);159    }160    for (int ch = 0xA1; ch <= 0xAC; ++ch) {  // u'¡' to u'¬'161        assert(0 <= ch && ch < 256);162        map[ch] = unicode_cpt_to_utf8(ch);163    }164    for (int ch = 0xAE; ch <= 0xFF; ++ch) {  // u'®' to u'ÿ'165        assert(0 <= ch && ch < 256);166        map[ch] = unicode_cpt_to_utf8(ch);167    }168    auto n = 0;169    for (int ch = 0; ch < 256; ++ch) {170        if (map.find(ch) == map.end()) {171            map[ch] = unicode_cpt_to_utf8(256 + n);172            ++n;173        }174    }175    return map;176}177 178static std::unordered_map<std::string, uint8_t> unicode_utf8_to_byte_map() {179    std::unordered_map<std::string, uint8_t> map;180    for (int ch = 0x21; ch <= 0x7E; ++ch) {  // u'!' to u'~'181        assert(0 <= ch && ch < 256);182        map[unicode_cpt_to_utf8(ch)] = ch;183    }184    for (int ch = 0xA1; ch <= 0xAC; ++ch) {  // u'¡' to u'¬'185        assert(0 <= ch && ch < 256);186        map[unicode_cpt_to_utf8(ch)] = ch;187    }188    for (int ch = 0xAE; ch <= 0xFF; ++ch) {  // u'®' to u'ÿ'189        assert(0 <= ch && ch < 256);190        map[unicode_cpt_to_utf8(ch)] = ch;191    }192    auto n = 0;193    for (int ch = 0; ch < 256; ++ch) {194        if (map.find(unicode_cpt_to_utf8(ch)) == map.end()) {195            map[unicode_cpt_to_utf8(256 + n)] = ch;196            ++n;197        }198    }199    return map;200}201 202static inline std::wstring unicode_wstring_from_utf8(const std::string & s) {203#if defined(__clang__)204    // disable C++17 deprecation warning for std::codecvt_utf8205#    pragma clang diagnostic push206#    pragma clang diagnostic ignored "-Wdeprecated-declarations"207#endif208 209    std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;210 211#if defined(__clang__)212#    pragma clang diagnostic pop213#endif214 215    return conv.from_bytes(s);216}217 218static std::vector<std::string> unicode_byte_encoding_process(const std::vector<std::string> & bpe_words) {219    std::vector<std::string> bpe_encoded_words;220    for (const auto & word : bpe_words) {221        std::string text_utf;222        auto utf_word =  unicode_cpts_from_utf8(word);223        for (size_t i = 0; i < utf_word.size(); ++i) {224            text_utf += unicode_cpt_to_utf8(utf_word[i]);225        }226 227        std::string encoded_token;228        for (char & c : text_utf) {229            encoded_token += unicode_byte_to_utf8(c);230        }231        bpe_encoded_words.emplace_back(encoded_token);232    }233    return bpe_encoded_words;234}235 236// GPT2 system regex:  's|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+237static std::vector<size_t> unicode_regex_split_custom_gpt2(const std::string & text, const std::vector<size_t> & offsets) {238    std::vector<size_t> bpe_offsets; // store the offset of each word239    bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size240 241    const auto cpts = unicode_cpts_from_utf8(text);242 243    size_t start = 0;244    for (auto offset : offsets) {245        const size_t offset_ini = start;246        const size_t offset_end = start + offset;247        assert(offset_end <= cpts.size());248        start = offset_end;249 250        static const uint32_t OUT_OF_RANGE = 0xFFFFFFFF;251        auto _get_cpt = [&] (const size_t pos) -> uint32_t {252            return (offset_ini <= pos && pos < offset_end) ? cpts[pos] : OUT_OF_RANGE;253        };254 255        auto _get_flags = [&] (const size_t pos) -> unicode_cpt_flags {256            return (offset_ini <= pos && pos < offset_end) ? unicode_cpt_flags_from_cpt(cpts[pos]) : unicode_cpt_flags{};257        };258 259        size_t _prev_end = offset_ini;260        auto _add_token = [&] (const size_t end) -> size_t {261            assert(_prev_end <= end && end <= offset_end);262            size_t len = end - _prev_end;263            if (len > 0) {264                bpe_offsets.push_back(len);265            }266            _prev_end = end;267            //if (len > 0) {268            //    std::string s = "";269            //    for(size_t p = end-len; p < end; p++)270            //        s += unicode_cpt_to_utf8(cpts[p]);271            //    printf(">>> '%s'\n", s.c_str());272            //}273            return len;274        };275 276        for (size_t pos = offset_ini; pos < offset_end; /*pos++*/ ) {277            const uint32_t cpt = _get_cpt(pos);278            const auto flags = _get_flags(pos);279 280            // regex: 's|'t|'re|'ve|'m|'ll|'d281            if (cpt == '\'' && pos+1 < offset_end) {282                uint32_t cpt_next = _get_cpt(pos+1);283                if (cpt_next == 's' || cpt_next == 't' || cpt_next == 'm' || cpt_next == 'd') {284                    pos += _add_token(pos+2);285                    continue;286                }287                if (pos+2 < offset_end) {288                    uint32_t cpt_next_next = _get_cpt(pos+2);289                    if ((cpt_next == 'r' && cpt_next_next == 'e') ||290                        (cpt_next == 'v' && cpt_next_next == 'e') ||291                        (cpt_next == 'l' && cpt_next_next == 'l')) {292                        pos += _add_token(pos+3);293                        continue;294                    }295                }296            }297 298            auto flags2 = (cpt == ' ' ? _get_flags(pos+1) : flags);299            // regex: <space>?\p{L}+300            if (flags2.is_letter) {301                pos += (cpt == ' ');302                while (flags2.is_letter) {303                    flags2 = _get_flags(++pos);304                }305                _add_token(pos);306                continue;307            }308            // regex: <space>?\p{N}+309            if (flags2.is_number) {310                pos += (cpt == ' ');311                while (flags2.is_number) {312                    flags2 = _get_flags(++pos);313                }314                _add_token(pos);315                continue;316            }317            // regex: <space>?[^\s\p{L}\p{N}]+318            if (!(flags2.is_whitespace | flags2.is_letter | flags2.is_number) && flags2.as_uint()) {319                pos += (cpt == ' ');320                while (!(flags2.is_whitespace | flags2.is_letter | flags2.is_number) && flags2.as_uint()) {321                    flags2 = _get_flags(++pos);322                }323                _add_token(pos);324                continue;325            }326 327            size_t num_whitespaces = 0;328            while (_get_flags(pos+num_whitespaces).is_whitespace) {329                num_whitespaces++;330            }331 332            // regex: \s+(?!\S)333            if (num_whitespaces > 1 && _get_cpt(pos+num_whitespaces) != OUT_OF_RANGE) {334                pos += num_whitespaces - 1;335                _add_token(pos);336                continue;337            }338 339            // regex: \s+340            if (num_whitespaces > 0) {341                pos += num_whitespaces;342                _add_token(pos);343                continue;344            }345 346            // no matches347            _add_token(++pos);348        }349    }350 351    return bpe_offsets;352}353 354// LLAMA3 system regex: "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\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+"355static std::vector<size_t> unicode_regex_split_custom_llama3(const std::string & text, const std::vector<size_t> & offsets) {356    std::vector<size_t> bpe_offsets; // store the offset of each word357    bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size358 359    const auto cpts = unicode_cpts_from_utf8(text);360 361    size_t start = 0;362    for (auto offset : offsets) {363        const size_t offset_ini = start;364        const size_t offset_end = start + offset;365        assert(offset_end <= cpts.size());366        start = offset_end;367 368        static const uint32_t OUT_OF_RANGE = 0xFFFFFFFF;369        auto _get_cpt = [&] (const size_t pos) -> uint32_t {370            return (offset_ini <= pos && pos < offset_end) ? cpts[pos] : OUT_OF_RANGE;371        };372 373        auto _get_flags = [&] (const size_t pos) -> unicode_cpt_flags {374            return (offset_ini <= pos && pos < offset_end) ? unicode_cpt_flags_from_cpt(cpts[pos]) : unicode_cpt_flags{};375        };376 377        size_t _prev_end = offset_ini;378        auto _add_token = [&] (const size_t end) -> size_t {379            assert(_prev_end <= end && end <= offset_end);380            size_t len = end - _prev_end;381            if (len > 0) {382                bpe_offsets.push_back(len);383            }384            _prev_end = end;385            //if (len > 0) {386            //    std::string s = "";387            //    for(size_t p = end-len; p < end; p++)388            //        s += unicode_cpt_to_utf8(cpts[p]);389            //    printf(">>> '%s'\n", s.c_str());390            //}391            return len;392        };393 394        for (size_t pos = offset_ini; pos < offset_end; /*pos++*/ ) {395            const uint32_t cpt = _get_cpt(pos);396            const auto flags = _get_flags(pos);397 398            // regex: (?i:'s|'t|'re|'ve|'m|'ll|'d) // case insensitive399            if (cpt == '\'' && pos+1 < offset_end) {400                uint32_t cpt_next = unicode_tolower(_get_cpt(pos+1));401                if (cpt_next == 's' || cpt_next == 't' || cpt_next == 'm' || cpt_next == 'd') {402                    pos += _add_token(pos+2);403                    continue;404                }405                if (pos+2 < offset_end) {406                    uint32_t cpt_next_next = unicode_tolower(_get_cpt(pos+2));407                    if ((cpt_next == 'r' && cpt_next_next == 'e') ||408                        (cpt_next == 'v' && cpt_next_next == 'e') ||409                        (cpt_next == 'l' && cpt_next_next == 'l')) {410                        pos += _add_token(pos+3);411                        continue;412                    }413                }414            }415 416            // regex: [^\r\n\p{L}\p{N}]?\p{L}+417            if (!(cpt == '\r' || cpt == '\n' || flags.is_number)) {418                if (flags.is_letter || _get_flags(pos+1).is_letter) {  // one or more letters419                    pos++;420                    while (_get_flags(pos).is_letter) {421                        pos++;422                    }423                    _add_token(pos);424                    continue;425                }426            }427 428            // regex: \p{N}{1,3}429            if (flags.is_number) {430                size_t ini = pos;431                while (_get_flags(pos).is_number) {432                    if (++pos - ini >= 3 ) {433                        _add_token(pos);434                        ini = pos;435                    }436                }437                _add_token(pos);438                continue;439            }440 441            // regex: <space>?[^\s\p{L}\p{N}]+[\r\n]*442            auto flags2 = (cpt == ' ' ? _get_flags(pos+1) : flags);443            if (!(flags2.is_whitespace | flags2.is_letter | flags2.is_number) && flags.as_uint()) {444                pos += (cpt == ' ');445                while (!(flags2.is_whitespace | flags2.is_letter | flags2.is_number) && flags2.as_uint()) {446                    flags2 = _get_flags(++pos);447                }448                uint32_t cpt2 = _get_cpt(pos);449                while (cpt2 == '\r' || cpt2 == '\n') {450                    cpt2 = _get_cpt(++pos);451                }452                _add_token(pos);453                continue;454            }455 456            size_t num_whitespaces = 0;457            size_t last_end_r_or_n = 0;458            while (_get_flags(pos+num_whitespaces).is_whitespace) {459                uint32_t cpt2 = _get_cpt(pos+num_whitespaces);460                if (cpt2 == '\r' || cpt2 == '\n') {461                    last_end_r_or_n = pos + num_whitespaces + 1;462                }463                num_whitespaces++;464            }465 466            // regex: \s*[\r\n]+467            if (last_end_r_or_n > 0) {468                pos = last_end_r_or_n;469                _add_token(pos);470                continue;471            }472 473            // regex: \s+(?!\S)474            if (num_whitespaces > 1 && _get_cpt(pos+num_whitespaces) != OUT_OF_RANGE) {475                pos += num_whitespaces - 1;476                _add_token(pos);477                continue;478            }479 480            // regex: \s+481            if (num_whitespaces > 0) {482                pos += num_whitespaces;483                _add_token(pos);484                continue;485            }486 487            // no matches488            _add_token(++pos);489        }490    }491 492    return bpe_offsets;493}494 495// use std::wregex to split the text496static std::vector<size_t> unicode_regex_split_stl(const std::wstring & wtext, const std::wstring & regex_expr, const std::vector<size_t> & offsets) {497    std::wregex expr(regex_expr);498    std::vector<size_t> bpe_offsets; // store the offset of each word499    bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size500    size_t start = 0;501    for (auto offset : offsets) {502        std::wcregex_iterator it(wtext.data() + start, wtext.data() + start + offset, expr);503        std::wcregex_iterator end;504 505        int64_t start_idx = 0;506        while (it != end) {507            std::wcmatch match = *it;508            if (match.position() > start_idx) {509                bpe_offsets.emplace_back(match.position() - start_idx);510            }511            bpe_offsets.emplace_back(match.length());512            start_idx = match.position() + match.length();513            ++it;514        }515 516        if (start_idx < (int64_t) offset) {517            bpe_offsets.emplace_back(offset - start_idx);518        }519        start += offset;520    }521 522    return bpe_offsets;523}524 525// use std::regex to split the text526static std::vector<size_t> unicode_regex_split_stl(const std::string & text, const std::string & regex_expr, const std::vector<size_t> & offsets) {527    std::regex expr(regex_expr);528    std::vector<size_t> bpe_offsets; // store the offset of each word529    bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size530    size_t start = 0;531    for (auto offset : offsets) {532        std::cregex_iterator it(text.data() + start, text.data() + start + offset, expr);533        std::cregex_iterator end;534 535        int64_t start_idx = 0;536        while (it != end) {537            std::cmatch match = *it;538            if (match.position() > start_idx) {539                bpe_offsets.emplace_back(match.position() - start_idx);540            }541            bpe_offsets.emplace_back(match.length());542            start_idx = match.position() + match.length();543            ++it;544        }545 546        if (start_idx < (int64_t) offset) {547            bpe_offsets.emplace_back(offset - start_idx);548        }549        start += offset;550    }551 552    return bpe_offsets;553}554 555static std::vector<size_t> unicode_regex_split_custom(const std::string & text, const std::string & regex_expr, const std::vector<size_t> & offsets) {556    std::vector<size_t> bpe_offsets;557 558    if (regex_expr == "'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)") {559        bpe_offsets = unicode_regex_split_custom_gpt2(text, offsets);560    } else if (561            regex_expr == "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\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+" ||562            regex_expr == "(?:'[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+") {563 564        bpe_offsets = unicode_regex_split_custom_llama3(text, offsets);565    }566 567    return bpe_offsets;568}569 570//571// interface572//573 574std::string unicode_cpt_to_utf8(uint32_t cpt) {575    std::string result;576 577    if (/* 0x00 <= cpt && */ cpt <= 0x7f) {578        result.push_back(cpt);579        return result;580    }581    if (0x80 <= cpt && cpt <= 0x7ff) {582        result.push_back(0xc0 | ((cpt >> 6) & 0x1f));583        result.push_back(0x80 | (cpt & 0x3f));584        return result;585    }586    if (0x800 <= cpt && cpt <= 0xffff) {587        result.push_back(0xe0 | ((cpt >> 12) & 0x0f));588        result.push_back(0x80 | ((cpt >> 6) & 0x3f));589        result.push_back(0x80 | (cpt & 0x3f));590        return result;591    }592    if (0x10000 <= cpt && cpt <= 0x10ffff) {593        result.push_back(0xf0 | ((cpt >> 18) & 0x07));594        result.push_back(0x80 | ((cpt >> 12) & 0x3f));595        result.push_back(0x80 | ((cpt >> 6) & 0x3f));596        result.push_back(0x80 | (cpt & 0x3f));597        return result;598    }599 600    throw std::invalid_argument("invalid codepoint");601}602 603std::vector<uint32_t> unicode_cpts_normalize_nfd(const std::vector<uint32_t> & cpts) {604    auto comp = [] (const uint32_t cpt, const range_nfd & range) {605        return cpt < range.first;606    };607    std::vector<uint32_t> result(cpts.size());608    for (size_t i = 0; i < cpts.size(); ++i) {609        const uint32_t cpt = cpts[i];610        auto it = std::upper_bound(unicode_ranges_nfd.begin(), unicode_ranges_nfd.end(), cpt, comp) - 1;611        result[i] = (it->first <= cpt && cpt <= it->last) ? it->nfd : cpt;612    }613    return result;614}615 616std::vector<uint32_t> unicode_cpts_from_utf8(const std::string & utf8) {617    std::vector<uint32_t> result;618    result.reserve(utf8.size());619    size_t offset = 0;620    while (offset < utf8.size()) {621        try {622            result.push_back(unicode_cpt_from_utf8(utf8, offset));623        }624        catch (const std::invalid_argument & /*ex*/) {625            // Silently ignore invalid UTF-8 input to avoid leaking the exception beyond llama_tokenize626            ++offset;627            result.emplace_back(0xFFFD); // replacement character628        }629    }630    return result;631}632 633unicode_cpt_flags unicode_cpt_flags_from_cpt(const uint32_t cpt) {634    static const unicode_cpt_flags undef(unicode_cpt_flags::UNDEFINED);635    static const auto cpt_flags = unicode_cpt_flags_array();636    return cpt < cpt_flags.size() ? cpt_flags[cpt] : undef;637}638 639unicode_cpt_flags unicode_cpt_flags_from_utf8(const std::string & utf8) {640    static const unicode_cpt_flags undef(unicode_cpt_flags::UNDEFINED);641    if (utf8.empty()) {642        return undef;  // undefined643    }644    size_t offset = 0;645    return unicode_cpt_flags_from_cpt(unicode_cpt_from_utf8(utf8, offset));646}647 648std::string unicode_byte_to_utf8(uint8_t byte) {649    static std::unordered_map<uint8_t, std::string> map = unicode_byte_to_utf8_map();650    return map.at(byte);651}652 653uint8_t unicode_utf8_to_byte(const std::string & utf8) {654    static std::unordered_map<std::string, uint8_t> map = unicode_utf8_to_byte_map();655    return map.at(utf8);656}657 658uint32_t unicode_tolower(uint32_t cpt) {659    // binary search660    auto it = std::lower_bound(unicode_map_lowercase.begin(), unicode_map_lowercase.end(), cpt,661        [](const std::pair<uint32_t, uint32_t> & pair, uint32_t value) {662            return pair.first < value;663        });664    if (it != unicode_map_lowercase.end() && it->first == cpt) {665        return it->second;666    }667    return cpt;  // Return the original code point if no lowercase mapping is found668}669 670std::vector<std::string> unicode_regex_split(const std::string & text, const std::vector<std::string> & regex_exprs) {671    // unicode categories672    static const std::map<std::string, int> k_ucat_enum = {673        { "\\p{N}", unicode_cpt_flags::NUMBER },674        { "\\p{L}", unicode_cpt_flags::LETTER },675        { "\\p{P}", unicode_cpt_flags::PUNCTUATION },676        { "\\p{M}", unicode_cpt_flags::ACCENT_MARK },677        { "\\p{S}", unicode_cpt_flags::SYMBOL },678    };679 680    static const std::map<int, int> k_ucat_cpt = {681        { unicode_cpt_flags::NUMBER,      0xD1 },682        { unicode_cpt_flags::LETTER,      0xD2 },683        { unicode_cpt_flags::PUNCTUATION, 0xD3 },684        { unicode_cpt_flags::ACCENT_MARK, 0xD4 },685        { unicode_cpt_flags::SYMBOL,      0xD5 },686    };687 688    static const std::map<int, std::string> k_ucat_map = {689        { unicode_cpt_flags::NUMBER,      "\x30-\x39" }, // 0-9690        { unicode_cpt_flags::LETTER,      "\x41-\x5A\x61-\x7A" }, // A-Za-z691        { unicode_cpt_flags::PUNCTUATION, "\x21-\x23\x25-\x2A\x2C-\x2F\x3A-\x3B\x3F-\x40\\\x5B-\\\x5D\x5F\\\x7B\\\x7D" }, // !-#%-*,-/:-;?-@\[-\]_\{\}692        { unicode_cpt_flags::ACCENT_MARK, "" }, // no sub-128 codepoints693        { unicode_cpt_flags::SYMBOL,      "\\\x24\\\x2B\x3C-\x3E\x5E\x60\\\x7C" }, // $+<=>^`|694    };695 696    // compute collapsed codepoints only if needed by at least one regex697    bool need_collapse = false;698    for (const auto & regex_expr : regex_exprs) {699        // search for unicode categories700        for (const auto & ucat : k_ucat_enum) {701            if (std::string::npos != regex_expr.find(ucat.first)) {702                need_collapse = true;703                break;704            }705        }706    }707 708    const auto cpts = unicode_cpts_from_utf8(text);709 710    // generate a "collapsed" representation of the text, where all codepoints are replaced by a single byte711    // ref: https://github.com/ggerganov/llama.cpp/pull/6920#issuecomment-2081479935712    std::string text_collapsed;713    if (need_collapse) {714        // collapse all unicode categories715        text_collapsed.resize(cpts.size());716 717        for (size_t i = 0; i < cpts.size(); ++i) {718            // keep single-byte codepoints as is719            if (cpts[i] < 128) {720                text_collapsed[i] = cpts[i];721                continue;722            }723 724            const auto flags = unicode_cpt_flags_from_cpt(cpts[i]);725 726            if (flags.is_whitespace) {727                //NOTE: C++ std::regex \s does not mach 0x85, Rust and Python regex does.728                //text_collapsed[i] = (char) 0x85;  // <Next Line> as whitespace fallback729                text_collapsed[i] = (char) 0x0B;    // <vertical tab> as whitespace fallback730            } else if (k_ucat_cpt.find(flags.category_flag()) != k_ucat_cpt.end()) {731                text_collapsed[i] = k_ucat_cpt.at(flags.category_flag());732            } else {733                text_collapsed[i] = (char) 0xD0; // fallback734            }735        }736    }737 738    std::vector<size_t> bpe_offsets = { cpts.size() };739 740    for (const auto & regex_expr : regex_exprs) {741        // first, see if we have an efficient custom regex implementation742        auto tmp = unicode_regex_split_custom(text, regex_expr, bpe_offsets);743 744        if (!tmp.empty()) {745            bpe_offsets = std::move(tmp);746            continue;747        }748 749        // fallback to general-purpose std::regex / std::wregex750        try {751            // if a unicode category is used in the regex, we use the collapsed text and replace the unicode category752            // with the corresponding collapsed representation753            bool use_collapsed = false;754            for (const auto & ucat : k_ucat_enum) {755                if (std::string::npos != regex_expr.find(ucat.first)) {756                    use_collapsed = true;757                    break;758                }759            }760 761            if (use_collapsed) {762                // sanity-check that the original regex does not contain any non-ASCII characters763                const auto cpts_regex = unicode_cpts_from_utf8(regex_expr);764                for (size_t i = 0; i < cpts_regex.size(); ++i) {765                    if (cpts_regex[i] >= 128) {766                        throw std::runtime_error("Regex includes both unicode categories and non-ASCII characters - not supported");767                    }768                }769 770                // generate a collapsed representation of the regex771                std::string regex_expr_collapsed;772 773                // track if we are inside [], because nested [] are not allowed774                bool inside = false;775                for (size_t i = 0; i < regex_expr.size(); ++i) {776                    if (regex_expr[i] == '[' && (i == 0 || regex_expr[i - 1] != '\\')) {777                        regex_expr_collapsed += '[';778                        inside = true;779                        continue;780                    }781 782                    if (inside && regex_expr[i] == ']' && regex_expr[i - 1] != '\\') {783                        regex_expr_collapsed += ']';784                        inside = false;785                        continue;786                    }787 788                    if (regex_expr[i + 0] == '\\' && i + 4 < regex_expr.size() &&789                        regex_expr[i + 1] == 'p' &&790                        regex_expr[i + 2] == '{' &&791                        regex_expr[i + 4] == '}') {792                        const std::string pat = regex_expr.substr(i, 5);793                        if (k_ucat_enum.find(pat) != k_ucat_enum.end()) {794                            if (!inside) {795                                regex_expr_collapsed += '[';796                            }797                            regex_expr_collapsed += k_ucat_cpt.at(k_ucat_enum.at(pat));798                            regex_expr_collapsed += k_ucat_map.at(k_ucat_enum.at(pat));799                            if (!inside) {800                                regex_expr_collapsed += ']';801                            }802                            i += 4;803                            continue;804                        }805                    }806 807                    regex_expr_collapsed += regex_expr[i];808                }809 810                //printf("text_collapsed: %s\n", text_collapsed.c_str());811                //printf("regex_expr_collapsed: %s\n", regex_expr_collapsed.c_str());812                bpe_offsets = unicode_regex_split_stl(text_collapsed, regex_expr_collapsed, bpe_offsets);813            } else {814                // no unicode category used, we can use std::wregex directly815                const std::wstring wregex_expr = unicode_wstring_from_utf8(regex_expr);816 817                // std::wregex \s does not mach non-ASCII whitespaces, using 0x0B as fallback818                std::wstring wtext(cpts.begin(), cpts.end());819                for (size_t i = 0; i < wtext.size(); ++i) {820                    if (wtext[i] > 0x7F && unicode_cpt_flags_from_cpt(wtext[i]).is_whitespace) {821                        wtext[i] = 0x0B;822                    }823                }824 825                //printf("text: %s\n", text.c_str());826                //printf("regex_expr: %s\n", regex_expr.c_str());827                bpe_offsets = unicode_regex_split_stl(wtext, wregex_expr, bpe_offsets);828            }829        } catch (std::regex_error & e) {830            fprintf(stderr, "Failed to process regex: '%s'\n", regex_expr.c_str());831            fprintf(stderr, "Regex error: %s\n", e.what());832            throw std::runtime_error("Failed to process regex");833        }834    }835 836    std::vector<std::string> bpe_words;837    bpe_words.reserve(bpe_offsets.size()); // reserve memory for the approximate size838 839    size_t start = 0;840    for (size_t & offset : bpe_offsets) {841        bpe_words.emplace_back();842        for (size_t i = start; i < start + offset; ++i) {843            bpe_words.back() += unicode_cpt_to_utf8(cpts[i]);844        }845        start += offset;846    }847 848    return unicode_byte_encoding_process(bpe_words);849}850