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

Function llama_sampler_softmax_impl

smallthinker/src/llama-sampling.cpp:203–226  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

201}
202
203static 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
228static 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

Calls

no outgoing calls

Tested by

no test coverage detected