BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Sampling and decoding12 of 17 in topic

423. Score next tokens with contrastive decoding

MediumSampling and decoding

Why it matters Contrastive decoding and related expert-minus-amateur tricks improve open-ended generation quality without retraining.

Contrastive decoding prefers tokens a large expert model likes much more than a small amateur model does, which removes generic, repetitive choices. A plausibility filter keeps it from picking tokens the expert itself finds unlikely.

Write contrastive_scores(expert_logits, amateur_logits, alpha). Both inputs are length-V logits. Let p_e and p_a be their softmax probabilities. Plausible tokens are those with p_e >= alpha * max(p_e). For plausible tokens, score = log p_e - log p_a; all other tokens get -inf. Return (scores, best) where best is the index of the highest score (lowest index on ties).

Functiondef contrastive_scores(expert_logits, amateur_logits, alpha)

Example 1

contrastive_scores([2, 1, 0], [2, -1, 0], 0.5) keeps only token 0, so scores is about [-0.2378, -inf, -inf] and best is 0.

Example 2

Input: expert_logits = [2.0, 1.0, 0.0], amateur_logits = [2.0, -1.0, 0.0], alpha = 0.1

Output: ([-0.23776, 1.76224, -0.23776], 1)

Example 3

Input: expert_logits = [0.0, 3.0, 1.0, -2.0], amateur_logits = [0.0, 1.0, 1.0, -5.0], alpha = 0.1

Output: ([float('-inf'), 0.687526, -1.312474, float('-inf')], 1)

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 3 tests to be accepted.
Hint 1

Compute log-softmax for both models; build the plausibility mask from the expert only.

Hint 2

Work in log space.

Hint 3

The filter uses only the expert.

Hint 4

np.where for the -inf entries.

Common mistakes
  • Removing the plausibility filter.
  • Comparing raw logits instead of log-probabilities.
Understand the idea

Large and small language models share the same boring habits (repeating phrases, generic words), but only the large model has the extra knowledge. Contrastive decoding subtracts the small model's opinion to cancel the shared habits, like removing background noise by subtracting a recording of the empty room.

Worked example

expert probabilities [0.665, 0.245, 0.090], amateur [0.844, 0.042, 0.114]. Log ratios: token 0 = ln(0.665 / 0.844) = -0.238, token 1 = ln(0.245 / 0.042) = 1.762, token 2 = -0.238. Token 1 wins with a loose filter. With alpha 0.5, only tokens with p_e >= 0.33 survive, which leaves token 0.

plausible = p_e >= alpha * max p_e; score = log p_e - log p_a on plausible tokens, -inf elsewhere

The filter is essential: without it, a token the expert gives 0.1% can win just because the amateur gives it 0.0001%. Related ideas compare a model with an earlier layer of itself instead of a separate small model.

Shapes and data flow
expert_logits, amateur_logits (V,) -> log-probs (V,) -> mask (V,) -> scores (V,)
Topics
Sampling and decoding
Similar questions
  • Keep the nucleus: top-p filteringMedium
  • Apply repetition, frequency and presence penalties to logitsMedium
  • Filter logits with locally typical samplingMedium
  • Run batched greedy decoding with per-row EOS trackingMedium
All Sampling and decoding problems
Report
423 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def contrastive_scores(expert_logits, amateur_logits, alpha):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 3 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS