apply_lz_penalty

Function apply_lz_penalty 

Source
pub fn apply_lz_penalty(logits: &mut [f32], history: &[u32], penalty: f32)
Expand description

Sampling pipeline: repetition_penalty → top-n-sigma → temperature → top-k → softmax → min-p → top-p → sample.

data contains FP32 logits (4 bytes per element, little-endian). token_history: previous token IDs for repetition penalty (empty = no penalty). LZ penalty: penalize tokens that would extend repeated n-gram patterns in the recent token history. Based on arXiv:2504.20131.

For each candidate token that appears in the history, check if appending it creates a repeated 3/4/5-gram. Penalize proportional to n-gram length and frequency: logit -= penalty * (ngram_len - 2) * count.