xgr.CompiledGrammar¶
- class xgrammar.CompiledGrammar[source]¶
This is the primary object to store compiled grammar.
A CompiledGrammar can be used to construct GrammarMatcher to generate token masks efficiently.
Notes
Do not construct this class directly, instead use
GrammarCompiler
to construct the object.Attributes:
The original grammar.
The tokenizer info associated with the compiled grammar.
The approximate memory usage of the compiled grammar in bytes.
Methods:
Serialize the compiled grammar to a JSON string.
deserialize_json
(json_str, tokenizer_info)Deserialize the compiled grammar from a JSON string and tokenizer info.
- property tokenizer_info: TokenizerInfo¶
The tokenizer info associated with the compiled grammar.
- static deserialize_json(json_str: str, tokenizer_info: TokenizerInfo) CompiledGrammar [source]¶
Deserialize the compiled grammar from a JSON string and tokenizer info.