Thanks to visit codestin.com
Credit goes to github.com

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

Conversation

Fabio3rs
Copy link
Contributor

@Fabio3rs Fabio3rs commented Mar 19, 2023

If it is not necessary sorted maps, change std::map to std::unordered_map

std::unordered_map is a hash table so it should be faster than std::map when storing many items.

std::map<id, token> can be a std::vector since the vector index can be equal to the token id

@Fabio3rs Fabio3rs changed the title If it is not necessary sorted maps, change std::map to std::unordered_map Use unordered_map over std::map Mar 19, 2023
@Fabio3rs Fabio3rs changed the title Use unordered_map over std::map Use std::unordered_map over std::map Mar 19, 2023
@eiz
Copy link
Contributor

eiz commented Mar 20, 2023

the token vector should prob be a struct now which also includes the score (see 074bea2)

@Fabio3rs Fabio3rs changed the title Use std::unordered_map over std::map We could use std::unordered_map over std::map Mar 20, 2023
@Fabio3rs
Copy link
Contributor Author

the token vector should prob be a struct now which also includes the score (see 074bea2)

I did a commit merging with the new changes using struct, I am not sure about the names or the organization of the struct

@gjmulder gjmulder added the enhancement New feature or request label Mar 20, 2023
@z11h
Copy link

z11h commented Mar 20, 2023

Are you able to measure the performance gains from these changes? Interested to see how much of an impact they have.

Great work!

@Fabio3rs
Copy link
Contributor Author

Fabio3rs commented Mar 21, 2023

Are you able to measure the performance gains from these changes? Interested to see how much of an impact they have.

Great work!

Thanks!

Sadly it's hard to do consistent tests, but what I got is:
last commit (bd4b46d) result with: ./build/llama -m models/7B/ggml-model-f16.bin --color -p 'tdd is' --seed 0

main: mem per token = 14696644 bytes
main:     load time =  3236.45 ms
main:   sample time =    57.25 ms
main:  predict time = 37547.44 ms / 286.62 ms per token
main:    total time = 41166.49 ms

last commit (bd4b46d) + this pull request: (same command above)

main: mem per token = 14696644 bytes
main:     load time =  3204.60 ms
main:   sample time =    57.64 ms
main:  predict time = 37372.86 ms / 285.29 ms per token
main:    total time = 40956.10 ms

This differences should be more noticeable with larger datasets and more tokens, I am using the model 7B.

@Green-Sky
Copy link
Collaborator

Are you able to measure the performance gains from these changes? Interested to see how much of an impact they have.

there should not really be any. none of the code is particularly hot.

utils.h Outdated
@@ -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
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.

@Fabio3rs Fabio3rs requested a review from Green-Sky March 21, 2023 12:36
@ggerganov
Copy link
Member

Apologies for the conflicts - lets resolve and merge.

Regarding the C++ standard question from the other thread:
There are a few reasons and I know there will be people that will disagree and I totally understand. I just think this way it is less incentivising to use overly-complicated constructs. You are correct that in this case we can actually gain some performance with std::string_view and even save unnecessary allocations, but this part of the code is totally negligible compared to the full transformer evaluation. Plus, we can do the suggested change using raw pointers - yes, bit more ugly, but the performance will be there.

Overall, my experience tells me this is the better way - or at least it is better in my views and understandings. And if there ever appears a very good reason to bump the standard - we will do it. But at the moment, there is no good enough reason to do it.

@Fabio3rs
Copy link
Contributor Author

Apologies for the conflicts - lets resolve and merge.

Regarding the C++ standard question from the other thread: There are a few reasons and I know there will be people that will disagree and I totally understand. I just think this way it is less incentivising to use overly-complicated constructs. You are correct that in this case we can actually gain some performance with std::string_view and even save unnecessary allocations, but this part of the code is totally negligible compared to the full transformer evaluation. Plus, we can do the suggested change using raw pointers - yes, bit more ugly, but the performance will be there.

Overall, my experience tells me this is the better way - or at least it is better in my views and understandings. And if there ever appears a very good reason to bump the standard - we will do it. But at the moment, there is no good enough reason to do it.

Thanks!

I think I resolved the conflicts, if there are some problems I am happy to fix.

@ggerganov ggerganov merged commit 353ec25 into ggml-org:master Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants