wcfg
WCFG
Bases: Potential
A weighted context-free grammar potential.
This class wraps a genlm_grammar.CFG
and provides methods for computing the log-weight of a sequence,
the prefix log-weight of a sequence, and the log-weights of the next token given a sequence.
Source code in genlm/control/potential/built_in/wcfg.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
__init__(cfg)
Initialize the WCFG potential.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
CFG
|
The context-free grammar configuration to use. The CFG must in the Float semiring. |
required |
Source code in genlm/control/potential/built_in/wcfg.py
from_string(grammar, to_bytes=True, **kwargs)
classmethod
Create a WCFG from a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grammar
|
str
|
The string grammar specification to create the WCFG from. |
required |
to_bytes
|
bool
|
Whether to convert the WCFG terminals to indivudual bytes. Defaults to True. |
True
|
**kwargs
|
dict
|
Additional arguments passed to the WCFG constructor. |
{}
|
Returns:
Type | Description |
---|---|
WCFG
|
The created WCFG. |
Source code in genlm/control/potential/built_in/wcfg.py
complete(context)
async
Compute the log weight of context
under the WCFG.
For example, if the WCFG accepts "cat" and "car" with weights \(w_{cat}\) and \(w_{car}\):
-
complete("c")
returns \(-\infty\) since this sequence is not accepted by the WCFG -
complete("cat")
returns \(\log(w_{cat})\) -
complete("d")
returns \(-\infty\) since this sequence is not accepted by the WCFG
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
A sequence of tokens in the WCFG's alphabet. |
required |
Returns:
Type | Description |
---|---|
float
|
The log weight of |
Source code in genlm/control/potential/built_in/wcfg.py
prefix(context)
async
Compute the log prefix weight of context
under the WCFG.
This corresponds to the log of the sum of the weights of all sequences with prefix context
.
For example, if the WCFG accepts "cat" and "car" with weights \(w_{cat}\) and \(w_{car}\):
-
prefix("c")
returns \(\log(w_{cat} + w_{car})\) -
prefix("cat")
returns \(\log(w_{cat})\) -
prefix("d")
returns \(-\infty\) since the WCFG does not accept any sequences with prefix "d"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
A sequence of tokens in the WCFG's alphabet. |
required |
Returns:
Type | Description |
---|---|
float
|
The log prefix weight of |
Source code in genlm/control/potential/built_in/wcfg.py
logw_next(context)
async
Compute the next token log weights given context
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
A sequence of tokens in the WCFG's alphabet. |
required |
Returns:
Type | Description |
---|---|
LazyWeights
|
The log weights for the next tokens and EOS given |
Source code in genlm/control/potential/built_in/wcfg.py
clear_cache()
BoolCFG
Bases: Potential
BoolCFG represents a boolean context-free grammar.
Source code in genlm/control/potential/built_in/wcfg.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
from_lark(lark_string, charset='core')
classmethod
Create a BoolCFG instance from a Lark grammar string.
The output grammar will be defined at the byte-level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lark_string
|
str
|
The Lark grammar string to parse. See Lark documentation for correct syntax. |
required |
charset
|
str
|
The character set to use. Defaults to "core".
See |
'core'
|
Returns:
Type | Description |
---|---|
BoolCFG
|
An instance of BoolCFG created from the provided Lark grammar. |
Source code in genlm/control/potential/built_in/wcfg.py
complete(context)
async
Checks whether the context is accepted by the CFG.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
A sequence of tokens in the CFG's alphabet. |
required |
Returns:
Type | Description |
---|---|
float
|
Log weight for whether |
Source code in genlm/control/potential/built_in/wcfg.py
prefix(context)
async
Checks whether context
is accepted as a prefix by the CFG, i.e.,
whether there exists a completion to context
that is accepted by the CFG.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
A sequence of tokens in the CFG's alphabet. |
required |
Returns:
Type | Description |
---|---|
float
|
Log weight for whether |
Source code in genlm/control/potential/built_in/wcfg.py
logw_next(context)
async
Compute the next token log weights given context
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
A sequence of tokens in the CFG's alphabet. |
required |
Returns:
Type | Description |
---|---|
LazyWeights
|
The log weights for the next tokens and EOS given |
Source code in genlm/control/potential/built_in/wcfg.py
batch_logw_next(contexts)
async
Batch version of logw_next
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contexts
|
list
|
A list of sequences of tokens in the CFG's alphabet. |
required |
Returns:
Type | Description |
---|---|
list
|
A list of log-weights for next token, one per context. |