-
Notifications
You must be signed in to change notification settings - Fork 99
add rwkv world tokenizer #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /*! | ||
| * Copyright (c) 2023 by Contributors daquexian | ||
| * \file rwkv_world_tokenizer.h | ||
| * \brief Implementation of llm chat. | ||
| */ | ||
|
|
||
| #include <unordered_map> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace tokenizers { | ||
| class RWKVWorldToolTokenizer { | ||
| public: | ||
| RWKVWorldToolTokenizer(const std::string &path); | ||
| std::vector<int> encode(std::string_view str) const; | ||
| std::string decode(const std::vector<int> &ids) const; | ||
| std::string decode(int id) const; | ||
|
|
||
| private: | ||
| std::unordered_map<std::string, int> _word2idx; | ||
| std::unordered_map<int, std::string> _idx2word; | ||
| }; | ||
| } // namespace tokenizers | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /*! | ||
| * Copyright (c) 2023 by Contributors | ||
| * \file rwkv_world_tokenizer.cpp | ||
| * \brief Implementation of llm chat. | ||
| */ | ||
| #include <tokenizers_cpp.h> | ||
| #include "rwkv_world_tokenizer.h" | ||
|
|
||
| #include <iostream> | ||
| #include <fstream> | ||
| #include <string_view> | ||
| #include <msgpack.hpp> | ||
|
|
||
| namespace tokenizers { | ||
|
|
||
| RWKVWorldToolTokenizer::RWKVWorldToolTokenizer(const std::string &path) { | ||
| std::ifstream infile; | ||
| infile.open(path, std::ios::binary | std::ios::in); | ||
| infile.seekg(0, std::ios::end); | ||
| int64_t length = infile.tellg(); | ||
| infile.seekg(0, std::ios::beg); | ||
| char *data = new char[length]; | ||
| infile.read(data, length); | ||
| infile.close(); | ||
|
|
||
| auto unpacker = msgpack::unpack(data, length); | ||
| auto obj = unpacker.get(); | ||
| _idx2word = obj.as<std::unordered_map<int, std::string>>(); | ||
| for (auto &pair : _idx2word) { | ||
| _word2idx[pair.second] = pair.first; | ||
| } | ||
| } | ||
|
|
||
| std::vector<int> RWKVWorldToolTokenizer::encode(std::string_view str) const { | ||
| std::vector<int> ids; | ||
| int str_idx = 0; | ||
| int word_len = 1; | ||
| int id = 0; | ||
| while (str_idx < str.size()) { | ||
| if (str_idx + word_len > str.size()) { | ||
| ids.push_back(id); | ||
| break; | ||
| } | ||
| auto substr = str.substr(str_idx, word_len); | ||
| auto it = _word2idx.find(std::string(substr)); | ||
| if (it == _word2idx.end()) { | ||
| ids.push_back(id); | ||
| str_idx += (word_len - 1); | ||
| word_len = 1; | ||
| } else { | ||
| id = it->second; | ||
| word_len++; | ||
| } | ||
| } | ||
| return ids; | ||
| } | ||
|
|
||
| std::string RWKVWorldToolTokenizer::decode(int id) const { | ||
| auto it = _idx2word.find(id); | ||
| if (it == _idx2word.end()) { | ||
| return "<unk>"; | ||
| } else { | ||
| return it->second; | ||
| } | ||
| } | ||
|
|
||
| std::string RWKVWorldToolTokenizer::decode(const std::vector<int> &ids) const { | ||
| std::string str; | ||
| for (auto id : ids) { | ||
| str += decode(id); | ||
| } | ||
| return str; | ||
| } | ||
|
|
||
| RWKVWorldToolTokenizer createRWKVWorldToolTokenizer(const std::string &path) { | ||
| return RWKVWorldToolTokenizer(path); | ||
| } | ||
|
|
||
| class RWKVWorldTokenizer : public Tokenizer { | ||
| public: | ||
| explicit RWKVWorldTokenizer(const std::string& model_blob) : rwkv_world_tokenizer_(model_blob) { | ||
| } | ||
|
|
||
| std::vector<int32_t> Encode(const std::string& text) final { | ||
| return rwkv_world_tokenizer_.encode(text); | ||
| } | ||
|
|
||
| std::string Decode(const std::vector<int32_t>& ids) final { | ||
| return rwkv_world_tokenizer_.decode(ids); | ||
| } | ||
|
|
||
| private: | ||
| // the tokenizer | ||
| RWKVWorldToolTokenizer rwkv_world_tokenizer_; | ||
| }; | ||
|
|
||
| std::unique_ptr<Tokenizer> Tokenizer::FromBlobRwkvWorld(const std::string& model_blob) { | ||
| return std::make_unique<RWKVWorldTokenizer>(model_blob); | ||
| } | ||
|
|
||
| } // namespace tokenizers |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder the differences between
fetch_contentand setting it as a 3rdparty in the repoThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There shouldn't be much difference, whether it's 3rd or FetchContent, both pull repositories online for building. Personally, I prefer this approach.