| 83 | return parts; |
| 84 | } |
| 85 | |
| 86 | std::pair<uint32_t, size_t> read_utf8_codepoint_at(const std::string & text, size_t pos) { |
| 87 | if (pos >= text.size()) { |
| 88 | throw std::runtime_error("invalid utf-8 offset in chatterbox tokenizer"); |
| 89 | } |
| 90 | const unsigned char lead = static_cast<unsigned char>(text[pos]); |
| 91 | if ((lead & 0x80U) == 0) { |
| 92 | return {lead, 1}; |
| 93 | } |
| 94 | if ((lead & 0xE0U) == 0xC0U) { |
| 95 | if (pos + 2 > text.size()) { |
| 96 | throw std::runtime_error("truncated utf-8 text in chatterbox tokenizer"); |
| 97 | } |
| 98 | return {((lead & 0x1FU) << 6U) | (static_cast<unsigned char>(text[pos + 1]) & 0x3FU), 2}; |
| 99 | } |
| 100 | if ((lead & 0xF0U) == 0xE0U) { |
| 101 | if (pos + 3 > text.size()) { |
| 102 | throw std::runtime_error("truncated utf-8 text in chatterbox tokenizer"); |
| 103 | } |
| 104 | return { |
| 105 | ((lead & 0x0FU) << 12U) | |
| 106 | ((static_cast<unsigned char>(text[pos + 1]) & 0x3FU) << 6U) | |
| 107 | (static_cast<unsigned char>(text[pos + 2]) & 0x3FU), |
| 108 | 3}; |
| 109 | } |
| 110 | if ((lead & 0xF8U) == 0xF0U) { |
| 111 | if (pos + 4 > text.size()) { |
| 112 | throw std::runtime_error("truncated utf-8 text in chatterbox tokenizer"); |
| 113 | } |
| 114 | return { |
| 115 | ((lead & 0x07U) << 18U) | |
| 116 | ((static_cast<unsigned char>(text[pos + 1]) & 0x3FU) << 12U) | |
| 117 | ((static_cast<unsigned char>(text[pos + 2]) & 0x3FU) << 6U) | |
| 118 | (static_cast<unsigned char>(text[pos + 3]) & 0x3FU), |
| 119 | 4}; |
| 120 | } |
| 121 | throw std::runtime_error("invalid utf-8 text in chatterbox tokenizer"); |
| 122 | } |
| 123 | |
| 124 | std::vector<int32_t> bpe_encode_segment( |
no test coverage detected