From 4e97bdb497bfe3d6fc020d8a2851940c087ff252 Mon Sep 17 00:00:00 2001 From: l3utterfly Date: Mon, 22 Jan 2024 22:16:05 +0900 Subject: [PATCH] return earlier if there is only 1 candiate (i.e. max_entropy == 0) --- llama.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/llama.cpp b/llama.cpp index d1519214bca19..111caf0d89cd0 100644 --- a/llama.cpp +++ b/llama.cpp @@ -7786,6 +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(); + // 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(ctx, candidates_p); // Calculate entropy of the softmax probabilities @@ -7797,14 +7805,6 @@ void llama_sample_entropy(struct llama_context * ctx, llama_token_data_array * c } } - // Calculate maximum possible entropy - float max_entropy = -logf(1.0f / candidates_p->size); - - // Guard against division by zero - if (max_entropy == 0.0f) { - max_entropy = 1.0f; // This ensures that normalized_entropy will be 0 when entropy is 0 - } - // Normalize the entropy float normalized_entropy = entropy / max_entropy;