Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 86 additions & 2 deletions common/sampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "ggml.h"

#include "../src/llama-ext.h"

#include <algorithm>
#include <atomic>
#include <cctype>
Expand Down Expand Up @@ -263,6 +265,9 @@ struct common_sampler {
};
std::vector<prepared_row> prepared;

// a backend greedy sampler selects the token of every row (see common_sampler_set_backend_greedy)
bool backend_greedy = false;

void reset() {
prev.clear();

Expand Down Expand Up @@ -849,7 +854,7 @@ void common_sampler_prepare(struct llama_context * ctx, const std::vector<common
return;
}

llama_synchronize(ctx);
llama_synchronize_outputs(ctx);

const llama_vocab * vocab = llama_model_get_vocab(llama_get_model(ctx));
const int n_vocab = llama_vocab_n_tokens(vocab);
Expand Down Expand Up @@ -911,12 +916,91 @@ void common_sampler_prepare(struct llama_context * ctx, const std::vector<common
workers.run(items.size(), n_threads, fn);
}

bool common_sampler_is_greedy(const struct common_sampler * gsmpl, const struct llama_model * model) {
if (!gsmpl || gsmpl->grmr || gsmpl->rbudget) {
return false;
}

const auto & p = gsmpl->params;

if (p.mirostat != 0 || p.n_probs != 0 || p.samplers.empty() || !p.logit_bias.empty() || p.dynatemp_range != 0.0f) {
return false;
}

// at temp > 0 the speculative loop verifies by rejection and reads the candidates of the rows
if (!(p.temp <= 0.0f)) {
return false;
}

// the chain ends in the greedy sampler, see common_sampler_init
const auto last = p.samplers.back();
if (!((last == COMMON_SAMPLER_TYPE_TEMPERATURE && p.temp == 0.0f) || (last == COMMON_SAMPLER_TYPE_TOP_K && p.top_k == 1))) {
return false;
}

int32_t n_suppress = 0;
llama_vocab_get_suppress_tokens(llama_model_get_vocab(model), &n_suppress);
if (n_suppress != 0) {
return false;
}

// the samplers before it must keep the token with the largest logit
for (const auto & cnstr : p.samplers) {
switch (cnstr) {
case COMMON_SAMPLER_TYPE_TOP_K:
case COMMON_SAMPLER_TYPE_TOP_P:
case COMMON_SAMPLER_TYPE_MIN_P:
case COMMON_SAMPLER_TYPE_TOP_N_SIGMA:
case COMMON_SAMPLER_TYPE_TEMPERATURE:
break;
case COMMON_SAMPLER_TYPE_PENALTIES:
if (p.penalty_last_n > 0 && (p.penalty_repeat != 1.0f || p.penalty_freq != 0.0f || p.penalty_present != 0.0f)) {
return false;
}
break;
case COMMON_SAMPLER_TYPE_DRY:
if (p.dry_multiplier != 0.0f && p.dry_penalty_last_n != 0) {
return false;
}
break;
case COMMON_SAMPLER_TYPE_TYPICAL_P:
if (p.typ_p < 1.0f) {
return false;
}
break;
case COMMON_SAMPLER_TYPE_XTC:
if (p.xtc_probability > 0.0f) {
return false;
}
break;
default:
return false;
}
}

return true;
}

void common_sampler_set_backend_greedy(struct common_sampler * gsmpl, bool value) {
if (gsmpl) {
gsmpl->backend_greedy = value;
}
}

llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first) {
llama_synchronize(ctx);
llama_synchronize_outputs(ctx);

// start measuring sampling time after the llama_context synchronization in order to not measure any ongoing async operations
const auto tm = gsmpl->tm();

// the backend picked the largest logit of the row: no logits were copied, and there are no candidates to build
if (gsmpl->backend_greedy) {
const llama_token id = llama_get_sampled_token_ith(ctx, idx);
if (id != LLAMA_TOKEN_NULL) {
return id;
}
}

llama_token id = LLAMA_TOKEN_NULL;

auto & grmr = gsmpl->grmr;
Expand Down
9 changes: 9 additions & 0 deletions common/sampling.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ struct llama_sampler * common_sampler_get(const struct common_sampler * gsmpl);
// Returns that number of logits, 0 if the chain needs all the logits. n_suppress is the number of suppressed tokens of the vocabulary.
size_t common_sampler_n_preselect(const common_params_sampling & params, size_t n_suppress);

// True if the chain selects the token with the largest logit and nothing else (a greedy request, temp <= 0, no grammar,
// no reasoning budget, no logit bias, no sampler that changes the order of the logits). The token is then the argmax of the logits.
bool common_sampler_is_greedy(const struct common_sampler * gsmpl, const struct llama_model * model);

// The caller registered a backend greedy sampler (llama_sampler_init_greedy) for the sequence, so common_sampler_sample()
// returns the token the backend selected and leaves the candidates (common_sampler_get_candidates) untouched.
// Only valid if common_sampler_is_greedy(); a row without a backend token falls back to the host as before.
void common_sampler_set_backend_greedy(struct common_sampler * gsmpl, bool value);

// Fills cur with the n largest logits and the tokens with a positive logit bias, in token order.
void common_sampler_preselect(const float * logits, int32_t n_vocab, size_t n, const std::vector<llama_logit_bias> & logit_bias, std::vector<llama_token_data> & cur);

Expand Down
Loading
Loading