From 806652fd86967a38cd5a7191277111df8afbaf3d Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Tue, 27 Oct 2026 19:58:15 +0100 Subject: [PATCH] feat: implement a basic tokenizer --- CMakeLists.txt | 4 +- example/tokenizer.cpp | 14 +++ example/tokenizer.hpp | 261 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 278 insertions(+), 1 deletion(-) create mode 100644 example/tokenizer.cpp create mode 100644 example/tokenizer.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ecdd8b0..57476a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,6 @@ set(CMAKE_CXX_STANDARD 20) add_library(cenet INTERFACE include/cenet/mat.hpp include/cenet/tensor.hpp - include/cenet/input.hpp include/cenet/layer.hpp include/cenet/network.hpp include/cenet/cenet.hpp) @@ -18,3 +17,6 @@ target_link_libraries(xor PRIVATE cenet) add_executable(mnist example/mnist.cpp) target_link_libraries(mnist PRIVATE cenet) + +add_executable(tokenizer example/tokenizer.cpp) +target_link_libraries(tokenizer PRIVATE cenet) diff --git a/example/tokenizer.cpp b/example/tokenizer.cpp new file mode 100644 index 0000000..4e0bc36 --- /dev/null +++ b/example/tokenizer.cpp @@ -0,0 +1,14 @@ +#include "tokenizer.hpp" + +#include + +int main(void) { + // static constexpr const char* filepath = "./gpt-training.txt"; + tokenizer<259> bpe; + bpe.train("abcabcd"); + auto tokens = bpe.encode("abc"); + for (auto tok : tokens) { + std::printf("0x%08X\n", tok); // NOLINT + } + return 0; +} diff --git a/example/tokenizer.hpp b/example/tokenizer.hpp new file mode 100644 index 0000000..0e94f5e --- /dev/null +++ b/example/tokenizer.hpp @@ -0,0 +1,261 @@ +#ifndef TOKENIZER_HPP +#define TOKENIZER_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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(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(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(this + 1); } + }; +private: + region* m_head = nullptr; + region* m_tail = nullptr; +}; + +template +class tokenizer { + static_assert(DictionarySize >= 256); +public: + tokenizer() { + m_dict.reserve(DictionarySize); + for (int i = 0; i < 256; ++i) { + char ch = static_cast(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 tokens; + std::unordered_map freaks{}; + + const auto* it = text.begin(); + for (; it + 1 < text.end(); ++it) { + tokens.push_back(static_cast(*it) & 0xFFULL); + ++freaks[(static_cast(*it) & 0xFFULL) | + (((static_cast(*(it + 1))) & 0xFFULL) << 32ULL)]; + } + if (it < text.end()) tokens.push_back(static_cast(*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 encode(std::string_view source) const { + if (source.empty()) return {}; + + std::vector 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& 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(static_cast(ch)); + } +private: + string_storage m_strings; + + std::vector m_dict; + std::unordered_map m_merges; +}; + +#endif // TOKENIZER_HPP