Prompting, lesson 5
Grammars that constrain decoding
Mask invalid tokens at each step so output always fits a grammar.
You know a model gives a score to every token in its vocabulary at each step ("Logits and softmax over the vocabulary" in Transformers), and why apps want replies that fit a schema ("JSON that always parses").
Part 1. Some tokens would break the format
A model is tagging product reviews. The schema says sentiment must be one of three exact words: positive, negative or neutral. The review is "Love it, works great." and the model has written {"sentiment": " so far.
Here are its probabilities for the next token. To keep the sums small, pretend the vocabulary has only five tokens: positive 40.1%, Positive 26.9%, mixed 18.0%, negative 9.0%, neutral 6.0%.
Your turn
If you sample normally, how likely is this step to break the schema?
Part 2. Mask, then sample
Keep going with Pro
You've done the first part. Pro opens the rest of this lesson, and every other one.
- Mask, then sample
- A grammar decides the mask
- Valid isn't the same as right
Prompting overviewSpotted a mistake? Tell us.