Decode tokens from LLM using top K sampling without using PyTorch
Asked in the Technical interview (ML coding) stage. No documentation was allowed; candidates were advised to brush up on NumPy.
Implement decode_tokens(logits, k, random_values). logits is a list of token-logit rows, k is the number of candidates, and random_values contains one deterministic value in [0, 1) per row. For each row, return a token ID sampled from its top k logits using softmax probabilities. Break equal-logit ties by lower token ID.
def decode_tokens(logits, k, random_values):