Neural Mastery
← Practice
Greedy Decoding Stepeasy

Greedy Decoding Step

easy Inside an LLM 15–20 min

NumPy allowed · plain Python earns 10 bonus points

LLM step 1/4

Logits become a next-token distribution

Softmax turns a model score vector into probabilities.
Open lesson →
01prompt
02tokens
03logits
04probabilities
attention→next token
mechanism65%
layer20%
pattern10%
model5%

Softmax(logits) → probability distribution

Implement standard greedy autoregressive decoding by selecting the argmax logit with stable softmax confidence.

Task

Implement greedy_decoding_step(logits). Find the token index with the highest logit (breaking ties by lower index). Compute its softmax probability using numerically stable subtraction of the maximum logit, rounded to 4 decimal places. Return a dict with keys chosen_token (int), is_deterministic (bool, True), and confidence (float). Raise ValueError if logits is empty.

Function Signature

greedy_decoding_step(logits: list[float]) -> dict

Examples

Example 1: Clear peak logit
Input: logits = [2, 1, 0.5]
Output: {'chosen_token': 0, 'is_deterministic': True, 'confidence': 0.6285}
Example 2: Tie breaking lowest index
Input: logits = [1, 3.5, 3.5, 2]
Output: {'chosen_token': 1, 'is_deterministic': True, 'confidence': 0.4338}
Example 3: Empty logits error
Input: logits = []
Output: Raises ValueError

Constraints

  • Empty logits list must raise ValueError.
  • Softmax confidence must be rounded to 4 decimal places.
  • Ties must be broken by lowest token index.
  • A plain-Python solution earns 10 bonus points.
Python3Saved ✓
def greedy_decoding_step(logits: list[float]) -> dict:
"""Select the token with the highest logit and return deterministic confidence.
Args:
logits: Unnormalized token scores across vocabulary.
Returns:
dict with keys 'chosen_token', 'is_deterministic', and 'confidence'.
"""
# Your implementation here
pass

Case 1: Clear peak logit
Input: logits = [2, 1, 0.5]
Expected: {'chosen_token': 0, 'is_deterministic': True, 'confidence': 0.6285}
Case 2: Tie breaking lowest index
Input: logits = [1, 3.5, 3.5, 2]
Expected: {'chosen_token': 1, 'is_deterministic': True, 'confidence': 0.4338}
Case 3: Empty logits error
Input: logits = []
Expected: Raises ValueError