syncode icon indicating copy to clipboard operation
syncode copied to clipboard

Grammar_mask vs grammar_strict mode

Open Sindre01 opened this issue 8 months ago • 1 comments

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?

Sindre01 avatar Apr 20 '25 16:04 Sindre01

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.

shubhamugare avatar Apr 21 '25 23:04 shubhamugare