Files
leleka/crates/sampler-ref/tests/gen_keys.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 45bad22b88 feat(sampler-ref): CPU reference of the Original sampler chain with host-oracle fixtures
New pure-Rust crate leleka-sampler-ref: Philox draw, penalties -> top_k ->
top_p -> min_p -> temperature -> inverse-CDF pick, greedy argmax, coupled-draft
counter/ring math, penalty_rows, exact-match acceptance, and the request-level
SamplingParams validation with the Original's GEN defaults and wire keys.

Golden fixtures come from compiling the Original's sampler_parity.cpp host
reference (included unmodified) and sampler/coupled_draft headers with clang++
(-ffp-contract=off), plus the verbatim StrataEngine.sampling_keys lifted from
server.py. No GPU, no speed or quality claim.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 14:42:51 +02:00

88 lines
3.2 KiB
Rust

//! `RequestSampling::gen_keys` against the strings the Original's Python
//! `StrataEngine.sampling_keys` emits (fixture produced by
//! `oracle/gen_sampling_keys.py`, which lifts that function verbatim).
use leleka_sampler_ref::params::{RequestSampling, SamplingParams};
use serde_json::Value;
fn request(v: &Value) -> RequestSampling {
let f = |k: &str| v.get(k).and_then(Value::as_f64);
let i = |k: &str| v.get(k).and_then(Value::as_i64);
RequestSampling {
temperature: f("temperature"),
top_p: f("top_p"),
top_k: i("top_k"),
min_p: f("min_p"),
repetition_penalty: f("repetition_penalty"),
frequency_penalty: f("frequency_penalty"),
presence_penalty: f("presence_penalty"),
penalty_last_n: i("penalty_last_n"),
seed: v.get("seed").and_then(Value::as_u64),
}
}
#[test]
fn gen_keys_equal_the_originals_python_output() {
let fixture: Value = serde_json::from_str(include_str!("fixtures/sampling_keys.json")).unwrap();
let cases = fixture["cases"].as_array().unwrap();
assert!(cases.len() >= 35);
let mut rejected = 0;
for case in cases {
let req = request(&case["request"]);
let want = case["keys"].as_str().unwrap();
match req.gen_keys() {
Ok(got) => assert_eq!(got, want, "request {}", case["request"]),
Err(error) => {
// The Original silently emits keys for out-of-contract values; this port refuses them.
// Only the deliberately out-of-range temperatures may be refused.
assert!(
req.temperature.is_some_and(|t| t > 2.0),
"unexpected refusal: {error}"
);
rejected += 1;
}
}
}
assert_eq!(
rejected, 1,
"exactly the temperature=1e16 case is out of the request contract"
);
}
#[test]
fn engine_floats_are_what_strtof_reads_from_the_wire_text() {
// Every float the engine would parse from the emitted text equals the f32 SamplingParams carries.
let fixture: Value = serde_json::from_str(include_str!("fixtures/sampling_keys.json")).unwrap();
for case in fixture["cases"].as_array().unwrap() {
let req = request(&case["request"]);
let Ok(keys) = req.gen_keys() else { continue };
let sp = SamplingParams::resolve(&req).unwrap();
let wire = |name: &str| -> Option<f32> {
keys.split(' ').find_map(|tok| {
tok.strip_prefix(name)?
.strip_prefix('=')?
.parse::<f32>()
.ok()
})
};
assert_eq!(wire("temperature").unwrap_or(0.0), sp.temperature, "{keys}");
assert_eq!(wire("top_p").unwrap_or(1.0), sp.top_p, "{keys}");
assert_eq!(wire("min_p").unwrap_or(0.0), sp.min_p, "{keys}");
assert_eq!(
wire("penalty_repeat").unwrap_or(1.0),
sp.penalty_repeat,
"{keys}"
);
assert_eq!(
wire("penalty_freq").unwrap_or(0.0),
sp.penalty_freq,
"{keys}"
);
assert_eq!(
wire("penalty_present").unwrap_or(0.0),
sp.penalty_present,
"{keys}"
);
}
}