BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / LLM inference and serving14 of 36 in topic

440. Verify a draft model's guesses in speculative decoding

MediumLLM inference and serving Pro

In speculative decoding a small draft model guesses several tokens ahead, then the big model checks all of them in one pass. Here we use the greedy version.

Write verify_draft(draft, target) where draft is the list of k tokens the small model proposed, and target is the list of k + 1 tokens the big model would pick at each position (target[i] is the big model's choice for position i, and target[k] is its choice after the whole draft).

Walk through the draft. Accept draft[i] while it equals target[i]. At the first mismatch, stop and add target[i] instead (the big model's correction). If every draft token was accepted, add target[k] as a free bonus token.

Return the list of tokens produced this step.

Functiondef verify_draft(draft, target)

Example 1

Input: verify_draft(["the","cat","sat"], ["the","cat","ran","off"])

Output: ["the","cat","ran"]

Explanation: The bonus "off" is NOT added, because a rejection happened.

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Walk the draft with an index.

Hint 2

On the first mismatch append the target token and return.

Hint 3

If all match, also append target[k].

Understand the idea

Speculative decoding pairs a fast small model with a slow big one. The small model guesses a few tokens ahead, and the big model checks all the guesses in one go. Think of a junior writing a draft and a senior reading it until the first mistake. In the greedy version you accept draft tokens while they equal the big model's choice at the same position. At the first mismatch you take the big model's token and stop. If every guess matched, you also get the big model's next token as a free bonus.

Worked example

draft [x, y, z], target [x, y, z, w]. All three match, so the output is [x, y, z, w], four tokens from one big-model pass. With draft [x, y] and target [x, q, r], x matches, y does not, so the output is [x, q] and r is not added.

the output is always exactly what the big model alone would have produced

Production LLM servers use this to cut latency.

Topics
LLM inference and servingLLM architecture track
Similar questions
  • Generate one token at a time with a KV cacheMedium
  • Allocate KV cache memory in pages, like vLLMMedium
  • Is decoding memory-bound? A roofline estimateMedium
  • Count prefix cache hits with a token trie and block granularityMedium
All LLM inference and serving problems
Report
440 / 903
Python 3solution.pyPython idle
1
2
3
4
def verify_draft(draft, target):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

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