pub struct TopPSampler {
pub p: f64,
pub temperature: f64,
/* private fields */
}Expand description
Nucleus (top-p) sampler: keeps the smallest set of tokens whose cumulative
probability is at least p, then samples from that nucleus.
Fields§
§p: f64Cumulative probability threshold in (0, 1].
temperature: f64Temperature applied before the nucleus filter.
Implementations§
Source§impl TopPSampler
impl TopPSampler
Sourcepub fn new(p: f64, temperature: f64, seed: u64) -> Result<Self, SamplingError>
pub fn new(p: f64, temperature: f64, seed: u64) -> Result<Self, SamplingError>
Construct a TopPSampler.
Fails if p <= 0.0 || p > 1.0 or if temperature <= 0.0.
Sourcepub fn sample(&mut self, logits: &[f64]) -> Result<SampledToken, SamplingError>
pub fn sample(&mut self, logits: &[f64]) -> Result<SampledToken, SamplingError>
Sample one token from logits using nucleus sampling.
Sourcepub fn apply_top_p(probs: &[f64], p: f64) -> Vec<f64>
pub fn apply_top_p(probs: &[f64], p: f64) -> Vec<f64>
Given a probability vector probs, return a logit vector in which
tokens outside the nucleus are set to f64::NEG_INFINITY.
Algorithm:
- Sort indices by descending probability.
- Accumulate until the sum >=
p. - All tokens beyond the cutoff become
NEG_INFINITY.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TopPSampler
impl RefUnwindSafe for TopPSampler
impl Send for TopPSampler
impl Sync for TopPSampler
impl Unpin for TopPSampler
impl UnsafeUnpin for TopPSampler
impl UnwindSafe for TopPSampler
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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