-
Notifications
You must be signed in to change notification settings - Fork 91
feat: add generative recommendation tokenizer. #317
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
Open
magicheng0816
wants to merge
2
commits into
jd-opensource:main
Choose a base branch
from
magicheng0816:add_rec_tokenizer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+558
−8
Open
Changes from all commits
Commits
Show all changes
2 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
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,109 @@ | ||
| /* Copyright 2025 The xLLM Authors. All Rights Reserved. | ||
| Copyright 2024 The ScaleLLM Authors. All Rights Reserved. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| https://github.com/jd-opensource/xllm/blob/main/LICENSE | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ==============================================================================*/ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <list> | ||
| #include <memory> | ||
| #include <mutex> | ||
| #include <shared_mutex> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| namespace xllm { | ||
| // a singleton mode by version | ||
| template <typename T> | ||
| class VersionSingleton { | ||
| public: | ||
| template <typename... Args> | ||
| static T* GetInstance(const std::string& version, | ||
| bool delete_old_versions = true, | ||
| int reserved_version_size = | ||
| 2, // default retention of the last two versions | ||
| Args&&... args) { | ||
| T* instance = nullptr; | ||
|
|
||
| { | ||
| std::shared_lock<std::shared_mutex> lock(instance_map_mutex_); | ||
| auto it = instance_map_.find(version); | ||
| if (it != instance_map_.end()) { | ||
| instance = it->second.get(); | ||
| } | ||
| } | ||
|
|
||
| if (instance == nullptr) { | ||
| std::unique_lock<std::shared_mutex> lock(instance_map_mutex_); | ||
|
|
||
| auto it = instance_map_.find(version); | ||
| if (it == instance_map_.end()) { | ||
magicheng0816 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| instance = new T(std::forward<Args>(args)...); | ||
| instance_map_[version] = std::unique_ptr<T>(instance); | ||
| instance_version_list_.push_front(version); | ||
| if (delete_old_versions) { | ||
| if (instance_version_list_.size() > reserved_version_size) { | ||
| auto it = instance_version_list_.begin(); | ||
| std::advance(it, reserved_version_size); | ||
| for (; it != instance_version_list_.end(); it++) { | ||
| instance_map_.erase(*it); | ||
| } | ||
| instance_version_list_.resize(reserved_version_size); | ||
| } | ||
| } | ||
| } else { | ||
| instance = it->second.get(); | ||
| } | ||
| } | ||
|
|
||
| return instance; | ||
| } | ||
|
|
||
| static std::vector<std::string> GetVersions() { | ||
| std::lock_guard<std::mutex> lock(instance_map_mutex_); | ||
| std::vector<std::string> versions; | ||
| for (const auto& pair : instance_map_) { | ||
| versions.push_back(pair.first); | ||
| } | ||
| return versions; | ||
| } | ||
|
|
||
| static void DestroyAllInstances() { | ||
| std::lock_guard<std::mutex> lock(instance_map_mutex_); | ||
| instance_map_.clear(); | ||
| instance_version_list_.clear(); | ||
| } | ||
|
|
||
| VersionSingleton(const VersionSingleton&) = delete; | ||
| VersionSingleton& operator=(const VersionSingleton&) = delete; | ||
|
|
||
| private: | ||
| VersionSingleton() = default; | ||
| ~VersionSingleton() = default; | ||
|
|
||
| static std::unordered_map<std::string, std::unique_ptr<T>> instance_map_; | ||
| static std::list<std::string> instance_version_list_; | ||
| static std::shared_mutex instance_map_mutex_; | ||
| }; | ||
|
|
||
| template <typename T> | ||
| std::unordered_map<std::string, std::unique_ptr<T>> | ||
| VersionSingleton<T>::instance_map_; | ||
| template <typename T> | ||
| std::list<std::string> VersionSingleton<T>::instance_version_list_; | ||
| template <typename T> | ||
| std::shared_mutex VersionSingleton<T>::instance_map_mutex_; | ||
|
|
||
| } // namespace xllm | ||
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,138 @@ | ||
| #include "rec_vocab_dict.h" | ||
magicheng0816 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <filesystem> | ||
| #include <fstream> | ||
|
|
||
| #include "common/global_flags.h" | ||
| #include "util/timer.h" | ||
|
|
||
| namespace xllm { | ||
|
|
||
| bool RecVocabDict::initialize(const std::string& vocab_file) { | ||
| if (initialized_) { | ||
| return true; | ||
| } | ||
|
|
||
| Timer timer; | ||
|
|
||
| if (vocab_file.empty()) { | ||
| LOG(ERROR) << "content data file is empty, file: " << vocab_file; | ||
| return false; | ||
| } | ||
| if (!std::filesystem::exists(vocab_file)) { | ||
| LOG(ERROR) << "fail to find content data file: " << vocab_file; | ||
| return false; | ||
| } | ||
| std::ifstream ifs(vocab_file.data(), std::ios::binary | std::ios::ate); | ||
| if (!ifs.is_open()) { | ||
| LOG(ERROR) << "fail to load content data file: " << vocab_file; | ||
| return false; | ||
| } | ||
|
|
||
| const size_t file_size = ifs.tellg(); | ||
| ifs.seekg(0, std::ios::beg); | ||
|
|
||
| // each line of content : 1 * int64_t(item id) + REC_TOKEN_SIZE * | ||
| // int32_t(token id); | ||
| const size_t itemid_size = sizeof(int64_t); | ||
| const size_t tokens_size = REC_TOKEN_SIZE * sizeof(int32_t); | ||
| const size_t line_size = tokens_size + itemid_size; | ||
| const size_t estimated_lines = (file_size + line_size - 1) / line_size; | ||
|
|
||
| // 2 and 4 are only empirical values | ||
| item_to_tokens_map_.reserve(estimated_lines); | ||
| tokens_to_items_map_.reserve(estimated_lines / 2); | ||
| prefix_tokens_to_next_tokens_map_.reserve(estimated_lines / 4); | ||
|
|
||
| int64_t item_id = 0; | ||
| RecTokenTriple tokens; | ||
|
|
||
| while (ifs.read(reinterpret_cast<char*>(&item_id), itemid_size) && | ||
| ifs.read(reinterpret_cast<char*>(tokens.data()), tokens_size)) { | ||
| if (FLAGS_enable_constrained_decoding) { | ||
| for (int i = 0; i < tokens.size(); i++) { | ||
| std::vector<int32_t> prefix_tokens; | ||
|
|
||
| for (int j = 0; j < i; j++) { | ||
| prefix_tokens.emplace_back(tokens[j]); | ||
| } | ||
|
|
||
| prefix_tokens_to_next_tokens_map_[prefix_tokens].insert(tokens[i]); | ||
| } | ||
| } | ||
|
|
||
| item_to_tokens_map_[item_id] = tokens; | ||
|
|
||
| tokens_to_items_map_[tokens].emplace_back(item_id); | ||
| } | ||
|
|
||
| if (ifs.gcount() != 0 && ifs.gcount() != line_size) { | ||
| LOG(ERROR) << "possibly containing incomplete lines : " << vocab_file; | ||
| item_to_tokens_map_.clear(); | ||
| tokens_to_items_map_.clear(); | ||
| prefix_tokens_to_next_tokens_map_.clear(); | ||
| return false; | ||
| } | ||
|
|
||
| initialized_ = true; | ||
| LOG(INFO) << "total line size:" << estimated_lines | ||
| << ",parse tokens to item id map size: " | ||
| << tokens_to_items_map_.size() | ||
| << ", parse item to tokens map size:" << item_to_tokens_map_.size() | ||
| << ", parse prefix tokens to next tokens map size:" | ||
| << prefix_tokens_to_next_tokens_map_.size() | ||
| << ", cost: " << timer.elapsed_seconds() << " seconds"; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool RecVocabDict::get_items_by_tokens(const RecTokenTriple& rec_token_triple, | ||
| std::vector<int64_t>* item_ids) const { | ||
magicheng0816 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| CHECK_EQ(initialized_, true); | ||
| CHECK_NE(item_ids, nullptr); | ||
|
|
||
| auto iter = tokens_to_items_map_.find(rec_token_triple); | ||
| if (iter == tokens_to_items_map_.end()) { | ||
| return false; | ||
| } | ||
|
|
||
| std::copy( | ||
| iter->second.begin(), iter->second.end(), std::back_inserter(*item_ids)); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool RecVocabDict::get_tokens_by_item(int64_t item_id, | ||
| std::vector<int32_t>* token_ids) const { | ||
| CHECK_EQ(initialized_, true); | ||
| CHECK_NE(token_ids, nullptr); | ||
|
|
||
| auto iter = item_to_tokens_map_.find(item_id); | ||
| if (iter == item_to_tokens_map_.end()) { | ||
| return false; | ||
| } | ||
|
|
||
| std::copy( | ||
| iter->second.begin(), iter->second.end(), std::back_inserter(*token_ids)); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| const std::set<int32_t>& RecVocabDict::get_next_tokens_by_prefix_tokens( | ||
| const Slice<int32_t>& prefix_token_ids) const { | ||
| CHECK_EQ(initialized_, true); | ||
| CHECK_LT(prefix_token_ids.size(), REC_TOKEN_SIZE); | ||
|
|
||
| std::vector<int32_t> prefix_tokens_ids_vec = prefix_token_ids; | ||
| auto iter = prefix_tokens_to_next_tokens_map_.find(prefix_tokens_ids_vec); | ||
| if (iter == prefix_tokens_to_next_tokens_map_.end()) { | ||
| static std::set<int32_t> empty_set; | ||
| return empty_set; | ||
| } | ||
|
|
||
| return iter->second; | ||
| } | ||
|
|
||
| } // namespace xllm | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.