We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Decoding Strategies step 3 of 8
Top-k Sampling
Keep the k highest-scoring tokens, then draw one token according to their relative probabilities. Filtering chooses which tokens are eligible; sampling chooses one of those tokens.
Your task
Implement top_k_sample(logits, k, seed).
- logits is a one-dimensional floating-point tensor of finite scores, with one score per vocabulary token. A token's ID is its index.
- k is an integer with
1 <= k <= len(logits). - seed initializes the random generator for this single draw.
- Return one Python integer token ID. Leave logits unchanged.
Rank tokens by descending score, breaking ties by lower token ID. Retain exactly the first k tokens in that ordering. A stable descending sort gives this tie rule; an unspecified top-k tie order does not.
Keep the original vocabulary positions and replace excluded scores with negative infinity. Softmax then assigns excluded tokens exactly zero probability and renormalizes the retained scores. Do not add an epsilon to excluded probabilities, which would make forbidden tokens eligible again.
For logits [3, 2, 0] and k=2, only IDs 0 and 1 are eligible. Their
probabilities are approximately [0.731, 0.269]; ID 2 has probability zero.
The largest score is more likely, but a draw can still select ID 1.
Reproduce the framework's draw
The fixtures check a seeded draw, not just whether the returned token belongs to the retained set. Use the full vocabulary-sized distribution:
In PyTorch
Select indices with torch.argsort(logits, descending=True, stable=True)[:k],
mask the other logits, apply softmax and call torch.multinomial with
num_samples=1 and a fresh torch.Generator().manual_seed(seed).
In JAX
Select indices with jnp.argsort(-logits, stable=True)[:k], mask the
other logits and call jax.random.categorical(jax.random.PRNGKey(seed), filtered).
Categorical sampling accepts logits directly, so no softmax/log round trip is needed.
Convert the sampled index to a Python int. These seeded fixtures use float32
inputs and a CPU generator for PyTorch. JAX fixtures use the runtime's
jax_threefry_partitionable=True setting. Different frameworks need not draw
the same token from the same seed; each has its own expected outputs.
Reordering or shrinking the sampling distribution can also change seeded results.
With k=1, the retained token is always selected. With k equal to vocabulary size, all tokens are eligible. Limiting the candidate set does not guarantee coherent text. This function performs one draw, not a generation loop.
The diagram illustrates how scores become probabilities. This exercise has no temperature parameter and uses the supplied scores without rescaling them.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Choose the next token using the prefix so far
An autoregressive model predicts a distribution for the next token given the existing sequence. Greedy decoding picks the most likely token and repeats. Beam search keeps several candidate prefixes, expands each with possible next tokens, and retains the best candidates under its scoring rule.
Beam search considers more alternatives but can still discard a prefix that would later lead to the best complete sequence. Keep each candidate's state, score, and stopping status separate. A real model's next-token distribution depends on its prefix; a fixed table of scores by time step is only a simplified exercise.
Sequence log probability is a sum of token log probabilities. Since these terms are nonpositive, shorter sequences can be favored. Length normalization changes candidate rankings, so use the exact rule in the task.
Sample from a restricted distribution
Top-k sampling retains the k highest-scoring tokens. Nucleus, or top-p, sampling
sorts tokens by probability and keeps the smallest prefix reaching the requested
cumulative probability. Include the token that crosses the threshold.
For probabilities [0.6, 0.3, 0.1] and threshold 0.8, nucleus sampling retains
the first two tokens because their total is 0.9. Renormalizing gives retained
probabilities [2/3, 1/3]. Map any sampled sorted index back to its original
vocabulary ID.
Temperature divides logits before softmax. A positive temperature changes how concentrated the distribution is. Check the task's end-of-sequence handling, maximum length, whether returned tokens include the prompt, and random-key usage.
Evaluate overlap without confusing it with meaning
BLEU-1 measures clipped unigram overlap with a reference and applies a brevity penalty. Repeating a reference word cannot earn unlimited credit. This unigram score ignores word order and does not establish semantic correctness. Follow the specified tokenization and empty-output conventions when computing it.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.