Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

sampler: an empty penalty window must not touch the unsized shared bitmap

Jakub Luwierski81601a512460
diff --git a/src/kernels/cuda/sampler.cu b/src/kernels/cuda/sampler.cu
index 55fe3aa92..e2c5a8862 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 3b1d86017..802ad2fc0 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<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;