KatInterview

Truncate and Renormalize a Next-Token Distribution

Heaps, Sorting & SearchAuto-graded · 20 test cases
MediumAuto-gradedHeaps, Sorting & Searchsortingprefix-sumprobability

Last updated

Sort a probability list with deterministic tie-breaking, then build two truncated, renormalized versions: fixed-size top-k and cumulative-mass top-p.

Truncate and Renormalize a Next-Token Distribution

A language model assigns a probability to each token in a small vocabulary. Before drawing a token, a decoder often discards unlikely candidates and rescales the survivors so they sum to 1. You will compute two such truncated distributions from the same input.

Task

Given token_probs, where token_probs[i] is the probability of the token at index i, a positive integer k, and a threshold p, return a dictionary with two keys, each mapping to a list of floats with the same length and index order as token_probs:

  • "top_k": the fixed-size truncation.
  • "top_p": the cumulative-mass truncation.

Ranking order

Rank tokens by probability from highest to lowest. If two tokens have equal probability, the one with the smaller original index ranks first. Both truncations use this one ranking.

Top-k list

Keep the k highest-ranked tokens. If k is at least the number of tokens, keep all of them. Each kept token's value is its probability divided by the total probability of the kept tokens. Every other position is 0.0.

Top-p list

Walk through tokens in ranked order, adding each token's probability to a running total. The total is a plain floating-point sum, accumulated in ranked order. Add a token to the kept set first, then check whether the running total is >= p. As soon as it is, stop. The kept set is therefore the shortest ranked prefix whose cumulative probability reaches p.

  • The comparison is non-strict (>=), not >.
  • At least one token is always kept, even when p <= 0.
  • If the running total never reaches p, for example because p >= 1 or because of rounding, keep every token.

Each kept token's value is its probability divided by the total probability of the kept tokens. Every other position is 0.0.

Constraints

  • 1 <= len(token_probs) <= 10^5
  • Each probability is non-negative, and they sum to 1 up to floating-point error.
  • 1 <= k
  • p is a real number. It may be at most 0 or at least 1, and both cases follow the rules above.
  • Positions are never reordered. Results are indexed exactly like the input.
  • Results are compared as floating-point numbers, so follow the summation order described above.

Examples

Example 1

Input: truncate_distribution([0.4, 0.3, 0.15, 0.1, 0.05], 3, 0.7)

Output: {'top_k': [0.4705882352941177, 0.35294117647058826, 0.17647058823529413, 0, 0], 'top_p': [0.5714285714285715, 0.4285714285714286, 0, 0, 0]}

Example 2

Input: truncate_distribution([0.95, 0.02, 0.01, 0.01, 0.01], 2, 0.9)

Output: {'top_k': [0.979381443298969, 0.020618556701030927, 0, 0, 0], 'top_p': [1, 0, 0, 0, 0]}

Example 3

Input: truncate_distribution([0.2, 0.2, 0.2, 0.2, 0.2], 3, 0.6)

Output: {'top_k': [0.3333333333333333, 0.3333333333333333, 0.3333333333333333, 0, 0], 'top_p': [0.3333333333333333, 0.3333333333333333, 0.3333333333333333, 0, 0]}

Example 4

Input: truncate_distribution([0.7, 0.2, 0.1], 1, 1)

Output: {'top_k': [1, 0, 0], 'top_p': [0.7, 0.2, 0.1]}

Hints

Hint 1

Compute the ranking once as a list of original indices sorted by the key (-probability, index). Both outputs can reuse it.

Hint 2

For the top-k list, take the first k indices of the ranking, sum their probabilities, and divide each kept probability by that sum. Put 0.0 everywhere else.

Hint 3

For the top-p list, loop over the ranking, append the index, add its probability to a running total, and break as soon as the total is >= p. Renormalize by the sum over the kept set.