From 66477a4fed1964c1af3ae97cb86a9e4776d8c3d7 Mon Sep 17 00:00:00 2001 From: l3utterfly Date: Wed, 24 Jan 2024 09:09:37 +0900 Subject: [PATCH] check for one or zero candidates case in llama_sample_entropy --- common/sampling.cpp | 4 ++-- llama.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/common/sampling.cpp b/common/sampling.cpp index 27def158a..efd7eab6e 100644 --- a/common/sampling.cpp +++ b/common/sampling.cpp @@ -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); diff --git a/llama.cpp b/llama.cpp index 74982a268..5e25387e2 100644 --- a/llama.cpp +++ b/llama.cpp @@ -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