pub struct Perplexity { /* private fields */ }Expand description
Additive token-NLL moments behind perplexity.
Perplexity is exp(sum(nll × tokens) / sum(tokens)) with the exponential
applied only at observation time. Batches contribute their token-weighted
mean negative log-likelihood — exactly what cross_entropy_masked returns
when its valid_count is the token denominator — so partitioned language
evaluation merges exactly. The contract forbids the mean of per-batch
perplexities, which is not the corpus perplexity. Empty denominators
report NaN.
Implementations§
Source§impl Perplexity
impl Perplexity
Sourcepub fn update(&mut self, mean_nll: f64, token_count: u64) -> Result<()>
pub fn update(&mut self, mean_nll: f64, token_count: u64) -> Result<()>
Add one batch’s token-weighted mean NLL with its valid token count.
The accumulated numerator is mean_nll × token_count, so callers may
pass the mean their loss reports rather than re-summing per-token
values.
§Errors
Returns an error when the mean is non-finite.
Sourcepub fn update_device(
&mut self,
mean_nll: &Matrix,
token_count: u64,
) -> Result<()>
pub fn update_device( &mut self, mean_nll: &Matrix, token_count: u64, ) -> Result<()>
Read back one batch’s masked-NLL scalar and accumulate it.
mean_nll must be the device scalar cross_entropy_masked produces
for the same tokens, keeping observation on the once-per-batch
evaluation plane.
§Errors
Returns an error when the scalar readback fails.