Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Reject invalid repetition penalty settings (#878)

pawan27aad23e8347
diff --git a/crates/legacy/cli/src/interactive/flows/settings.rs b/crates/legacy/cli/src/interactive/flows/settings.rs
index 3f97e997d..21a113ec6 100644
--- a/crates/legacy/cli/src/interactive/flows/settings.rs
+++ b/crates/legacy/cli/src/interactive/flows/settings.rs
@@ -20,6 +20,7 @@ const TOP_K_STEP: i64 = 1;
const TOP_K_MIN: i64 = 1;
const TOP_K_MAX: i64 = 4096;
const REPETITION_PENALTY_STEP: f64 = 0.05;
+const REPETITION_PENALTY_MIN: f64 = 1.0;
const REPETITION_PENALTY_MAX: f64 = 2.0;
const SUFFIX_REPETITION_LENGTH_STEP: i64 = 32;
const SUFFIX_REPETITION_LENGTH_MIN: i64 = 32;
@@ -104,7 +105,7 @@ fn adjust(
preferences.sampling.repetition_penalty,
delta,
REPETITION_PENALTY_STEP,
- 1.0,
+ REPETITION_PENALTY_MIN,
REPETITION_PENALTY_MAX,
);
},
@@ -144,6 +145,8 @@ fn toggle(
{
sampling.suffix_repetition_length = value;
}
+ sampling.repetition_penalty = sampling.repetition_penalty.max(REPETITION_PENALTY_MIN);
+ sampling.suffix_repetition_length = sampling.suffix_repetition_length.max(SUFFIX_REPETITION_LENGTH_MIN);
},
Field::SuffixRepetitionLength => {
assert!(sampling.suffix_repetition_length_enabled(), "suffix_repetition_length cannot be enabled");
diff --git a/crates/legacy/uzu/src/engine/bridge/helpers.rs b/crates/legacy/uzu/src/engine/bridge/helpers.rs
index 882c2e058..df9f03fec 100644
--- a/crates/legacy/uzu/src/engine/bridge/helpers.rs
+++ b/crates/legacy/uzu/src/engine/bridge/helpers.rs
@@ -133,6 +133,19 @@ fn get_custom_sampling_method(method: &ShojiSamplingMethod) -> Result<UzuSamplin
"stochastic sampling needs temperature > 0, got {temperature}, use greedy sampling instead"
));
}
+ if let Some(repetition_penalty) = repetition_penalty
+ && (repetition_penalty.is_nan() || *repetition_penalty <= 0.0)
+ {
+ return Err(format!("repetition_penalty must be > 0, got {repetition_penalty}"));
+ }
+ if let Some(suffix_repetition_length) = suffix_repetition_length
+ && *suffix_repetition_length < 1
+ {
+ return Err(format!("suffix_repetition_length must be >= 1, got {suffix_repetition_length}"));
+ }
+ if repetition_penalty.is_some() && suffix_repetition_length.is_none() {
+ return Err("repetition_penalty needs suffix_repetition_length".to_string());
+ }
Ok(UzuSamplingMethod::Stochastic {
temperature: temperature.map(|value| value as f32),
top_k: top_k.map(|value| value as u32),
@@ -219,4 +232,41 @@ mod sampling_tests {
));
}
}
+
+ fn with_repetition(
+ repetition_penalty: Option<f64>,
+ suffix_repetition_length: Option<i64>,
+ ) -> ShojiSamplingMethod {
+ ShojiSamplingMethod::Stochastic {
+ temperature: Some(0.7),
+ top_k: None,
+ top_p: Some(0.9),
+ min_p: None,
+ repetition_penalty,
+ suffix_repetition_length,
+ }
+ }
+
+ #[test]
+ fn stochastic_rejects_invalid_repetition_settings() {
+ for (penalty, suffix) in [
+ (Some(0.0), Some(64)),
+ (Some(f64::NAN), Some(64)),
+ (Some(1.1), Some(0)),
+ (None, Some(0)),
+ (Some(1.1), None),
+ ] {
+ assert!(get_custom_sampling_method(&with_repetition(penalty, suffix)).is_err(), "{penalty:?} {suffix:?}");
+ }
+ }
+
+ #[test]
+ fn stochastic_accepts_valid_repetition_settings() {
+ for (penalty, suffix) in [(Some(1.1), Some(64)), (None, None), (None, Some(64))] {
+ assert!(matches!(
+ get_custom_sampling_method(&with_repetition(penalty, suffix)),
+ Ok(UzuSamplingMethod::Stochastic { .. })
+ ));
+ }
+ }
}