From 81601a512460e2ea1b91e54e7532c0af28e1dc0d Mon Sep 17 00:00:00 2001 From: Jakub Luwierski Date: Mon, 28 Sep 2026 15:11:44 +0200 Subject: [PATCH] sampler: an empty penalty window must not touch the unsized shared bitmap `use_bits` required only a non-null history, but the launch sizes the bitmap for `penalty_last_n > 0` alone - a caller handing over a stale history buffer with the penalties disabled made the kernel zero `bits_words` words of a ZERO-BYTE dynamic shared allocation. Unreachable from the serve path (set_history passes nullptr when the window is empty) and invisible to memcheck (the write lands in the unallocated SMEM window), but undefined per the contract of the public `sample_tokens`. The gate now also requires a non-empty window (`hlen > 0`, both kernels), and fixture 10 pins a stale history with last_n = 0 to the no-history result in both kernels. --- src/kernels/cuda/sampler.cu | 10 +++++++--- src/kernels/sampler_parity.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/kernels/cuda/sampler.cu b/src/kernels/cuda/sampler.cu index 55fe3aa9..e2c5a886 100644 --- a/src/kernels/cuda/sampler.cu +++ b/src/kernels/cuda/sampler.cu @@ -109,7 +109,9 @@ __global__ void sampler_greedy_kernel(const float* __restrict__ logits, int n_vo // the counts - and therefore every sampled value - are exactly what the per-candidate scan produced. extern __shared__ unsigned int penal_bits[]; const int bits_words = (int) ((n_vocab + 31) / 32); - const bool use_bits = hrow != nullptr && bits_words > 0; + // The gate needs a NON-EMPTY WINDOW (`hlen > 0`): the launch sizes the shared bitmap only when penalties + // are on, so a caller handing over a history buffer with `penalty_last_n == 0` must not touch it. + const bool use_bits = hrow != nullptr && hlen > 0 && bits_words > 0; if (use_bits) { for (int w = threadIdx.x; w < bits_words; w += blockDim.x) penal_bits[w] = 0u; __syncthreads(); @@ -188,10 +190,12 @@ __global__ void sampler_kernel(const float* __restrict__ logits, int n_vocab, in hrow += history_len - hlen; // the window is the TAIL } - // the membership bitmap, as in `sampler_greedy_kernel` - see the cost note there + // the membership bitmap, as in `sampler_greedy_kernel` - see the cost note there. The gate needs an + // EMPTY WINDOW too: the launch sizes the bitmap only when penalties are on, so a caller that hands over + // a stale history buffer with `penalty_last_n == 0` must not touch it. extern __shared__ unsigned int penal_bits[]; const int bits_words = (int) ((n_vocab + 31) / 32); - const bool use_bits = hrow != nullptr && bits_words > 0; + const bool use_bits = hrow != nullptr && hlen > 0 && bits_words > 0; if (use_bits) { for (int w = threadIdx.x; w < bits_words; w += blockDim.x) penal_bits[w] = 0u; __syncthreads(); diff --git a/src/kernels/sampler_parity.cpp b/src/kernels/sampler_parity.cpp index 3b1d8601..802ad2fc 100644 --- a/src/kernels/sampler_parity.cpp +++ b/src/kernels/sampler_parity.cpp @@ -621,6 +621,32 @@ int main(int argc, char** argv) { bad += run("sampled chain: top_p then min_p", l, NT10, p, want); } + // ---- fixture 10: A STALE HISTORY WITH last_n = 0 IS INERT. A caller can hand over a history buffer + // from a previous penalised request while this request disables the penalties - the run must equal the + // no-history run in both kernels, and the bitmap the launch did not size must stay untouched. + { + std::mt19937 rng(11); std::normal_distribution g(0.0f, 1.0f); + std::vector l((size_t) NV * NT); + for (auto& v : l) v = g(rng); + std::vector hist((size_t) NT * 8, -1); + for (int t = 0; t < NT; ++t) hist[(size_t) t * 8] = 3; + + strata::kernels::SamplerParams p; + p.top_k = 20; p.top_p = 0.95f; p.temperature = 0.8f; p.seed = 5; + std::vector want((size_t) NT); + for (int t = 0; t < NT; ++t) + want[(size_t) t] = sampled_reference({l.begin() + (size_t) t * NV, + l.begin() + (size_t) (t + 1) * NV}, {}, p, t); + bad += run("stale history, last_n=0 (sampled)", l, NT, p, want, hist, 8); + + strata::kernels::SamplerParams gp = p; gp.greedy = true; gp.top_k = 0; gp.top_p = 1.0f; + std::vector gwant((size_t) NT); + for (int t = 0; t < NT; ++t) + gwant[(size_t) t] = reference_pick({l.begin() + (size_t) t * NV, + l.begin() + (size_t) (t + 1) * NV}, gp, false); + bad += run("stale history, last_n=0 (greedy)", l, NT, gp, gwant, hist, 8); + } + // A continuous stream and individual decode calls consume the same draw counters. { constexpr int count = 32, vocab = 16;