8000 We could use std::unordered_map over std::map by Fabio3rs · Pull Request #305 · ggml-org/llama.cpp · GitHub
[go: up one dir, main page]

Skip to content

We could use std::unordered_map over std::map #305

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 7 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
F 8000 ile 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
Prev Previous commit
Next Next commit
Merge unordered_map/vector changes with trunk updates
  • Loading branch information
Fabio3rs committed Mar 20, 2023
commit ef792ae8bdae3d1f7e2dc04b691676b8bbd099eb
12 changes: 7 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
fin.read((char *) &score, sizeof(score));

vocab.token_to_id[word] = i;
vocab.id_to_token[i] = word;
vocab.score[i] = score;

auto &tok_score = vocab.id_to_token[i];
tok_score.token = word;
tok_score.score = score;

//if (i < 30000) {
// fprintf(stderr, "%s: vocab[%d] = '%s'\n", __func__, i, word.c_str());
Expand Down Expand Up @@ -894,7 +896,7 @@ int main(int argc, char ** argv) {
fprintf(stderr, "%s: prompt: '%s'\n", __func__, params.prompt.c_str());
fprintf(stderr, "%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
for (int i = 0; i < (int) embd_inp.size(); i++) {
fprintf(stderr, "%6d -> '%s'\n", embd_inp[i], vocab.id_to_token.at(embd_inp[i]).c_str());
fprintf(stderr, "%6d -> '%s'\n", embd_inp[i], vocab.id_to_token.at(embd_inp[i]).token.c_str());
}
fprintf(stderr, "\n");
if (params.interactive) {
Expand All @@ -916,7 +918,7 @@ int main(int argc, char ** argv) {
fprintf(stderr, "%s: reverse prompt: '%s'\n", __func__, params.antiprompt.at(apindex).c_str());
fprintf(stderr, "%s: number of tokens in reverse prompt = %zu\n", __func__, antiprompt_inp.size());
for (int i = 0; i < (int) antiprompt_inp.size(); i++) {
fprintf(stderr, "%6d -> '%s'\n", antiprompt_inp[i], vocab.id_to_token.at(antiprompt_inp[i]).c_str());
fprintf(stderr, "%6d -> '%s'\n", antiprompt_inp[i], vocab.id_to_token.at(antiprompt_inp[i]).token.c_str());
}
fprintf(stderr, "\n");
}
Expand Down Expand Up @@ -1022,7 +1024,7 @@ int main(int argc, char ** argv) {
// display text
if (!input_noecho) {
for (auto id : embd) {
printf("%s", vocab.id_to_token[id].c_str());
printf("%s", vocab.id_to_token[id].token.c_str());
}
fflush(stdout);
}
Expand Down
8000
6 changes: 4 additions & 2 deletions quantize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ bool llama_model_quantize(const std::string & fname_inp, const std::string & fna
fout.write((char *) &score, sizeof(score));

vocab.token_to_id[word] = i;
vocab.id_to_token[i] = word;
vocab.score[i] = score;

auto &tok_score = vocab.id_to_token[i];
tok_score.token = word;
tok_score.score = score;
}
}

Expand Down
38 changes: 34 additions & 4 deletions utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,39 @@ struct llama_tokenizer {
if (left == -1 || right == -1) {
return;
}
res.push_back(token_id);
const auto &token = vocab.id_to_token.at(token_id);
i -= token.length();

std::string_view text(symbols_[left].text.data(), symbols_[left].text.size() + symbols_[right].text.size());
auto token = vocab_.token_to_id.find(std::string(text));

if (token == vocab_.token_to_id.end()) {
return;
}

if (static_cast<size_t>((*token).second) >= vocab_.id_to_token.size()) {
return;
}

const auto &tok_score = vocab_.id_to_token[(*token).second];

llama_sp_bigram bigram;
bigram.left = left;
bigram.right = right;
bigram.score = tok_score.score;
bigram.size = text.size();
work_queue_.push(bigram);
}

const gpt_vocab & vocab_;
std::vector<llama_sp_symbol> symbols_;
llama_sp_bigram::queue work_queue_;
};

std::vector<gpt_vocab::id> llama_tokenize(const gpt_vocab & vocab, std::string_view text, bool bos) {
llama_tokenizer tokenizer(vocab);
std::vector<gpt_vocab::id> output;

if (text.size() == 0) {
return output;
}

if (bos) {
Expand All @@ -414,7 +444,7 @@ bool gpt_vocab_init(const std::string & fname, gpt_vocab & vocab) {

vocab.id_to_token.resize(vocab.token_to_id.size());
for (const auto & kv : vocab.token_to_id) {
vocab.id_to_token[kv.second] = kv.first;
vocab.id_to_token[kv.second].token = kv.first;
}

printf("%s: vocab size = %d\n", __func__, (int) vocab.token_to_id.size());
Expand Down
8 changes: 7 additions & 1 deletion utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,18 @@ std::string gpt_random_prompt(std::mt19937 & rng);
// Vocab utils
//

struct token_score {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is confusingly named, same with token_t. the type is only used inside gpt_vocab, so why not nest it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also gpt_vocab is token_t already in this case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

What name can I give this struct?
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, first thought was token_t, but that is too close to token, so, just leave it as token_score.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This compiler version seems to not accept token token;

/home/runner/work/llama.cpp/llama.cpp/utils.h:68:15: error: declaration of ‘gpt_vocab::token gpt_vocab::token_score::token’ changes meaning of ‘token’ [-fpermissive]
   68 |         token token;
      |               ^~~~~
/home/runner/work/llama.cpp/llama.cpp/utils.h:65:11: note: ‘token’ declared here as ‘using token = std::string’
   65 |     using token = std::string;

Should I rename the using token = std::string; to token_t?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The quickest and simplest fix to that would be to just rename the data member to tok.

using token_t = std::string;
token_t token;
float score;
};

struct gpt_vocab {
using id = int32_t;
using token = std::string;

std::unordered_map<token, id> token_to_id;
std::vector<token> id_to_token;
std::vector<token_score> id_to_token;
};

void replace(std::string & str, const std::string & needle, const std::string & replacement);
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0