| 8631 | } |
| 8632 | |
| 8633 | llama_token llama_sample_token(struct llama_context * ctx, llama_token_data_array * candidates) { |
| 8634 | GGML_ASSERT(ctx); |
| 8635 | |
| 8636 | const int64_t t_start_sample_us = ggml_time_us(); |
| 8637 | llama_sample_softmax(nullptr, candidates); |
| 8638 | |
| 8639 | std::vector<float> probs; |
| 8640 | probs.reserve(candidates->size); |
| 8641 | for (size_t i = 0; i < candidates->size; ++i) { |
| 8642 | probs.push_back(candidates->data[i].p); |
| 8643 | } |
| 8644 | |
| 8645 | std::discrete_distribution<> dist(probs.begin(), probs.end()); |
| 8646 | auto & rng = ctx->rng; |
| 8647 | int idx = dist(rng); |
| 8648 | |
| 8649 | llama_token result = candidates->data[idx].id; |
| 8650 | |
| 8651 | ctx->t_sample_us += ggml_time_us() - t_start_sample_us; |
| 8652 | ctx->n_sample++; |
| 8653 | return result; |
| 8654 | } |
| 8655 | |
| 8656 | void llama_grammar_accept_token(struct llama_context * ctx, struct llama_grammar * grammar, llama_token token) { |
| 8657 | const int64_t t_start_sample_us = ggml_time_us(); |
no test coverage detected