Skip to content
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

[WIP] using std::array in vocab for additional speed-ups #1263

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion torchtext/csrc/vocab.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <ATen/Parallel.h> // @manual
#include <algorithm>
#include <common.h>
#include <iostream>
#include <stdexcept>
Expand All @@ -8,7 +9,8 @@
namespace torchtext {

Vocab::Vocab(const StringList &tokens, const std::string &unk_token)
: stoi_(MAX_VOCAB_SIZE, -1), unk_token_(std::move(unk_token)) {
: unk_token_(std::move(unk_token)) {
std::fill(stoi_.begin(), stoi_.end(), -1);
for (std::size_t i = 0; i < tokens.size(); i++) {
// tokens should not have any duplicates
auto token_position =
Expand Down
4 changes: 2 additions & 2 deletions torchtext/csrc/vocab.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typedef std::tuple<std::string, std::vector<int64_t>, std::vector<std::string>,
struct Vocab : torch::CustomClassHolder {
static const int32_t MAX_VOCAB_SIZE = 30000000;
int64_t unk_index_;
std::vector<int32_t> stoi_;
std::array<uint32_t, MAX_VOCAB_SIZE> stoi_;
const std::string version_str_ = "0.0.1";
StringList itos_;
std::string unk_token_;
Expand Down Expand Up @@ -44,7 +44,7 @@ struct Vocab : torch::CustomClassHolder {
uint32_t _find(const c10::string_view &w) const {
uint32_t stoi_size = stoi_.size();
uint32_t id = _hash(w) % stoi_size;
while (stoi_[id] != -1 && itos_[stoi_[id]]!= w) {
while (stoi_[id] != -1 && itos_[stoi_[id]] != w) {
id = (id + 1) % stoi_size;
}
return id;
Expand Down