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 4 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
18 changes: 10 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>

Expand All @@ -32,7 +31,7 @@
static const int EOS_TOKEN_ID = 2;

// determine number of model parts based on the dimension
static const std::map<int, int> LLAMA_N_PARTS = {
static const std::unordered_map<int, int> LLAMA_N_PARTS = {
{ 4096, 1 },
{ 5120, 2 },
{ 6656, 4 },
Expand Down Expand Up @@ -86,7 +85,7 @@ struct llama_model {

//
struct ggml_context * ctx;
std::map<std::string, struct ggml_tensor *> tensors;
std::unordered_map<std::string, struct ggml_tensor *> tensors;
};

// load the model's weights from a file
Expand Down Expand Up @@ -162,6 +161,7 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
// load vocab
{
std::string word;
vocab.id_to_token.resize(model.hparams.n_vocab);
for (int i = 0; i < model.hparams.n_vocab; i++) {
uint32_t len;
fin.read((char *) &len, sizeof(len));
Expand All @@ -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
8 changes: 5 additions & 3 deletions quantize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <cstdio>
#include <cstring>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include <regex>
Expand Down Expand Up @@ -130,6 +129,7 @@ bool llama_model_quantize(const std::string & fname_inp, const std::string & fna
}

std::string word;
vocab.id_to_token.resize(n_vocab);
for (int i = 0; i < n_vocab; i++) {
uint32_t len;
finp.read ((char *) &len, sizeof(len));
Expand All @@ -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
15 changes: 8 additions & 7 deletions utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ void replace(std::string & str, const std::string & needle, const std::string &
}
}

std::map<std::string, int32_t> json_parse(const std::string & fname) {
std::map<std::string, int32_t> result;
std::unordered_map<std::string, int32_t> json_parse(const std::string & fname) {
std::unordered_map<std::string, int32_t> result;

// read file into string
std::string json;
Expand Down Expand Up @@ -402,16 +402,16 @@ struct llama_tokenizer {
return;
}

auto score = vocab_.score.find((*token).second);

if (score == vocab_.score.end()) {
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 = (*score).second;
bigram.score = tok_score.score;
bigram.size = text.size();
work_queue_.push(bigram);
}
Expand Down Expand Up @@ -442,8 +442,9 @@ bool gpt_vocab_init(const std::string & fname, gpt_vocab & vocab) {

vocab.token_to_id = ::json_parse(fname);

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
15 changes: 10 additions & 5 deletions utils.h
Original file line number Diff line number D 67E6 iff line change
Expand Up @@ -3,7 +3,7 @@
#pragma once

#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <random>
#include <thread>
Expand Down Expand Up @@ -52,19 +52,24 @@ 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
Co A3E2 llaborator

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::map<token, id> token_to_id;
std::map<id, token> id_to_token;
std::map<id, float> score;
std::unordered_map<token, id> token_to_id;
std::vector<token_score> id_to_token;
};

void replace(std::string & str, const std::string & needle, const std::string & replacement);

// poor-man's JSON parsing
std::map<std::string, int32_t> json_parse(const std::string & fname);
std::unordered_map<std::string, int32_t> json_parse(const std::string & fname);

// split text into tokens
//
Expand Down
0