-
Notifications
You must be signed in to change notification settings - Fork 20.7k
spec : add ngram-mod #19164
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
spec : add ngram-mod #19164
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
37298a6
spec : add ngram-mod
ggerganov 7f52bca
cont : simplify + keep track of occupancy
ggerganov 57173c3
cont : cleanup
ggerganov a9a076f
cont : move initialization to common/speculative
ggerganov 1644da7
cont : cleanup
ggerganov 20a0483
cont : cleanup
ggerganov 518926d
cont : fix
ggerganov 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 |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| // | ||
|
|
||
| #include "llama.h" | ||
| #include "common.h" | ||
|
|
||
| #include <vector> | ||
|
|
||
|
|
||
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,107 @@ | ||
| #include "ngram-mod.h" | ||
|
|
||
| // | ||
| // common_ngram_mod | ||
| // | ||
|
|
||
| common_ngram_mod::common_ngram_mod(uint16_t n, size_t size) : n(n), used(0) { | ||
| entries.resize(size); | ||
|
|
||
| std::fill(entries.begin(), entries.end(), EMPTY); | ||
| } | ||
|
|
||
| size_t common_ngram_mod::idx(const entry_t * tokens) const { | ||
| size_t res = 0; | ||
|
|
||
| for (size_t i = 0; i < n; ++i) { | ||
| res = (res * 6364136223846793005ULL + tokens[i]); | ||
| } | ||
|
|
||
| res = res % entries.size(); | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| void common_ngram_mod::add(const entry_t * tokens) { | ||
| const size_t i = idx(tokens); | ||
|
|
||
| if (entries[i] != EMPTY) { | ||
| used++; | ||
| } | ||
|
|
||
| entries[i] = tokens[n]; | ||
| } | ||
|
|
||
| common_ngram_mod::entry_t common_ngram_mod::get(const entry_t * tokens) const { | ||
| const size_t i = idx(tokens); | ||
|
|
||
| return entries[i]; | ||
| } | ||
|
|
||
| void common_ngram_mod::reset() { | ||
| std::fill(entries.begin(), entries.end(), EMPTY); | ||
| used = 0; | ||
| } | ||
|
|
||
| size_t common_ngram_mod::get_n() const { | ||
| return n; | ||
| } | ||
|
|
||
| size_t common_ngram_mod::get_used() const { | ||
| return used; | ||
| } | ||
|
|
||
| size_t common_ngram_mod::size() const { | ||
| return entries.size(); | ||
| } | ||
|
|
||
| size_t common_ngram_mod::size_bytes() const { | ||
| return entries.size() * sizeof(entries[0]); | ||
| } | ||
|
|
||
| // | ||
| // common_ngram_mod_ext | ||
| // | ||
|
|
||
| common_ngram_mod_ext::common_ngram_mod_ext(uint16_t n, size_t size) : n(n) { | ||
| entries.resize(size); | ||
| } | ||
|
|
||
| size_t common_ngram_mod_ext::idx(const int32_t * tokens) const { | ||
| size_t res = 0; | ||
|
|
||
| for (size_t i = 0; i < n; ++i) { | ||
| res = (res * 6364136223846793005ULL + tokens[i]); | ||
| } | ||
|
|
||
| res = res % entries.size(); | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| void common_ngram_mod_ext::add(const int32_t * tokens) { | ||
| const size_t i = idx(tokens); | ||
|
|
||
| common_ngram_mod_ext_entry & entry = entries[i]; | ||
|
|
||
| if (entry.n_choices < COMMON_NGRAM_MOD_MAX_CHOICES) { | ||
| entry.n_choices++; | ||
| } | ||
|
|
||
| entry.choices[entry.head] = tokens[n]; | ||
| entry.head = (entry.head + 1) % COMMON_NGRAM_MOD_MAX_CHOICES; | ||
| } | ||
|
|
||
| int32_t common_ngram_mod_ext::get(const int32_t * tokens, int32_t offs) const { | ||
| const size_t i = idx(tokens); | ||
|
|
||
| const common_ngram_mod_ext_entry & entry = entries[i]; | ||
|
|
||
| if (entry.n_choices == 0) { | ||
| return -1; | ||
| } | ||
|
|
||
| const int32_t k = (offs + entry.head) % entry.n_choices; | ||
|
|
||
| return entry.choices[k]; | ||
| } |
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,67 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <vector> | ||
| #include <memory> | ||
|
|
||
| // | ||
| // common_ngram_mod | ||
| // | ||
|
|
||
| // basic n-gram hasher | ||
| struct common_ngram_mod { | ||
| using entry_t = int32_t; | ||
|
|
||
| static constexpr entry_t EMPTY = -1; | ||
|
|
||
| common_ngram_mod(uint16_t n, size_t size); | ||
|
|
||
| size_t idx(const entry_t * tokens) const; | ||
| void add(const entry_t * tokens); | ||
| entry_t get(const entry_t * tokens) const; // return -1 if not found | ||
|
|
||
| void reset(); | ||
|
|
||
| size_t get_n() const; | ||
| size_t get_used() const; | ||
|
|
||
| size_t size() const; | ||
| size_t size_bytes() const; | ||
|
|
||
| private: | ||
| size_t n; // ngram size to hash | ||
|
|
||
| size_t used; | ||
|
|
||
| std::vector<entry_t> entries; | ||
| }; | ||
|
|
||
| using common_ngram_mod_ptr = std::unique_ptr<common_ngram_mod>; | ||
|
|
||
| // | ||
| // common_ngram_mod_ext (for experiments) | ||
| // | ||
|
|
||
| #define COMMON_NGRAM_MOD_MAX_CHOICES 4 | ||
|
|
||
| struct common_ngram_mod_ext_entry { | ||
| uint16_t head = 0; | ||
| uint16_t n_choices = 0; | ||
|
|
||
| int32_t choices[COMMON_NGRAM_MOD_MAX_CHOICES]; | ||
| }; | ||
|
|
||
| struct common_ngram_mod_ext { | ||
| common_ngram_mod_ext(uint16_t n, size_t size); | ||
|
|
||
| size_t idx(const int32_t * tokens) const; | ||
|
|
||
| void add(const int32_t * tokens); | ||
| int32_t get(const int32_t * tokens, int32_t offs) const; // return -1 if not found | ||
|
|
||
| size_t n; // ngram size to hash | ||
|
|
||
| std::vector<common_ngram_mod_ext_entry> entries; | ||
| }; | ||
|
|
||
| using common_ngram_mod_ext_ptr = std::unique_ptr<common_ngram_mod_ext>; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems to be unused now |
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also unused