ember.cpp
0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
chat_gguf.h
Go to the documentation of this file.
1
#pragma once
2
// A GGUF's tokenizer block and template keys as chat_tokenizer.h's ChatVocab, from the metadata
3
// ember's GGUF parser has already read (TinyLlamaSession's load, tools/chat_template_check.cpp).
4
#include <string>
5
#include <variant>
6
7
#include "
bpe_tokenizer.h
"
8
#include "
chat_tokenizer.h
"
9
#include "
gguf_structs.h
"
10
11
namespace
ember
{
12
13
inline
ChatVocab
chat_vocab_from_gguf
(
const
GGUFData
&
gd
,
const
std::string&
path
) {
14
ChatVocab
v
;
15
auto
str
= [&](
const
char
* key) {
16
auto
it
=
gd
.metadata.find(key);
17
const
std::string*
s
=
it
==
gd
.metadata.end() ?
nullptr
: std::get_if<std::string>(&
it
->second);
18
return
s
? *
s
: std::string();
19
};
20
v
.arch =
str
(
"general.architecture"
);
21
v
.chat_template =
str
(
"tokenizer.chat_template"
);
22
v
.declared =
str
(
"paperkiln.chat_template"
);
23
auto
asp
=
gd
.metadata.find(
"tokenizer.ggml.add_space_prefix"
);
24
if
(
asp
!=
gd
.metadata.end())
25
if
(
const
bool
*
b
= std::get_if<bool>(&
asp
->second))
v
.add_space_prefix = *
b
;
26
// Qwen and SentencePiece vocabularies come from the metadata already parsed:
27
// read_gguf_tokenizer reads the whole file into memory (1.3 GB for Qwen2.5-0.5B f16, 1.1 GB for
28
// Gemma-3-270m f32). paperkiln files keep that reader.
29
const
std::string model =
str
(
"tokenizer.ggml.model"
);
30
if
((
str
(
"tokenizer.ggml.pre"
) ==
"qwen2"
|| model ==
"llama"
) && !
gd
.tokenizer_tokens.empty()) {
31
v
.g.model = model;
32
v
.g.pre =
str
(
"tokenizer.ggml.pre"
);
33
v
.g.tokens =
gd
.tokenizer_tokens;
34
v
.g.merges =
gd
.tokenizer_merges;
35
v
.g.token_type.assign(
gd
.tokenizer_token_types.begin(),
gd
.tokenizer_token_types.end());
36
v
.scores =
gd
.tokenizer_scores;
37
}
else
{
38
v
.g =
microtorch::bpe::read_gguf_tokenizer
(
path
);
39
}
40
return
v
;
41
}
42
43
}
// namespace ember
bpe_tokenizer.h
chat_tokenizer.h
gguf_structs.h
Data structures for GGUF (GPT-Generated Unified Format) file format.
v
std::vector< float > v
Definition
graph_ir_expr.cpp:257
j
size_t j
Definition
graph_ir_validate.cpp:222
str
std::string str
Definition
ir_digest.cpp:66
ember
Definition
chat_gguf.h:11
ember::chat_vocab_from_gguf
ChatVocab chat_vocab_from_gguf(const GGUFData &gd, const std::string &path)
Definition
chat_gguf.h:13
microtorch::bpe::read_gguf_tokenizer
GgufTokenizer read_gguf_tokenizer(const std::string &path)
Definition
bpe_tokenizer.h:164
GGUFData
Complete representation of a GGUF file's contents.
Definition
gguf_structs.h:80
ember::ChatVocab
Definition
chat_tokenizer.h:247
chat_gguf.h
Generated by
1.9.8