440. Verify a draft model's guesses in speculative decoding
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.
def 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.
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.