-
Notifications
You must be signed in to change notification settings - Fork 12.4k
added implementation of DRY sampler #6839
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
Changes from 3 commits
f64dea0
aea4ad0
4d603e3
75beda2
85dadac
793e1e2
3caec6b
49e078f
2f9a36a
802ddd7
12bfa78
0229fc8
236da59
e862def
9105cf4
20dc562
d1676a1
ed6b909
6579e64
a18fb2f
190898a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13233,6 +13233,90 @@ void llama_sample_min_p(struct llama_context * ctx, llama_token_data_array * can | |
} | ||
} | ||
|
||
void llama_sample_dry(struct llama_context * ctx, llama_token_data_array * candidates, const llama_token * last_tokens, int last_tokens_size, float dry_base, float dry_multiplier, int dry_allowed_length, const llama_token * seq_breakers, int seq_breakers_size) { | ||
l3utterfly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// sanity check | ||
GGML_ASSERT(last_tokens_size > 0); | ||
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. Do all models use BOS tokens? Because if not, this assertion might fail with an empty context. 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. I replaced this with an |
||
|
||
// get the last token | ||
auto last_token = last_tokens[last_tokens_size - 1]; | ||
|
||
// if last token is part of the sequence breakers, skip whole sampler | ||
if(std::find(seq_breakers, seq_breakers + seq_breakers_size, last_token) != seq_breakers + seq_breakers_size) { | ||
return; | ||
} | ||
|
||
// create an unordered map of "next tokens" <-> max match length | ||
std::unordered_map<llama_token, size_t> match_lengths; | ||
|
||
// loop through each previous token (exclude the last token) | ||
for (size_t i = 0; i < last_tokens_size - 1; ++i) { | ||
// skip if the compare token if it's not the same as the last token | ||
l3utterfly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if(last_tokens[i] != last_token) { | ||
continue; | ||
} | ||
|
||
// get the next token (i + 1 is always less than last_tokens_size) | ||
auto next_token = last_tokens[i + 1]; | ||
l3utterfly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// try to extend the match backwards (match length starts a 1 because last token is already matched) | ||
l3utterfly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
size_t match_length = 1; | ||
|
||
// loop through the previous tokens | ||
for(;; match_length++) { | ||
// if we have reached the start of our last tokens, break | ||
if(i < match_length) break; | ||
|
||
// compare token starts at our prev index, going backwards by match length | ||
auto compare_token = last_tokens[i - match_length]; | ||
|
||
// head token starts at the end of last tokens, going backwards by match length, minus 1 because we start at the last token itself | ||
auto head_token = last_tokens[last_tokens_size - 1 - match_length]; | ||
|
||
// if compare token is part of the sequence breakers, break out of the match | ||
if(std::find(seq_breakers, seq_breakers + seq_breakers_size, compare_token) != seq_breakers + seq_breakers_size) | ||
break; | ||
l3utterfly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// break out of the match if any tokens don't match | ||
if(compare_token != head_token) | ||
l3utterfly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break; | ||
} | ||
|
||
// Check if the next token exists in the map | ||
auto it = match_lengths.find(next_token); | ||
|
||
if (it == match_lengths.end()) { | ||
// Key does not exist, insert the new value | ||
match_lengths[next_token] = match_length; | ||
} else { | ||
// Key exists, update it with the max of the new value or the existing value | ||
it->second = std::max(it->second, match_length); | ||
} | ||
} | ||
|
||
// apply penalties | ||
for (const auto& pair : match_lengths) { | ||
auto next_token = pair.first; | ||
auto match_length = pair.second; | ||
|
||
// if the match length is greater than our allowed length in config, we apply penalities | ||
if(match_length > dry_allowed_length) { | ||
l3utterfly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// find our next token in the candidates->data | ||
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. Aren't the If this isn't true for llama.cpp, how are the candidates ordered? 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. I looked through the creation of We can check for that condition here? But I cannot determine if the candidates are guaranteed to have 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. I see, I guess the purpose of sorting by logit is to simplify truncation samplers. Probably best to keep the current code then. There are of course possible optimizations (such as interchanging the two loops and deleting tokens from |
||
size_t i = 0; | ||
l3utterfly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (; i < candidates->size; ++i) { | ||
if (candidates->data[i].id == next_token) { | ||
// calculate the penalty | ||
float penalty = dry_multiplier * pow(dry_base, match_length - dry_allowed_length); | ||
|
||
// apply the dry penalty | ||
candidates->data[i].logit -= penalty; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
void llama_sample_tail_free(struct llama_context * ctx, llama_token_data_array * candidates, float z, size_t min_keep) { | ||
if (z >= 1.0f || candidates->size <= 2) { | ||
return; | ||
|
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.
Too much indentation before the assignment operator for this block.
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.
The indentation here is trying to match the ones below from
dry_allowed_length
. What is the convention here?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.
Not sure what you mean? Both the blocks above the new parameters (ending with
penalize_nl
) and below them (starting withprev
) have the equals signs left-aligned as closely as possible to the LHS, whereas the new parameters have three extra spaces.But code style is really the maintainers' business. I don't care that much, just something I noticed.