Files
leleka/crates/sampler-ref/tests/gen_keys.rs
T
Pavlo Ratushnyi da8c974f68 Drop the stale engine/strata paths and retired generator names from comments, doc strings and generated fixtures (STRA-407)
Rust comments that named scripts/offline/*, gen_goldens.py, oracle/gen_*.{sh,cpp,py}, build_oracle.sh and similar retired generators now say where the data
came from (the Original's code, Strata 41cf05a0) or drop the sentence. The path/generator text of seven generated fixtures (sampler_oracle.json, sampling_keys.json,
memory_plan_golden.json, rope_golden.json, cvec_oracle.json, stra161 fixtures.json, commentary-tool-prompt.json) is reworded; no test reads those fields and no pin
covers their bytes. cmd_time/doc.rs: the two path texts in the format string; its lines are re-broken so they stay within 120 columns (the string is unchanged
otherwise). Comment-only except those data/string spots; no behaviour change.
2026-11-23 22:37:17 +01:00

68 lines
3.0 KiB
Rust

//! `RequestSampling::gen_keys` against the strings the Original's Python
//! `StrataEngine.sampling_keys` emits (fixture produced by
//! lifting 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");
}
/// The float a key contributes to the wire text, as the engine's `strtof` reads it.
fn wire_float(keys: &str, name: &str) -> Option<f32> {
keys.split(' ').find_map(|tok| tok.strip_prefix(name)?.strip_prefix('=')?.parse::<f32>().ok())
}
#[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| wire_float(&keys, name);
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}");
}
}