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

419. Keep the nucleus: top-p filtering

MediumSampling and decoding
Asked atAnthropicCohereDeepMindOpenAI

Top-p sampling keeps the smallest set of tokens whose probabilities add up to at least p, and drops the long tail. Unlike top-k, the set grows when the model is unsure and shrinks when it is confident.

Write top_p_filter(probs, p) for a 1D probability vector. Sort tokens by probability (ties: lower index first), keep tokens until the running total reaches at least p (always keep at least one), zero the rest, renormalise, and return the new vector in the original order.

Functiondef top_p_filter(probs, p)
Play with it

Nucleus sampling keeps the smallest set above p

Tokens are kept, most likely first, until their probability sums to at least p. Lower the temperature and fewer tokens are needed. Example logits.

0%25%50%75%100%probability, before (outline) and after the filterthe47.3%a26%my12.9%this8.6%one5.2%our0%that0%his0%
entropy (nats)
1.334
top class
the
tokens kept
5 of 8

Example 1

Input: probs = [0.1, 0.5, 0.25, 0.15], p = 0.8

Output: [0.0, 0.555556, 0.277778, 0.166667]

Example 2

Input: probs = [0.05, 0.9, 0.05], p = 0.5

Output: [0.0, 1.0, 0.0]

Example 3

Input: probs = [0.2, 0.3, 0.5], p = 1.0

Output: [0.2, 0.3, 0.5]

Constraints

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

order = sorted(range(n), key=lambda i: (-probs[i], i)).

Hint 2

Walk it adding to a total; stop after the token that makes total >= p.

Common mistakes
  • The buggy version stops BEFORE crossing p, which can keep nothing when the top token alone exceeds p.
Understand the idea

Top-p (nucleus) sampling keeps the smallest group of most likely tokens whose probabilities add up to at least p, then renormalises. When the model is confident, that group is small; when unsure, it grows. Like ordering from the dishes that together cover most of what customers usually order. Sort by probability (lower index first on ties), add up until the running total reaches p, and include the token that crosses p.

Worked example

probs [0.1, 0.5, 0.25, 0.15], p = 0.7. Sorted: index 1 (0.5), index 2 (0.25), index 3, index 0. After index 1 the total is 0.5, below 0.7. After index 2 it is 0.75, which reaches 0.7, so stop. Kept sum 0.75, output [0, 0.667, 0.333, 0] in original order. With p = 0.4, the top token alone is 0.5, so only it is kept.

keep the shortest prefix of the sorted list whose sum >= p (at least one token), zero the rest, divide by the kept sum

Most LLM APIs expose top_p as a setting.

Topics
Sampling and decoding
Companies4
  • Anthropic the topic appears in interview reports Coding Exercises & ML System Design: Real AI Interview Questions (landedjobs)
  • Cohere a candidate reported this question Cohere Machine Learning Engineer Interview Questions | Screna AI
  • DeepMind the topic appears in interview reports AI Engineering Interview Questions Company Wise (Outcome School)
  • OpenAI the topic appears in interview reports Coding Exercises & ML System Design: Real AI Interview Questions (landedjobs)

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Apply repetition, frequency and presence penalties to logitsMedium
  • Filter logits with locally typical samplingMedium
  • Run batched greedy decoding with per-row EOS trackingMedium
  • Score next tokens with contrastive decodingMedium
All Sampling and decoding problems
Report
419 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def top_p_filter(probs, p):
# 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