base
VocabTables
Bases: NamedTuple
The vocabulary-derived tables a potential needs.
Built by build_tables and shareable by every potential over the same vocabulary.
Attributes:
| Name | Type | Description |
|---|---|---|
token_type |
TokenType
|
The type of tokens in the vocabulary. |
eos |
EndOfSequence
|
Special token to use as end-of-sequence. |
vocab_eos |
list
|
List of tokens in the vocabulary and |
lookup |
dict
|
Mapping from tokens and |
Source code in genlm/control/potential/base.py
Potential
Bases: ABC, PotentialOps, PotentialTests
Abstract base class for potentials.
A Potential is a function that maps sequences of tokens in a vocabulary to non-negative real numbers (weights).
Potentials assign weights to sequences of tokens based on whether they are complete sequences or prefixes of complete sequences.
complete: Assess the log weight of a sequence of tokens in the vocabulary as a complete sequence.prefix: Assess the log weight of a sequence of tokens in the vocabulary as a prefix.
Potentials additionally implement a logw_next method:
logw_next: Compute the next-token log weights of each token in the vocabulary and a special EOS (end-of-sequence) token given a context.
Subclasses must minimally implement complete and prefix. logw_next and batched versions of the above methods
come with default implementations, but may be overridden by subclasses for improved performance.
All Potentials must satisfy a set of properties which can be tested using PotentialTests.
Attributes:
| Name | Type | Description |
|---|---|---|
token_type |
TokenType
|
The type of tokens in the vocabulary. |
vocab |
list
|
List of tokens making up the vocabulary. |
eos |
EndOfSequence
|
Special token to use as end-of-sequence. |
vocab_eos |
list
|
List of tokens in |
lookup |
dict
|
Mapping from tokens and |
Source code in genlm/control/potential/base.py
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 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 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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 | |
build_tables(vocabulary, token_type=None, eos=None)
staticmethod
Build the vocabulary tables a potential is initialized from.
Potentials over the same vocabulary, token_type and eos can share one result
via tables=.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocabulary
|
list
|
List of tokens that make up the vocabulary. |
required |
token_type
|
TokenType
|
Optional TokenType of all elements of the vocabulary. If None, will be inferred from vocabulary. |
None
|
eos
|
EndOfSequence
|
Special token to use as end-of-sequence. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
VocabTables
|
The tables |
Raises:
| Type | Description |
|---|---|
ValueError
|
If vocabulary is empty or contains duplicate tokens. |
TypeError
|
If vocabulary contains tokens which are not of |
Source code in genlm/control/potential/base.py
__init__(vocabulary, token_type=None, eos=None, tables=None)
Initialize the potential.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocabulary
|
list
|
List of tokens that make up the vocabulary. |
required |
token_type
|
TokenType
|
Optional TokenType of all elements of the vocabulary. If None, will be inferred from vocabulary. |
None
|
eos
|
EndOfSequence
|
Special token to use as end-of-sequence. Defaults to |
None
|
tables
|
VocabTables
|
Prebuilt tables for |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If vocabulary is empty, or |
TypeError
|
If vocabulary contains tokens which are not of |
Source code in genlm/control/potential/base.py
tables
property
The VocabTables this potential was built from.
complete(context)
abstractmethod
async
Assess the weight of context as a complete sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
list
|
Sequence of tokens. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Log weight of the context under the language. |
Source code in genlm/control/potential/base.py
prefix(context)
abstractmethod
async
Assess the weight of context as a prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
list
|
Sequence of tokens. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Log weight of the context as a prefix. |
Source code in genlm/control/potential/base.py
logw_eos(context)
async
Assess the log-weight of terminating (EOS) after context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
list
|
Sequence of tokens. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Log weight of terminating after |
Source code in genlm/control/potential/base.py
score(context)
async
Assess the weight of context based on EOS-termination.
This is a convenience method which dispatches to complete if context ends with self.eos, otherwise to prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
list
|
Sequence of tokens. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Log weight of the context, either as a prefix or complete sequence. |
Source code in genlm/control/potential/base.py
is_terminal_only()
Whether this potential contributes weight only at sequence termination.
A terminal-only potential has prefix(context) == 0 for every context; as an SMC
critic it is applied only at termination. Override only in subclasses which satisfy
this invariant.
Returns:
| Type | Description |
|---|---|
bool
|
Whether the potential is terminal-only. Defaults to |
Source code in genlm/control/potential/base.py
sparse_logw_next(context)
async
Compute the finite next-token log weights given context, or None if unsupported.
The result is (indices, values, eos): the vocabulary indices with finite weight,
their log weights (array-like, or one float shared by all of them), and the EOS log
weight. Potentials which implement this override _logw_next_dense rather than
logw_next. A None for any context sends the whole batch in batch_logw_next to
the dense path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
list
|
Sequence of tokens. |
required |
Returns:
| Type | Description |
|---|---|
tuple | None
|
|
Source code in genlm/control/potential/base.py
logw_next(context)
async
Compute the next-token weights of each token in self.vocab_eos given context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
list
|
Sequence of tokens. |
required |
Returns:
| Type | Description |
|---|---|
LazyWeights
|
Weights of each token in the vocabulary and EOS. |
Source code in genlm/control/potential/base.py
batch_complete(contexts)
async
Batched equivalent to complete.
Assess the weight of each context as a complete sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contexts
|
list
|
List of sequences of tokens. |
required |
Returns:
| Type | Description |
|---|---|
array
|
Array of log weights for each context. |
Source code in genlm/control/potential/base.py
batch_prefix(contexts)
async
Batched equivalent to prefix.
Assess the weight of each context as a prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contexts
|
list
|
List of sequences of tokens. |
required |
Returns:
| Type | Description |
|---|---|
array
|
Array of log weights for each context. |
Source code in genlm/control/potential/base.py
batch_score(contexts)
async
Batched equivalent to score.
Assess the weight of each context based on EOS-termination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contexts
|
list
|
List of sequences of tokens. |
required |
Returns:
| Type | Description |
|---|---|
array
|
Array of log weights for each context. |
Source code in genlm/control/potential/base.py
batch_logw_next(contexts)
async
Batched equivalent to logw_next.
Computes the next-token weights of each token in self.vocab_eos given each context in the batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contexts
|
list
|
List of sequences of tokens. |
required |
Returns:
| Type | Description |
|---|---|
LazyWeights
|
Batched weights, |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any context has zero weight (log weight of -inf) under |
Source code in genlm/control/potential/base.py
make_lazy_weights(weights, log=True)
Helper method to create a LazyWeights object over the potential's vocabulary and EOS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
array
|
Array of weights. |
required |
log
|
bool
|
Whether the weights are in log space. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
LazyWeights
|
LazyWeights object defined over |
Source code in genlm/control/potential/base.py
alloc_logws(default=float('-inf'))
Allocate a new array of log weights for the potential's vocabulary and EOS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
float
|
Default log weight. Defaults to -inf. |
float('-inf')
|
Returns:
| Type | Description |
|---|---|
array
|
Array of length |
Source code in genlm/control/potential/base.py
alloc_rows(n, default=float('-inf'))
Allocate a block of log weights for n contexts.
Override to place the block on a device or in another backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of rows. |
required |
default
|
float
|
Default log weight. Defaults to -inf. |
float('-inf')
|
Returns:
| Type | Description |
|---|---|
array
|
Array of shape |
Source code in genlm/control/potential/base.py
spawn()
Spawn a fresh instance of the potential.
This method is not required by default, but may be implemented by subclasses
to support CPU-parallelism using (MultiProcPotential)[genlm.control.potential.multi_proc.MultiProcPotential].