built_in
PromptedLLM
Bases: Potential
A potential representing a language model conditioned on a fixed prompt prefix.
PromptedLLM
s operate on byte sequences.
Notes on EOS Token Handling:
-
Tokens to treat as end-of-sequence tokens are specified via the
eos_tokens
argument. -
These tokens are excluded from the potential's vocabulary and as such do not appear in the
vocab
attribute.This means they cannot appear in any input contexts to the potential nor in the output of
logw_next
. They can be used in the prompt however. -
The log probability assigned to the
genlm.control
's reservedEOS
token is the sum of the log probabilities of all the specified EOS tokens.
This class wraps an AsyncLM
instance.
Source code in genlm/control/potential/built_in/llm.py
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 133 134 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
|
__init__(llm, prompt_ids=None, eos_tokens=None, temperature=1)
` Initializes the PromptedLLM potential.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
AsyncLM
|
The language model to use. |
required |
prompt_ids
|
list[int]
|
Optional prompt to use as a prompt prefix for all input contexts.
Must be a list of token IDs. Defaults to None. The prompt ids can be set post-init via |
None
|
eos_tokens
|
list[bytes]
|
List of tokens to treat as end-of-sequence tokens. Defaults to the EOS token of the language model's tokenizer. |
None
|
temperature
|
float
|
The temperature to apply to the language model's logits. Defaults to 1. |
1
|
Raises:
Type | Description |
---|---|
ValueError
|
If any EOS token is not in the language model vocabulary. |
Source code in genlm/control/potential/built_in/llm.py
from_name(name, backend=None, eos_tokens=None, prompt_ids=None, temperature=1.0, **kwargs)
classmethod
Create a PromptedLLM
from a HugginFace model name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the model to load |
required |
backend
|
str
|
Defaults to 'vllm' if CUDA is available, otherwise 'hf'. |
None
|
eos_tokens
|
list[bytes]
|
List of tokens to treat as end-of-sequence tokens. Defaults to the EOS token of the language model's tokenizer. |
None
|
prompt_ids
|
list[int]
|
Optional prompt to use as a prompt prefix for all input contexts.
Must be a list of token IDs. Defaults to None. The prompt ids can be set post-init via |
None
|
temperature
|
float
|
The temperature to apply to the language model's logits. Defaults to 1. |
1.0
|
**kwargs
|
dict
|
Additional arguments passed to AsyncLM constructor |
{}
|
Returns:
Type | Description |
---|---|
PromptedLLM
|
An instance of PromptedLLM |
Source code in genlm/control/potential/built_in/llm.py
prompt
property
Get the current prompt as a list of byte sequences corresponding to the prompt token IDs.
Returns:
Type | Description |
---|---|
list[bytes] | None
|
The current prompt as a list of bytes sequences or None if no prompt_ids are set. |
set_prompt_from_str(prompt_str)
Set the fixed prompt from a string.
Modifies prompt_ids
to be the token IDs of the input prompt according to the language model's tokenizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt_str
|
str
|
The prompt to set. |
required |
Source code in genlm/control/potential/built_in/llm.py
encode_tokens(tokens)
Encode a list of byte tokens to a list of token IDs in the underlying language model's vocabulary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokens
|
list[bytes]
|
List of byte tokens to encode |
required |
Returns:
Type | Description |
---|---|
list[int]
|
A list of token IDs corresponding to the input tokens. |
Raises:
Type | Description |
---|---|
ValueError
|
If any token is not in the vocabulary |
Source code in genlm/control/potential/built_in/llm.py
decode_tokens(ids)
Decode a list of token IDs in the language model's vocabulary to a list of byte tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ids
|
list[int]
|
A list of token IDs in the language model's vocabulary. |
required |
Returns:
Type | Description |
---|---|
list[bytes]
|
A list of byte tokens corresponding to the input token IDs. |
Source code in genlm/control/potential/built_in/llm.py
tokenize(context_str)
Tokenize a string to a list of bytes
objects, each corresponding to a token in the vocabulary.
Uses the language model's tokenizer to map context_str
to a list of token IDs, and then decodes the token IDs to bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context_str
|
str
|
A string to encode |
required |
Returns:
Type | Description |
---|---|
List[bytes]
|
A list of byte tokens corresponding to the input string. |
Source code in genlm/control/potential/built_in/llm.py
log_probability(context)
async
Compute the log probability of context
given the prompt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list[bytes]
|
A sequence of bytes tokens. |
required |
Returns:
Type | Description |
---|---|
float
|
The log probability of |
Source code in genlm/control/potential/built_in/llm.py
prefix(context)
async
Compute the log probability of context
given the prompt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list[bytes]
|
A sequence of bytes tokens. |
required |
Returns:
Type | Description |
---|---|
float
|
The log probability of |
Source code in genlm/control/potential/built_in/llm.py
complete(context)
async
Compute the log probability of context
and the eos tokens given the prompt.
If the model has multiple eos tokens, their probabilities will be summed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list[bytes]
|
A sequence of bytes tokens. |
required |
Returns:
Type | Description |
---|---|
float
|
The log probability of the context. |
Source code in genlm/control/potential/built_in/llm.py
logw_next(context)
async
Get log probabilities for next tokens given the prompt and context
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
List[bytes]
|
A sequence of bytes tokens. |
required |
Returns:
Type | Description |
---|---|
LazyWeights
|
Log probabilities for next tokens and EOS. |
Source code in genlm/control/potential/built_in/llm.py
batch_logw_next(contexts)
async
Get log probabilities for next tokens given the prompt and context
, for a batch of contexts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contexts
|
list[list[bytes]]
|
A list of sequences of bytes tokens. |
required |
Returns:
Type | Description |
---|---|
List[LazyWeights]
|
Log probabilities for next tokens and EOS for each context. |
Source code in genlm/control/potential/built_in/llm.py
spawn()
Spawn a new PromptedLLM with the same prompt and eos tokens.
Returns:
Type | Description |
---|---|
PromptedLLM
|
A new PromptedLLM with the same prompt and eos tokens. |
Note
This is a shallow copy. The new PromptedLLM will share the underlying AsyncLM instance.
Source code in genlm/control/potential/built_in/llm.py
spawn_new_eos(eos_tokens)
Create a new PromptedLLM with a different set of end-of-sequence tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eos_tokens
|
list[bytes]
|
A list of tokens to treat as end-of-sequence tokens. |
required |
Returns:
Type | Description |
---|---|
PromptedLLM
|
A new PromptedLLM with the specified end-of-sequence tokens.
The new model will have the same prompt_ids as |
Source code in genlm/control/potential/built_in/llm.py
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. |
Source code in genlm/control/potential/built_in/wcfg.py
spawn()
WFSA
Bases: Potential
A weighted finite state automaton (WFSA) potential.
This class wraps a genlm_grammar.WFSA
and provides methods for computing the log-weight of a context,
the prefix log-weight of a context, and the log-weights of the next token given a context.
Attributes:
Name | Type | Description |
---|---|---|
wfsa |
WFSA
|
The weighted finite state automaton used for potential calculations. |
Source code in genlm/control/potential/built_in/wfsa.py
11 12 13 14 15 16 17 18 19 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 133 134 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 |
|
__init__(wfsa)
Initializes the WFSA potential.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wfsa
|
WFSA
|
The weighted finite state automaton. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the semiring of the provided WFSA is not Float or Log. |
Note
The WFSA will be converted to the Log semiring to avoid underflow if the semiring is Float.
Source code in genlm/control/potential/built_in/wfsa.py
from_regex(pattern, charset=None, to_bytes=True)
classmethod
Create a WFSA from a regex pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
str
|
The regex pattern to convert into a WFSA. |
required |
charset
|
set
|
The character set to use for negative character classes. Defaults to characters in string.printable. |
None
|
to_bytes
|
bool
|
Whether to convert the WFSA transitions to bytes. Defaults to True. When set to False, the WFSA transitions will be strings. |
True
|
Returns:
Type | Description |
---|---|
WFSA
|
An instance of the WFSA class. |
Note
The transition weights are automatically normalized to form a probability distribution.
For each state, the weights of all outgoing transitions (including final state transitions)
sum to 1.0. This means if a state has n possible transitions, each transition will have
weight 1/n. To create a WFSA from a regex with non-probabilistic transitions, use BoolFSA
.
Source code in genlm/control/potential/built_in/wfsa.py
complete(context)
async
Computes the log weight of the context under the weighted language represented by the WFSA.
For example, if the WFSA accepts "cat" and "car" with weights \(w_{cat}\) and \(w_{car}\):
-
complete("c")
returns \(-\infty\) since this sequence is not accepted by the WFSA -
complete("cat")
returns \(\log(w_{cat})\) -
complete("d")
returns \(-\infty\) since this sequence is not accepted by the WFSA
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
A sequence of tokens in the WFSA's alphabet. |
required |
Returns:
Type | Description |
---|---|
float
|
Log weight of context under the WFSA. |
Source code in genlm/control/potential/built_in/wfsa.py
prefix(context)
async
Computes the prefix log weight of context
under the WFSA.
This corresponds to the log of the sum of the weights of all sequences with prefix context
.
For example, if the WFSA accepts "cat" and "car" with weights \(w_{cat}\) and \(w_{car}\):
-
prefix("c")
returns \(\log(w_{cat} + w_{car})\) -
prefix("ca")
returns \(\log(w_{cat})\) -
prefix("d")
returns \(-\infty\) since the WFSA does not accept any sequences with prefix "d"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
A sequence of tokens in the WFSA's alphabet. |
required |
Returns:
Type | Description |
---|---|
float
|
Log weight of |
Source code in genlm/control/potential/built_in/wfsa.py
logw_next(context)
async
Returns next token log weights given context
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
A sequence of tokens in the WFSA's alphabet. |
required |
Returns:
Type | Description |
---|---|
LazyWeights
|
Log-weights for next token and EOS. |
Source code in genlm/control/potential/built_in/wfsa.py
BoolFSA
Bases: WFSA
Boolean FSA potential.
Source code in genlm/control/potential/built_in/wfsa.py
prefix(context)
async
Computes whether the context is accepted as a prefix by the FSA.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
A sequence of tokens in the WFSA's alphabet. |
required |
Returns:
Type | Description |
---|---|
float
|
|
Source code in genlm/control/potential/built_in/wfsa.py
complete(context)
async
Computes whether the context is accepted by the FSA.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
A sequence of tokens in the WFSA's alphabet. |
required |
Returns:
Type | Description |
---|---|
float
|
|
Source code in genlm/control/potential/built_in/wfsa.py
logw_next(context)
async
Returns next token log weights given context
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
A sequence of tokens in the WFSA's alphabet. |
required |
Returns:
Type | Description |
---|---|
LazyWeights
|
Boolean log-weights for next token. |
Source code in genlm/control/potential/built_in/wfsa.py
batch_logw_next(contexts)
async
Returns next token log weights for a batch of contexts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contexts
|
list
|
The list of contexts. |
required |
Returns:
Type | Description |
---|---|
list
|
List of log-weights for next token, one per context. |
Source code in genlm/control/potential/built_in/wfsa.py
CanonicalTokenization
Bases: Potential
A custom potential that enforces canonical BPE tokenization.
This potential ensures that tokens follow the canonical tokenization rules by using the FastCanonicalityFilterBPE under the hood.
Source code in genlm/control/potential/built_in/canonical.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
|
__init__(canonicality_filter)
Initialize the Canonical Potential
Parameters:
Name | Type | Description | Default |
---|---|---|---|
canonicality_filter
|
FastCanonicalityFilterBPE
|
An initialized FastCanonicalityFilterBPE instance. |
required |
Source code in genlm/control/potential/built_in/canonical.py
from_llm(llm)
classmethod
Factory method to create CanonicalTokenization from a PromptedLLM instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
PromptedLLM
|
An instance of PromptedLLM containing the model and tokenizer. |
required |
Returns:
Type | Description |
---|---|
CanonicalTokenization
|
An initialized CanonicalTokenization instance. |
Source code in genlm/control/potential/built_in/canonical.py
complete(context)
async
Assess if a complete sequence follows canonical tokenization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
Sequence of tokens |
required |
Returns:
Type | Description |
---|---|
float
|
0.0 if canonical, float('-inf') otherwise |
Source code in genlm/control/potential/built_in/canonical.py
prefix(context)
async
Assess if a prefix sequence could potentially extend to a canonical sequence. For canonicality, this is the same as complete.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
Sequence of tokens |
required |
Returns:
Type | Description |
---|---|
float
|
0.0 if potentially canonical, float('-inf') otherwise |
Source code in genlm/control/potential/built_in/canonical.py
logw_next(context)
async
Compute weights for each possible next token given the context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
list
|
Sequence of tokens |
required |
Returns:
Type | Description |
---|---|
LazyWeights
|
Weights for each token in the vocabulary and EOS |