Skip to content

phase 1.56: DRY (Don't Repeat Yourself) LogitProcessor (#1082) - #1160

Merged
ericflo merged 1 commit into
mainfrom
phase1/dry
May 23, 2026
Merged

ericflo merged 1 commit into
mainfrom
phase1/dry

Conversation

@ericflo

@ericflo ericflo commented May 23, 2026

Copy link
Copy Markdown
Owner

Repetition penalty that scales exponentially with matched n-gram length, per the oobabooga/Aphrodite/koboldcpp formulation.

API

let p = DryProcessor::new(
    /*multiplier=*/ 0.8,
    /*base=*/        1.75,
    /*allowed_length=*/ 2,
    /*history=*/ vec![row_history],
)
.with_sequence_breakers(newline_set);
let penalized = p.apply(&logits)?;

Algorithm

For each batch row, walk history once and for each position i:

  1. Count how many tokens before i match the tail of history (with sequence-breaker resets). Call this k.
  2. Match length for history[i] is k + 1.
  3. Take the max across all positions for each token id.

For each token whose best match m exceeds allowed_length, subtract multiplier * base^(m - allowed_length) from its logit.

Reference

oobabooga/textgen#5677

Tests

9 unit tests, CPU-only.

Chain status

11 of 12 menu items shipped. Remaining: XTC + grammar-mask.

Refs #1082

Repetition penalty that scales **exponentially** with matched
n-gram length, per the oobabooga/Aphrodite/koboldcpp formulation.

## API

```rust
let p = DryProcessor::new(
    /*multiplier=*/ 0.8,
    /*base=*/        1.75,
    /*allowed_length=*/ 2,
    /*history=*/ vec![row_history],
)
.with_sequence_breakers(newline_set);

let penalized = p.apply(&logits)?;
```

## Algorithm

For each batch row, walk history once and for each position `i`:

1. Count how many tokens *before* `i` match the tail of history
   (with sequence-breaker resets). Call this `k`.
2. Match length for `history[i]` is `k + 1`.
3. Take the max across all positions for each token id.

For each token whose best match `m` exceeds `allowed_length`,
subtract `multiplier * base^(m - allowed_length)` from its logit.

`base ≤ 1` rejected (penalty must grow). `multiplier < 0` rejected
(would be a reward, not a penalty).

## Reference

oobabooga/textgen#5677 — the
canonical DRY implementation everyone has converged on.

## Tests

9 unit tests, CPU-only:

- No repetition → no penalty (sanity)
- Extended match: `[1,2,3,1,2]` + allowed=1 penalizes tok 3 by
  `1.0 * 2^2 = 4.0`
- multiplier=0 is no-op
- Sequence breakers reset the match (insert a breaker between
  two would-be repeats → no penalty)
- Longer match grows exponentially: `[1,2,3,1,2]` + allowed=0
  → tok 3 penalty `1.0 * 2^3 = 8.0`
- Negative multiplier errors
- base ≤ 1 errors
- History/batch mismatch errors
- name() == "dry"

## Chain status

11 of 12 menu items shipped. Remaining: grammar-mask (separate
schema-compiler effort).

Refs #1082
@ericflo
ericflo merged commit 72a1bc4 into main May 23, 2026
2 of 4 checks passed
@ericflo
ericflo deleted the phase1/dry branch May 23, 2026 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant