Skip to content

Commit

Permalink
sort before apply softmax
Browse files Browse the repository at this point in the history
  • Loading branch information
ngxson committed Dec 12, 2024
1 parent cc90cdb commit 29c1495
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions examples/server/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,11 @@ static std::vector<llama_token_data> get_token_probabilities(llama_context * ctx
cur[token_id] = llama_token_data{token_id, logits[token_id], 0.0f};
}

// sort tokens by logits
std::sort(cur.begin(), cur.end(), [](const llama_token_data & a, const llama_token_data & b) {
return a.logit > b.logit;
});

// apply softmax
float max_l = cur[0].logit;
float cum_sum = 0.0f;
Expand All @@ -717,10 +722,5 @@ static std::vector<llama_token_data> get_token_probabilities(llama_context * ctx
cur[i].p /= cum_sum;
}

// sort tokens by probability
std::sort(cur.begin(), cur.end(), [](const llama_token_data & a, const llama_token_data & b) {
return a.p > b.p;
});

return cur;
}

0 comments on commit 29c1495

Please sign in to comment.