Engine 0.1.19: penalties on every drafted token; the CLI samples; sampler hardening

- serve: a verify window samples T rows, but only row 0's penalty history was staged; rows 1..T-1 read
  unwritten slots (past the allocation at penalty_last_n=4096). Every row now gets its own history
  (strata::kernels::penalty_rows: the consumed tail, the fed-back token, the drafts before that row).
- prompt windows (their picks are discarded) skip the head sampling: Verifier::set_head_sampling.
- sampler: history ids outside [0, n_vocab) are ignored (they set bits past the shared bitmap); sampled
  top_k 0 or > 64 means 64 and every row is written (0 used to leave the row unwritten).
- server: a client's top_k 0 or > 64 is sent as 64 (it fell back to the default 20).
- CLI: the speculative loop gets the CLI's sampling (it was always greedy), the per-token path draws
  Philox(seed, position) like a verify window, and the effective sampling is printed.
- sampler_parity: fixtures 12-15 (per-row histories T=1..8 x H=1..4096, out-of-vocab ids, the top_k
  contract, penalty_rows); outputs are pre-filled so an unwritten row fails.
- setup requires engine 0.1.19.

Found by the engineering performance review (C1, C2).
This commit is contained in:
Niko1221 committed 2026-09-28 16:53:36 +02:00
1 parent f32577e3c2
commit de71982be9
11 files changed
+287 -48

No files matched your search

+36 -18
View File
@@ -26,6 +26,7 @@
#include "strata/kernels/ngram.hpp"
#include "strata/kernels/s2_expert_grouped.hpp"
#include "strata/kernels/sampler.hpp"
#include "strata/kernels/verify_kernels.hpp"
#include "strata/kernels/shared_expert.hpp"
#include "strata/kernels/native_moe.hpp"
#include "strata/kernels/native_gdn.hpp"
@@ -1825,6 +1826,16 @@ int main(int argc, char** argv) {
sp.top_k = o.top_k;
sp.top_p = o.top_p;
sp.temperature = o.temperature;
// what this run actually samples with (the speculative loop below gets the same parameters); serve samples
// per request instead
if (!o.serve) {
if (sp.greedy || sp.temperature <= 0.0f)
std::fprintf(stderr, "strata generate: sampling greedy\n");
else
std::fprintf(stderr, "strata generate: sampling temperature=%g top_k=%d top_p=%g seed=%llu\n",
(double) sp.temperature, sp.top_k > 0 && sp.top_k < 64 ? sp.top_k : 64, (double) sp.top_p,
(unsigned long long) sp.seed);
}
std::FILE* dump = nullptr;
const int64_t dump_positions = (int64_t) o.tokens.size() - 1 + o.max_new;
@@ -2337,13 +2348,15 @@ 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).
// the penalty-history buffer: one row per verify-window row (`penalty_rows`), each the last
// `penalty_last_n` tokens that row's pick follows, -1 padded in front. Allocated once at the cap for
// the widest window; a request without penalties gets a null buffer and takes the byte-for-byte
// neutral path (no upload, no buffer handed to the sampler).
constexpr int kPenaltyWindowCap = 4096;
constexpr size_t kHistSlots = (size_t) kPenaltyWindowCap * (size_t) strata::kernels::kVerifyMaxT;
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::vector<int32_t> hist_stage(kHistSlots, -1);
if (cudaMalloc(&d_hist, kHistSlots * sizeof(int32_t)) != cudaSuccess) {
std::fprintf(stderr, "strata serve: the penalty-history allocation failed\n");
return 1;
}
@@ -2854,6 +2867,12 @@ int main(int argc, char** argv) {
};
// tokens [a, b) through the windows: commit all of them, then give the draft layer their residuals
auto read_windows = [&](int64_t a, int64_t b, std::string& e) -> bool {
// every token is committed and the picks are discarded: no head sampling (see set_head_sampling)
struct NoHeadSampling {
strata::core::Verifier& v;
explicit NoHeadSampling(strata::core::Verifier& x) : v(x) { v.set_head_sampling(false); }
~NoHeadSampling() { v.set_head_sampling(true); }
} no_head_sampling(ver);
std::vector<int32_t> win((size_t) S), outw((size_t) S), nxt((size_t) S);
for (int64_t q = a; q < b;) {
if (stop_req.load()) { e = "cancelled"; return false; }
@@ -3041,17 +3060,14 @@ int main(int argc, char** argv) {
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);
// the tails the penalties count over, ONE PER ROW: the tokens the state has consumed, the
// fed-back head `x` (it joins `consumed` only after this window commits), then the drafts
// before that row - what plain decode would have counted there. (Until 0.1.19 only row 0
// was staged, and the drafted rows read unwritten slots.)
strata::kernels::penalty_rows(consumed.data(), (int64_t) consumed.size(), window.data(), T,
hist_n, hist_stage.data());
cudaMemcpy(d_hist, hist_stage.data(), (size_t) T * (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) {
@@ -3475,8 +3491,9 @@ int main(int argc, char** argv) {
}
int next = 0;
// Teacher-forced prompt rows consume no generation draws.
sp.counter = (uint64_t) produced.size();
// The draw is Philox(seed, position), as in a verify window (row t at pos0 draws pos0 + t): a seed gives
// the same text whether a token comes from this path or from the speculative loop below.
sp.counter = (uint64_t) pos;
strata::kernels::sample_tokens(d_logits, 1, (int) n_vocab, nullptr, 0, sp, d_next, token_stream);
if (cudaMemcpyAsync(&next, d_next, sizeof(int), cudaMemcpyDeviceToHost,
(cudaStream_t) token_stream) != cudaSuccess ||
@@ -3572,6 +3589,7 @@ int main(int argc, char** argv) {
return 1;
}
mem_mark("the verifier and the drafter's binding");
ver.set_sampling(sp); // the CLI's own sampling (until 0.1.19 this loop was always greedy); no penalties here
ver.set_split(o.spec_split);
// auto: the copy kernel for every pack. DMA (the native packs' default until 0.1.13) has the host call
// cudaMemcpyAsync + cudaLaunchHostFunc inside a verify window while the GPU spins on the flag they raise;