Felipe97/llama-cpp-compiled
01.1k
1#include "unicode.h"2#include "unicode-data.h"3 4#include <algorithm>5#include <cassert>6#include <cstddef>7#include <cstdint>8#include <map>9#include <regex>10#include <stdexcept>11#include <string>12#include <unordered_map>13#include <utility>14#include <vector>15 16size_t unicode_len_utf8(char src) {17 const size_t lookup[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4 };18 uint8_t highbits = static_cast<uint8_t>(src) >> 4;19 return lookup[highbits];20}21 22static std::string unicode_cpts_to_utf8(const std::vector<uint32_t> & cps) {23 std::string result;24 for (size_t i = 0; i < cps.size(); ++i) {25 result.append(unicode_cpt_to_utf8(cps[i]));26 }27 return result;28}29 30uint32_t unicode_cpt_from_utf8(const std::string & utf8, size_t & offset) {31 assert(offset < utf8.size());32 if (!(utf8[offset + 0] & 0x80)) {33 auto result = utf8[offset + 0];34 offset += 1;35 return result;36 }37 if (!(utf8[offset + 0] & 0x40)) {38 throw std::invalid_argument("invalid character");39 }40 if (!(utf8[offset + 0] & 0x20)) {41 if (offset + 1 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80)) {42 throw std::invalid_argument("invalid character");43 }44 auto result = ((utf8[offset + 0] & 0x1f) << 6) | (utf8[offset + 1] & 0x3f);45 offset += 2;46 return result;47 }48 if (!(utf8[offset + 0] & 0x10)) {49 if (offset + 2 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80) || ! ((utf8[offset + 2] & 0xc0) == 0x80)) {50 throw std::invalid_argument("invalid character");51 }52 auto result = ((utf8[offset + 0] & 0x0f) << 12) | ((utf8[offset + 1] & 0x3f) << 6) | (utf8[offset + 2] & 0x3f);53 offset += 3;54 return result;55 }56 if (!(utf8[offset + 0] & 0x08)) {57 if (offset + 3 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80) || ! ((utf8[offset + 2] & 0xc0) == 0x80) || !((utf8[offset + 3] & 0xc0) == 0x80)) {58 throw std::invalid_argument("invalid character");59 }60 auto result = ((utf8[offset + 0] & 0x07) << 18) | ((utf8[offset + 1] & 0x3f) << 12) | ((utf8[offset + 2] & 0x3f) << 6) | (utf8[offset + 3] & 0x3f);61 offset += 4;62 return result;63 }64 throw std::invalid_argument("failed to convert utf8 to codepoint");65}66 67//static std::vector<uint16_t> unicode_cpt_to_utf16(uint32_t cpt) {68// std::vector<uint16_t> result;69// if (/* 0x0000 <= cpt && */ cpt <= 0xffff) {70// result.emplace_back(cpt);71// return result;72// }73// if (0x10000 <= cpt && cpt <= 0x10ffff) {74// result.emplace_back(0xd800 | ((cpt - 0x10000) >> 10));75// result.emplace_back(0xdc00 | ((cpt - 0x10000) & 0x03ff));76// return result;77// }78// throw std::invalid_argument("failed to convert codepoint to utf16");79//}80 81//static std::vector<uint16_t> unicode_cpts_to_utf16(const std::vector<uint32_t> & cps) {82// std::vector<uint16_t> result;83// for (size_t i = 0; i < cps.size(); ++i) {84// auto temp = unicode_cpt_to_utf16(cps[i]);85// result.insert(result.end(), temp.begin(), temp.end());86// }87// return result;88//}89 90//static uint32_t unicode_cpt_from_utf16(const std::vector<uint16_t> & utf16, size_t & offset) {91// assert(offset < utf16.size());92// if (((utf16[0] >> 10) << 10) != 0xd800) {93// auto result = utf16[offset + 0];94// offset += 1;95// return result;96// }97//98// if (offset + 1 >= utf16.size() || !((utf16[1] & 0xdc00) == 0xdc00)) {99// throw std::invalid_argument("invalid character");100// }101//102// auto result = 0x10000 + (((utf16[0] & 0x03ff) << 10) | (utf16[1] & 0x03ff));103// offset += 2;104// return result;105//}106 107//static std::vector<uint32_t> unicode_cpts_from_utf16(const std::vector<uint16_t> & utf16) {108// std::vector<uint32_t> result;109// size_t offset = 0;110// while (offset < utf16.size()) {111// result.push_back(unicode_cpt_from_utf16(utf16, offset));112// }113// return result;114//}115 116static std::vector<unicode_cpt_flags> unicode_cpt_flags_array() {117 std::vector<unicode_cpt_flags> cpt_flags(MAX_CODEPOINTS, unicode_cpt_flags::UNDEFINED);118 119 assert (unicode_ranges_flags.begin()[0].first == 0);120 assert (unicode_ranges_flags.begin()[unicode_ranges_flags.size()-1].first == MAX_CODEPOINTS);121 for (size_t i = 1; i < unicode_ranges_flags.size(); ++i) {122 const auto range_ini = unicode_ranges_flags.begin()[i-1]; // codepoint_ini, flags123 const auto range_end = unicode_ranges_flags.begin()[i]; // codepoint_end, flags124 for (uint32_t cpt = range_ini.first; cpt < range_end.first; ++cpt) {125 cpt_flags[cpt] = range_ini.second;126 }127 }128 129 for (auto cpt : unicode_set_whitespace) {130 cpt_flags[cpt].is_whitespace = true;131 }132 133 for (auto p : unicode_map_lowercase) {134 cpt_flags[p.second].is_lowercase = true;135 }136 137 for (auto p : unicode_map_uppercase) {138 cpt_flags[p.second].is_uppercase = true;139 }140 141 for (auto &range : unicode_ranges_nfd) { // start, last, nfd142 cpt_flags[range.nfd].is_nfd = true;143 }144 145 return cpt_flags;146}147 148static std::unordered_map<uint8_t, std::string> unicode_byte_to_utf8_map() {149 std::unordered_map<uint8_t, std::string> map;150 for (int ch = 0x21; ch <= 0x7E; ++ch) { // u'!' to u'~'151 assert(0 <= ch && ch < 256);152 map[ch] = unicode_cpt_to_utf8(ch);153 }154 for (int ch = 0xA1; ch <= 0xAC; ++ch) { // u'¡' to u'¬'155 assert(0 <= ch && ch < 256);156 map[ch] = unicode_cpt_to_utf8(ch);157 }158 for (int ch = 0xAE; ch <= 0xFF; ++ch) { // u'®' to u'ÿ'159 assert(0 <= ch && ch < 256);160 map[ch] = unicode_cpt_to_utf8(ch);161 }162 auto n = 0;163 for (int ch = 0; ch < 256; ++ch) {164 if (map.find(ch) == map.end()) {165 map[ch] = unicode_cpt_to_utf8(256 + n);166 ++n;167 }168 }169 return map;170}171 172static std::unordered_map<std::string, uint8_t> unicode_utf8_to_byte_map() {173 std::unordered_map<std::string, uint8_t> map;174 for (int ch = 0x21; ch <= 0x7E; ++ch) { // u'!' to u'~'175 assert(0 <= ch && ch < 256);176 map[unicode_cpt_to_utf8(ch)] = ch;177 }178 for (int ch = 0xA1; ch <= 0xAC; ++ch) { // u'¡' to u'¬'179 assert(0 <= ch && ch < 256);180 map[unicode_cpt_to_utf8(ch)] = ch;181 }182 for (int ch = 0xAE; ch <= 0xFF; ++ch) { // u'®' to u'ÿ'183 assert(0 <= ch && ch < 256);184 map[unicode_cpt_to_utf8(ch)] = ch;185 }186 auto n = 0;187 for (int ch = 0; ch < 256; ++ch) {188 if (map.find(unicode_cpt_to_utf8(ch)) == map.end()) {189 map[unicode_cpt_to_utf8(256 + n)] = ch;190 ++n;191 }192 }193 return map;194}195 196static std::vector<std::string> unicode_byte_encoding_process(const std::vector<std::string> & bpe_words) {197 std::vector<std::string> bpe_encoded_words;198 for (const auto & word : bpe_words) {199 std::string text_utf;200 auto utf_word = unicode_cpts_from_utf8(word);201 for (size_t i = 0; i < utf_word.size(); ++i) {202 text_utf += unicode_cpt_to_utf8(utf_word[i]);203 }204 205 std::string encoded_token;206 for (char & c : text_utf) {207 encoded_token += unicode_byte_to_utf8(c);208 }209 bpe_encoded_words.emplace_back(encoded_token);210 }211 return bpe_encoded_words;212}213 214// GPT2 system regex: 's|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+215static std::vector<size_t> unicode_regex_split_custom_gpt2(const std::string & text, const std::vector<size_t> & offsets) {216 std::vector<size_t> bpe_offsets; // store the offset of each word217 bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size218 219 const auto cpts = unicode_cpts_from_utf8(text);220 221 size_t start = 0;222 for (auto offset : offsets) {223 const size_t offset_ini = start;224 const size_t offset_end = start + offset;225 assert(offset_end <= cpts.size());226 start = offset_end;227 228 static const uint32_t OUT_OF_RANGE = 0xFFFFFFFF;229 auto _get_cpt = [&] (const size_t pos) -> uint32_t {230 return (offset_ini <= pos && pos < offset_end) ? cpts[pos] : OUT_OF_RANGE;231 };232 233 auto _get_flags = [&] (const size_t pos) -> unicode_cpt_flags {234 return (offset_ini <= pos && pos < offset_end) ? unicode_cpt_flags_from_cpt(cpts[pos]) : unicode_cpt_flags{};235 };236 237 size_t _prev_end = offset_ini;238 auto _add_token = [&] (const size_t end) -> size_t {239 assert(_prev_end <= end && end <= offset_end);240 size_t len = end - _prev_end;241 if (len > 0) {242 bpe_offsets.push_back(len);243 }244 _prev_end = end;245 //if (len > 0) {246 // std::string s = "";247 // for(size_t p = end-len; p < end; p++)248 // s += unicode_cpt_to_utf8(cpts[p]);249 // printf(">>> '%s'\n", s.c_str());250 //}251 return len;252 };253 254 for (size_t pos = offset_ini; pos < offset_end; /*pos++*/ ) {255 const uint32_t cpt = _get_cpt(pos);256 const auto flags = _get_flags(pos);257 258 // regex: 's|'t|'re|'ve|'m|'ll|'d259 if (cpt == '\'' && pos+1 < offset_end) {260 uint32_t cpt_next = _get_cpt(pos+1);261 if (cpt_next == 's' || cpt_next == 't' || cpt_next == 'm' || cpt_next == 'd') {262 pos += _add_token(pos+2);263 continue;264 }265 if (pos+2 < offset_end) {266 uint32_t cpt_next_next = _get_cpt(pos+2);267 if ((cpt_next == 'r' && cpt_next_next == 'e') ||268 (cpt_next == 'v' && cpt_next_next == 'e') ||269 (cpt_next == 'l' && cpt_next_next == 'l')) {270 pos += _add_token(pos+3);271 continue;272 }273 }274 }275 276 auto flags2 = (cpt == ' ' ? _get_flags(pos+1) : flags);277 // regex: <space>?\p{L}+278 if (flags2.is_letter) {279 pos += (cpt == ' ');280 while (flags2.is_letter) {281 flags2 = _get_flags(++pos);282 }283 _add_token(pos);284 continue;285 }286 // regex: <space>?\p{N}+287 if (flags2.is_number) {288 pos += (cpt == ' ');289 while (flags2.is_number) {290 flags2 = _get_flags(++pos);291 }292 _add_token(pos);293 continue;294 }295 // regex: <space>?[^\s\p{L}\p{N}]+296 if (!(flags2.is_whitespace | flags2.is_letter | flags2.is_number) && flags2.as_uint()) {297 pos += (cpt == ' ');298 while (!(flags2.is_whitespace | flags2.is_letter | flags2.is_number) && flags2.as_uint()) {299 flags2 = _get_flags(++pos);300 }301 _add_token(pos);302 continue;303 }304 305 size_t num_whitespaces = 0;306 while (_get_flags(pos+num_whitespaces).is_whitespace) {307 num_whitespaces++;308 }309 310 // regex: \s+(?!\S)311 if (num_whitespaces > 1 && _get_cpt(pos+num_whitespaces) != OUT_OF_RANGE) {312 pos += num_whitespaces - 1;313 _add_token(pos);314 continue;315 }316 317 // regex: \s+318 if (num_whitespaces > 0) {319 pos += num_whitespaces;320 _add_token(pos);321 continue;322 }323 324 // no matches325 _add_token(++pos);326 }327 }328 329 return bpe_offsets;330}331 332// 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+"333static std::vector<size_t> unicode_regex_split_custom_llama3(const std::string & text, const std::vector<size_t> & offsets) {334 std::vector<size_t> bpe_offsets; // store the offset of each word335 bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size336 337 const auto cpts = unicode_cpts_from_utf8(text);338 339 size_t start = 0;340 for (auto offset : offsets) {341 const size_t offset_ini = start;342 const size_t offset_end = start + offset;343 assert(offset_end <= cpts.size());344 start = offset_end;345 346 static const uint32_t OUT_OF_RANGE = 0xFFFFFFFF;347 auto _get_cpt = [&] (const size_t pos) -> uint32_t {348 return (offset_ini <= pos && pos < offset_end) ? cpts[pos] : OUT_OF_RANGE;349 };350 351 auto _get_flags = [&] (const size_t pos) -> unicode_cpt_flags {352 return (offset_ini <= pos && pos < offset_end) ? unicode_cpt_flags_from_cpt(cpts[pos]) : unicode_cpt_flags{};353 };354 355 size_t _prev_end = offset_ini;356 auto _add_token = [&] (const size_t end) -> size_t {357 assert(_prev_end <= end && end <= offset_end);358 size_t len = end - _prev_end;359 if (len > 0) {360 bpe_offsets.push_back(len);361 }362 _prev_end = end;363 //if (len > 0) {364 // std::string s = "";365 // for(size_t p = end-len; p < end; p++)366 // s += unicode_cpt_to_utf8(cpts[p]);367 // printf(">>> '%s'\n", s.c_str());368 //}369 return len;370 };371 372 for (size_t pos = offset_ini; pos < offset_end; /*pos++*/ ) {373 const uint32_t cpt = _get_cpt(pos);374 const auto flags = _get_flags(pos);375 376 // regex: (?i:'s|'t|'re|'ve|'m|'ll|'d) // case insensitive377 if (cpt == '\'' && pos+1 < offset_end) {378 uint32_t cpt_next = unicode_tolower(_get_cpt(pos+1));379 if (cpt_next == 's' || cpt_next == 't' || cpt_next == 'm' || cpt_next == 'd') {380 pos += _add_token(pos+2);381 continue;382 }383 if (pos+2 < offset_end) {384 uint32_t cpt_next_next = unicode_tolower(_get_cpt(pos+2));385 if ((cpt_next == 'r' && cpt_next_next == 'e') ||386 (cpt_next == 'v' && cpt_next_next == 'e') ||387 (cpt_next == 'l' && cpt_next_next == 'l')) {388 pos += _add_token(pos+3);389 continue;390 }391 }392 }393 394 // regex: [^\r\n\p{L}\p{N}]?\p{L}+395 if (!(cpt == '\r' || cpt == '\n' || flags.is_number)) {396 if (flags.is_letter || _get_flags(pos+1).is_letter) { // one or more letters397 pos++;398 while (_get_flags(pos).is_letter) {399 pos++;400 }401 _add_token(pos);402 continue;403 }404 }405 406 // regex: \p{N}{1,3}407 if (flags.is_number) {408 size_t ini = pos;409 while (_get_flags(pos).is_number) {410 if (++pos - ini >= 3 ) {411 _add_token(pos);412 ini = pos;413 }414 }415 _add_token(pos);416 continue;417 }418 419 // regex: <space>?[^\s\p{L}\p{N}]+[\r\n]*420 auto flags2 = (cpt == ' ' ? _get_flags(pos+1) : flags);421 if (!(flags2.is_whitespace | flags2.is_letter | flags2.is_number) && flags.as_uint()) {422 pos += (cpt == ' ');423 while (!(flags2.is_whitespace | flags2.is_letter | flags2.is_number) && flags2.as_uint()) {424 flags2 = _get_flags(++pos);425 }426 uint32_t cpt2 = _get_cpt(pos);427 while (cpt2 == '\r' || cpt2 == '\n') {428 cpt2 = _get_cpt(++pos);429 }430 _add_token(pos);431 continue;432 }433 434 size_t num_whitespaces = 0;435 size_t last_end_r_or_n = 0;436 while (_get_flags(pos+num_whitespaces).is_whitespace) {437 uint32_t cpt2 = _get_cpt(pos+num_whitespaces);438 if (cpt2 == '\r' || cpt2 == '\n') {439 last_end_r_or_n = pos + num_whitespaces + 1;440 }441 num_whitespaces++;442 }443 444 // regex: \s*[\r\n]+445 if (last_end_r_or_n > 0) {446 pos = last_end_r_or_n;447 _add_token(pos);448 continue;449 }450 451 // regex: \s+(?!\S)452 if (num_whitespaces > 1 && _get_cpt(pos+num_whitespaces) != OUT_OF_RANGE) {453 pos += num_whitespaces - 1;454 _add_token(pos);455 continue;456 }457 458 // regex: \s+459 if (num_whitespaces > 0) {460 pos += num_whitespaces;461 _add_token(pos);462 continue;463 }464 465 // no matches466 _add_token(++pos);467 }468 }469 470 return bpe_offsets;471}472 473// Qwen2 system regex: "(?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+"474static std::vector<size_t> unicode_regex_split_custom_qwen2(const std::string & text, const std::vector<size_t> & offsets) {475 std::vector<size_t> bpe_offsets; // store the offset of each word476 bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size477 478 const auto cpts = unicode_cpts_from_utf8(text);479 480 size_t start = 0;481 for (auto offset : offsets) {482 const size_t offset_ini = start;483 const size_t offset_end = start + offset;484 assert(offset_end <= cpts.size());485 start = offset_end;486 487 static const uint32_t OUT_OF_RANGE = 0xFFFFFFFF;488 auto _get_cpt = [&] (const size_t pos) -> uint32_t {489 return (offset_ini <= pos && pos < offset_end) ? cpts[pos] : OUT_OF_RANGE;490 };491 492 auto _get_flags = [&] (const size_t pos) -> unicode_cpt_flags {493 return (offset_ini <= pos && pos < offset_end) ? unicode_cpt_flags_from_cpt(cpts[pos]) : unicode_cpt_flags{};494 };495 496 size_t _prev_end = offset_ini;497 auto _add_token = [&] (const size_t end) -> size_t {498 assert(_prev_end <= end && end <= offset_end);499 size_t len = end - _prev_end;500 if (len > 0) {501 bpe_offsets.push_back(len);502 }503 _prev_end = end;504 //if (len > 0) {505 // std::string s = "";506 // for(size_t p = end-len; p < end; p++)507 // s += unicode_cpt_to_utf8(cpts[p]);508 // printf(">>> '%s'\n", s.c_str());509 //}510 return len;511 };512 513 for (size_t pos = offset_ini; pos < offset_end; /*pos++*/ ) {514 const uint32_t cpt = _get_cpt(pos);515 const auto flags = _get_flags(pos);516 517 // regex: (?i:'s|'t|'re|'ve|'m|'ll|'d) // case insensitive518 if (cpt == '\'' && pos+1 < offset_end) {519 uint32_t cpt_next = unicode_tolower(_get_cpt(pos+1));520 if (cpt_next == 's' || cpt_next == 't' || cpt_next == 'm' || cpt_next == 'd') {521 pos += _add_token(pos+2);522 continue;523 }524 if (pos+2 < offset_end) {525 uint32_t cpt_next_next = unicode_tolower(_get_cpt(pos+2));526 if ((cpt_next == 'r' && cpt_next_next == 'e') ||527 (cpt_next == 'v' && cpt_next_next == 'e') ||528 (cpt_next == 'l' && cpt_next_next == 'l')) {529 pos += _add_token(pos+3);530 continue;531 }532 }533 }534 535 // regex: [^\r\n\p{L}\p{N}]?\p{L}+536 if (!(cpt == '\r' || cpt == '\n' || flags.is_number)) {537 if (flags.is_letter || _get_flags(pos+1).is_letter) { // one or more letters538 pos++;539 while (_get_flags(pos).is_letter) {540 pos++;541 }542 _add_token(pos);543 continue;544 }545 }546 547 // regex: \p{N}548 if (flags.is_number) {549 pos++;550 _add_token(pos);551 continue;552 }553 554 // regex: <space>?[^\s\p{L}\p{N}]+[\r\n]*555 auto flags2 = (cpt == ' ' ? _get_flags(pos+1) : flags);556 if (!(flags2.is_whitespace | flags2.is_letter | flags2.is_number) && flags.as_uint()) {557 pos += (cpt == ' ');558 while (!(flags2.is_whitespace | flags2.is_letter | flags2.is_number) && flags2.as_uint()) {559 flags2 = _get_flags(++pos);560 }561 uint32_t cpt2 = _get_cpt(pos);562 while (cpt2 == '\r' || cpt2 == '\n') {563 cpt2 = _get_cpt(++pos);564 }565 _add_token(pos);566 continue;567 }568 569 size_t num_whitespaces = 0;570 size_t last_end_r_or_n = 0;571 while (_get_flags(pos+num_whitespaces).is_whitespace) {572 uint32_t cpt2 = _get_cpt(pos+num_whitespaces);573 if (cpt2 == '\r' || cpt2 == '\n') {574 last_end_r_or_n = pos + num_whitespaces + 1;575 }576 num_whitespaces++;577 }578 579 // regex: \s*[\r\n]+580 if (last_end_r_or_n > 0) {581 pos = last_end_r_or_n;582 _add_token(pos);583 continue;584 }585 586 // regex: \s+(?!\S)587 if (num_whitespaces > 1 && _get_cpt(pos+num_whitespaces) != OUT_OF_RANGE) {588 pos += num_whitespaces - 1;589 _add_token(pos);590 continue;591 }592 593 // regex: \s+594 if (num_whitespaces > 0) {595 pos += num_whitespaces;596 _add_token(pos);597 continue;598 }599 600 // no matches601 _add_token(++pos);602 }603 }604 605 return bpe_offsets;606}607 608// Qwen3.5 system regex: "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\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+"609// Compared to Qwen2, letter-runs also consume Unicode combining marks (\p{M}): [\p{L}\p{M}]+ instead of \p{L}+610static std::vector<size_t> unicode_regex_split_custom_qwen35(const std::string & text, const std::vector<size_t> & offsets) {611 std::vector<size_t> bpe_offsets; // store the offset of each word612 bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size613 614 const auto cpts = unicode_cpts_from_utf8(text);615 616 size_t start = 0;617 for (auto offset : offsets) {618 const size_t offset_ini = start;619 const size_t offset_end = start + offset;620 assert(offset_end <= cpts.size());621 start = offset_end;622 623 static const uint32_t OUT_OF_RANGE = 0xFFFFFFFF;624 auto _get_cpt = [&] (const size_t pos) -> uint32_t {625 return (offset_ini <= pos && pos < offset_end) ? cpts[pos] : OUT_OF_RANGE;626 };627 628 auto _get_flags = [&] (const size_t pos) -> unicode_cpt_flags {629 return (offset_ini <= pos && pos < offset_end) ? unicode_cpt_flags_from_cpt(cpts[pos]) : unicode_cpt_flags{};630 };631 632 size_t _prev_end = offset_ini;633 auto _add_token = [&] (const size_t end) -> size_t {634 assert(_prev_end <= end && end <= offset_end);635 size_t len = end - _prev_end;636 if (len > 0) {637 bpe_offsets.push_back(len);638 }639 _prev_end = end;640 return len;641 };642 643 for (size_t pos = offset_ini; pos < offset_end; /*pos++*/ ) {644 const uint32_t cpt = _get_cpt(pos);645 const auto flags = _get_flags(pos);646 647 // regex: (?i:'s|'t|'re|'ve|'m|'ll|'d) // case insensitive648 if (cpt == '\'' && pos+1 < offset_end) {649 uint32_t cpt_next = unicode_tolower(_get_cpt(pos+1));650 if (cpt_next == 's' || cpt_next == 't' || cpt_next == 'm' || cpt_next == 'd') {651 pos += _add_token(pos+2);652 continue;653 }654 if (pos+2 < offset_end) {655 uint32_t cpt_next_next = unicode_tolower(_get_cpt(pos+2));656 if ((cpt_next == 'r' && cpt_next_next == 'e') ||657 (cpt_next == 'v' && cpt_next_next == 'e') ||658 (cpt_next == 'l' && cpt_next_next == 'l')) {659 pos += _add_token(pos+3);660 continue;661 }662 }663 }664 665 // regex: [^\r\n\p{L}\p{N}]?[\p{L}\p{M}]+666 if (!(cpt == '\r' || cpt == '\n' || flags.is_number)) {667 if (flags.is_letter || flags.is_accent_mark || _get_flags(pos + 1).is_accent_mark || _get_flags(pos+1).is_letter) {668 pos++;669 while (_get_flags(pos).is_letter || _get_flags(pos).is_accent_mark) {670 pos++;671 }672 _add_token(pos);673 continue;674 }675 }676 677 // regex: \p{N}678 if (flags.is_number) {679 pos++;680 _add_token(pos);681 continue;682 }683 684 // regex: <space>?[^\s\p{L}\p{M}\p{N}]+[\r\n]*685 auto flags2 = (cpt == ' ' ? _get_flags(pos+1) : flags);686 if (!(flags2.is_whitespace | flags2.is_letter | flags2.is_accent_mark | flags2.is_number) && flags.as_uint()) {687 pos += (cpt == ' ');688 while (!(flags2.is_whitespace | flags2.is_letter | flags2.is_accent_mark | flags2.is_number) && flags2.as_uint()) {689 flags2 = _get_flags(++pos);690 }691 uint32_t cpt2 = _get_cpt(pos);692 while (cpt2 == '\r' || cpt2 == '\n') {693 cpt2 = _get_cpt(++pos);694 }695 _add_token(pos);696 continue;697 }698 699 size_t num_whitespaces = 0;700 size_t last_end_r_or_n = 0;701 while (_get_flags(pos+num_whitespaces).is_whitespace) {702 uint32_t cpt2 = _get_cpt(pos+num_whitespaces);703 if (cpt2 == '\r' || cpt2 == '\n') {704 last_end_r_or_n = pos + num_whitespaces + 1;705 }706 num_whitespaces++;707 }708 709 // regex: \s*[\r\n]+710 if (last_end_r_or_n > 0) {711 pos = last_end_r_or_n;712 _add_token(pos);713 continue;714 }715 716 // regex: \s+(?!\S)717 if (num_whitespaces > 1 && _get_cpt(pos+num_whitespaces) != OUT_OF_RANGE) {718 pos += num_whitespaces - 1;719 _add_token(pos);720 continue;721 }722 723 // regex: \s+724 if (num_whitespaces > 0) {725 pos += num_whitespaces;726 _add_token(pos);727 continue;728 }729 730 // no matches731 _add_token(++pos);732 }733 }734 735 return bpe_offsets;736}737 738template <typename CharT>739static std::vector<size_t> unicode_regex_split_stl(const std::basic_string<CharT> & text, const std::basic_string<CharT> & regex, const std::vector<size_t> & offsets) {740 using BidirIt = typename std::basic_string<CharT>::const_iterator;741#ifdef _MSC_VER742 // Bypass bug in MSVC: https://github.com/ggml-org/llama.cpp/issues/17830743 constexpr auto regex_flags = std::regex_constants::ECMAScript;744#else745 constexpr auto regex_flags = std::regex_constants::optimize | std::regex_constants::nosubs;746#endif747 std::basic_regex<CharT> expr(regex, regex_flags);748 std::vector<size_t> bpe_offsets; // store the offset of each word749 bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size750 size_t start = 0;751 for (auto offset : offsets) {752 std::regex_iterator<BidirIt> it(text.begin() + start, text.begin() + start + offset, expr);753 std::regex_iterator<BidirIt> end;754 755 int64_t start_idx = 0;756 while (it != end) {757 std::match_results<BidirIt> match = *it;758 if (match.position() > start_idx) {759 bpe_offsets.emplace_back(match.position() - start_idx);760 }761 bpe_offsets.emplace_back(match.length());762 start_idx = match.position() + match.length();763 ++it;764 }765 766 if (start_idx < (int64_t) offset) {767 bpe_offsets.emplace_back(offset - start_idx);768 }769 start += offset;770 }771 772 return bpe_offsets;773}774 775// K2 system regex patterns (from tokenization_kimi.py):776// [\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+777static std::vector<size_t> unicode_regex_split_custom_kimi_k2(const std::string & text, const std::vector<size_t> & offsets) {778 std::vector<size_t> bpe_offsets;779 bpe_offsets.reserve(offsets.size());780 781 const auto cpts = unicode_cpts_from_utf8(text);782 783 size_t start = 0;784 for (auto offset : offsets) {785 const size_t offset_ini = start;786 const size_t offset_end = start + offset;787 assert(offset_end <= cpts.size());788 start = offset_end;789 790 static const uint32_t OUT_OF_RANGE = 0xFFFFFFFF;791 auto _get_cpt = [&] (const size_t pos) -> uint32_t {792 return (offset_ini <= pos && pos < offset_end) ? cpts[pos] : OUT_OF_RANGE;793 };794 795 auto _get_flags = [&] (const size_t pos) -> unicode_cpt_flags {796 return (offset_ini <= pos && pos < offset_end) ? unicode_cpt_flags_from_cpt(cpts[pos]) : unicode_cpt_flags{};797 };798 799 size_t _prev_end = offset_ini;800 auto _add_token = [&] (const size_t end) -> size_t {801 assert(_prev_end <= end && end <= offset_end);802 size_t len = end - _prev_end;803 if (len > 0) {804 bpe_offsets.push_back(len);805 }806 _prev_end = end;807 return len;808 };809 810 for (size_t pos = offset_ini; pos < offset_end; /*pos++*/ ) {811 const uint32_t cpt = _get_cpt(pos);812 const auto flags = _get_flags(pos);813 814 // Pattern 1: [\p{Han}]+ (Chinese characters)815 if (unicode_cpt_is_han(cpt)) {816 while (unicode_cpt_is_han(_get_cpt(pos))) {817 pos++;818 }819 _add_token(pos);820 continue;821 }822 823 // Pattern 2 & 3: Letter words excluding Han characters with optional contractions824 // [^\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}]]+(?:'s|'t|'re|'ve|'m|'ll|'d)?825 // [^\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}]]*(?:'s|'t|'re|'ve|'m|'ll|'d)?826 // Check if current char is a letter OR if current char could be a leading char and next char is a letter827 bool is_letter_pattern = (flags.is_letter && !unicode_cpt_is_han(cpt)) ||828 (!(cpt == '\r' || cpt == '\n' || flags.is_letter || flags.is_number) &&829 _get_flags(pos + 1).is_letter && !unicode_cpt_is_han(_get_cpt(pos + 1)));830 831 if (is_letter_pattern) {832 // Handle optional leading non-letter/non-number character833 bool has_leading_char = false;834 if (!(cpt == '\r' || cpt == '\n' || flags.is_letter || flags.is_number)) {835 has_leading_char = true;836 pos++;837 }838 839 // Match letter sequence (excluding Han characters)840 bool has_letters = false;841 while (_get_flags(pos).is_letter && !unicode_cpt_is_han(_get_cpt(pos))) {842 has_letters = true;843 pos++;844 }845 846 // Only proceed if we found letters (after potentially skipping leading char)847 if (has_letters || (!has_leading_char && _get_flags(pos).is_letter && !unicode_cpt_is_han(_get_cpt(pos)))) {848 if (!has_letters) pos++; // consume the first letter if we didn't already849 850 // Continue consuming letters851 while (_get_flags(pos).is_letter && !unicode_cpt_is_han(_get_cpt(pos))) {852 pos++;853 }854 855 // Check for optional contractions (?:'s|'t|'re|'ve|'m|'ll|'d)856 if (_get_cpt(pos) == '\'' && pos + 1 < offset_end) {857 uint32_t cpt_next = unicode_tolower(_get_cpt(pos + 1));858 if (cpt_next == 's' || cpt_next == 't' || cpt_next == 'm' || cpt_next == 'd') {859 pos += 2;860 } else if (pos + 2 < offset_end) {861 uint32_t cpt_next_next = unicode_tolower(_get_cpt(pos + 2));862 if ((cpt_next == 'r' && cpt_next_next == 'e') ||863 (cpt_next == 'v' && cpt_next_next == 'e') ||864 (cpt_next == 'l' && cpt_next_next == 'l')) {865 pos += 3;866 }867 }868 }869 870 _add_token(pos);871 continue;872 } else if (has_leading_char) {873 // We consumed a leading char but found no letters, backtrack874 pos--;875 }876 }877 878 // Pattern 4: \p{N}{1,3} (numbers 1-3 digits)879 if (flags.is_number) {880 size_t ini = pos;881 while (_get_flags(pos).is_number) {882 if (++pos - ini >= 3) {883 _add_token(pos);884 ini = pos;885 }886 }887 _add_token(pos);888 continue;889 }890 891 // Pattern 5: ?[^\s\p{L}\p{N}]+[\r\n]* (optional space + non-word chars + optional newlines)892 auto flags2 = (cpt == ' ' ? _get_flags(pos + 1) : flags);893 if (!(flags2.is_whitespace || flags2.is_letter || flags2.is_number) && flags2.as_uint()) {894 pos += (cpt == ' ');895 while (!(flags2.is_whitespace || flags2.is_letter || flags2.is_number) && flags2.as_uint()) {896 flags2 = _get_flags(++pos);897 }898 // Match optional [\r\n]*899 uint32_t cpt2 = _get_cpt(pos);900 while (cpt2 == '\r' || cpt2 == '\n') {901 cpt2 = _get_cpt(++pos);902 }903 _add_token(pos);904 continue;905 }906 907 // Count whitespace characters908 size_t num_whitespaces = 0;909 size_t last_end_r_or_n = 0;910 while (_get_flags(pos + num_whitespaces).is_whitespace) {911 uint32_t cpt2 = _get_cpt(pos + num_whitespaces);912 if (cpt2 == '\r' || cpt2 == '\n') {913 last_end_r_or_n = pos + num_whitespaces + 1;914 }915 num_whitespaces++;916 }917 918 // Pattern 6: \s*[\r\n]+ (whitespace with newlines)919 if (last_end_r_or_n > 0) {920 pos = last_end_r_or_n;921 _add_token(pos);922 continue;923 }924 925 // Pattern 7: \s+(?!\S) (trailing whitespace)926 if (num_whitespaces > 1 && _get_cpt(pos + num_whitespaces) != OUT_OF_RANGE) {927 pos += num_whitespaces - 1;928 _add_token(pos);929 continue;930 }931 932 // Pattern 8: \s+ (general whitespace)933 if (num_whitespaces > 0) {934 pos += num_whitespaces;935 _add_token(pos);936 continue;937 }938 939 // No matches - consume single character940 _add_token(++pos);941 }942 }943 944 return bpe_offsets;945}946 947// AFMOE digit handling: splits digits with leading 1-2 based on total length modulo 3948static std::vector<size_t> unicode_regex_split_custom_afmoe(const std::string & text, const std::vector<size_t> & offsets) {949 std::vector<size_t> bpe_offsets;950 bpe_offsets.reserve(offsets.size());951 952 const auto cpts = unicode_cpts_from_utf8(text);953 954 size_t start = 0;955 for (auto offset : offsets) {956 const size_t offset_ini = start;957 const size_t offset_end = start + offset;958 assert(offset_end <= cpts.size());959 start = offset_end;960 961 auto _get_flags = [&] (const size_t pos) -> unicode_cpt_flags {962 return (offset_ini <= pos && pos < offset_end) ? unicode_cpt_flags_from_cpt(cpts[pos]) : unicode_cpt_flags{};963 };964 965 size_t _prev_end = offset_ini;966 auto _add_token = [&] (const size_t end) -> size_t {967 assert(_prev_end <= end && end <= offset_end);968 size_t len = end - _prev_end;969 if (len > 0) {970 bpe_offsets.push_back(len);971 }972 _prev_end = end;973 return len;974 };975 976 for (size_t pos = offset_ini; pos < offset_end; ) {977 const auto flags = _get_flags(pos);978 979 // Handle digit sequences with special splitting logic980 if (flags.is_number) {981 size_t digit_start = pos;982 size_t digit_count = 0;983 984 // Count consecutive digits985 while (_get_flags(pos).is_number && pos < offset_end) {986 digit_count++;987 pos++;988 }989 990 // Split based on total length modulo 3991 size_t remainder = digit_count % 3;992 size_t current = digit_start;993 994 // Emit leading 1-2 digits if needed995 if (remainder > 0) {996 _add_token(current + remainder);997 current += remainder;998 }999 1000 // Emit groups of 31001 while (current < digit_start + digit_count) {1002 _add_token(current + 3);1003 current += 3;1004 }1005 continue;1006 }1007 1008 // For non-digits, just move forward1009 pos++;1010 }1011 1012 // Add any remaining content1013 if (_prev_end < offset_end) {1014 _add_token(offset_end);1015 }1016 }1017 1018 return bpe_offsets;1019}1020 1021// regex: [^\n]+|[\n]+1022// splits text into runs of non-newline characters and runs of newline characters1023static std::vector<size_t> unicode_regex_split_custom_newlines(const std::string & text, const std::vector<size_t> & offsets) {1024 std::vector<size_t> bpe_offsets;1025 bpe_offsets.reserve(offsets.size());1026 1027 const auto cpts = unicode_cpts_from_utf8(text);1028 1029 size_t start = 0;1030 for (auto offset : offsets) {1031 const size_t offset_ini = start;1032 const size_t offset_end = start + offset;1033 assert(offset_end <= cpts.size());1034 start = offset_end;1035 1036 size_t pos = offset_ini;1037 while (pos < offset_end) {1038 const bool is_newline = (cpts[pos] == '\n');1039 const size_t run_start = pos;1040 while (pos < offset_end && (cpts[pos] == '\n') == is_newline) {1041 pos++;1042 }1043 bpe_offsets.push_back(pos - run_start);1044 }1045 }1046 1047 return bpe_offsets;1048}1049 1050static std::vector<size_t> unicode_regex_split_custom(const std::string & text, const std::string & regex_expr, const std::vector<size_t> & offsets) {1051 std::vector<size_t> bpe_offsets;1052 1053 if (regex_expr == "'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)") {1054 bpe_offsets = unicode_regex_split_custom_gpt2(text, offsets);1055 } else if (1056 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+" ||1057 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+") {1058 bpe_offsets = unicode_regex_split_custom_llama3(text, offsets);1059 } else if (1060 regex_expr == "(?:'[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+") {1061 bpe_offsets = unicode_regex_split_custom_qwen2(text, offsets);1062 } else if (1063 regex_expr == "(?:'[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+") {1064 bpe_offsets = unicode_regex_split_custom_qwen35(text, offsets);1065 } else if (regex_expr == "\\p{Han}+") {1066 // K2's first pattern - handle all K2 patterns together1067 bpe_offsets = unicode_regex_split_custom_kimi_k2(text, offsets);1068 } else if (regex_expr == "\\p{AFMoE_digits}") {1069 // AFMOE digit pattern - use custom implementation for proper splitting1070 bpe_offsets = unicode_regex_split_custom_afmoe(text, offsets);1071 } else if (regex_expr == "[^\\n]+|[\\n]+") {1072 bpe_offsets = unicode_regex_split_custom_newlines(text, offsets);1073 } else if (regex_expr == "\\d{1,3}(?=(?:\\d{3})*\\b)") {1074 // tiny_aya digit grouping pattern from tokenizer.json:1075 // {"type": "Split", "pattern": {"Regex": "\\d{1,3}(?=(?:\\d{3})*\\b)"}, "behavior": "Isolated"}1076 // Splits digits into groups of 3 from the right (e.g., 1234567 -> 1, 234, 567)1077 // TODO: Revisit this regex, in case there are any subtle tokenization differences with the original regex.1078 bpe_offsets = unicode_regex_split_custom_afmoe(text, offsets);1079 }1080 1081 return bpe_offsets;1082}1083 1084//1085// interface1086//1087 1088std::string unicode_cpt_to_utf8(uint32_t cpt) {1089 std::string result;1090 1091 if (/* 0x00 <= cpt && */ cpt <= 0x7f) {1092 result.push_back(cpt);1093 return result;1094 }1095 if (0x80 <= cpt && cpt <= 0x7ff) {1096 result.push_back(0xc0 | ((cpt >> 6) & 0x1f));1097 result.push_back(0x80 | (cpt & 0x3f));1098 return result;1099 }1100 if (0x800 <= cpt && cpt <= 0xffff) {1101 result.push_back(0xe0 | ((cpt >> 12) & 0x0f));1102 result.push_back(0x80 | ((cpt >> 6) & 0x3f));1103 result.push_back(0x80 | (cpt & 0x3f));1104 return result;1105 }1106 if (0x10000 <= cpt && cpt <= 0x10ffff) {1107 result.push_back(0xf0 | ((cpt >> 18) & 0x07));1108 result.push_back(0x80 | ((cpt >> 12) & 0x3f));1109 result.push_back(0x80 | ((cpt >> 6) & 0x3f));1110 result.push_back(0x80 | (cpt & 0x3f));1111 return result;1112 }1113 1114 throw std::invalid_argument("invalid codepoint");1115}1116 1117std::vector<uint32_t> unicode_cpts_normalize_nfd(const std::vector<uint32_t> & cpts) {1118 auto comp = [] (const uint32_t cpt, const range_nfd & range) {1119 return cpt < range.first;1120 };1121 std::vector<uint32_t> result(cpts.size());1122 for (size_t i = 0; i < cpts.size(); ++i) {1123 const uint32_t cpt = cpts[i];1124 auto it = std::upper_bound(unicode_ranges_nfd.begin(), unicode_ranges_nfd.end(), cpt, comp) - 1;1125 result[i] = (it->first <= cpt && cpt <= it->last) ? it->nfd : cpt;1126 }1127 return result;1128}1129 1130std::vector<uint32_t> unicode_cpts_from_utf8(const std::string & utf8) {1131 std::vector<uint32_t> result;1132 result.reserve(utf8.size());1133 size_t offset = 0;1134 while (offset < utf8.size()) {1135 try {1136 result.push_back(unicode_cpt_from_utf8(utf8, offset));1137 }1138 catch (const std::invalid_argument & /*ex*/) {1139 // Silently ignore invalid UTF-8 input to avoid leaking the exception beyond llama_tokenize1140 ++offset;1141 result.emplace_back(0xFFFD); // replacement character1142 }1143 }1144 return result;1145}1146 1147unicode_cpt_flags unicode_cpt_flags_from_cpt(const uint32_t cpt) {1148 static const unicode_cpt_flags undef(unicode_cpt_flags::UNDEFINED);1149 static const auto cpt_flags = unicode_cpt_flags_array();1150 return cpt < cpt_flags.size() ? cpt_flags[cpt] : undef;1151}1152 1153unicode_cpt_flags unicode_cpt_flags_from_utf8(const std::string & utf8) {1154 static const unicode_cpt_flags undef(unicode_cpt_flags::UNDEFINED);1155 if (utf8.empty()) {1156 return undef; // undefined1157 }1158 size_t offset = 0;1159 return unicode_cpt_flags_from_cpt(unicode_cpt_from_utf8(utf8, offset));1160}1161 1162std::string unicode_byte_to_utf8(uint8_t byte) {1163 static std::unordered_map<uint8_t, std::string> map = unicode_byte_to_utf8_map();1164 return map.at(byte);1165}1166 1167uint8_t unicode_utf8_to_byte(const std::string & utf8) {1168 static std::unordered_map<std::string, uint8_t> map = unicode_utf8_to_byte_map();1169 return map.at(utf8);1170}1171 1172uint32_t unicode_tolower(uint32_t cpt) {1173 // binary search1174 auto it = std::lower_bound(unicode_map_lowercase.begin(), unicode_map_lowercase.end(), cpt,1175 [](const std::pair<uint32_t, uint32_t> & pair, uint32_t value) {1176 return pair.first < value;1177 });1178 if (it != unicode_map_lowercase.end() && it->first == cpt) {1179 return it->second;1180 }1181 return cpt; // Return the original code point if no lowercase mapping is found1182}1183 1184bool unicode_cpt_is_han(uint32_t cpt) {1185 // Han character ranges (Chinese/CJK characters)1186 // CJK Unified Ideographs (most common)1187 if (cpt >= 0x4E00 && cpt <= 0x9FFF) return true;1188 1189 // CJK Extension A1190 if (cpt >= 0x3400 && cpt <= 0x4DBF) return true;1191 1192 // CJK Extension B1193 if (cpt >= 0x20000 && cpt <= 0x2A6DF) return true;1194 1195 // CJK Extension C1196 if (cpt >= 0x2A700 && cpt <= 0x2B73F) return true;1197 1198 // CJK Extension D1199 if (cpt >= 0x2B740 && cpt <= 0x2B81F) return true;1200 