memra_sampling/lib.rs
1//! Host-side sampler chain (BASE-2, MEMRA-BUILD-MAP §BASE-2). Ports llama.cpp CPU sampler
2//! semantics (llama-sampler.cpp): repetition/freq/presence penalties -> temperature -> top-k ->
3//! top-p -> min-p -> categorical draw. Greedy (temp<=0) = argmax, the bit-exact reference.
4//!
5//! Runs on the host over the full [n_vocab] f32 logit vector already brought back by the per-step
6//! D2H sync (decode.rs) — at B=2-4 this is single-µs, no GPU kernel needed (the GPU-fused sampler
7//! is a deferred PERF item, only needed once CUDA-graph removes the D2H barrier).
8
9use std::collections::HashMap;
10
11/// Sampler configuration. Defaults = greedy (temp 0). Order of application matches llama.cpp.
12#[derive(Clone, Debug)]
13pub struct SamplerConfig {
14 pub temperature: f32, // <= 0.0 => greedy argmax (penalties/top-k/p ignored)
15 pub top_k: usize, // 0 => disabled (keep all)
16 pub top_p: f32, // 1.0 => disabled
17 pub min_p: f32, // 0.0 => disabled
18 pub penalty_last_n: usize, // window of recent tokens for penalties (0 => disabled)
19 pub penalty_repeat: f32, // 1.0 => disabled (llama default 1.0)
20 pub penalty_freq: f32, // 0.0 => disabled
21 pub penalty_present: f32, // 0.0 => disabled
22 pub seed: u64,
23}
24
25impl Default for SamplerConfig {
26 fn default() -> Self {
27 SamplerConfig {
28 temperature: 0.0,
29 top_k: 0,
30 top_p: 1.0,
31 min_p: 0.0,
32 penalty_last_n: 0,
33 penalty_repeat: 1.0,
34 penalty_freq: 0.0,
35 penalty_present: 0.0,
36 seed: 0,
37 }
38 }
39}
40
41/// SESSION-RESUME SAMPLER IDENTITY (lane/session-resume-sampler-predicate-20260820; receipts
42/// `research/spec-cache-20260818/SESSION-RESUME-PREDICATE.md`).
43///
44/// The canonical form of a request's sampler, for exactly one question: **may this request resume
45/// a parked whole session that some OTHER request's sampler shaped?** The spec pool's resume probe
46/// compared prompts and never samplers — that omission is how a filtered request inherited a draft
47/// graph captured unfiltered (`memra-engine` `SampledGraphKey`, lane/graph-s-key-exactness-
48/// 20260819). Keying the graph closed the exactness hole; it did not make cross-sampler resume
49/// SOUND, and the house posture in that situation is refuse-on-ambiguity with the refusal naming
50/// itself. This type is that predicate.
51///
52/// CANONICALIZATION, and why each rule is safe. Two encodings that name the same program must
53/// compare equal, or the predicate refuses resumes that cost nothing to allow:
54/// - `temperature <= 0.0` is GREEDY — `-1.0` and `0.0` are one program, so `temp_bits` is pinned
55/// to `0.0` in that regime and `greedy` carries the distinction. The greedy/sampled flip is
56/// itself a refusal: the two arms consume a parked `next_pred`/`pending_tok` differently and
57/// engage different captured draft graphs.
58/// - `top_k == 0`, `top_p >= 1.0`, `min_p <= 0.0` are each the OFF sentinel (matching
59/// `is_spec_sampling` and `SampledGraphKey::pure_temp`), canonicalized so `top_p 1.5` and
60/// `top_p 1.0` do not look like a change.
61/// - Penalties are OFF as a group iff `penalty_last_n == 0` or all three coefficients are neutral
62/// — the same `pen_on` predicate `spec.rs` computes. Off canonicalizes to the whole disabled
63/// tuple, so `penalty_last_n 64` with neutral coefficients equals penalties absent.
64///
65/// Float fields compare by BITS after canonicalization (no NaN/`-0.0` surprise), the same
66/// discipline `SampledGraphKey` uses.
67///
68/// `seed` IS carried and DELIBERATELY NOT COMPARED — see [`SamplerIdentity::mismatch`].
69#[derive(Clone, Copy, PartialEq, Eq, Debug)]
70pub struct SamplerIdentity {
71 greedy: bool,
72 temp_bits: u32,
73 /// Carried for the record and for callers that want to log it; NOT part of `mismatch`.
74 seed: u64,
75 top_k: usize,
76 top_p_bits: u32,
77 min_p_bits: u32,
78 penalty_last_n: usize,
79 penalty_repeat_bits: u32,
80 penalty_freq_bits: u32,
81 penalty_present_bits: u32,
82}
83
84impl SamplerIdentity {
85 /// Canonical identity of a sampler configuration.
86 pub fn of(cfg: &SamplerConfig) -> Self {
87 let greedy = cfg.temperature <= 0.0;
88 let pen_on = cfg.penalty_last_n > 0
89 && (cfg.penalty_repeat != 1.0 || cfg.penalty_freq != 0.0 || cfg.penalty_present != 0.0);
90 SamplerIdentity {
91 greedy,
92 temp_bits: if greedy { 0.0f32 } else { cfg.temperature }.to_bits(),
93 seed: cfg.seed,
94 top_k: cfg.top_k,
95 top_p_bits: if cfg.top_p >= 1.0 { 1.0f32 } else { cfg.top_p }.to_bits(),
96 min_p_bits: if cfg.min_p <= 0.0 { 0.0f32 } else { cfg.min_p }.to_bits(),
97 penalty_last_n: if pen_on { cfg.penalty_last_n } else { 0 },
98 penalty_repeat_bits: if pen_on { cfg.penalty_repeat } else { 1.0f32 }.to_bits(),
99 penalty_freq_bits: if pen_on { cfg.penalty_freq } else { 0.0f32 }.to_bits(),
100 penalty_present_bits: if pen_on { cfg.penalty_present } else { 0.0f32 }.to_bits(),
101 }
102 }
103
104 /// The seed this identity was built from (logging/receipts only — never compared).
105 pub fn seed(&self) -> u64 {
106 self.seed
107 }
108
109 /// The FIRST field on which `self` (an incoming request) differs from `parked` (the sampler
110 /// that shaped a parked session), as a stable name for the refusal line — `None` when the two
111 /// samplers are equivalent and the resume is legal. A refusal that does not say why is
112 /// indistinguishable from an unwired mechanism, so the name is the deliverable, not a nicety.
113 ///
114 /// Order is fixed and coarsest-first (`regime` before the field that only exists inside one
115 /// regime), so the reported name is the most informative one rather than an artifact of struct
116 /// layout.
117 ///
118 /// **`seed` IS NOT COMPARED, deliberately.** It is the one sampler field a resume may change,
119 /// for two reasons that are both mechanical:
120 /// - The only parked state that BAKES the seed is the sampled draft graph, and
121 /// `SampledGraphKey` already carries `seed`: a seed change drops the parked graph and
122 /// recaptures. (`memra-engine` `spec.rs`; pinned by `seed_alone_still_rekeys_the_draft_graph`
123 /// in that crate's `sampled_graph_key` tests.)
124 /// - The session's persisted Philox counters (`SpecSession::sctr/uctr`) are counter-based:
125 /// `philox(seed', ctr)` continued from another seed's counter position is an independent
126 /// stream, not a repeated one. Reproducibility is already scoped per `(seed, session)`
127 /// rather than per seed (`memra-server` `worker.rs`, the spec-burst sampling note), so a
128 /// changed seed costs nothing that same-seed resume was not already costing.
129 ///
130 /// Comparing it would refuse essentially ALL sampled traffic: omitting `seed` on a serve
131 /// request draws fresh per-request entropy, so every turn of every seed-omitting conversation
132 /// would carry a "changed" seed. That is a cost with no soundness gain, which is exactly the
133 /// trade this predicate exists to make explicitly rather than by accident.
134 pub fn mismatch(&self, parked: &Self) -> Option<&'static str> {
135 if self.greedy != parked.greedy {
136 return Some("regime");
137 }
138 if self.temp_bits != parked.temp_bits {
139 return Some("temperature");
140 }
141 if self.top_k != parked.top_k {
142 return Some("top_k");
143 }
144 if self.top_p_bits != parked.top_p_bits {
145 return Some("top_p");
146 }
147 if self.min_p_bits != parked.min_p_bits {
148 return Some("min_p");
149 }
150 if self.penalty_last_n != parked.penalty_last_n {
151 return Some("penalty_last_n");
152 }
153 if self.penalty_repeat_bits != parked.penalty_repeat_bits {
154 return Some("penalty_repeat");
155 }
156 if self.penalty_freq_bits != parked.penalty_freq_bits {
157 return Some("penalty_freq");
158 }
159 if self.penalty_present_bits != parked.penalty_present_bits {
160 return Some("penalty_present");
161 }
162 None
163 }
164
165 /// THE PRE-LANE PREDICATE, RESTATED (teeth, not production). The spec pool-resume probe
166 /// applied no sampler test at all — it compared prompts and nothing else — so every sampler
167 /// pair was admitted. Restating it here keeps the refusal tests DECISIVE instead of
168 /// tautological: the same pair that `mismatch` names must be admitted by this, or the test is
169 /// asserting against a mechanism that never existed.
170 ///
171 /// It is also what `MEMRA_SPEC_RESUME_SAMPLER=0` selects at runtime (the rollback door and the
172 /// A/B arm the cost measurement needs), so this function is the single definition of "legacy"
173 /// for both the tests and the server.
174 pub fn legacy_admits(&self, _parked: &Self) -> bool {
175 true
176 }
177}
178
179/// Stateful sampler: owns the RNG + the recent-token history (for penalties).
180pub struct Sampler {
181 cfg: SamplerConfig,
182 rng: SplitMix64,
183 history: Vec<u32>, // recently emitted tokens (for penalty window)
184 // Counts over exactly the active penalty window. Keeping this incrementally makes the host
185 // oracle cheaper too, and lets the serving path upload O(unique ids) sparse penalty state
186 // instead of either the full vocabulary or an O(history^2) device-side dedup walk.
187 penalty_counts: HashMap<u32, u32>,
188}
189
190impl Sampler {
191 pub fn new(cfg: SamplerConfig) -> Self {
192 let rng = SplitMix64::new(cfg.seed);
193 Sampler {
194 cfg,
195 rng,
196 history: Vec::new(),
197 penalty_counts: HashMap::new(),
198 }
199 }
200
201 pub fn is_greedy(&self) -> bool {
202 self.cfg.temperature <= 0.0
203 }
204 /// Sampled spec in its FASTEST regime: pure temperature, no truncation filters, no
205 /// penalties. Filters and penalties are also distribution-exact under the rejection
206 /// verify (spec.rs applies both symmetrically to draft q and target p), so they remain
207 /// spec-ELIGIBLE — see `spec_eligible` in memra-server's worker, the authoritative
208 /// predicate. What they cost is the in-graph draft chain: the captured sampled draft
209 /// samples from the RAW softmax and can hold neither per-row filter stats nor a varying
210 /// penalty history, so `spec.rs` engages `graph_s` only in this pure-temp regime
211 /// (`pure_temp`) and otherwise falls back to the eager draft chain. This predicate names
212 /// that regime; it is NOT an eligibility test.
213 pub fn is_spec_sampling(&self) -> bool {
214 self.cfg.temperature > 0.0
215 && self.cfg.penalty_repeat == 1.0
216 && self.cfg.penalty_freq == 0.0
217 && self.cfg.penalty_present == 0.0
218 && self.cfg.top_k == 0
219 && self.cfg.top_p >= 1.0
220 && self.cfg.min_p <= 0.0
221 }
222 pub fn top_k(&self) -> usize {
223 self.cfg.top_k
224 }
225 pub fn penalty_last_n(&self) -> usize {
226 self.cfg.penalty_last_n
227 }
228 pub fn penalty_repeat(&self) -> f32 {
229 self.cfg.penalty_repeat
230 }
231 pub fn penalty_freq(&self) -> f32 {
232 self.cfg.penalty_freq
233 }
234 pub fn penalty_present(&self) -> f32 {
235 self.cfg.penalty_present
236 }
237 pub fn top_p(&self) -> f32 {
238 self.cfg.top_p
239 }
240 pub fn min_p(&self) -> f32 {
241 self.cfg.min_p
242 }
243 pub fn temperature(&self) -> f32 {
244 self.cfg.temperature
245 }
246 pub fn seed(&self) -> u64 {
247 self.cfg.seed
248 }
249 /// This sampler's canonical [`SamplerIdentity`] — the whole-session resume predicate's input.
250 pub fn identity(&self) -> SamplerIdentity {
251 SamplerIdentity::of(&self.cfg)
252 }
253
254 fn penalties_on(&self) -> bool {
255 self.cfg.penalty_last_n > 0
256 && (self.cfg.penalty_repeat != 1.0
257 || self.cfg.penalty_freq != 0.0
258 || self.cfg.penalty_present != 0.0)
259 }
260
261 /// Sparse `(token_id, count)` rows for the current penalty window. The order cannot affect
262 /// the arithmetic because every entry mutates one distinct logit; avoiding a per-token sort
263 /// is material on long agent histories.
264 pub fn penalty_counts(&self) -> Vec<(u32, u32)> {
265 debug_assert!(self.penalty_counts.values().all(|&count| count > 0));
266 self.penalty_counts
267 .iter()
268 .map(|(&id, &n)| (id, n))
269 .collect()
270 }
271
272 /// Record an emitted token so subsequent penalties see it.
273 pub fn accept(&mut self, token: u32) {
274 if self.penalties_on() {
275 let n = self.cfg.penalty_last_n;
276 if self.history.len() >= n {
277 let expired = self.history[self.history.len() - n];
278 let remove = {
279 let count = self
280 .penalty_counts
281 .get_mut(&expired)
282 .expect("active penalty window lost an accepted token");
283 *count -= 1;
284 *count == 0
285 };
286 if remove {
287 self.penalty_counts.remove(&expired);
288 }
289 }
290 *self.penalty_counts.entry(token).or_insert(0) += 1;
291 }
292 self.history.push(token);
293 }
294
295 /// Sample the next token id from raw logits [n_vocab]. Does NOT mutate logits in place beyond
296 /// a local copy. Returns the chosen token id. (Caller should `accept()` it afterwards.)
297 pub fn sample(&mut self, logits: &[f32]) -> u32 {
298 // Greedy fast path: argmax over RAW logits (penalties don't change the argmax direction
299 // enough to matter for the reference path; llama greedy is also pre-penalty argmax only
300 // when no penalties set — but to stay correct under penalties we still apply them first).
301 if self.is_greedy()
302 && self.cfg.penalty_repeat == 1.0
303 && self.cfg.penalty_freq == 0.0
304 && self.cfg.penalty_present == 0.0
305 {
306 return argmax_u32(logits);
307 }
308
309 // Work on (id, logit) candidates.
310 let mut cand: Vec<(u32, f32)> = logits
311 .iter()
312 .enumerate()
313 .map(|(i, &l)| (i as u32, l))
314 .collect();
315
316 // 1. Penalties (operate on logits, over the last-n history window).
317 // `cand` is DENSE and INDEX-ALIGNED here by construction (built from
318 // `logits.iter().enumerate()` immediately above, nothing has filtered it yet), so the
319 // penalty pass indexes straight into it instead of hashing every candidate. See
320 // `apply_penalties_dense`.
321 self.apply_penalties_dense(&mut cand);
322
323 // Greedy-with-penalties: argmax after penalties, no sampling.
324 if self.is_greedy() {
325 let mut best = cand[0];
326 for &c in &cand[1..] {
327 if c.1 > best.1 {
328 best = c;
329 }
330 }
331 return best.0;
332 }
333
334 // 2. Temperature scale.
335 if self.cfg.temperature > 0.0 && self.cfg.temperature != 1.0 {
336 let inv = 1.0 / self.cfg.temperature;
337 for c in cand.iter_mut() {
338 c.1 *= inv;
339 }
340 }
341
342 // 3. top-k: keep the k highest-logit candidates (partial sort by logit desc).
343 if self.cfg.top_k > 0 && self.cfg.top_k < cand.len() {
344 cand.sort_unstable_by(|a, b| b.1.total_cmp(&a.1));
345 cand.truncate(self.cfg.top_k);
346 }
347
348 // softmax over the surviving candidates (numerically stable).
349 softmax_inplace(&mut cand);
350
351 // 4. top-p (nucleus): smallest set whose cumulative prob >= top_p. Needs desc-by-prob order.
352 if self.cfg.top_p < 1.0 {
353 cand.sort_unstable_by(|a, b| b.1.total_cmp(&a.1));
354 let mut cum = 0.0f32;
355 let mut keep = 0usize;
356 for (i, c) in cand.iter().enumerate() {
357 cum += c.1;
358 keep = i + 1;
359 if cum >= self.cfg.top_p {
360 break;
361 }
362 }
363 cand.truncate(keep.max(1));
364 }
365
366 // 5. min-p: keep candidates with prob >= min_p * max_prob.
367 if self.cfg.min_p > 0.0 {
368 let maxp = cand.iter().map(|c| c.1).fold(0.0f32, f32::max);
369 let thresh = self.cfg.min_p * maxp;
370 cand.retain(|c| c.1 >= thresh);
371 if cand.is_empty() {
372 return argmax_u32(logits);
373 } // safety
374 }
375
376 // renormalize the surviving probs and draw.
377 let sum: f32 = cand.iter().map(|c| c.1).sum();
378 let r = self.rng.next_f32() * sum;
379 let mut acc = 0.0f32;
380 for c in &cand {
381 acc += c.1;
382 if acc >= r {
383 return c.0;
384 }
385 }
386 cand.last().unwrap().0
387 }
388
389 /// llama.cpp penalty over a DENSE, INDEX-ALIGNED candidate slice: `cand[i].0 == i`.
390 ///
391 /// Same arithmetic as `apply_penalties_scan_reference`, on exactly the same elements, in the
392 /// same order — but O(distinct penalized tokens) instead of O(n_vocab) hash lookups.
393 ///
394 /// WHY (lane/glm5-host-audit, 2026-09-01). The scan form did one `HashMap<u32,u32>` SipHash
395 /// probe PER CANDIDATE, i.e. one per vocabulary entry: ~152k probes per token on the Qwen
396 /// class. `penalty_counts` holds at most `PEN_WINDOW_MAX` distinct ids and in practice the
397 /// number of distinct generated tokens so far, so the loop was inverted the expensive way
398 /// round. This matters in production, not in theory: `devsample_meta` refuses the device
399 /// sampler for any penalized config unless `MEMRA_SERVE_DEVPENALTY=1`, the whole fleet runs
400 /// it at 0, and a served model whose VENDOR-RECOMMENDED non-thinking arm carries
401 /// `presence_penalty` therefore lands every token of every request on this function.
402 ///
403 /// BIT-IDENTICAL BY CONSTRUCTION, and gated as such rather than asserted in prose: the set of
404 /// touched entries is identical (`penalty_counts` covers exactly the window, which the
405 /// debug_assert below re-checks), the per-entry arithmetic is copied unchanged, and each
406 /// entry is touched exactly once in both forms, so no float re-association is possible.
407 /// `apply_penalties_scan_reference` is kept as the ORACLE and
408 /// `dense_penalties_match_the_scan_reference_bitwise` compares them over randomized inputs.
409 fn apply_penalties_dense(&self, cand: &mut [(u32, f32)]) {
410 let n = self.cfg.penalty_last_n;
411 if n == 0 {
412 return;
413 }
414 if self.cfg.penalty_repeat == 1.0
415 && self.cfg.penalty_freq == 0.0
416 && self.cfg.penalty_present == 0.0
417 {
418 return;
419 }
420 let start = self.history.len().saturating_sub(n);
421 let window = &self.history[start..];
422 if window.is_empty() {
423 return;
424 }
425 debug_assert_eq!(
426 self.penalty_counts
427 .values()
428 .map(|&n| n as usize)
429 .sum::<usize>(),
430 window.len(),
431 "incremental penalty counts must cover the active history window"
432 );
433 for (&id, &cnt) in self.penalty_counts.iter() {
434 let Some(c) = cand.get_mut(id as usize) else {
435 // A count for an id outside the logits row: a vocab/count mismatch, which is a
436 // caller bug rather than something to silently skip. Loud in debug, inert in
437 // release (dropping the penalty is strictly safer than indexing out of bounds).
438 debug_assert!(
439 false,
440 "penalty count for id {id} is outside the {} candidate row",
441 cand.len()
442 );
443 continue;
444 };
445 debug_assert_eq!(
446 c.0, id,
447 "apply_penalties_dense requires an index-aligned candidate slice"
448 );
449 // repeat: llama divides if logit>0 else multiplies (penalize toward 0)
450 if self.cfg.penalty_repeat != 1.0 {
451 if c.1 > 0.0 {
452 c.1 /= self.cfg.penalty_repeat;
453 } else {
454 c.1 *= self.cfg.penalty_repeat;
455 }
456 }
457 c.1 -= cnt as f32 * self.cfg.penalty_freq;
458 c.1 -= self.cfg.penalty_present; // presence: applied once if count>0
459 }
460 }
461
462 /// llama.cpp penalty: for each token in the last-n history, repeat-divide/multiply its logit
463 /// and apply frequency*count + presence. (llama-sampler.cpp penalties.)
464 ///
465 /// THE ORACLE, not the serving path. `apply_penalties_dense` replaced it on the hot path
466 /// 2026-09-01; this form is retained verbatim so the replacement has something to be proven
467 /// bit-identical against, which is worth more than deleting it.
468 #[cfg(test)]
469 fn apply_penalties_scan_reference(&self, cand: &mut [(u32, f32)]) {
470 let n = self.cfg.penalty_last_n;
471 if n == 0 {
472 return;
473 }
474 if self.cfg.penalty_repeat == 1.0
475 && self.cfg.penalty_freq == 0.0
476 && self.cfg.penalty_present == 0.0
477 {
478 return;
479 }
480 let start = self.history.len().saturating_sub(n);
481 let window = &self.history[start..];
482 if window.is_empty() {
483 return;
484 }
485 debug_assert_eq!(
486 self.penalty_counts
487 .values()
488 .map(|&n| n as usize)
489 .sum::<usize>(),
490 window.len(),
491 "incremental penalty counts must cover the active history window"
492 );
493 for c in cand.iter_mut() {
494 if let Some(&cnt) = self.penalty_counts.get(&c.0) {
495 // repeat: llama divides if logit>0 else multiplies (penalize toward 0)
496 if self.cfg.penalty_repeat != 1.0 {
497 if c.1 > 0.0 {
498 c.1 /= self.cfg.penalty_repeat;
499 } else {
500 c.1 *= self.cfg.penalty_repeat;
501 }
502 }
503 c.1 -= cnt as f32 * self.cfg.penalty_freq;
504 c.1 -= self.cfg.penalty_present; // presence: applied once if count>0
505 }
506 }
507 }
508}
509
510fn argmax_u32(logits: &[f32]) -> u32 {
511 let mut best = 0u32;
512 let mut bv = f32::NEG_INFINITY;
513 for (i, &v) in logits.iter().enumerate() {
514 if v > bv {
515 bv = v;
516 best = i as u32;
517 }
518 }
519 best
520}
521
522/// Stable softmax over candidate logits, writing probs back into the logit slot.
523fn softmax_inplace(cand: &mut [(u32, f32)]) {
524 let maxl = cand.iter().map(|c| c.1).fold(f32::NEG_INFINITY, f32::max);
525 let mut sum = 0.0f32;
526 for c in cand.iter_mut() {
527 let e = (c.1 - maxl).exp();
528 c.1 = e;
529 sum += e;
530 }
531 let inv = if sum > 0.0 { 1.0 / sum } else { 0.0 };
532 for c in cand.iter_mut() {
533 c.1 *= inv;
534 }
535}
536
537/// SplitMix64 — deterministic seedable RNG (so a fixed seed reproduces the token stream for the
538/// validation gate). Not crypto; fine for sampling.
539struct SplitMix64 {
540 state: u64,
541}
542impl SplitMix64 {
543 fn new(seed: u64) -> Self {
544 SplitMix64 {
545 state: seed.wrapping_add(0x9E3779B97F4A7C15),
546 }
547 }
548 fn next_u64(&mut self) -> u64 {
549 self.state = self.state.wrapping_add(0x9E3779B97F4A7C15);
550 let mut z = self.state;
551 z = (z ^ (z >> 30)).wrapping_mul(0xBF58476D1CE4E5B9);
552 z = (z ^ (z >> 27)).wrapping_mul(0x94D049BB133111EB);
553 z ^ (z >> 31)
554 }
555 /// uniform f32 in [0,1).
556 fn next_f32(&mut self) -> f32 {
557 // top 24 bits -> [0,1)
558 ((self.next_u64() >> 40) as f32) / (1u32 << 24) as f32
559 }
560}
561
562#[cfg(test)]
563mod tests {
564 use super::*;
565
566 fn sorted_counts(s: &Sampler) -> Vec<(u32, u32)> {
567 let mut counts = s.penalty_counts();
568 counts.sort_unstable_by_key(|&(id, _)| id);
569 counts
570 }
571
572 /// THE GATE for the 2026-09-01 host-cost fix (lane/glm5-host-audit): the dense penalty pass
573 /// must reproduce the O(n_vocab) scan reference BIT FOR BIT, not merely closely.
574 ///
575 /// Compared as raw bit patterns (`to_bits`), because `==` on f32 would call two different
576 /// NaNs equal and would call `-0.0` and `0.0` equal — and `penalty_present` subtraction can
577 /// produce exactly `-0.0`. The inputs deliberately include negative logits (the repeat rule
578 /// branches on sign), repeated tokens (frequency count > 1), a token never generated (must be
579 /// untouched), and the three penalty coefficients both neutral and active.
580 #[test]
581 fn dense_penalties_match_the_scan_reference_bitwise() {
582 // A small deterministic LCG: this gate must not depend on a dev-dependency.
583 let mut state: u64 = 0x9E3779B97F4A7C15;
584 let mut next = || {
585 state = state
586 .wrapping_mul(6364136223846793005)
587 .wrapping_add(1442695040888963407);
588 ((state >> 33) as u32) as f32 / (u32::MAX >> 1) as f32 - 1.0
589 };
590
591 let vocab = 257usize; // prime-ish, and > any window used below
592 let mut cases = 0;
593 for &(repeat, freq, present) in &[
594 (1.0f32, 0.0f32, 0.0f32), // all neutral: both forms must no-op
595 (1.1, 0.0, 0.0), // repeat only (exercises the sign branch)
596 (1.0, 0.5, 0.0), // frequency only (count-scaled)
597 (1.0, 0.0, 1.5), // presence only — the q38 non-thinking vendor arm
598 (1.1, 0.5, 1.5), // all three together
599 (0.8, -0.25, -0.5), // below/negative coefficients are legal API values
600 ] {
601 for &window in &[1usize, 3, 64, 8192] {
602 let cfg = SamplerConfig {
603 temperature: 1.0,
604 penalty_last_n: window,
605 penalty_repeat: repeat,
606 penalty_freq: freq,
607 penalty_present: present,
608 ..SamplerConfig::default()
609 };
610 let mut s = Sampler::new(cfg);
611 // Feed a history with repeats, and never feed id 0 or id 256 so the "untouched
612 // candidate" case is covered at both ends of the row.
613 for step in 0..40u32 {
614 s.accept(1 + (step * 7) % 200);
615 s.accept(1 + (step * 3) % 50); // guarantees counts > 1
616 }
617
618 let base: Vec<(u32, f32)> = (0..vocab).map(|i| (i as u32, next() * 8.0)).collect();
619 let mut dense = base.clone();
620 let mut reference = base.clone();
621 s.apply_penalties_dense(&mut dense);
622 s.apply_penalties_scan_reference(&mut reference);
623
624 assert_eq!(dense.len(), reference.len());
625 for (i, (d, r)) in dense.iter().zip(reference.iter()).enumerate() {
626 assert_eq!(d.0, r.0, "candidate id moved at {i}");
627 assert_eq!(
628 d.1.to_bits(),
629 r.1.to_bits(),
630 "BIT DIVERGENCE at id {i} (repeat={repeat} freq={freq} \
631 present={present} window={window}): dense {} vs reference {}",
632 d.1,
633 r.1
634 );
635 }
636 // Ids never generated must be untouched — otherwise "identical" could be two
637 // equally-wrong passes.
638 assert_eq!(
639 dense[0].1.to_bits(),
640 base[0].1.to_bits(),
641 "id 0 was penalized"
642 );
643 assert_eq!(
644 dense[256].1.to_bits(),
645 base[256].1.to_bits(),
646 "id 256 was penalized"
647 );
648 cases += 1;
649 }
650 }
651 assert_eq!(cases, 24, "the coefficient x window matrix must all run");
652 }
653
654 /// The precondition the dense form trades correctness for speed on: the candidate row it is
655 /// handed is dense and index-aligned. Asserted on the REAL call path (`sample`), so a future
656 /// caller that filters before penalizing cannot quietly pass.
657 #[test]
658 fn the_sampled_path_penalizes_the_token_it_generated() {
659 let cfg = SamplerConfig {
660 temperature: 1.0,
661 penalty_last_n: 64,
662 penalty_present: 100.0, // large enough that the penalized id cannot win
663 seed: 7,
664 ..SamplerConfig::default()
665 };
666 let mut s = Sampler::new(cfg);
667 // Logits that make id 2 the runaway favourite, then penalize exactly id 2.
668 let logits = vec![0.0, 0.0, 20.0, 0.0];
669 s.accept(2);
670 for _ in 0..32 {
671 assert_ne!(
672 s.sample(&logits),
673 2,
674 "presence penalty on the generated id must move the draw off it, which only \
675 happens if the dense pass indexed the right candidate"
676 );
677 }
678 }
679
680 #[test]
681 fn greedy_is_argmax() {
682 let mut s = Sampler::new(SamplerConfig::default()); // temp 0
683 let logits = vec![0.1, 5.0, 2.0, -1.0];
684 assert_eq!(s.sample(&logits), 1);
685 }
686
687 #[test]
688 fn temp_sampling_deterministic_with_seed() {
689 let cfg = SamplerConfig {
690 temperature: 1.0,
691 seed: 42,
692 ..Default::default()
693 };
694 let logits = vec![1.0, 2.0, 3.0, 0.5];
695 let a = Sampler::new(cfg.clone()).sample(&logits);
696 let b = Sampler::new(cfg).sample(&logits);
697 assert_eq!(a, b, "same seed must reproduce the draw");
698 assert!(a < 4);
699 }
700
701 #[test]
702 fn top_k_one_is_argmax() {
703 let cfg = SamplerConfig {
704 temperature: 1.0,
705 top_k: 1,
706 seed: 7,
707 ..Default::default()
708 };
709 let logits = vec![0.1, 5.0, 2.0, -1.0];
710 assert_eq!(
711 Sampler::new(cfg).sample(&logits),
712 1,
713 "top_k=1 collapses to argmax"
714 );
715 }
716
717 #[test]
718 fn min_p_keeps_only_high_prob() {
719 // logit 10 dominates; min_p 0.5 should drop the rest -> always pick id 2.
720 let cfg = SamplerConfig {
721 temperature: 1.0,
722 min_p: 0.5,
723 seed: 3,
724 ..Default::default()
725 };
726 let logits = vec![0.0, 0.0, 10.0, 0.0];
727 for _ in 0..16 {
728 assert_eq!(Sampler::new(cfg.clone()).sample(&logits), 2);
729 }
730 }
731
732 #[test]
733 fn repeat_penalty_suppresses_recent() {
734 // greedy + heavy repeat penalty: id 1 is argmax but recently emitted -> should drop it.
735 let cfg = SamplerConfig {
736 penalty_last_n: 8,
737 penalty_repeat: 100.0,
738 ..Default::default()
739 };
740 let mut s = Sampler::new(cfg);
741 s.accept(1); // 1 was just emitted
742 let logits = vec![4.0, 5.0, 4.5, 1.0]; // raw argmax = 1
743 let got = s.sample(&logits);
744 assert_ne!(
745 got, 1,
746 "recent token must be penalized out of greedy argmax"
747 );
748 assert_eq!(got, 2, "next-highest after penalizing 1");
749 }
750
751 #[test]
752 fn penalty_counts_follow_the_sliding_window() {
753 let cfg = SamplerConfig {
754 penalty_last_n: 4,
755 penalty_repeat: 1.1,
756 penalty_freq: 0.5,
757 penalty_present: 0.25,
758 ..Default::default()
759 };
760 let mut s = Sampler::new(cfg);
761 for tok in [7, 8, 7, 9] {
762 s.accept(tok);
763 }
764 assert_eq!(sorted_counts(&s), vec![(7, 2), (8, 1), (9, 1)]);
765
766 s.accept(8); // active window is now [8, 7, 9, 8]
767 assert_eq!(sorted_counts(&s), vec![(7, 1), (8, 2), (9, 1)]);
768 s.accept(10); // active window is now [7, 9, 8, 10]
769 assert_eq!(sorted_counts(&s), vec![(7, 1), (8, 1), (9, 1), (10, 1)]);
770 s.accept(11); // active window is now [9, 8, 10, 11]; final 7 expires
771 assert_eq!(sorted_counts(&s), vec![(8, 1), (9, 1), (10, 1), (11, 1)]);
772 assert!(!s.penalty_counts().iter().any(|&(id, n)| id == 7 || n == 0));
773 }
774
775 #[test]
776 fn full_context_and_disabled_penalty_counts_are_exact() {
777 let mut full = Sampler::new(SamplerConfig {
778 penalty_last_n: usize::MAX,
779 penalty_present: 1.5,
780 ..Default::default()
781 });
782 for tok in [3, 3, 4, 5, 3] {
783 full.accept(tok);
784 }
785 assert_eq!(sorted_counts(&full), vec![(3, 3), (4, 1), (5, 1)]);
786
787 let mut neutral = Sampler::new(SamplerConfig {
788 penalty_last_n: usize::MAX,
789 ..Default::default()
790 });
791 neutral.accept(3);
792 assert!(neutral.penalty_counts().is_empty());
793 }
794}
795
796/// SESSION-RESUME SAMPLER PREDICATE teeth (lane/session-resume-sampler-predicate-20260820).
797/// CPU-only, no GPU: the predicate is a pure function, so its whole contract is testable here and
798/// a regression cannot hide behind "needs a card".
799///
800/// TEETH BOTH DIRECTIONS is the point. Every refusal test also asserts that `legacy_admits`
801/// ADMITS the same pair — the pre-lane probe compared prompts and never samplers — so the test is
802/// decisive (it fails on the old code) rather than tautological (passing because the pair was
803/// never resumable for some other reason).
804#[cfg(test)]
805mod resume_sampler_predicate_tests {
806 use super::*;
807
808 /// The vendor-default sampled shape the flip makes the majority of traffic.
809 fn vendor() -> SamplerConfig {
810 SamplerConfig {
811 temperature: 0.7,
812 top_k: 20,
813 top_p: 0.95,
814 seed: 20260820,
815 ..Default::default()
816 }
817 }
818
819 /// Today's pure-temp shape — the one that parks a `graph_s`.
820 fn pure_temp() -> SamplerConfig {
821 SamplerConfig {
822 temperature: 0.7,
823 seed: 20260820,
824 ..Default::default()
825 }
826 }
827
828 fn id(cfg: &SamplerConfig) -> SamplerIdentity {
829 SamplerIdentity::of(cfg)
830 }
831
832 // ---- direction 1: a SAME-sampler resume still resumes (no regression) ----
833
834 #[test]
835 fn identical_sampler_resumes() {
836 for cfg in [pure_temp(), vendor(), SamplerConfig::default()] {
837 assert_eq!(
838 id(&cfg).mismatch(&id(&cfg)),
839 None,
840 "a request must resume a session its own sampler shaped: {cfg:?}"
841 );
842 }
843 }
844
845 #[test]
846 fn disabled_sentinels_are_the_same_program() {
847 // top_p >= 1.0, min_p <= 0.0, top_k == 0 all mean OFF; a client that spells OFF
848 // differently on turn 2 must not lose its cache.
849 let a = SamplerConfig {
850 temperature: 0.7,
851 top_p: 1.0,
852 min_p: 0.0,
853 ..Default::default()
854 };
855 let b = SamplerConfig {
856 temperature: 0.7,
857 top_p: 1.5,
858 min_p: -1.0,
859 ..Default::default()
860 };
861 assert_eq!(id(&a).mismatch(&id(&b)), None, "off spelled two ways");
862 }
863
864 #[test]
865 fn greedy_temperature_encodings_are_one_program() {
866 let a = SamplerConfig {
867 temperature: 0.0,
868 ..Default::default()
869 };
870 let b = SamplerConfig {
871 temperature: -1.0,
872 ..Default::default()
873 };
874 assert_eq!(id(&a).mismatch(&id(&b)), None, "temp<=0 is one regime");
875 }
876
877 #[test]
878 fn neutral_penalty_coefficients_equal_penalties_absent() {
879 // penalty_last_n set but every coefficient neutral == `pen_on == false` in spec.rs.
880 let a = SamplerConfig {
881 temperature: 0.7,
882 penalty_last_n: 64,
883 penalty_repeat: 1.0,
884 penalty_freq: 0.0,
885 penalty_present: 0.0,
886 ..Default::default()
887 };
888 let b = SamplerConfig {
889 temperature: 0.7,
890 penalty_last_n: 0,
891 ..Default::default()
892 };
893 assert_eq!(
894 id(&a).mismatch(&id(&b)),
895 None,
896 "an inert penalty window is not a penalty change"
897 );
898 }
899
900 // ---- direction 2: a sampler-DIFFERING resume refuses, and names the field ----
901
902 #[test]
903 fn the_reproduced_collision_pair_refuses_and_names_a_filter() {
904 // The exact pair the predecessor reproduced on a live server: turn 1 pure-temp parks,
905 // turn 2 adds top_p 0.95 / top_k 20 and resumes. Same seed, same temperature.
906 let parked = id(&pure_temp());
907 let incoming = id(&vendor());
908 let field = incoming
909 .mismatch(&parked)
910 .expect("the reproduced collision pair must refuse");
911 assert_eq!(field, "top_k", "coarsest-first order names top_k here");
912 // DECISIVE: the pre-lane probe admitted exactly this pair.
913 assert!(
914 incoming.legacy_admits(&parked),
915 "legacy must admit the collision pair, or this test proves nothing"
916 );
917 }
918
919 #[test]
920 fn every_compared_field_refuses_on_its_own_and_names_itself() {
921 let base = pure_temp();
922 // A penalized base, so the three coefficients can each move ALONE: with penalties off on
923 // the parked side, turning any of them on also moves `penalty_last_n`, and coarsest-first
924 // order would (correctly) name the window instead of the coefficient.
925 let pen_base = SamplerConfig {
926 penalty_last_n: 64,
927 penalty_repeat: 1.1,
928 penalty_freq: 0.5,
929 penalty_present: 0.5,
930 ..base.clone()
931 };
932 // (field, parked, incoming) — exactly one canonical field differs in each row.
933 let cases: [(&str, SamplerConfig, SamplerConfig); 9] = [
934 (
935 "regime",
936 base.clone(),
937 SamplerConfig {
938 temperature: 0.0,
939 ..base.clone()
940 },
941 ),
942 (
943 "temperature",
944 base.clone(),
945 SamplerConfig {
946 temperature: 0.8,
947 ..base.clone()
948 },
949 ),
950 (
951 "top_k",
952 base.clone(),
953 SamplerConfig {
954 top_k: 20,
955 ..base.clone()
956 },
957 ),
958 (
959 "top_p",
960 base.clone(),
961 SamplerConfig {
962 top_p: 0.95,
963 ..base.clone()
964 },
965 ),
966 (
967 "min_p",
968 base.clone(),
969 SamplerConfig {
970 min_p: 0.05,
971 ..base.clone()
972 },
973 ),
974 (
975 "penalty_last_n",
976 pen_base.clone(),
977 SamplerConfig {
978 penalty_last_n: 128,
979 ..pen_base.clone()
980 },
981 ),
982 (
983 "penalty_repeat",
984 pen_base.clone(),
985 SamplerConfig {
986 penalty_repeat: 1.2,
987 ..pen_base.clone()
988 },
989 ),
990 (
991 "penalty_freq",
992 pen_base.clone(),
993 SamplerConfig {
994 penalty_freq: 0.6,
995 ..pen_base.clone()
996 },
997 ),
998 (
999 "penalty_present",
1000 pen_base.clone(),
1001 SamplerConfig {
1002 penalty_present: 0.6,
1003 ..pen_base.clone()
1004 },
1005 ),
1006 ];
1007 for (expect, parked_cfg, cfg) in cases {
1008 let parked = id(&parked_cfg);
1009 let incoming = id(&cfg);
1010 assert_eq!(
1011 incoming.mismatch(&parked),
1012 Some(expect),
1013 "changing {expect} alone must refuse and name {expect} ({cfg:?})"
1014 );
1015 assert!(
1016 incoming.legacy_admits(&parked),
1017 "legacy must admit the {expect} change, or the refusal test is tautological"
1018 );
1019 }
1020 // Turning penalties ON from an unpenalized parked session is a `penalty_last_n` refusal —
1021 // the coarsest true statement about that pair, asserted so the order is pinned.
1022 assert_eq!(
1023 id(&pen_base).mismatch(&id(&base)),
1024 Some("penalty_last_n"),
1025 "penalties on vs off is named at the window, not at a coefficient"
1026 );
1027 }
1028
1029 #[test]
1030 fn greedy_to_sampled_and_back_both_refuse_as_regime() {
1031 let g = id(&SamplerConfig::default());
1032 let s = id(&pure_temp());
1033 assert_eq!(s.mismatch(&g), Some("regime"));
1034 assert_eq!(g.mismatch(&s), Some("regime"));
1035 }
1036
1037 // ---- the seed decision, pinned so it cannot change silently ----
1038
1039 #[test]
1040 fn seed_alone_does_not_refuse() {
1041 // DELIBERATE (see SamplerIdentity::mismatch): the draft graph is re-keyed on seed by
1042 // SampledGraphKey and the session's Philox counters are counter-based, so a changed seed
1043 // is sound — and comparing it would refuse every seed-omitting sampled conversation,
1044 // because an omitted seed draws fresh per-request entropy.
1045 let a = pure_temp();
1046 let b = SamplerConfig {
1047 seed: 999,
1048 ..a.clone()
1049 };
1050 assert_eq!(
1051 id(&a).mismatch(&id(&b)),
1052 None,
1053 "seed is carried but not compared"
1054 );
1055 assert_ne!(id(&a).seed(), id(&b).seed(), "the seed is still recorded");
1056 }
1057
1058 #[test]
1059 fn mismatch_is_symmetric_and_identity_is_an_equivalence() {
1060 let a = id(&pure_temp());
1061 let b = id(&vendor());
1062 assert_eq!(a.mismatch(&b).is_some(), b.mismatch(&a).is_some());
1063 assert_eq!(a.mismatch(&a), None);
1064 assert_eq!(b.mismatch(&b), None);
1065 }
1066}