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.
This commit is contained in:
Jakub Luwierski authored and Niko1221 committed 2026-09-28 15:46:00 +02:00
1 parent ec4cebfe52
commit 81601a5124
2 files changed
+33 -3

No files matched your search

+7 -3
View File
@@ -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();
+26
View File
@@ -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<float> g(0.0f, 1.0f);
std::vector<float> l((size_t) NV * NT);
for (auto& v : l) v = g(rng);
std::vector<int> 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<int> 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<int> 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;