| 201 | } |
| 202 | |
| 203 | static void llama_sampler_softmax_impl(llama_token_data_array * cur_p) { |
| 204 | GGML_ASSERT(cur_p->size > 0); |
| 205 | |
| 206 | // Sort the logits in descending order |
| 207 | if (!cur_p->sorted) { |
| 208 | std::sort(cur_p->data, cur_p->data + cur_p->size, [](const llama_token_data & a, const llama_token_data & b) { |
| 209 | return a.logit > b.logit; |
| 210 | }); |
| 211 | cur_p->sorted = true; |
| 212 | } |
| 213 | |
| 214 | float max_l = cur_p->data[0].logit; |
| 215 | float cum_sum = 0.0f; |
| 216 | |
| 217 | for (size_t i = 0; i < cur_p->size; ++i) { |
| 218 | float p = expf(cur_p->data[i].logit - max_l); |
| 219 | cur_p->data[i].p = p; |
| 220 | cum_sum += p; |
| 221 | } |
| 222 | |
| 223 | for (size_t i = 0; i < cur_p->size; ++i) { |
| 224 | cur_p->data[i].p /= cum_sum; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static void llama_sampler_top_k_impl(llama_token_data_array * cur_p, int32_t k) { |
| 229 | // TODO: move bucket sort to separate function so that top_p/typical/softmax first is equally fast |
no outgoing calls
no test coverage detected