feat: implement a basic tokenizer
This commit is contained in:
@@ -0,0 +1,261 @@
|
||||
#ifndef TOKENIZER_HPP
|
||||
#define TOKENIZER_HPP
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
class string_storage {
|
||||
private:
|
||||
static constexpr std::size_t MIN_CHUNK_CAP = 8ULL * 1024;
|
||||
public:
|
||||
string_storage() = default;
|
||||
|
||||
~string_storage() { free(); }
|
||||
|
||||
string_storage(string_storage&& other) noexcept { *this = std::move(other); }
|
||||
string_storage& operator=(string_storage&& other) noexcept {
|
||||
if (this == &other) return *this;
|
||||
free();
|
||||
m_head = other.m_head;
|
||||
m_tail = other.m_tail;
|
||||
other.m_head = nullptr;
|
||||
other.m_tail = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
string_storage(const string_storage&) = delete;
|
||||
string_storage& operator=(const string_storage&) = delete;
|
||||
public:
|
||||
std::string_view allocate(const char* begin, std::size_t length) {
|
||||
region* reg = m_head;
|
||||
while (reg != nullptr && reg->size > reg->used + length)
|
||||
reg = reg->next;
|
||||
|
||||
if (reg == nullptr) {
|
||||
const std::size_t size = (std::max(MIN_CHUNK_CAP, length) + 3) & ~0x4ULL;
|
||||
|
||||
reg = reinterpret_cast<region*>(std::malloc(sizeof(region) + size)); // NOLINT
|
||||
new (reg) region(size);
|
||||
if (m_head == nullptr) {
|
||||
m_head = m_tail = reg;
|
||||
} else {
|
||||
m_tail = m_tail->next = reg;
|
||||
}
|
||||
}
|
||||
|
||||
char* string = reg->data() + reg->used;
|
||||
std::memcpy(string, begin, length);
|
||||
reg->used += length;
|
||||
return { string, length };
|
||||
}
|
||||
|
||||
std::string_view allocate(std::string_view lhs, std::string_view rhs) {
|
||||
std::size_t length = lhs.size() + rhs.size();
|
||||
|
||||
region* reg = m_head;
|
||||
while (reg != nullptr && reg->size > reg->used + length)
|
||||
reg = reg->next;
|
||||
|
||||
if (reg == nullptr) {
|
||||
const std::size_t size = (std::max(MIN_CHUNK_CAP, length) + 3) & ~0x4ULL;
|
||||
|
||||
reg = reinterpret_cast<region*>(std::malloc(sizeof(region) + size)); // NOLINT
|
||||
new (reg) region(size);
|
||||
if (m_head == nullptr) {
|
||||
m_head = m_tail = reg;
|
||||
} else {
|
||||
m_tail = m_tail->next = reg;
|
||||
}
|
||||
}
|
||||
|
||||
char* string = reg->data() + reg->used;
|
||||
std::memcpy(string, lhs.data(), lhs.size());
|
||||
std::memcpy(string + lhs.size(), rhs.data(), rhs.size());
|
||||
reg->used += length;
|
||||
return { string, length };
|
||||
}
|
||||
private:
|
||||
void free() {
|
||||
while (m_head != nullptr) {
|
||||
region* next = m_head->next;
|
||||
std::free(m_head); // NOLINT
|
||||
m_head = next;
|
||||
}
|
||||
m_tail = m_head = nullptr;
|
||||
}
|
||||
private:
|
||||
struct region {
|
||||
region* next = nullptr;
|
||||
std::size_t size;
|
||||
std::size_t used = 0;
|
||||
|
||||
explicit region(std::size_t size)
|
||||
: size(size) {}
|
||||
|
||||
char* data() { return reinterpret_cast<char*>(this + 1); }
|
||||
};
|
||||
private:
|
||||
region* m_head = nullptr;
|
||||
region* m_tail = nullptr;
|
||||
};
|
||||
|
||||
template <std::uint32_t DictionarySize>
|
||||
class tokenizer {
|
||||
static_assert(DictionarySize >= 256);
|
||||
public:
|
||||
tokenizer() {
|
||||
m_dict.reserve(DictionarySize);
|
||||
for (int i = 0; i < 256; ++i) {
|
||||
char ch = static_cast<char>(i);
|
||||
m_dict.push_back(m_strings.allocate(&ch, 1));
|
||||
}
|
||||
}
|
||||
public:
|
||||
void train(std::string_view text, std::size_t minFreq = 2) {
|
||||
// TODO: split the tokens into groupings so that "abc123" is treated as "abc" and "123"
|
||||
std::vector<std::uint32_t> tokens;
|
||||
std::unordered_map<std::uint64_t, std::size_t> freaks{};
|
||||
|
||||
const auto* it = text.begin();
|
||||
for (; it + 1 < text.end(); ++it) {
|
||||
tokens.push_back(static_cast<std::uint8_t>(*it) & 0xFFULL);
|
||||
++freaks[(static_cast<std::uint8_t>(*it) & 0xFFULL) |
|
||||
(((static_cast<std::uint8_t>(*(it + 1))) & 0xFFULL) << 32ULL)];
|
||||
}
|
||||
if (it < text.end()) tokens.push_back(static_cast<std::uint8_t>(*it) & 0xFFULL);
|
||||
if (tokens.empty()) return;
|
||||
|
||||
while (tokens.size() > 1 && m_dict.size() < DictionarySize) {
|
||||
auto best = freaks.begin();
|
||||
auto it = freaks.begin();
|
||||
++it;
|
||||
while (it != freaks.end()) {
|
||||
if (it->second == 0) {
|
||||
it = freaks.erase(it);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (it->second > best->second || (it->second == best->second && it->first > best->first)) best = it;
|
||||
++it;
|
||||
}
|
||||
if (best->second < minFreq) break;
|
||||
|
||||
std::uint32_t newToken = m_dict.size();
|
||||
m_merges.emplace(best->first, newToken);
|
||||
m_dict.push_back(
|
||||
m_strings.allocate(m_dict[best->first & 0xFFFFFFFF], m_dict[(best->first >> 32ULL) & 0xFFFFFFFF]));
|
||||
|
||||
std::size_t newIdx = 0; // Index pointing to the current element in the "new version" of tokens vector.
|
||||
// Essentially, we just override tokens instead of back buffering or whatever.
|
||||
for (std::size_t i = 0; i < tokens.size();) {
|
||||
if (i + 1 >= tokens.size()) {
|
||||
tokens[newIdx++] = tokens[i++];
|
||||
continue;
|
||||
}
|
||||
|
||||
std::uint64_t pair = (tokens[i] & 0xFFFFFFFF) | ((tokens[i + 1] & 0xFFFFFFFFULL) << 32ULL);
|
||||
if (pair == best->first) {
|
||||
if (i > 0) {
|
||||
pair <<= 32ULL;
|
||||
pair |= (tokens[newIdx - 1] & 0xFFFFFFFF);
|
||||
auto it = freaks.find(pair);
|
||||
assert(it != freaks.end());
|
||||
assert(it != freaks.end() && it->second > 0);
|
||||
--it->second;
|
||||
|
||||
pair &= 0xFFFFFFFF;
|
||||
pair |= (newToken & 0xFFFFFFFFULL) << 32ULL;
|
||||
++freaks[pair];
|
||||
}
|
||||
|
||||
pair = best->first;
|
||||
auto it = freaks.find(pair);
|
||||
assert(it != freaks.end());
|
||||
assert(it->second > 0);
|
||||
--it->second;
|
||||
|
||||
tokens[(++i, newIdx++)] = newToken;
|
||||
|
||||
if (i + 1 < tokens.size()) {
|
||||
pair = tokens[i] | (tokens[i + 1] & 0xFFFFFFFFULL) << 32ULL;
|
||||
auto it = freaks.find(pair);
|
||||
assert(it != freaks.end() && it->second > 0);
|
||||
--it->second;
|
||||
|
||||
pair &= ~0xFFFFFFFFULL;
|
||||
pair |= newToken & 0xFFFFFFFFULL;
|
||||
++freaks[pair];
|
||||
}
|
||||
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
tokens[newIdx++] = tokens[i++];
|
||||
}
|
||||
|
||||
tokens.resize(newIdx);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::uint32_t> encode(std::string_view source) const {
|
||||
if (source.empty()) return {};
|
||||
|
||||
std::vector<std::uint32_t> tokens;
|
||||
const auto* it = source.begin();
|
||||
|
||||
std::uint64_t pair = (extend_char(*it++) << 32);
|
||||
while ((pair & 0xFFFFFFFF00000000) != 0) {
|
||||
pair >>= 32ULL;
|
||||
if (it != source.end()) pair |= (extend_char(*it++) << 32);
|
||||
|
||||
auto jt = m_merges.find(pair);
|
||||
if (jt == m_merges.end()) {
|
||||
tokens.push_back(pair & 0xFFFFFFFF);
|
||||
continue;
|
||||
}
|
||||
|
||||
pair = (jt->second & 0xFFFFFFFFULL) << 32ULL;
|
||||
|
||||
if (!tokens.empty()) {
|
||||
while (!tokens.empty()) {
|
||||
jt = m_merges.find(tokens.back() | pair);
|
||||
if (jt == m_merges.end()) break;
|
||||
pair = (jt->second & 0xFFFFFFFFULL) << 32ULL;
|
||||
tokens.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
std::string decode(const std::vector<std::uint32_t>& tokens) const {
|
||||
if (tokens.empty()) return {};
|
||||
|
||||
std::string string;
|
||||
for (auto tok : tokens) {
|
||||
string.append(m_dict.at(tok));
|
||||
}
|
||||
|
||||
return string;
|
||||
}
|
||||
private:
|
||||
constexpr std::uint64_t extend_char(char ch) const {
|
||||
return static_cast<std::uint64_t>(static_cast<std::uint8_t>(ch));
|
||||
}
|
||||
private:
|
||||
string_storage m_strings;
|
||||
|
||||
std::vector<std::string_view> m_dict;
|
||||
std::unordered_map<std::uint64_t, std::uint32_t> m_merges;
|
||||
};
|
||||
|
||||
#endif // TOKENIZER_HPP
|
||||
Reference in New Issue
Block a user