syncode
syncode copied to clipboard
Grammar_mask vs grammar_strict mode
Hi :) Can someone please tell me the difference between the grammar_mask and grammar_strict mode? What are the difference in calculation of tokens etc.
I am using constrained decoding for a low-resource language. The grammar_strict mode fails in nearly every task. When i try grammar_mask mode it passes much more often without an exception. Also, for best results in my case should i set opportunistic to True or False?
The main difference between the two modes is the following. Which one is more useful depends on the task. In general, I observed that grammar_mask works better for JSON and some other well-known PLs.
1. `grammar_mask` (Conservative/Overapproximation):
This mode is more permissive and overapproximates the set of acceptable tokens.
It allows a wider range of tokens that might be syntactically valid given the
limited lookahead of the parser. This mode preserves more of the LLM's original
token distribution while still enforcing basic syntactic correctness.
2. `grammar_strict` (Strict/Underapproximation):
This mode is stricter and underapproximates the set of acceptable tokens.
It enforces tighter grammatical constraints and may be more invasive in the
LLM's generation process. It sometimes breaks LLM tokens that would have been
syntactically correct when considered as a whole, potentially affecting the
accuracy of generation.