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

#include <bpe_tokenizer.h>

Collaboration diagram for microtorch::bpe::Tokenizer:
Collaboration graph

Classes

struct  Merge
 

Public Member Functions

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

Static Public Member Functions

static bool letter (unsigned char c)
 
static bool digit (unsigned char c)
 
static bool space (unsigned char c)
 
static std::vector< std::pair< std::string, int > > pretokenize (const std::string &b)
 

Private Member Functions

void encode_chunk (const std::string &chunk, std::vector< int > &out) const
 

Static Private Member Functions

static uint64_t key (int a, int b)
 

Private Attributes

std::vector< std::string > tokens_
 
std::unordered_map< uint64_t, Merge > rank_
 

Detailed Description

Definition at line 42 of file bpe_tokenizer.h.

Constructor & Destructor Documentation

◆ Tokenizer()

microtorch::bpe::Tokenizer::Tokenizer ( std::vector< std::string >  tokens,
const std::vector< std::string > &  merges 
)
inline

Definition at line 46 of file bpe_tokenizer.h.

46 : tokens_(std::move(tokens)) {
47 const int base = static_cast<int>(specials().size()) + 256;
48 for (size_t r = 0; r < merges.size(); ++r) {
49 const auto sp = merges[r].find(' ');
50 const int a = std::stoi(merges[r].substr(0, sp)), b = std::stoi(merges[r].substr(sp + 1));
51 rank_[key(a, b)] = {static_cast<int>(r), base + static_cast<int>(r)};
52 }
53 }
static uint64_t key(int a, int b)
std::vector< std::string > tokens_
std::unordered_map< uint64_t, Merge > rank_
const std::vector< std::string > & tokens() const
std::vector< E > a
size_t j
const std::vector< std::string > & specials()

References a, j, key(), rank_, and microtorch::bpe::specials().

Member Function Documentation

◆ decode()

std::string microtorch::bpe::Tokenizer::decode ( const std::vector< int > &  ids) const
inline

Definition at line 67 of file bpe_tokenizer.h.

67 {
68 std::string s;
69 for (int id : ids)
70 if (id >= 0 && static_cast<size_t>(id) < tokens_.size()) s += tokens_[id];
71 return s;
72 }

References j, and tokens_.

◆ digit()

static bool microtorch::bpe::Tokenizer::digit ( unsigned char  c)
inlinestatic

Definition at line 80 of file bpe_tokenizer.h.

80{ return c >= '0' && c <= '9'; }
float c

References c.

Referenced by pretokenize().

◆ encode()

std::vector< int > microtorch::bpe::Tokenizer::encode ( const std::string &  text) const
inline

Definition at line 55 of file bpe_tokenizer.h.

55 {
56 std::vector<int> out;
57 for (const auto& [chunk, special] : pretokenize(text)) {
58 if (special >= 0) {
59 out.push_back(special);
60 continue;
61 }
63 }
64 return out;
65 }
void encode_chunk(const std::string &chunk, std::vector< int > &out) const
static std::vector< std::pair< std::string, int > > pretokenize(const std::string &b)
std::string text
node out

References encode_chunk(), j, out, pretokenize(), and text.

◆ encode_chunk()

void microtorch::bpe::Tokenizer::encode_chunk ( const std::string &  chunk,
std::vector< int > &  out 
) const
inlineprivate

Definition at line 129 of file bpe_tokenizer.h.

129 {
130 const int off = static_cast<int>(specials().size());
131 std::vector<int> ids;
132 for (unsigned char c : chunk) ids.push_back(off + c);
133 while (ids.size() > 1) {
134 int best = -1, at = -1, id = -1;
135 for (size_t k = 0; k + 1 < ids.size(); ++k) {
136 const auto it = rank_.find(key(ids[k], ids[k + 1]));
137 if (it != rank_.end() && (best < 0 || it->second.rank < best)) {
138 best = it->second.rank;
139 at = static_cast<int>(k);
140 id = it->second.id;
141 }
142 }
143 if (best < 0) break;
144 ids[at] = id;
145 ids.erase(ids.begin() + at + 1);
146 }
147 out.insert(out.end(), ids.begin(), ids.end());
148 }

References c, id, j, k, key(), off, out, rank_, and microtorch::bpe::specials().

Referenced by encode().

◆ key()

static uint64_t microtorch::bpe::Tokenizer::key ( int  a,
int  b 
)
inlinestaticprivate

Definition at line 127 of file bpe_tokenizer.h.

127{ return (static_cast<uint64_t>(a) << 32) | static_cast<uint32_t>(b); }

References a, and j.

Referenced by encode_chunk(), and Tokenizer().

◆ letter()

static bool microtorch::bpe::Tokenizer::letter ( unsigned char  c)
inlinestatic

Definition at line 77 of file bpe_tokenizer.h.

77 {
78 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '\'' || c >= 0x80;
79 }

References c.

Referenced by pretokenize().

◆ pretokenize()

static std::vector< std::pair< std::string, int > > microtorch::bpe::Tokenizer::pretokenize ( const std::string &  b)
inlinestatic

Definition at line 84 of file bpe_tokenizer.h.

84 {
85 std::vector<std::pair<std::string, int>> out;
86 const size_t n = b.size();
87 auto special_at = [&](size_t i) -> int {
88 for (size_t k = 1; k < specials().size(); ++k)
89 if (b.compare(i, specials()[k].size(), specials()[k]) == 0) return static_cast<int>(k);
90 return -1;
91 };
92 size_t i = 0;
93 while (i < n) {
94 const int sp = special_at(i);
95 if (sp >= 0) {
96 out.emplace_back(specials()[sp], sp);
97 i += specials()[sp].size();
98 continue;
99 }
100 const unsigned char c = b[i];
101 const size_t j = (c == ' ' && i + 1 < n && letter(b[i + 1])) ? i + 1 : i;
102 size_t k;
103 if (letter(b[j])) {
104 k = j;
105 while (k < n && letter(b[k])) ++k;
106 } else if (digit(c)) {
107 k = i + 1;
108 } else if (space(c)) {
109 k = i;
110 while (k < n && space(b[k]) && !(b[k] == ' ' && k + 1 < n && letter(b[k + 1]))) ++k;
111 if (k == i) k = i + 1;
112 } else {
113 k = i;
114 while (k < n && !(space(b[k]) || letter(b[k]) || digit(b[k]) || special_at(k) >= 0)) ++k;
115 if (k == i) k = i + 1;
116 }
117 out.emplace_back(b.substr(i, k - i), -1);
118 i = k;
119 }
120 return out;
121 }
static bool digit(unsigned char c)
static bool letter(unsigned char c)
static bool space(unsigned char c)
int n

References c, digit(), j, k, letter(), n, out, size(), space(), and microtorch::bpe::specials().

Referenced by encode().

◆ size()

size_t microtorch::bpe::Tokenizer::size ( ) const
inline

Definition at line 74 of file bpe_tokenizer.h.

74{ return tokens_.size(); }

References tokens_.

Referenced by pretokenize().

◆ space()

static bool microtorch::bpe::Tokenizer::space ( unsigned char  c)
inlinestatic

Definition at line 81 of file bpe_tokenizer.h.

81{ return c == ' ' || (c >= '\t' && c <= '\r'); }

References c.

Referenced by pretokenize().

◆ tokens()

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

Definition at line 75 of file bpe_tokenizer.h.

75{ return tokens_; }

References tokens_.

Member Data Documentation

◆ rank_

std::unordered_map<uint64_t, Merge> microtorch::bpe::Tokenizer::rank_
private

Definition at line 151 of file bpe_tokenizer.h.

Referenced by encode_chunk(), and Tokenizer().

◆ tokens_

std::vector<std::string> microtorch::bpe::Tokenizer::tokens_
private

Definition at line 150 of file bpe_tokenizer.h.

Referenced by decode(), size(), and tokens().


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