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

Classes

struct  GgufTokenizer
 
class  Tokenizer
 

Functions

const std::vector< std::string > & specials ()
 
GgufTokenizer read_gguf_tokenizer (const std::string &path)
 
Tokenizer load_gguf (const std::string &path)
 

Function Documentation

◆ load_gguf()

Tokenizer microtorch::bpe::load_gguf ( const std::string &  path)
inline

Definition at line 229 of file bpe_tokenizer.h.

229 {
231 if (g.model != "bpe") throw std::runtime_error(path + ": tokenizer.ggml.model is '" + g.model + "', not 'bpe'");
232 return Tokenizer(std::move(g.tokens), g.merges);
233}
size_t j
GgufTokenizer read_gguf_tokenizer(const std::string &path)
std::vector< std::string > merges
std::vector< std::string > tokens

References j, microtorch::bpe::GgufTokenizer::merges, microtorch::bpe::GgufTokenizer::model, read_gguf_tokenizer(), and microtorch::bpe::GgufTokenizer::tokens.

◆ read_gguf_tokenizer()

GgufTokenizer microtorch::bpe::read_gguf_tokenizer ( const std::string &  path)
inline

Definition at line 164 of file bpe_tokenizer.h.

164 {
165 std::ifstream f(path, std::ios::binary);
166 if (!f) throw std::runtime_error("cannot open " + path);
167 std::string buf((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
168 size_t p = 0;
169 auto need = [&](size_t n) {
170 if (p + n > buf.size()) throw std::runtime_error(path + ": truncated GGUF");
171 };
172 auto u32 = [&]() { need(4); uint32_t v; std::memcpy(&v, buf.data() + p, 4); p += 4; return v; };
173 auto u64 = [&]() { need(8); uint64_t v; std::memcpy(&v, buf.data() + p, 8); p += 8; return v; };
174 auto str = [&]() { const uint64_t n = u64(); need(n); std::string s = buf.substr(p, n); p += n; return s; };
175 if (u32() != 0x46554747u) throw std::runtime_error(path + ": not GGUF");
176 u32();
177 u64();
178 const uint64_t nkv = u64();
179 std::vector<std::string> tokens, merges;
180 std::string model, pre;
181 std::vector<int32_t> token_type;
182 std::function<void(uint32_t)> skip = [&](uint32_t t) {
183 static const size_t sz[] = {1, 1, 2, 2, 4, 4, 4, 1, 0, 0, 8, 8, 8};
184 if (t == 8) {
185 str();
186 } else if (t == 9) {
187 const uint32_t et = u32();
188 const uint64_t n = u64();
189 for (uint64_t i = 0; i < n; ++i) skip(et);
190 } else if (t < 13) {
191 need(sz[t]);
192 p += sz[t];
193 } else {
194 throw std::runtime_error(path + ": unknown GGUF type");
195 }
196 };
197 for (uint64_t i = 0; i < nkv; ++i) {
198 const std::string k = str();
199 const uint32_t t = u32();
200 if ((k == "tokenizer.ggml.tokens" || k == "tokenizer.ggml.merges") && t == 9) {
201 const uint32_t et = u32();
202 const uint64_t n = u64();
203 if (et != 8) throw std::runtime_error(path + ": " + k + " is not a string array");
204 auto& dst = k == "tokenizer.ggml.tokens" ? tokens : merges;
205 for (uint64_t j = 0; j < n; ++j) dst.push_back(str());
206 } else if (k == "tokenizer.ggml.token_type" && t == 9) {
207 const uint32_t et = u32();
208 const uint64_t n = u64();
209 for (uint64_t j = 0; j < n; ++j) { // another element type is skipped, not an error
210 if (et == 5)
211 token_type.push_back(static_cast<int32_t>(u32()));
212 else
213 skip(et);
214 }
215 } else if (k == "tokenizer.ggml.model" && t == 8) {
216 model = str();
217 } else if (k == "tokenizer.ggml.pre" && t == 8) {
218 pre = str();
219 } else {
220 skip(t);
221 }
222 }
223 return {std::move(model), std::move(tokens), std::move(merges), std::move(pre), std::move(token_type)};
224}
const float * p[kIrDepthSourcesMax]
int n
std::vector< float > v
std::string str
Definition ir_digest.cpp:66

References j, k, n, p, str, and v.

Referenced by ember::chat_vocab_from_gguf(), load_gguf(), microtorch::llama3::load_gguf(), and tinyllama::TinyLlamaSession::TinyLlamaSession().

◆ specials()

const std::vector< std::string > & microtorch::bpe::specials ( )
inline

Definition at line 37 of file bpe_tokenizer.h.

37 {
38 static const std::vector<std::string> s = {"<unk>", "<|endoftext|>", "<|user|>", "<|assistant|>"};
39 return s;
40}

References j.

Referenced by microtorch::bpe::Tokenizer::encode_chunk(), microtorch::bpe::Tokenizer::pretokenize(), and microtorch::bpe::Tokenizer::Tokenizer().