feat: add dump and load to tokenizer
This commit is contained in:
+17
-5
@@ -1,14 +1,26 @@
|
|||||||
#include "tokenizer.hpp"
|
#include "tokenizer.hpp"
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
// static constexpr const char* filepath = "./gpt-training.txt";
|
static constexpr const char* filepath = "./gpt/shakespeare.txt";
|
||||||
tokenizer<259> bpe;
|
|
||||||
bpe.train("abcabcd");
|
std::ifstream file(filepath, std::ios::binary);
|
||||||
auto tokens = bpe.encode("abc");
|
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) {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
#ifndef TOKENIZER_HPP
|
#ifndef TOKENIZER_HPP
|
||||||
#define TOKENIZER_HPP
|
#define TOKENIZER_HPP
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class string_storage {
|
class string_storage {
|
||||||
@@ -247,6 +250,93 @@ public:
|
|||||||
|
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
public:
|
||||||
|
bool dump(const char* filepath) const {
|
||||||
|
std::ofstream file(filepath, std::ios::binary | std::ios::out | std::ios::trunc);
|
||||||
|
if (!file.is_open()) return false;
|
||||||
|
|
||||||
|
dump_u64(file, m_dict.size());
|
||||||
|
for (auto sv : m_dict) {
|
||||||
|
dump_sv(file, sv);
|
||||||
|
}
|
||||||
|
|
||||||
|
dump_u64(file, m_merges.size());
|
||||||
|
for (const auto& [pair, merge] : m_merges) {
|
||||||
|
dump_u32(file, (pair >> 0ULL) & 0xFFFFFFFF);
|
||||||
|
dump_u32(file, (pair >> 32ULL) & 0xFFFFFFFF);
|
||||||
|
dump_u32(file, merge);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
void dump_u64(std::ofstream& file, std::uint64_t value) const {
|
||||||
|
dump(file, value, std::make_index_sequence<sizeof(value)>{});
|
||||||
|
}
|
||||||
|
|
||||||
|
void dump_u32(std::ofstream& file, std::uint32_t value) const {
|
||||||
|
dump(file, value, std::make_index_sequence<sizeof(value)>{});
|
||||||
|
}
|
||||||
|
|
||||||
|
void dump_sv(std::ofstream& file, std::string_view value) const {
|
||||||
|
dump_u32(file, value.size());
|
||||||
|
file.write(value.data(), static_cast<std::streamsize>(value.size()));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t... Is>
|
||||||
|
void dump(std::ofstream& file, auto value, std::index_sequence<Is...>) const {
|
||||||
|
char buf[] = { static_cast<char>((value >> (Is << 3ULL)) & 0xFF)... };
|
||||||
|
file.write(buf, sizeof(buf));
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
bool load(const char* filepath) {
|
||||||
|
std::ifstream file(filepath, std::ios::binary | std::ios::in);
|
||||||
|
if (!file.is_open()) return false;
|
||||||
|
|
||||||
|
m_dict.clear();
|
||||||
|
auto dictSize = load_u64(file);
|
||||||
|
assert(dictSize <= DictionarySize);
|
||||||
|
m_dict.resize(dictSize);
|
||||||
|
for (std::uint64_t i = 0; i < dictSize; ++i) {
|
||||||
|
m_dict[i] = load_sv(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_merges.clear();
|
||||||
|
auto mergesSize = load_u64(file);
|
||||||
|
m_merges.reserve(mergesSize);
|
||||||
|
for (std::uint64_t i = 0; i < mergesSize; ++i) {
|
||||||
|
auto lhs = load_u32(file);
|
||||||
|
auto rhs = load_u32(file);
|
||||||
|
auto merge = load_u32(file);
|
||||||
|
m_merges.insert(lhs | (rhs << 32ULL), merge);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
auto load_u64(std::ifstream& file) const { return load<std::uint64_t>(file); }
|
||||||
|
auto load_u32(std::ifstream& file) const { return load<std::uint32_t>(file); }
|
||||||
|
|
||||||
|
auto load_sv(std::ifstream& file) {
|
||||||
|
auto length = load_u32(file);
|
||||||
|
char buf[length];
|
||||||
|
file.read(buf, length);
|
||||||
|
return m_strings.allocate(buf, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T load(std::ifstream& file) const {
|
||||||
|
return load(file, std::make_index_sequence<sizeof(T)>{});
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, std::size_t... Is>
|
||||||
|
T load(std::ifstream& file, std::index_sequence<Is...>) const {
|
||||||
|
char buf[sizeof(T)];
|
||||||
|
file.read(buf, sizeof(buf));
|
||||||
|
return (((buf[Is] & 0xFFULL) << Is) | ...);
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
std::string_view token_text(std::uint32_t token) const { return m_dict.at(token); }
|
||||||
private:
|
private:
|
||||||
constexpr std::uint64_t extend_char(char ch) const {
|
constexpr std::uint64_t extend_char(char ch) const {
|
||||||
return static_cast<std::uint64_t>(static_cast<std::uint8_t>(ch));
|
return static_cast<std::uint64_t>(static_cast<std::uint8_t>(ch));
|
||||||
|
|||||||
Reference in New Issue
Block a user