pub struct BeamSearchDecoder {
pub config: BeamSearchConfig,
}Expand description
Beam search decoder.
Fields§
§config: BeamSearchConfigConfiguration controlling all search behaviour.
Implementations§
Source§impl BeamSearchDecoder
impl BeamSearchDecoder
Sourcepub fn new(config: BeamSearchConfig) -> Self
pub fn new(config: BeamSearchConfig) -> Self
Create a decoder with the supplied configuration.
Sourcepub fn with_default() -> Self
pub fn with_default() -> Self
Create a decoder with default configuration.
Sourcepub fn initial_state(&self, bos_token_id: usize) -> BeamState
pub fn initial_state(&self, bos_token_id: usize) -> BeamState
Create an initial BeamState seeded with bos_token_id.
Sourcepub fn step(
&self,
state: BeamState,
input: &BeamStepInput,
) -> Result<BeamState, BeamSearchError>
pub fn step( &self, state: BeamState, input: &BeamStepInput, ) -> Result<BeamState, BeamSearchError>
Perform one step of beam search.
Given the current state and per-beam log-probabilities input, advance
each beam by one token, prune to the top-beam_width candidates, and
handle EOS/completion logic.
Sourcepub fn decode<F>(
&self,
bos_token_id: usize,
score_fn: F,
) -> Result<BeamSearchResult, BeamSearchError>
pub fn decode<F>( &self, bos_token_id: usize, score_fn: F, ) -> Result<BeamSearchResult, BeamSearchError>
Run a full beam search.
score_fn is called at each step with the current beam sequences and must
return log-probabilities of shape [num_active_beams][vocab_size].
Sourcepub fn top_k_results(&self, state: &BeamState, k: usize) -> Vec<BeamHypothesis>
pub fn top_k_results(&self, state: &BeamState, k: usize) -> Vec<BeamHypothesis>
Extract the top-k hypotheses from a beam state, sorted by score descending.
Sourcepub fn apply_temperature(logits: &[f64], temperature: f64) -> Vec<f64>
pub fn apply_temperature(logits: &[f64], temperature: f64) -> Vec<f64>
Apply temperature scaling to logits: logits[i] / temperature.
Sourcepub fn log_softmax(logits: &[f64]) -> Vec<f64>
pub fn log_softmax(logits: &[f64]) -> Vec<f64>
Compute numerically stable log-softmax.
Uses the log-sum-exp trick:
lse = max + log(sum(exp(x - max))), log_softmax(x_i) = x_i - lse.
Sourcepub fn top_k_filter_logits(logits: &mut [f64], k: usize)
pub fn top_k_filter_logits(logits: &mut [f64], k: usize)
Zero out all but the top-k logits in place (set others to NEG_INFINITY).
Auto Trait Implementations§
impl Freeze for BeamSearchDecoder
impl RefUnwindSafe for BeamSearchDecoder
impl Send for BeamSearchDecoder
impl Sync for BeamSearchDecoder
impl Unpin for BeamSearchDecoder
impl UnsafeUnpin for BeamSearchDecoder
impl UnwindSafe for BeamSearchDecoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more