check for one or zero candidates case in llama_sample_entropy

This commit is contained in:
l3utterfly 2024-01-24 09:09:37 +09:00
parent 36103411f3
commit 66477a4fed
2 changed files with 8 additions and 8 deletions

View file

@ -147,8 +147,8 @@ static void sampler_queue(
case 'm': llama_sample_min_p (ctx_main, &cur_p, min_p, min_keep); break;
case 't':
if (dynatemp_range > 0) {
float dynatemp_min = std::max(0, temp - dynatemp_range);
float dynatemp_max = temp + dynatemp_range;
float dynatemp_min = std::max(0.0f, temp - dynatemp_range);
float dynatemp_max = std::max(0.0f, temp + dynatemp_range);
llama_sample_entropy(ctx_main, &cur_p, dynatemp_min, dynatemp_max, dynatemp_exponent);
} else {
llama_sample_temp(ctx_main, &cur_p, temp);

View file

@ -7786,14 +7786,14 @@ void llama_sample_typical(struct llama_context * ctx, llama_token_data_array * c
void llama_sample_entropy(struct llama_context * ctx, llama_token_data_array * candidates_p, float min_temp, float max_temp, float exponent_val) {
const int64_t t_start_sample_us = ggml_time_us();
// no need to do anything if there is only one (or zero) candidates
if(candidates_p->size <= 1) {
return;
}
// Calculate maximum possible entropy
float max_entropy = -logf(1.0f / candidates_p->size);
// Guard against division by zero
if (max_entropy == 0.0f) {
return;
}
llama_sample_softmax(nullptr, candidates_p);
// Calculate entropy of the softmax probabilities
@ -7805,7 +7805,7 @@ void llama_sample_entropy(struct llama_context * ctx, llama_token_data_array * c
}
}
// Normalize the entropy
// Normalize the entropy (max_entropy cannot be 0 here because we checked candidates_p->size != 1 above)
float normalized_entropy = entropy / max_entropy;
// Map the normalized entropy to the desired temperature range using the power function