xgrammar
Backend class for XGrammar.
XGrammarBackend
Bases: BaseBackend
Backend for XGRammar.
Source code in outlines/backends/xgrammar.py
__init__(model)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
SteerableModel
|
The Outlines model of the user. |
required |
Source code in outlines/backends/xgrammar.py
get_cfg_logits_processor(grammar)
Create a logits processor from a context-free grammar.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grammar
|
str
|
The context-free grammar to create a logits processor from. |
required |
Returns:
Type | Description |
---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/xgrammar.py
get_json_schema_logits_processor(json_schema)
Create a logits processor from a JSON schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_schema
|
str
|
The JSON schema to create a logits processor from. |
required |
Returns:
Type | Description |
---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/xgrammar.py
get_regex_logits_processor(regex)
Create a logits processor from a regex.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
regex
|
str
|
The regex to create a logits processor from. |
required |
Returns:
Type | Description |
---|---|
LogitsProcessor
|
The logits processor to use to constrain the generation. |
Source code in outlines/backends/xgrammar.py
XGrammarLogitsProcessor
Bases: OutlinesLogitsProcessor
Logits processor for XGrammar.
This class wraps the xgr.contrib.hf.LogitsProcessor
class and adds
a reset
method to reset the logits processor for a new generation.
Source code in outlines/backends/xgrammar.py
__init__(compiled_grammar)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
compiled_grammar
|
str
|
The compiled grammar to use to create the logits processor. |
required |
Source code in outlines/backends/xgrammar.py
process_logits(input_ids, logits)
Bias the logits.