spec : add probabilistic sampling for simple draft and MTP (#27694)

* Make the drafter probabilistic and the target verify by rejection sampling

* Drop stale spec_draft_q before drafting

* Fallback to argmax sampling for grammar-constrained requests and adding flag for enabling probabilistic draft sampling. Default flag value is greedy.

* Support grammar-constrained requests in rejection sampling

* Fix - renormalize distribution after masking

* copy rng on sampler copy and re-accept drafted tokens on replay

* Fix draft sampler sharing the target's rng stream

* Simplify the rejection sampler's inputs and move replay to the server

* Truncate the draft candidates along with the draft

---------

Co-authored-by: praneshgo <227579474+praneshgo@users.noreply.github.com>
Co-authored-by: Pranesh Gonegandla <pgonegandla@nvidia.com>
This commit is contained in:
Pranesh Gonegandla
2026-10-02 17:52:54 +03:00
committed by GitHub
co-authored by praneshgo Pranesh Gonegandla
parent 134b2bb756
commit 1fb7ef3e33
7 changed files with 304 additions and 11 deletions
+126
View File
@@ -12,6 +12,7 @@
#include <climits>
#include <cmath>
#include <cstring>
#include <random>
#include <unordered_map>
#include <vector>
@@ -121,6 +122,9 @@ struct common_sampler {
llama_token_data_array cur_p;
// for rejection sampling; independent of the draft, or the target distribution is not preserved
std::mt19937 rng;
void reset() {
prev.clear();
@@ -432,6 +436,8 @@ struct common_sampler * common_sampler_init(
/* .prev = */ ring_buffer<llama_token>(std::max(32, params.n_prev)),
/* .cur = */ {},
/* .cur_p = */ {},
// mix it, the chain and the draft are seeded from this one too
/* .rng = */ std::mt19937(llama_sampler_get_seed(chain) ^ 0x9e3779b9u),
};
return result;
@@ -515,6 +521,7 @@ struct common_sampler * common_sampler_clone(common_sampler * gsmpl) {
/* .prev = */ gsmpl->prev,
/* .cur = */ gsmpl->cur,
/* .cur_p = */ gsmpl->cur_p,
/* .rng = */ gsmpl->rng,
};
}
@@ -535,6 +542,7 @@ void common_sampler_copy(const common_sampler * src, common_sampler * dst) {
dst->cur = src->cur;
dst->cur_p = src->cur_p;
dst->cur_p.data = src->cur_p.data ? dst->cur.data() : nullptr; // re-point to dst's buffer
dst->rng = src->rng;
dst->t_total_us = src->t_total_us;
}
@@ -709,6 +717,124 @@ std::vector<llama_token> common_sampler_sample_and_accept_n(struct common_sample
return result;
}
static float prob_of(const llama_token_data * data, size_t n, llama_token id) {
for (size_t k = 0; k < n; ++k) {
if (data[k].id == id) {
return data[k].p;
}
}
return 0.0f;
}
// Accept a drafted token with probability min(1, p/q), else draw from norm(max(0, p - q)).
// Preserves the target distribution exactly, and accepts more often than matching does when the
// draft samples instead of taking its argmax.
std::vector<llama_token> common_sampler_sample_and_accept_n_rejection(struct common_sampler * gsmpl, struct llama_context * ctx, const std::vector<int> & idxs, const llama_tokens & draft, const std::vector<std::vector<llama_token_data>> & draft_q, bool grammar_first) {
GGML_ASSERT(idxs.size() == draft.size() + 1 && "idxs.size() must be draft.size() + 1");
GGML_ASSERT(draft_q.size() == draft.size() && "draft_q must have one entry per draft token");
std::vector<llama_token> result;
result.reserve(idxs.size());
// draws come from the sampler's own stream, so they stay independent of what was drafted
std::uniform_real_distribution<float> uni(0.0f, 1.0f);
std::vector<llama_token_data> residual;
std::vector<llama_token_data> cand; // candidate array masked by the grammar, if there is one
size_t i = 0;
for (; i < draft.size(); i++) {
// leaves the target distribution in the candidate array
const llama_token id_tgt = common_sampler_sample(gsmpl, ctx, idxs[i], grammar_first);
const auto * cur_p = common_sampler_get_candidates(gsmpl, true);
const auto & q = draft_q[i];
const bool masked = !grammar_first && grammar_should_apply(gsmpl);
if (masked) {
cand.assign(cur_p->data, cur_p->data + cur_p->size);
llama_token_data_array arr = { cand.data(), cand.size(), -1, false };
llama_sampler_apply(gsmpl->grmr, &arr);
}
// a candidate the grammar rejects carries no probability, whatever the target thinks
auto p_raw = [&](size_t k) {
return masked && cand[k].logit == -INFINITY ? 0.0f : cur_p->data[k].p;
};
// masking drops probability mass, so rescale what is left or the residual is over-weighted
float p_sum = 0.0f;
if (masked) {
for (size_t k = 0; k < cur_p->size; ++k) {
p_sum += p_raw(k);
}
}
const float p_norm = masked && p_sum > 0.0f ? 1.0f/p_sum : 1.0f;
auto p_of = [&](size_t k) {
return p_raw(k)*p_norm;
};
// q_x is never 0 for a token the draft produced, but guard the divide
const float q_x = prob_of(q.data(), q.size(), draft[i]);
float p_x = 0.0f;
for (size_t k = 0; k < cur_p->size; ++k) {
if (cur_p->data[k].id == draft[i]) {
p_x = p_of(k);
break;
}
}
if (q_x > 0.0f && (p_x >= q_x || uni(gsmpl->rng) < p_x / q_x)) {
common_sampler_accept(gsmpl, draft[i], true);
result.push_back(draft[i]);
continue;
}
// rejected: tokens outside q's support keep all of p
residual.clear();
float sum = 0.0f;
for (size_t k = 0; k < cur_p->size; ++k) {
const float r = p_of(k) - prob_of(q.data(), q.size(), cur_p->data[k].id);
if (r > 0.0f) {
residual.push_back({ cur_p->data[k].id, 0.0f, r });
sum += r;
}
}
llama_token id = id_tgt;
if (sum > 0.0f) {
float u = uni(gsmpl->rng) * sum;
id = residual.back().id;
for (const auto & e : residual) {
u -= e.p;
if (u <= 0.0f) {
id = e.id;
break;
}
}
}
common_sampler_accept(gsmpl, id, true);
result.push_back(id);
break;
}
if (i == draft.size()) {
const llama_token id = common_sampler_sample(gsmpl, ctx, idxs[i], grammar_first);
common_sampler_accept(gsmpl, id, true);
result.push_back(id);
}
return result;
}
std::vector<llama_token> common_sampler_sample_and_accept_n(struct common_sampler * gsmpl, struct llama_context * ctx, const llama_tokens & draft, bool grammar_first) {
std::vector<int> idxs(draft.size() + 1);
for (size_t i = 0; i < idxs.size(); ++i) {