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

Function llama_sample_top_k

llama.cpp:8169–8192  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8167}
8168
8169void llama_sample_top_k(struct llama_context * ctx, llama_token_data_array * candidates, int k, size_t min_keep) {
8170 const int64_t t_start_sample_us = ggml_time_us();
8171
8172 k = std::max(k, (int) min_keep);
8173 k = std::min(k, (int) candidates->size);
8174
8175 // Sort scores in descending order
8176 if (!candidates->sorted) {
8177 auto comp = [](const llama_token_data & a, const llama_token_data & b) {
8178 return a.logit > b.logit;
8179 };
8180 if (k == (int) candidates->size) {
8181 std::sort(candidates->data, candidates->data + candidates->size, comp);
8182 } else {
8183 std::partial_sort(candidates->data, candidates->data + k, candidates->data + candidates->size, comp);
8184 }
8185 candidates->sorted = true;
8186 }
8187 candidates->size = k;
8188
8189 if (ctx) {
8190 ctx->t_sample_us += ggml_time_us() - t_start_sample_us;
8191 }
8192}
8193
8194void llama_sample_top_p(struct llama_context * ctx, llama_token_data_array * candidates, float p, size_t min_keep) {
8195 if (p >= 1.0f) {

Callers 5

test_top_kFunction · 0.85
llama_sampling_sampleFunction · 0.85
sample_idFunction · 0.85
mainFunction · 0.85

Calls 3

maxFunction · 0.85
minFunction · 0.85
ggml_time_usFunction · 0.70

Tested by 1

test_top_kFunction · 0.68