From e0b83e824841de6681f3893bc942bc4474cb7f9b Mon Sep 17 00:00:00 2001 From: Niko Date: Sat, 2 Aug 2025 19:32:26 -0700 Subject: [PATCH] Remove `Default` for `SamplingStrategy` and add docs The default value was not a good choice whatsoever, and forcing the user to choose makes them at least put thought into it, especially with better documentation on it. --- src/whisper_params.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/whisper_params.rs b/src/whisper_params.rs index 0741364..a14b1ec 100644 --- a/src/whisper_params.rs +++ b/src/whisper_params.rs @@ -5,24 +5,29 @@ use std::marker::PhantomData; use std::sync::Arc; use whisper_rs_sys::whisper_token; +/// The sampling strategy to use to pick tokens from a list of likely possibilities. #[derive(Debug, Clone)] pub enum SamplingStrategy { + /// Greedy sampling: picks the token with the highest probability after having seen `best_of` tokens. Greedy { + /// Defaults to 5 in `whisper.cpp`. Will be clamped to at least 1. best_of: c_int, }, + /// Beam search. Much harder to explain in a blurb. + /// Tends to be more accurate in exchange for more CPU time. BeamSearch { + /// The maximum width of the beam. + /// Higher values are better (to a point) at the cost of exponential CPU time. + /// + /// Defaults to 5 in `whisper.cpp`. Will be clamped to at least 1. beam_size: c_int, - // not implemented in whisper.cpp as of this writing (v1.2.0) + /// Not implemented in `whisper.cpp` as of this writing (02-08-2025, `whisper.cpp` v1.7.6). + /// + /// Defaults to -1.0. patience: c_float, }, } -impl Default for SamplingStrategy { - fn default() -> Self { - Self::Greedy { best_of: 1 } - } -} - #[derive(Debug, Clone)] pub struct SegmentCallbackData { pub segment: i32, @@ -63,9 +68,13 @@ impl<'a, 'b> FullParams<'a, 'b> { fp.greedy.best_of = best_of; } SamplingStrategy::BeamSearch { - beam_size, + mut beam_size, patience, } => { + if beam_size < 1 { + beam_size = 1; + } + fp.beam_search.beam_size = beam_size; fp.beam_search.patience = patience; }