Which Tokens Stay in the Running

TransformersMedium+20 XP+20

Which Tokens Stay in the Running

At every step, a GPT model outputs one raw score (a logit) for each token in its vocabulary. Before picking the next token, most text generators reshape that distribution in three stages:

  1. Temperature. Divide every logit by the temperature TTT, then apply softmax to get probabilities.
  2. Top-k. Keep only the top_k most probable tokens. A top_k of 0 means skip this stage.
  3. Top-p. Renormalise the tokens that survived top-k so their probabilities sum to 1. Then, going from most to least probable, keep tokens until their running total reaches or passes top_p. The token that brings the total to top_p or beyond is kept, and everything after it is dropped. A top_p of 1.0 means skip this stage.

Finally, renormalise whatever survived so it sums to 1. Every dropped token gets probability 0.

When two tokens have exactly the same probability, the one with the lower index counts as the more probable.

Task: write filter_next_token(logits, temperature, top_k, top_p). Return the final distribution as a list with one entry per vocabulary token, in the original order, each rounded to 4 decimal places.

Top-k always keeps the same number of tokens. Top-p keeps however many the model's own confidence calls for: a short list when the model is sure, a long one when it is not.

Examples

logits = [2.0, 1.0, 0.5, -1.0]
temperature = 1.0
top_k = 2
top_p = 1.0
returns [0.7311, 0.2689, 0.0, 0.0]
logits = [3.0, 2.0, 1.0, 0.0, -1.0]
temperature = 1.0
top_k = 0
top_p = 0.8
returns [0.7311, 0.2689, 0.0, 0.0, 0.0]
logits = [1.2, 2.4, 0.3, 2.0, -0.5, 1.8]
temperature = 0.5
top_k = 0
top_p = 0.9
returns [0.0, 0.5713, 0.0, 0.2567, 0.0, 0.1721]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run