Skip to content

Commit

Permalink
check for one or zero candidates case in llama_sample_entropy
Browse files Browse the repository at this point in the history
  • Loading branch information
l3utterfly committed Jan 24, 2024
1 parent 3610341 commit 66477a4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions common/sampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 66477a4

Please sign in to comment.