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.
This commit is contained in:
Niko 2025-08-02 19:32:26 -07:00
parent 15e70ffd07
commit e0b83e8248
No known key found for this signature in database

View file

@ -5,24 +5,29 @@ use std::marker::PhantomData;
use std::sync::Arc; use std::sync::Arc;
use whisper_rs_sys::whisper_token; use whisper_rs_sys::whisper_token;
/// The sampling strategy to use to pick tokens from a list of likely possibilities.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum SamplingStrategy { pub enum SamplingStrategy {
/// Greedy sampling: picks the token with the highest probability after having seen `best_of` tokens.
Greedy { Greedy {
/// Defaults to 5 in `whisper.cpp`. Will be clamped to at least 1.
best_of: c_int, 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 { 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, 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, patience: c_float,
}, },
} }
impl Default for SamplingStrategy {
fn default() -> Self {
Self::Greedy { best_of: 1 }
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SegmentCallbackData { pub struct SegmentCallbackData {
pub segment: i32, pub segment: i32,
@ -63,9 +68,13 @@ impl<'a, 'b> FullParams<'a, 'b> {
fp.greedy.best_of = best_of; fp.greedy.best_of = best_of;
} }
SamplingStrategy::BeamSearch { SamplingStrategy::BeamSearch {
beam_size, mut beam_size,
patience, patience,
} => { } => {
if beam_size < 1 {
beam_size = 1;
}
fp.beam_search.beam_size = beam_size; fp.beam_search.beam_size = beam_size;
fp.beam_search.patience = patience; fp.beam_search.patience = patience;
} }