generate: min_p and the penalties ride the GEN line

The sampled chain's last two gaps close: `min_p` and the llama.cpp
penalty set (repeat/freq/present over a history window) are live end
to end - GEN keys, per-request params, and the token history the
penalties count over.

- GEN accepts min_p=F penalty_last_n=N penalty_repeat=F penalty_freq=F
  penalty_present=F between max_new and the ids (the server's
  spelling); the engine clamps defensively (min_p to 0..1, a negative
  last_n to 0) and skips what it does not know, as before.
- min_p cuts the top_k survivors to the prefix with p >= min_p * p_max
  - in logits, sel_logit >= sel_logit[0] + logf(min_p) - before top_p,
  so the two cuts compose; 0 disables, and the head always survives,
  so the kept count never reaches zero.
- the penalties need the token history: the serve loop uploads the
  last penalty_last_n tokens - the consumed tail plus the fed-back
  head, which joins consumed_seq only after the window commits - into
  one device row before every window.  A request without penalties
  hands the sampler a null row and takes the byte-for-byte neutral
  path (no upload, nothing handed over).

Verified: sampler_parity grows a full-chain host reference (the Philox
draw transcribed host-side) and fixtures for the sampled chain with
penalties and for min_p at 0/0.5/0.9 - the kernel matches the
reference on all of them, and each fixture first asserts it can SEE
the feature before asserting the kernel matches.  On the IQ3_XXS
artifact, same seed: a request with neutral penalty keys is
token-identical to one without them; a six-copy prompt that loops
24/24 on one token clean produces 24 distinct tokens with repeat 1.5
+ present 0.5; min_p 0.9 changes the pick.
This commit is contained in:
Jakub Luwierski committed 2026-09-27 07:55:19 +02:00
1 parent 9e6e5b7a1a
commit 4703da4363
6 files changed
+286 -16

No files matched your search

+40 -2
View File
@@ -1966,6 +1966,16 @@ int main(int argc, char** argv) {
return 1;
}
mem_mark("the head and the prompt path");
// the penalty-history buffer: ONE row, the request's last `penalty_last_n` tokens, -1 padded in
// front. Allocated once at the cap; a request without penalties gets a null row and takes the
// byte-for-byte neutral path (no upload, no buffer handed to the sampler).
constexpr int kPenaltyWindowCap = 4096;
int32_t* d_hist = nullptr;
std::vector<int32_t> hist_stage((size_t) kPenaltyWindowCap, -1);
if (cudaMalloc(&d_hist, (size_t) kPenaltyWindowCap * sizeof(int32_t)) != cudaSuccess) {
std::fprintf(stderr, "strata serve: the penalty-history allocation failed\n");
return 1;
}
strata::core::Verifier ver;
strata::core::VerifyHits vh;
vh.d_res = thits.d_res;
@@ -2154,11 +2164,14 @@ int main(int argc, char** argv) {
}
char* endp = nullptr;
const long long max_new = std::strtoll(line.c_str() + (geni ? 5 : 4), &endp, 10);
// optional sampling keys between max_new and the ids: temperature=F, top_p=F, top_k=N,
// seed=N (text requests only). Absent keys keep today's behavior: greedy.
// optional sampling keys between max_new and the ids: temperature=F, top_p=F, top_k=N, min_p=F,
// penalty_last_n=N, penalty_repeat=F, penalty_freq=F, penalty_present=F, seed=N (text requests
// only). Absent keys keep today's behavior: greedy, no penalties.
float req_temperature = 0.0f, req_top_p = 1.0f;
int req_top_k = 20; // the sampler's own default; the sampled path REQUIRES top_k in 1..64
unsigned long long req_seed = 0;
float req_min_p = 0.0f, req_penalty_repeat = 1.0f, req_penalty_freq = 0.0f, req_penalty_present = 0.0f;
int req_penalty_last_n = 0;
if (!geni && endp != nullptr) {
for (;;) {
while (*endp == ' ') ++endp;
@@ -2173,6 +2186,11 @@ int main(int argc, char** argv) {
if (key == "temperature") req_temperature = fv;
else if (key == "top_p") req_top_p = fv;
else if (key == "top_k") req_top_k = std::atoi(tok.c_str() + eq + 1);
else if (key == "min_p") req_min_p = fv;
else if (key == "penalty_last_n") req_penalty_last_n = std::atoi(tok.c_str() + eq + 1);
else if (key == "penalty_repeat") req_penalty_repeat = fv;
else if (key == "penalty_freq") req_penalty_freq = fv;
else if (key == "penalty_present") req_penalty_present = fv;
else if (key == "seed") req_seed = std::strtoull(tok.c_str() + eq + 1, nullptr, 10);
// unknown keys are skipped: the ids start at the first token without '='
}
@@ -2417,8 +2435,15 @@ int main(int argc, char** argv) {
req_sp.top_k = req_top_k;
req_sp.seed = req_seed ? req_seed
: (unsigned long long) std::chrono::steady_clock::now().time_since_epoch().count();
req_sp.min_p = std::clamp(req_min_p, 0.0f, 1.0f);
req_sp.penalty_last_n = std::max(req_penalty_last_n, 0);
req_sp.penalty_repeat = req_penalty_repeat;
req_sp.penalty_freq = req_penalty_freq;
req_sp.penalty_present = req_penalty_present;
req_sp.counter = 0;
ver.set_sampling(req_sp);
const int hist_n = std::min(req_sp.penalty_last_n, kPenaltyWindowCap);
ver.set_history(hist_n > 0 ? d_hist : nullptr, hist_n);
bool cancelled = false;
tr("prompt start", n - 1);
// The prompt is read in two parts when it has a turn boundary past `resume`: up to the last <|im_start|>
@@ -2498,6 +2523,19 @@ int main(int argc, char** argv) {
drive.d.experts = 0;
drive.d.failed = false;
apply_pending(false);
if (hist_n > 0) {
// the tail the penalties count over: the tokens the state has consumed plus the fed-back
// head `x` (it joins `consumed` only after this window commits). Most recent LAST,
// -1 in the unused front slots - the sampler's row layout.
const int64_t avail = (int64_t) consumed.size() + 1;
const int take = (int) std::min<int64_t>(hist_n, avail);
std::fill(hist_stage.begin(), hist_stage.begin() + hist_n, -1);
for (int j = 0; j < take - 1; ++j)
hist_stage[(size_t) (hist_n - take + j)] =
consumed[(size_t) ((int64_t) consumed.size() - (take - 1) + j)];
hist_stage[(size_t) (hist_n - 1)] = (int32_t) x;
cudaMemcpy(d_hist, hist_stage.data(), (size_t) hist_n * sizeof(int32_t), cudaMemcpyHostToDevice);
}
tr("window", p, T);
if (!ver.run(T, window.data(), p, &drive_pool_multi, &drive, outv.data(), err) || drive.d.failed) {
std::printf("ERR %s\n", drive.d.failed && drive.d.fail ? drive.d.fail : err.c_str());