Applications often need the model's output to be valid JSON, a number or a fixed set of labels. Asking nicely in the prompt fails some fraction of the time, and parsing failures break the pipeline. Constrained decoding guarantees validity by editing the logits at every step: tokens that would take the text outside the allowed language are set to -inf so they can never be sampled. The allowed language is described by a small state machine. At each step you ask, for every vocabulary token, whether feeding its characters to the machine from the current state stays valid, and mask the rest.
Implement advance, allowed_token_ids and mask_logits.
transitions mapping (state, char) to the next state. A character with no entry is invalid.advance(transitions, state, token) feeds the characters of token one at a time and returns the final state, or None if any character is invalid. An empty token returns state unchanged.allowed_token_ids(transitions, state, vocab) returns the sorted list of ids i such that advance(transitions, state, vocab[i]) is not None and vocab[i] is non-empty.mask_logits(logits, allowed) returns a copy of logits with -inf at every id not in allowed.Multi-character tokens are common in real vocabularies, so check every character of the token, not just the first.
Test with the language of non-negative integers: states 'start', 'digits', with digits allowed in both and '0' allowed first only as the whole number.
No results yet
Run your code to check it against the examples, or submit to run every test.