|
| 1 | +/*! |
| 2 | + * Copyright (c) 2023 by Contributors |
| 3 | + * \file rwkv_world_tokenizer.cpp |
| 4 | + * \brief Implementation of llm chat. |
| 5 | + */ |
| 6 | +#include <tokenizers_cpp.h> |
| 7 | +#include "rwkv_world_tokenizer.h" |
| 8 | + |
| 9 | +#include <iostream> |
| 10 | +#include <fstream> |
| 11 | +#include <string_view> |
| 12 | +#include <msgpack.hpp> |
| 13 | + |
| 14 | +namespace tokenizers { |
| 15 | + |
| 16 | +RWKVWorldToolTokenizer::RWKVWorldToolTokenizer(const std::string &path) { |
| 17 | + std::ifstream infile; |
| 18 | + infile.open(path, std::ios::binary | std::ios::in); |
| 19 | + infile.seekg(0, std::ios::end); |
| 20 | + int64_t length = infile.tellg(); |
| 21 | + infile.seekg(0, std::ios::beg); |
| 22 | + char *data = new char[length]; |
| 23 | + infile.read(data, length); |
| 24 | + infile.close(); |
| 25 | + |
| 26 | + auto unpacker = msgpack::unpack(data, length); |
| 27 | + auto obj = unpacker.get(); |
| 28 | + _idx2word = obj.as<std::unordered_map<int, std::string>>(); |
| 29 | + for (auto &pair : _idx2word) { |
| 30 | + _word2idx[pair.second] = pair.first; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +std::vector<int> RWKVWorldToolTokenizer::encode(std::string_view str) const { |
| 35 | + std::vector<int> ids; |
| 36 | + int str_idx = 0; |
| 37 | + int word_len = 1; |
| 38 | + int id = 0; |
| 39 | + while (str_idx < str.size()) { |
| 40 | + if (str_idx + word_len > str.size()) { |
| 41 | + ids.push_back(id); |
| 42 | + break; |
| 43 | + } |
| 44 | + auto substr = str.substr(str_idx, word_len); |
| 45 | + auto it = _word2idx.find(std::string(substr)); |
| 46 | + if (it == _word2idx.end()) { |
| 47 | + ids.push_back(id); |
| 48 | + str_idx += (word_len - 1); |
| 49 | + word_len = 1; |
| 50 | + } else { |
| 51 | + id = it->second; |
| 52 | + word_len++; |
| 53 | + } |
| 54 | + } |
| 55 | + return ids; |
| 56 | +} |
| 57 | + |
| 58 | +std::string RWKVWorldToolTokenizer::decode(int id) const { |
| 59 | + auto it = _idx2word.find(id); |
| 60 | + if (it == _idx2word.end()) { |
| 61 | + return "<unk>"; |
| 62 | + } else { |
| 63 | + return it->second; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +std::string RWKVWorldToolTokenizer::decode(const std::vector<int> &ids) const { |
| 68 | + std::string str; |
| 69 | + for (auto id : ids) { |
| 70 | + str += decode(id); |
| 71 | + } |
| 72 | + return str; |
| 73 | +} |
| 74 | + |
| 75 | +RWKVWorldToolTokenizer createRWKVWorldToolTokenizer(const std::string &path) { |
| 76 | + return RWKVWorldToolTokenizer(path); |
| 77 | +} |
| 78 | + |
| 79 | +class RWKVWorldTokenizer : public Tokenizer { |
| 80 | + public: |
| 81 | + explicit RWKVWorldTokenizer(const std::string& model_blob) : rwkv_world_tokenizer_(model_blob) { |
| 82 | + } |
| 83 | + |
| 84 | + std::vector<int32_t> Encode(const std::string& text) final { |
| 85 | + return rwkv_world_tokenizer_.encode(text); |
| 86 | + } |
| 87 | + |
| 88 | + std::string Decode(const std::vector<int32_t>& ids) final { |
| 89 | + return rwkv_world_tokenizer_.decode(ids); |
| 90 | + } |
| 91 | + |
| 92 | + private: |
| 93 | + // the tokenizer |
| 94 | + RWKVWorldToolTokenizer rwkv_world_tokenizer_; |
| 95 | +}; |
| 96 | + |
| 97 | +std::unique_ptr<Tokenizer> Tokenizer::FromBlobRWKVWorld(const std::string& model_blob) { |
| 98 | + return std::make_unique<RWKVWorldTokenizer>(model_blob); |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace tokenizers |
0 commit comments