Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

reject zero temperature for stochastic sampling (#869)

pawanf17eb1701843
diff --git a/crates/legacy/cli/src/interactive/flows/settings.rs b/crates/legacy/cli/src/interactive/flows/settings.rs
index e042ab651..3f97e997d 100644
--- a/crates/legacy/cli/src/interactive/flows/settings.rs
+++ b/crates/legacy/cli/src/interactive/flows/settings.rs
@@ -12,6 +12,7 @@ use crate::{
const LABEL_WIDTH: u16 = 14;
const TEMPERATURE_STEP: f64 = 0.05;
+const TEMPERATURE_MIN: f64 = TEMPERATURE_STEP;
const TEMPERATURE_MAX: f64 = 2.0;
const PROBABILITY_STEP: f64 = 0.05;
const PROBABILITY_MAX: f64 = 1.0;
@@ -85,7 +86,7 @@ fn adjust(
},
Field::Temperature => {
preferences.sampling.temperature =
- step_f64(preferences.sampling.temperature, delta, TEMPERATURE_STEP, 0.0, TEMPERATURE_MAX);
+ step_f64(preferences.sampling.temperature, delta, TEMPERATURE_STEP, TEMPERATURE_MIN, TEMPERATURE_MAX);
},
Field::TopK => {
preferences.sampling.top_k = (preferences.sampling.top_k + delta * TOP_K_STEP).clamp(TOP_K_MIN, TOP_K_MAX);
@@ -126,7 +127,8 @@ fn toggle(
Field::Thinking => adjust(preferences, Field::Thinking, 1, support),
Field::SamplingMode => sampling.mode = sampling.mode.next(),
Field::Temperature => {
- toggle_value(&mut sampling.temperature_enabled, &mut sampling.temperature, defaults.temperature)
+ toggle_value(&mut sampling.temperature_enabled, &mut sampling.temperature, defaults.temperature);
+ sampling.temperature = sampling.temperature.max(TEMPERATURE_MIN);
},
Field::TopK => toggle_value(&mut sampling.top_k_enabled, &mut sampling.top_k, defaults.top_k),
Field::TopP => toggle_value(&mut sampling.top_p_enabled, &mut sampling.top_p, defaults.top_p),
diff --git a/crates/legacy/uzu/src/engine/bridge/chat_token_backend.rs b/crates/legacy/uzu/src/engine/bridge/chat_token_backend.rs
index bef9955f7..c5372c92d 100644
--- a/crates/legacy/uzu/src/engine/bridge/chat_token_backend.rs
+++ b/crates/legacy/uzu/src/engine/bridge/chat_token_backend.rs
@@ -154,8 +154,15 @@ impl<B: Backend> BackendInstance for UzuChatTokenBackendInstance<B> {
return Box::pin(NoMetricsStream::new(error_stream("Grammar is not supported by this build".to_string())));
}
+ let sampling_method = match get_sampling_method::<B>(&self.model, &config.sampling_policy) {
+ Ok(sampling_method) => sampling_method,
+ Err(err) => {
+ return Box::pin(NoMetricsStream::new(error_stream(err)));
+ },
+ };
+
let mut options = self.model.default_stream_options();
- options.sampling_method = get_sampling_method::<B>(&self.model, &config.sampling_policy);
+ options.sampling_method = sampling_method;
#[cfg(feature = "capability-grammar")]
{
options.grammar = grammar;
diff --git a/crates/legacy/uzu/src/engine/bridge/helpers.rs b/crates/legacy/uzu/src/engine/bridge/helpers.rs
index 1208b2b82..882c2e058 100644
--- a/crates/legacy/uzu/src/engine/bridge/helpers.rs
+++ b/crates/legacy/uzu/src/engine/bridge/helpers.rs
@@ -102,32 +102,45 @@ pub fn get_max_context_length<B: Backend>(
pub fn get_sampling_method<B: Backend>(
model: &LanguageModel<B>,
sampling_method: &ShojiSamplingPolicy,
-) -> UzuSamplingMethod {
+) -> Result<UzuSamplingMethod, String> {
match sampling_method {
ShojiSamplingPolicy::Default {
..
- } => model.default_sampling_method(),
+ } => Ok(model.default_sampling_method()),
ShojiSamplingPolicy::Custom {
method,
- } => match method {
- ShojiSamplingMethod::Greedy {
- ..
- } => UzuSamplingMethod::Greedy,
- ShojiSamplingMethod::Stochastic {
- temperature,
- top_k,
- top_p,
- min_p,
- repetition_penalty,
- suffix_repetition_length,
- } => UzuSamplingMethod::Stochastic {
+ } => get_custom_sampling_method(method),
+ }
+}
+
+fn get_custom_sampling_method(method: &ShojiSamplingMethod) -> Result<UzuSamplingMethod, String> {
+ match method {
+ ShojiSamplingMethod::Greedy {
+ ..
+ } => Ok(UzuSamplingMethod::Greedy),
+ ShojiSamplingMethod::Stochastic {
+ temperature,
+ top_k,
+ top_p,
+ min_p,
+ repetition_penalty,
+ suffix_repetition_length,
+ } => {
+ if let Some(temperature) = temperature
+ && (temperature.is_nan() || *temperature <= 0.0)
+ {
+ return Err(format!(
+ "stochastic sampling needs temperature > 0, got {temperature}, use greedy sampling instead"
+ ));
+ }
+ Ok(UzuSamplingMethod::Stochastic {
temperature: temperature.map(|value| value as f32),
top_k: top_k.map(|value| value as u32),
top_p: top_p.map(|value| value as f32),
min_p: min_p.map(|value| value as f32),
repetition_penalty: repetition_penalty.map(|value| value as f32),
suffix_repetition_length: suffix_repetition_length.map(|value| value as u32),
- },
+ })
},
}
}
@@ -171,3 +184,39 @@ mod tests {
assert_eq!(grammar_trigger_token_sequence_for_prompt(Some(&[1]), &[1, 2, 3], &tokenizer), Some(vec![1]));
}
}
+
+#[cfg(test)]
+mod sampling_tests {
+ use shoji::types::basic::SamplingMethod as ShojiSamplingMethod;
+ use uzu_engine::engine::language_model::stream::SamplingMethod as UzuSamplingMethod;
+
+ use super::get_custom_sampling_method;
+
+ fn stochastic(temperature: Option<f64>) -> ShojiSamplingMethod {
+ ShojiSamplingMethod::Stochastic {
+ temperature,
+ top_k: None,
+ top_p: Some(0.9),
+ min_p: None,
+ repetition_penalty: None,
+ suffix_repetition_length: None,
+ }
+ }
+
+ #[test]
+ fn stochastic_rejects_non_positive_temperature() {
+ for temperature in [0.0, -0.5, f64::NAN] {
+ assert!(get_custom_sampling_method(&stochastic(Some(temperature))).is_err(), "temperature {temperature}");
+ }
+ }
+
+ #[test]
+ fn stochastic_accepts_positive_or_unset_temperature() {
+ for temperature in [Some(0.7), None] {
+ assert!(matches!(
+ get_custom_sampling_method(&stochastic(temperature)),
+ Ok(UzuSamplingMethod::Stochastic { .. })
+ ));
+ }
+ }
+}