Skip to content

Commit

Permalink
return earlier if there is only 1 candiate (i.e. max_entropy == 0)
Browse files Browse the repository at this point in the history
  • Loading branch information
l3utterfly committed Jan 22, 2024
1 parent babb76a commit 4e97bdb
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down

0 comments on commit 4e97bdb

Please sign in to comment.