MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / bpe_encode_segment

Function bpe_encode_segment

src/models/chatterbox/text_tokenizer.cpp:123–167  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

121 throw std::runtime_error("invalid utf-8 text in chatterbox tokenizer");
122}
123
124std::vector<int32_t> bpe_encode_segment(
125 const ChatterboxEnglishTokenizerModel & tokenizer,
126 const std::string & segment) {
127 auto symbols = split_utf8_codepoints(segment);
128 if (symbols.empty()) {
129 return {};
130 }
131 while (symbols.size() > 1) {
132 int32_t best_rank = std::numeric_limits<int32_t>::max();
133 size_t best_index = symbols.size();
134 for (size_t i = 0; i + 1 < symbols.size(); ++i) {
135 const auto it = tokenizer.merge_ranks.find({symbols[i], symbols[i + 1]});
136 if (it == tokenizer.merge_ranks.end()) {
137 continue;
138 }
139 if (it->second < best_rank) {
140 best_rank = it->second;
141 best_index = i;
142 }
143 }
144 if (best_index >= symbols.size()) {
145 break;
146 }
147 std::vector<std::string> merged;
148 merged.reserve(symbols.size() - 1);
149 for (size_t i = 0; i < symbols.size();) {
150 if (i == best_index) {
151 merged.push_back(symbols[i] + symbols[i + 1]);
152 i += 2;
153 } else {
154 merged.push_back(symbols[i]);
155 ++i;
156 }
157 }
158 symbols = std::move(merged);
159 }
160
161 std::vector<int32_t> ids;
162 ids.reserve(symbols.size());
163 for (const auto & symbol : symbols) {
164 const auto it = tokenizer.vocab.find(symbol);
165 ids.push_back(it != tokenizer.vocab.end() ? it->second : tokenizer.unk_id);
166 }
167 return ids;
168}
169
170bool starts_with_at(const std::string & text, size_t pos, const std::string & token) {

Callers 1

encode_marked_textFunction · 0.85

Calls 6

split_utf8_codepointsFunction · 0.85
maxFunction · 0.85
emptyMethod · 0.45
sizeMethod · 0.45
findMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected