#include <bpe_tokenizer.h>
Definition at line 42 of file bpe_tokenizer.h.
◆ 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.
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(
' ');
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
const std::vector< std::string > & specials()
References a, j, key(), rank_, and microtorch::bpe::specials().
◆ decode()
| std::string microtorch::bpe::Tokenizer::decode |
( |
const std::vector< int > & |
ids | ) |
const |
|
inline |
◆ digit()
◆ encode()
| std::vector< int > microtorch::bpe::Tokenizer::encode |
( |
const std::string & |
text | ) |
const |
|
inline |
Definition at line 55 of file bpe_tokenizer.h.
55 {
60 continue;
61 }
63 }
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)
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;
133 while (ids.size() > 1) {
134 int best = -1,
at = -1,
id = -1;
135 for (
size_t k = 0;
k + 1 < ids.size(); ++
k) {
139 at =
static_cast<int>(
k);
141 }
142 }
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()
◆ letter()
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();
90 return -1;
91 };
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;
111 if (
k ==
i)
k =
i + 1;
112 } else {
115 if (
k ==
i)
k =
i + 1;
116 }
117 out.emplace_back(
b.substr(
i,
k -
i), -1);
119 }
121 }
static bool digit(unsigned char c)
static bool letter(unsigned char c)
static bool space(unsigned char c)
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 |
◆ space()
◆ tokens()
| const std::vector< std::string > & microtorch::bpe::Tokenizer::tokens |
( |
| ) |
const |
|
inline |
◆ rank_
| std::unordered_map<uint64_t, Merge> microtorch::bpe::Tokenizer::rank_ |
|
private |
◆ tokens_
| std::vector<std::string> microtorch::bpe::Tokenizer::tokens_ |
|
private |
The documentation for this class was generated from the following file: