use crate::dry::DryParams;
use crate::sampler_order::SamplerOrder;
#[derive(Debug, Clone)]
pub struct SamplingParams {
pub temperature: f32,
pub top_p: f32,
pub min_p: f32,
pub top_k: usize,
pub typical_p: f32,
pub top_n_sigma: f32,
pub xtc_probability: f32,
pub xtc_threshold: f32,
pub dry: DryParams,
pub repetition_penalty: f32,
pub penalty_last_n: usize,
pub presence_penalty: f32,
pub frequency_penalty: f32,
pub sampler_order: SamplerOrder,
}
impl Default for SamplingParams {
fn default() -> Self {
SamplingParams {
temperature: 0.0,
top_p: 1.0,
min_p: 0.0,
top_k: 0,
typical_p: 1.0,
top_n_sigma: -1.0,
xtc_probability: 0.0,
xtc_threshold: 0.1,
dry: DryParams::off(),
repetition_penalty: 1.0,
penalty_last_n: 64,
presence_penalty: 0.0,
frequency_penalty: 0.0,
sampler_order: SamplerOrder::default(),
}
}
}
impl SamplingParams {
pub fn xtc_can_fire(&self) -> bool {
self.xtc_probability > 0.0 && self.xtc_threshold <= 0.5
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_new_sampler_defaults_to_its_own_no_op() {
let d = SamplingParams::default();
assert_eq!(d.typical_p, 1.0, "1.0 disables typical-p");
assert_eq!(d.top_n_sigma, -1.0, "<= 0 disables top-n-sigma");
assert_eq!(d.xtc_probability, 0.0, "0.0 disables xtc");
assert_eq!(d.xtc_threshold, 0.1, "llama.cpp's default threshold");
assert!(!d.xtc_can_fire());
assert!(!d.dry.is_enabled(), "dry_multiplier 0.0 disables dry");
}
#[test]
fn a_threshold_above_a_half_disables_xtc_as_surely_as_a_zero_probability() {
let live = SamplingParams {
xtc_probability: 0.5,
xtc_threshold: 0.1,
..SamplingParams::default()
};
assert!(live.xtc_can_fire());
assert!(!SamplingParams {
xtc_threshold: 0.51,
..live.clone()
}
.xtc_can_fire());
assert!(
SamplingParams {
xtc_threshold: 0.5,
..live.clone()
}
.xtc_can_fire(),
"0.5 itself is still live"
);
assert!(!SamplingParams {
xtc_probability: 0.0,
..live
}
.xtc_can_fire());
}
}