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

Function common_sampler_sample

smallthinker/common/sampling.cpp:338–382  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

336}
337
338llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first) {
339 gsmpl->set_logits(ctx, idx);
340
341 auto & grmr = gsmpl->grmr;
342 auto & chain = gsmpl->chain;
343 auto & cur_p = gsmpl->cur_p; // initialized by set_logits
344
345 if (grammar_first) {
346 llama_sampler_apply(grmr, &cur_p);
347 }
348
349 llama_sampler_apply(chain, &cur_p);
350
351 GGML_ASSERT(cur_p.selected != -1 && "no selected token during sampling - check your sampling configuration");
352
353 const llama_token id = cur_p.data[cur_p.selected].id;
354
355 if (grammar_first) {
356 return id;
357 }
358
359 // check if it the sampled token fits the grammar
360 {
361 llama_token_data single_token_data = { id, 1.0f, 0.0f };
362 llama_token_data_array single_token_data_array = { &single_token_data, 1, -1, false };
363
364 llama_sampler_apply(grmr, &single_token_data_array);
365
366 const bool is_valid = single_token_data_array.data[0].logit != -INFINITY;
367 if (is_valid) {
368 return id;
369 }
370 }
371
372 // resampling:
373 // if the token is not valid, sample again, but first apply the grammar sampler and then the sampling chain
374 gsmpl->set_logits(ctx, idx);
375
376 llama_sampler_apply(grmr, &cur_p);
377 llama_sampler_apply(chain, &cur_p);
378
379 GGML_ASSERT(cur_p.selected != -1 && "no selected token during re-sampling - check your sampling configuration");
380
381 return cur_p.data[cur_p.selected].id;
382}
383
384std::vector<llama_token> common_sampler_sample_and_accept_n(struct common_sampler * gsmpl, struct llama_context * ctx, const std::vector<int> & idxs, const llama_tokens & draft, bool grammar_first) {
385 GGML_ASSERT(idxs.size() == draft.size() + 1 && "idxs.size() must be draft.size() + 1");

Callers 10

generate_responseFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
update_slotsMethod · 0.85
mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85

Calls 2

llama_sampler_applyFunction · 0.85
set_logitsMethod · 0.80

Tested by 2

mainFunction · 0.68