Contrastive decoding

On this page 7

Contrastive decoding is a text generation method for language models that selects each next token by maximizing the difference in log-probabilities between a large expert model and a smaller amateur model, restricted to tokens the expert already considers plausible. In artificial intelligence practice, it belongs to the family of decoding strategies that sit between the trained network and the final text, alongside greedy decoding, beam search and top-p sampling.

The idea rests on a simple observation: the failure modes of a weak model, such as repetition, generic phrasing and incoherent drift, are usually shared by a strong model in milder form. By subtracting the amateur’s preferences, the method amplifies what the expert knows that the amateur does not, which tends to produce more coherent and informative text without any additional training.

How contrastive decoding scores candidate tokens

Contrastive decoding scores each candidate token by the expert log-probability minus the amateur log-probability, after removing implausible candidates. Both models read the same prefix and produce logits over the same vocabulary, so the method requires a shared tokenizer between the expert and the amateur.

The first step is the adaptive plausibility constraint. Only tokens whose expert probability is at least a fraction alpha of the expert’s top probability remain in the candidate set; a typical value is alpha = 0.1. Without this filter, the score would reward rare tokens that the amateur rates as almost impossible, because a tiny amateur probability produces a very large difference.

The second step ranks the remaining tokens by the contrastive score and usually picks the highest one greedily. The table below summarizes the components and their roles.

ComponentRoleTypical setting
Expert modelSupplies knowledge and fluencyLarge model, for example 13B-70B parameters
Amateur modelRepresents typical errors to avoidSmall model from the same family
Plausibility thresholdRemoves tokens the expert rejectsalpha between 0.05 and 0.2
Contrastive scoreRanks remaining candidateslog p(expert) minus log p(amateur)
Selection ruleTurns scores into one tokenGreedy choice or beam search
Components of a contrastive decoding step

Variants of the contrastive objective

Several variants keep the core idea of contrasting two distributions but change what plays the role of the amateur. The choice affects cost, deployment complexity and the kind of error that is suppressed.

A widely used reformulation adds a weighting coefficient beta to the logits, computing (1 + beta) times the expert logits minus beta times the amateur logits. This version behaves more smoothly and was shown to improve reasoning benchmarks such as grade-school mathematics when beta is around 0.5. Other variants contrast layers or contexts instead of separate models.

VariantWhat is contrastedMain benefit
Original contrastive decodingLarge expert versus small amateurLess repetition and drift
Weighted logit contrastExpert and amateur logits with betaGains on reasoning tasks
DoLa (layer contrast)Final layer versus an earlier layerBetter factual accuracy, one model
Context-aware decodingOutput with and without retrieved contextStronger grounding in the context
Visual contrastive decodingClean versus distorted image inputFewer object hallucinations
Main variants and what they contrast

The layer-contrast approach is attractive because it needs only one network: early layers act as a built-in amateur that captures surface statistics, while later layers add factual knowledge.

How it compares with other decoding strategies

Contrastive decoding is a deterministic search objective, unlike sampling methods that inject randomness to escape repetition. It targets the same problem as sampling, known as neural text degeneration, but solves it by reshaping the score rather than by randomizing the choice.

MethodDiversity sourceTypical weakness
Greedy decodingNoneLoops and repetitive text
Beam searchNone, wider searchBland, generic continuations
Top-p samplingRandom draw from nucleusOccasional incoherent tokens
Contrastive searchPenalty on similar hidden statesNeeds tuned degeneration penalty
Contrastive decodingExpert-amateur gapRequires a second forward pass
Contrastive decoding compared with common alternatives

The similar name contrastive search refers to a different technique that penalizes tokens whose hidden representation resembles earlier context. The two methods can be confused in documentation, so precise naming matters when comparing published results.

Configuring contrastive decoding in practice

Effective configuration depends mainly on choosing a suitable amateur and on tuning the plausibility threshold. The amateur should be clearly weaker but trained on similar data, so that its mistakes overlap with the expert’s tendencies.

  • Use an amateur from the same model family to guarantee an identical vocabulary and comparable training data.
  • Keep a sizable capability gap, for example a 1B amateur with a 30B expert; nearly equal models cancel each other out.
  • Raise alpha when output contains odd or rare words, and lower it when text remains generic.
  • Apply a lower temperature to the amateur to sharpen the errors that should be subtracted.
  • Benchmark against plain greedy decoding on the target task, because gains differ by domain.

Compute cost rises by the size of the amateur forward pass, which is modest when the amateur is small. Both models can share a key-value cache strategy, and in serving stacks the amateur often runs on the same accelerator.

Limitations and common mistakes

Contrastive decoding is not a universal improvement. Its benefits are strongest in open-ended generation and some reasoning tasks, and they can shrink or reverse on short factual answers where the expert’s top token is already correct.

SymptomLikely causeFix
Strange rare wordsPlausibility threshold too lowIncrease alpha
No visible changeAmateur too close to expertUse a smaller amateur
Tokenization errorsMismatched vocabulariesUse one model family
Worse factual answersExpert top token already correctDisable for short answers
Typical problems, causes and fixes

A further limitation is operational complexity: two models must be loaded, versioned and kept aligned. Closed APIs usually expose only one model and limited log-probabilities, which makes the method impractical through hosted endpoints and better suited to self-hosted open-weight models.

In plain words

Imagine two writers working on the same sentence. One is an expert. The other is a beginner. Both suggest the next word. Contrastive decoding picks the word that the expert likes much more than the beginner does.

Why does this help? Beginners make typical mistakes. They repeat themselves and choose dull, safe words. The expert shares these habits a little. By discounting what the beginner would say, the method keeps the expert’s real knowledge and drops the common mistakes. A safety rule makes sure the expert still finds the chosen word reasonable.

A real-world example

A research team runs a self-hosted 34B open-weight model to draft long technical summaries. With greedy decoding, outputs longer than about 400 tokens often loop, restating the same sentence with minor changes. Top-p sampling removes the loops but occasionally inserts invented details that reviewers must catch.

The team adds a 1B model from the same family as the amateur, sets alpha to 0.1 and uses greedy selection on the contrastive score. Generation time rises by roughly 10 percent because of the extra small forward pass. In a review of 200 summaries, editors report far fewer repetitive passages and more specific wording, while the factual error rate stays similar to greedy output. The team keeps contrastive decoding for long drafts and reverts to greedy decoding for short question answering, where it showed no benefit.