ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
ember::QwenNfcTokenizer Class Reference

#include <chat_tokenizer.h>

Collaboration diagram for ember::QwenNfcTokenizer:
Collaboration graph

Public Member Functions

 QwenNfcTokenizer (std::vector< std::string > tokens, const std::vector< std::string > &merges, const std::vector< int32_t > &token_type)
 
std::vector< int > encode (const std::string &text) const
 
std::string decode (const std::vector< int > &ids) const
 
size_t size () const
 

Private Attributes

std::unique_ptr< microtorch::llama3::Tokenizer > t_
 
std::vector< int > specials_
 
std::vector< std::string > texts_
 

Detailed Description

Definition at line 165 of file chat_tokenizer.h.

Constructor & Destructor Documentation

◆ QwenNfcTokenizer()

ember::QwenNfcTokenizer::QwenNfcTokenizer ( std::vector< std::string >  tokens,
const std::vector< std::string > &  merges,
const std::vector< int32_t > &  token_type 
)
inline

Definition at line 167 of file chat_tokenizer.h.

168 {
169 for (size_t i = 0; i < token_type.size() && i < tokens.size(); ++i) {
170 const int t = token_type[i];
171 if (t != 2 && t != 3 && t != 4) continue;
172 for (const char* c = nfc::tables::kNfcAsciiImages; *c; ++c)
173 if (tokens[i].find(*c) != std::string::npos)
174 throw std::runtime_error("Qwen vocabulary: added token '" + tokens[i] + "' holds '" +
175 std::string(1, *c) +
176 "', which NFC writes for another character; ember's NFC cut "
177 "would differ from HF's there");
178 specials_.push_back(static_cast<int>(i));
179 texts_.push_back(tokens[i]);
180 }
181 std::vector<size_t> order(specials_.size());
182 for (size_t k = 0; k < order.size(); ++k) order[k] = k;
183 std::stable_sort(order.begin(), order.end(),
184 [&](size_t a, size_t b) { return texts_[a].size() > texts_[b].size(); });
185 std::vector<int> s;
186 std::vector<std::string> t;
187 for (size_t k : order) {
188 s.push_back(specials_[k]);
189 t.push_back(texts_[k]);
190 }
191 specials_ = std::move(s);
192 texts_ = std::move(t);
193 t_ = std::make_unique<microtorch::llama3::Tokenizer>(std::move(tokens), merges, token_type, 1);
194 }
std::unique_ptr< microtorch::llama3::Tokenizer > t_
std::vector< std::string > texts_
std::vector< int > specials_
float c
std::vector< E > a
size_t j
constexpr char kNfcAsciiImages[]

References a, c, j, k, ember::nfc::tables::kNfcAsciiImages, specials_, t_, and texts_.

Member Function Documentation

◆ decode()

std::string ember::QwenNfcTokenizer::decode ( const std::vector< int > &  ids) const
inline

Definition at line 229 of file chat_tokenizer.h.

229{ return t_->decode(ids); }

References t_.

◆ encode()

std::vector< int > ember::QwenNfcTokenizer::encode ( const std::string &  text) const
inline

Definition at line 196 of file chat_tokenizer.h.

196 {
197 if (nfc::nfc_utf8(text) == text) return t_->encode(text); // already NFC: the port's ids
198 std::vector<std::pair<std::string, int>> frags = {{text, -1}};
199 for (size_t k = 0; k < specials_.size(); ++k) {
200 const std::string& s = texts_[k];
201 if (s.empty()) continue;
202 std::vector<std::pair<std::string, int>> next;
203 for (auto& [t, id] : frags) {
204 if (id >= 0) {
205 next.emplace_back(std::move(t), id);
206 continue;
207 }
208 size_t at = 0;
209 for (size_t m; (m = t.find(s, at)) != std::string::npos; at = m + s.size()) {
210 if (m > at) next.emplace_back(t.substr(at, m - at), -1);
211 next.emplace_back(std::string(), specials_[k]);
212 }
213 if (at < t.size()) next.emplace_back(t.substr(at), -1);
214 }
215 frags = std::move(next);
216 }
217 std::vector<int> out;
218 for (const auto& [t, id] : frags) {
219 if (id >= 0) {
220 out.push_back(id);
221 continue;
222 }
223 const std::vector<int> ids = t_->encode(nfc::nfc_utf8(t));
224 out.insert(out.end(), ids.begin(), ids.end());
225 }
226 return out;
227 }
std::string text
node out
std::string nfc_utf8(const std::string &text)

References id, j, k, ember::nfc::nfc_utf8(), out, specials_, t_, text, and texts_.

◆ size()

size_t ember::QwenNfcTokenizer::size ( ) const
inline

Definition at line 230 of file chat_tokenizer.h.

230{ return t_->size(); }

References t_.

Member Data Documentation

◆ specials_

std::vector<int> ember::QwenNfcTokenizer::specials_
private

Definition at line 234 of file chat_tokenizer.h.

Referenced by encode(), and QwenNfcTokenizer().

◆ t_

std::unique_ptr<microtorch::llama3::Tokenizer> ember::QwenNfcTokenizer::t_
private

Definition at line 233 of file chat_tokenizer.h.

Referenced by decode(), encode(), QwenNfcTokenizer(), and size().

◆ texts_

std::vector<std::string> ember::QwenNfcTokenizer::texts_
private

Definition at line 235 of file chat_tokenizer.h.

Referenced by encode(), and QwenNfcTokenizer().


The documentation for this class was generated from the following file: