| 8167 | } |
| 8168 | |
| 8169 | void 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 | |
| 8194 | void llama_sample_top_p(struct llama_context * ctx, llama_token_data_array * candidates, float p, size_t min_keep) { |
| 8195 | if (p >= 1.0f) { |