Logit Bias and Banned Tokens

easy
Unit 8 · Decoding0/3 to milestone

LLM APIs let you nudge or forbid specific tokens with a logit bias: add a value to a token's logit before sampling. A bias of -100 effectively bans a token, and +5 strongly encourages it.

Implement apply_logit_bias(logits, bias), where bias maps token id to an additive bias and returns a new array. Then implement ban_tokens(logits, banned), setting those logits to -inf, and greedy_with_bias(logits_per_step, bias): for each step's logit array, apply the bias and pick the argmax (lowest index on ties). Return the list of chosen ids.

Raise ValueError if a bias refers to a token id outside the vocabulary.

#decoding#llm#control2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Logit Bias and Banned Tokens · TechGradify