Skip to content

Commit e02b597

Browse files
lookup: fibonacci hashing, fix crashes (#8548)
1 parent b328344 commit e02b597

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

common/ngram-cache.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ struct llama_ngram {
3737
}
3838
};
3939

40+
struct llama_token_hash_function {
41+
size_t operator()(const llama_token token) const {
42+
// see https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/
43+
return token * 11400714819323198485llu;
44+
}
45+
};
46+
4047
struct llama_ngram_hash_function {
4148
size_t operator()(const llama_ngram & ngram) const {
42-
size_t hash = 0;
43-
for (int i = 0; i < LLAMA_NGRAM_MAX; ++i) {
44-
hash ^= std::hash<llama_token>{}(ngram.tokens[i]);
49+
size_t hash = llama_token_hash_function{}(ngram.tokens[0]);
50+
for (int i = 1; i < LLAMA_NGRAM_MAX; ++i) {
51+
hash ^= llama_token_hash_function{}(ngram.tokens[i]);
4552
}
4653
return hash;
4754
}

examples/lookup/lookup-stats.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ int main(int argc, char ** argv){
3131

3232
// load the model
3333
std::tie(model, ctx) = llama_init_from_gpt_params(params);
34-
GGML_ASSERT(llama_n_vocab(model) < (1 << 16));
3534

3635
// tokenize the prompt
3736
std::vector<llama_token> inp;
@@ -65,7 +64,7 @@ int main(int argc, char ** argv){
6564
}
6665

6766
const int n_input = inp.size();
68-
const int n_ctx = params.n_ctx;
67+
const int n_ctx = llama_n_ctx(ctx);
6968

7069
int n_drafted = 0;
7170
int n_accept = 0;

examples/lookup/lookup.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ int main(int argc, char ** argv){
3939

4040
// load the model
4141
std::tie(model, ctx) = llama_init_from_gpt_params(params);
42-
GGML_ASSERT(llama_n_vocab(model) < (1 << 16));
4342

4443
// tokenize the prompt
4544
std::vector<llama_token> inp;

0 commit comments

Comments
 (0)