Table of Contents

Class RepetitionPenalty

Namespace
LMKit.TextGeneration.Sampling
Assembly
LM-Kit.NET.dll

Configures the repetition controls applied during text generation to reduce redundant or monotonous output.

public sealed class RepetitionPenalty
Inheritance
RepetitionPenalty
Inherited Members

Examples

The following example shows how to configure repetition penalties on a conversation:

using LMKit.Model;
using LMKit.TextGeneration;

LM model = LM.LoadFromModelID("gemma4:e4b");
var chat = new MultiTurnConversation(model);

// Sequence-aware repetition control over a 512-token lookback.
chat.RepetitionPenalty.TokenCount = 512;
chat.RepetitionPenalty.NgramPenaltyMultiplier = 0.8f;

var result = chat.Submit("Tell me a long story about a brave knight.");

// Disable every repetition control when no longer needed.
chat.RepetitionPenalty.Disable();

Remarks

Three instruments share one lookback window, and they answer different problems:

  • RepeatPenalty, FrequencyPenalty and PresencePenalty act on INDIVIDUAL tokens. Cheap and blunt: they cannot tell an ordinary word used often from a passage being replayed, so pushing them hard degrades prose everywhere.
  • NgramPenaltyMultiplier acts on SEQUENCES, charging exponentially in the length of the repeat a candidate would extend. Normal text is untouched and loops become unaffordable, which makes it the control to reach for first. This is the sampler commonly known as DRY.
  • NoRepeatNgramSize is absolute: an n-gram already emitted simply cannot be emitted again. Right for structured output that must not loop, wrong for prose, where legitimate repeats exist.

TokenCount gates all of them: at zero, no repetition stage runs.

Constructors

RepetitionPenalty()

Initializes a new instance of the RepetitionPenalty class with TokenCount set to 0, effectively disabling all penalties.

RepetitionPenalty(int)

Initializes a new instance of the RepetitionPenalty class with the specified lookback window size.

Properties

FrequencyPenalty

Specifies an additive penalty that scales with how often a token has appeared in the lookback window.
Higher values more strongly discourage tokens that have been used frequently.
Positive values reduce repetition; negative values encourage it; zero disables this penalty.
Use a floating-point value within the range [-2, 2].

NgramAllowedLength

Specifies the repeat length that NgramPenaltyMultiplier leaves free of charge.
Repeats up to this many tokens are not penalized at all, which is what keeps common phrases and names usable.
Use a value within the range [1, 64].

NgramPenaltyBase

Specifies the exponential base of NgramPenaltyMultiplier.
Higher values make the penalty climb faster with the length of the repeat.
Use a floating-point value within the range [1, 8].

NgramPenaltyMultiplier

Specifies the strength of the sequence-aware repetition penalty, commonly known as DRY.

NgramSequenceBreakers

Specifies the texts after which a repeat is legitimate, such as a line break or a quotation mark.

NgramTokenCount

Specifies a lookback window for the sequence-aware penalty that differs from TokenCount.
Use -1 to follow TokenCount.

NoRepeatNgramSize

Specifies the length of n-gram that may never be repeated.

PresencePenalty

Specifies an additive penalty applied to any token that has appeared at least once in the lookback window.
Unlike FrequencyPenalty, this penalty does not scale with occurrence count.
Positive values encourage the model to explore new topics; negative values encourage staying on topic; zero disables this penalty.
Use a floating-point value within the range [-2, 2].

RepeatPenalty

Specifies a multiplicative penalty applied to any token that has already appeared in the lookback window.
Unlike FrequencyPenalty, this penalty is applied uniformly regardless of how many times the token occurred.
Values greater than 1 discourage repetition; a value of 1 disables this penalty.
Use a floating-point value within the range [0, 2].

TokenCount

Specifies the lookback window size, in tokens, shared by every repetition control.
Only tokens within this window are considered.
One token is roughly equivalent to 4 characters in standard English text.
Use a value within the range [0, 2048].
Use 0 to disable all repetition controls.

Methods

Clone()

Creates a new instance of RepetitionPenalty that is a copy of the current instance.

Disable()

Disables every repetition control by setting TokenCount to 0, RepeatPenalty to 1, and the additive, sequence-aware, and blocking controls to their inactive values.

Share