diff --git a/include/strata/kernels/sampler.hpp b/include/strata/kernels/sampler.hpp index a3121ba..83da53b 100644 --- a/include/strata/kernels/sampler.hpp +++ b/include/strata/kernels/sampler.hpp @@ -25,10 +25,41 @@ struct SamplerParams { uint64_t seed = 0; // drives Philox, which is counter-based on (seed, token index) uint64_t counter = 0; // absolute draw index of row 0; advance across decode calls bool greedy = false; + // DRY (llama.cpp's `dry` sampler, applied straight after the penalties above): a token that would EXTEND a + // repeat longer than `dry_allowed_length` loses `dry_multiplier * dry_base^(repeat - allowed_length)`, over + // the last `dry_penalty_last_n` history tokens. Restart sequences cap the repeat length. `dry_multiplier` + // 0, `dry_base` < 1, or `dry_penalty_last_n` 0 disables it (llama.cpp's `dry_enabled`). Only the last + // `kDryWindowCap` window tokens are examined; a longer `dry_penalty_last_n` is clamped there. + float dry_multiplier = 0.0f; + float dry_base = 1.75f; + int dry_allowed_length = 2; + int dry_penalty_last_n = 0; // 0 disables; the window over `history`, the same row the penalties count const float* logit_bias = nullptr; // optional device array [n_vocab], added BEFORE penalties/filters; -inf bans bool gumbel = false; // STRATA_SPEC_GUMBEL=1: Gumbel-max pick keyed by (seed, counter, token id) - see sampler.cu }; +// The DRY Z-algorithm runs in DEVICE SHARED memory, one window per row: the cap bounds that window (and the +// sequence-breaker scan). llama.cpp's default is 256; the engine's penalty window may be wider (up to 4096). +constexpr int kDryWindowCap = 512; + +// DRY sequence breakers, tokenized (llama.cpp's `dry_processed_breakers`): `n` restart sequences, sequence k is +// `flat[off[k] .. off[k+1])`, its head `flat[off[k]]` and its tail the rest. A head with an empty tail is a +// single-token sequence and is never penalised. DEVICE pointers; `n == 0` means no breakers. +struct DryBreakers { + const int* flat = nullptr; + const int* off = nullptr; + int n = 0; +}; + +// `dry_enabled` (llama.cpp's condition): the multiplier, the base and a non-zero window must all agree. Both the +// host launch path and the device kernels call it, so it is host-and-device (the header is not CUDA-only). +#if defined(__CUDACC__) +__host__ __device__ +#endif +inline bool dry_enabled(const SamplerParams& p) { + return p.dry_multiplier != 0.0f && p.dry_base >= 1.0f && p.dry_penalty_last_n != 0; +} + // logits (n_tokens, n_vocab) -> one sampled token id per row in `out`. // // **`logits` AND `out` ARE DEVICE POINTERS.** This is a CUDA kernel launch, not a host function, and nothing