feat: add dump and load to tokenizer

This commit is contained in:
2026-10-27 22:50:59 +01:00
parent 806652fd86
commit 8da41abbca
2 changed files with 107 additions and 5 deletions
+17 -5
View File
@@ -1,14 +1,26 @@
#include "tokenizer.hpp"
#include <cstdio>
#include <fstream>
#include <iostream>
int main(void) {
// static constexpr const char* filepath = "./gpt-training.txt";
tokenizer<259> bpe;
bpe.train("abcabcd");
auto tokens = bpe.encode("abc");
static constexpr const char* filepath = "./gpt/shakespeare.txt";
std::ifstream file(filepath, std::ios::binary);
if (!file.is_open()) return 1;
std::string contents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
tokenizer<2256> bpe;
bpe.train(contents);
bpe.dump("./gpt/shakespeare.bpe");
auto tokens = bpe.encode("Hello, World!");
for (auto tok : tokens) {
std::printf("0x%08X\n", tok); // NOLINT
std::printf("0x%08X: ", tok); // NOLINT
std::cout << bpe.token_text(tok) << '\n';
}
std::cout << bpe.decode(tokens) << '\n';
return 0;
}