| 653 | // helpers |
| 654 | |
| 655 | llama_token_data_array * common_sampler_get_candidates(struct common_sampler * gsmpl, bool do_sort) { |
| 656 | const auto tm = gsmpl->tm(); |
| 657 | |
| 658 | auto * res = &gsmpl->cur_p; |
| 659 | |
| 660 | if (do_sort && !res->sorted) { |
| 661 | // remember the selected token before sorting |
| 662 | const llama_token id = res->data[res->selected].id; |
| 663 | |
| 664 | std::sort(res->data, res->data + res->size, [](const llama_token_data & a, const llama_token_data & b) { |
| 665 | return a.p > b.p; |
| 666 | }); |
| 667 | |
| 668 | // restore the selected token after sorting |
| 669 | for (size_t i = 0; i < res->size; ++i) { |
| 670 | if (res->data[i].id == id) { |
| 671 | res->selected = i; |
| 672 | break; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | res->sorted = true; |
| 677 | } |
| 678 | |
| 679 | return res; |
| 680 | } |
| 681 | |
| 682 | llama_token common_sampler_last(const struct common_sampler * gsmpl) { |
| 683 | return gsmpl->prev.rat(0); |