BasicsDecodingLLMOpenAIAnthropicGoogle
Problem
Implement greedy_next_token(logits) that selects and returns the integer token ID (argmax) with the highest raw score from a 1D list or NumPy array of vocabulary logits.
Why It Matters: Greedy decoding (temperature = 0) is the deterministic baseline decoding strategy used in LLM structured extraction, code generation benchmarks, and unit-testable inference.
Function Signature
import numpy as np
def greedy_next_token(logits) -> int:
"""Return the token ID with the highest logit score."""
Sample Input & Output
Example 1: Standard Logit Vector
Input:
logits = [-1.2, 3.5, 0.4, 2.9]
Expected Output:
1
Explanation: Token ID 1 has the maximum logit 3.5.
Example 2: All-Negative Logits & Ties
Input:
greedy_next_token([-5.0, -0.5, -2.0]) | greedy_next_token([2.0, 2.0, 1.0])
Expected Output:
1 | 0
Explanation: Works identically on negative logits (-0.5 is largest) and breaks ties by returning the lowest index (0).
Constraints & Edge Cases
- Return a Python
inttoken ID. - You do not need to compute softmax first—
argmax(softmax(z)) == argmax(z)becauseexpis strictly increasing. - Return a standard Python
int.
Constraints & Evaluation Criteria
- Return a Python `int` token ID.
Hints
Learn the Concept First
What Is Generative AI & How Do LLMs Actually Work? (Next-Token Prediction From Zero)
Generative AI · Visual Chapter
Preventing LLM Loops: Repetition Penalty, Presence/Frequency & Min-P
Generative AI · Visual Chapter
Structured JSON Outputs & Constrained Grammar Decoding (FSM Masking)
Generative AI · Visual Chapter
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[-1.2, 3.5, 0.4, 2.9]
Expected Output / Criteria
1