MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / get_token_probabilities

Function get_token_probabilities

smallthinker/tools/server/utils.hpp:975–1007  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

973}
974
975static std::vector<llama_token_data> get_token_probabilities(llama_context * ctx, int idx) {
976 std::vector<llama_token_data> cur;
977 const auto * logits = llama_get_logits_ith(ctx, idx);
978
979 const llama_model * model = llama_get_model(ctx);
980 const llama_vocab * vocab = llama_model_get_vocab(model);
981
982 const int n_vocab = llama_vocab_n_tokens(vocab);
983
984 cur.resize(n_vocab);
985 for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
986 cur[token_id] = llama_token_data{token_id, logits[token_id], 0.0f};
987 }
988
989 // sort tokens by logits
990 std::sort(cur.begin(), cur.end(), [](const llama_token_data & a, const llama_token_data & b) {
991 return a.logit > b.logit;
992 });
993
994 // apply softmax
995 float max_l = cur[0].logit;
996 float cum_sum = 0.0f;
997 for (size_t i = 0; i < cur.size(); ++i) {
998 float p = expf(cur[i].logit - max_l);
999 cur[i].p = p;
1000 cum_sum += p;
1001 }
1002 for (size_t i = 0; i < cur.size(); ++i) {
1003 cur[i].p /= cum_sum;
1004 }
1005
1006 return cur;
1007}
1008
1009static bool are_lora_equal(
1010 const std::vector<common_adapter_lora_info> & l1,

Callers 1

populate_token_probsMethod · 0.85

Calls 8

llama_model_get_vocabFunction · 0.85
llama_vocab_n_tokensFunction · 0.85
llama_get_logits_ithFunction · 0.50
llama_get_modelFunction · 0.50
resizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected