bytes
genlm.bytes
ByteBeamState
Bases: StatefulByteLM
Represents the state of the beam during byte-level language modeling.
Tracks multiple candidate states and their probabilities, pruning low-probability candidates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
states
|
list[LazyTrieState]
|
List of candidate states to track |
required |
params
|
BeamParams
|
Parameters controlling beam search behavior |
required |
Source code in genlm/bytes/byte_lm/beam.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 |
|
initial(llm, params, trie_opts=None)
async
classmethod
Creates initial beam state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
StatefulTokenizedLM
|
Token-level language model to use. |
required |
params
|
BeamParams
|
Beam search parameters. |
required |
trie_opts
|
dict
|
Additional keyword arguments passed to AsyncTokenByteTrie.from_vocab. For example, {"max_batch_size": 100}. |
None
|
Returns:
Type | Description |
---|---|
ByteBeamState
|
Initial beam state. |
Source code in genlm/bytes/byte_lm/beam.py
logZ
cached
property
Estimate of the partition function (sum of weights) for current beam. This is the estimate of the prefix probability of the bytes consumed so far.
__lshift__(a)
async
Advances the beam state with a new byte.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
Byte to add to states. |
required |
Returns:
Type | Description |
---|---|
ByteBeamState
|
New beam state after processing the byte. |
Source code in genlm/bytes/byte_lm/beam.py
logp_next()
async
Computes log probabilities for the next byte across all beam candidates.
Returns:
Type | Description |
---|---|
LazyByteProbs
|
Log probabilities for next possible bytes. |
Source code in genlm/bytes/byte_lm/beam.py
extend(logZ)
async
Attempts to advance each candidate in the beam by a token (EOT).
For each candididate with EOT available, this ends the current token and starts a new one in preparation for the next byte.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logZ
|
float
|
Current estimated of the partition function for pruning |
required |
Returns:
Type | Description |
---|---|
list[LazyTrieState]
|
New candidate states after extension |
Source code in genlm/bytes/byte_lm/beam.py
prune()
Prunes beam to maintain beam width and probability threshold.
Returns:
Type | Description |
---|---|
ByteBeamState
|
New state with pruned candidates. |
Source code in genlm/bytes/byte_lm/beam.py
LazyTrieState
A lazy-evaluated state of a TokenByteTrie traversal.
This class maintains the state of a language model while traversing a trie structure, lazily evaluating probabilities and maintaining the weight of the current path through the trie for beam search.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lm_state
|
StatefulTokenizedLM
|
Current language model state |
required |
trie
|
TokenByteTrie
|
Trie structure mapping tokens to byte sequences |
required |
node
|
int
|
Current node in the trie |
required |
weight
|
float
|
Cumulative log probability of the path to this node |
required |
mass
|
ndarray
|
Masses for each node in the trie for the current state |
None
|
Source code in genlm/bytes/byte_lm/trie_state.py
9 10 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 |
|
initial(lm, trie)
classmethod
Creates an initial trie state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lm
|
AsyncLM
|
Language model to use |
required |
trie
|
TokenByteTrie
|
TokenByteTrie structure for byte-to-token mapping |
required |
Returns:
Type | Description |
---|---|
LazyTrieState
|
Initial state at root of trie with weight 0.0 |
Source code in genlm/bytes/byte_lm/trie_state.py
partial
property
Returns the byte sequence corresponding to the current node in the trie.
mass
property
Returns the log mass for each node in the trie.
The mass at a node corresponds to the sum of the probabilities of all
tokens which share the prefix (self.partial
) represented by that node.
Raises:
Type | Description |
---|---|
ValueError
|
If state hasn't been materialized yet |
actions()
get_EOT()
__lshift__(b)
Transitions to a new state by consuming a byte.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
b
|
int
|
Byte to consume |
required |
Returns:
Type | Description |
---|---|
LazyTrieState | None
|
New state after consuming byte, or None if transition invalid |
Source code in genlm/bytes/byte_lm/trie_state.py
extend()
Extends current state by consuming an end-of-token if possible.
Returns:
Type | Description |
---|---|
LazyTrieState | None
|
New state after consuming EOT, or None if not possible |
Source code in genlm/bytes/byte_lm/trie_state.py
logp_next
cached
property
Computes log probabilities for next possible transitions.
Returns:
Type | Description |
---|---|
LazyByteProbs
|
Lazy log probability distribution over possible next bytes |
materialize()
async
Materializes the masses for each node in the trie for the current state.
This makes a call to the language model and the underlying trie.
Returns:
Type | Description |
---|---|
LazyTrieState
|
Self with materialized masses |
Source code in genlm/bytes/byte_lm/trie_state.py
StatefulTokenizedLM
A stateful tokenized language model that maintains context and generates next token logprobs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
AsyncLM
|
The underlying language model |
required |
context
|
list
|
List of token IDs representing the current context |
required |
n_calls
|
int
|
Number of times the model has been called |
0
|
max_context_length
|
int
|
Maximum length of context to maintain |
None
|
Source code in genlm/bytes/byte_lm/lm_state.py
initial(model, initial_context=None, max_context_length=None)
classmethod
Creates an initial state for the language model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
AsyncLM
|
The language model to use |
required |
initial_context
|
list
|
Initial context of token IDs. Defaults to [tokenizer.bos_token_id] |
None
|
max_context_length
|
int
|
Maximum context length to maintain |
None
|
Returns:
Type | Description |
---|---|
StatefulTokenizedLM
|
A new instance with initial state |
Source code in genlm/bytes/byte_lm/lm_state.py
__lshift__(token)
Adds a new token to the context and returns a new state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
int
|
Token ID to add to context |
required |
Returns:
Type | Description |
---|---|
StatefulTokenizedLM
|
New state with updated context |
Source code in genlm/bytes/byte_lm/lm_state.py
logp_next()
async
Computes log probabilities for the next token given the current context.
Returns:
Type | Description |
---|---|
Tensor
|
Log probabilities for next tokens |
Source code in genlm/bytes/byte_lm/lm_state.py
BeamParams
dataclass
Parameters for byte-level beam summing algorithm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
K
|
int
|
Beam width - maximum number of candidates to maintain. |
required |
prune_threshold
|
float
|
Probability threshold for pruning candidates. Candidates with probability below this are removed. Defaults to 0.0 |
0.0
|
verbose
|
bool
|
Whether to print the beam state at each step. Defaults to False |
False
|
Source code in genlm/bytes/byte_lm/beam.py
TokenByteTrie
A trie data structure for efficient token-to-byte mapping.
Source code in genlm/bytes/trie.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 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 |
|
__init__(decode, device=None, atomic_tokens=None, eot_token=None, max_batch_size=64)
Initialize a TokenByteTrie
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
decode
|
list[bytes]
|
List representing the token vocabulary. |
required |
device
|
str
|
Device to use for weight sum and max computations ('cpu' or 'cuda'). |
None
|
atomic_tokens
|
list[bytes]
|
List of tokens that should be treated as atomic units rather than being split into bytes. |
None
|
eot_token
|
bytes | None
|
End-of-token token. Default is None, which represents EOT as None. |
None
|
max_batch_size
|
int
|
Maximum batch size for weight sum sparse matrix multiplication. |
64
|
Source code in genlm/bytes/trie.py
weight_sum(ws)
Computes the sum of weights of all leaf nodes (tokens) that are descendants of each node in the trie.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ws
|
Tensor
|
Token weights, shape ( |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Summed weights for each node in the trie, shape (num_nodes,). |
Source code in genlm/bytes/trie.py
batch_weight_sum(ws)
Batch version of weight_sum
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ws
|
Tensor
|
Batch of token weights, shape (batch_size × |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Summed weights for each node in the trie, shape (batch_size × num_nodes). |
Source code in genlm/bytes/trie.py
weight_max(ws)
Computes the maximum weight of all descendant leaf nodes (tokens) for each node in the trie.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ws
|
Tensor
|
Token weights, shape ( |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Maximum weights for each node in the trie, shape (num_nodes,). |
Source code in genlm/bytes/trie.py
batch_weight_max(ws)
Batch version of weight_max
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ws
|
Tensor
|
Batch of token weights, shape (batch_size × |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Maximum weights for each node in the trie, shape (batch_size × num_nodes). |
Source code in genlm/bytes/trie.py
visualize(ws=None)
Visualize the trie structure using Graphviz.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ws
|
ndarray | None
|
Optional weight vector to display at each node. Should be of length |
None
|
Returns:
Type | Description |
---|---|
Digraph
|
The generated graph object |
Source code in genlm/bytes/trie.py
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 |
|
AsyncTokenByteTrie
An asynchronous wrapper for TokenByteTrie implementations that provides automatic request batching.
Source code in genlm/bytes/trie.py
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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
|
__init__(trie)
Initialize an AsyncTokenByteTrie
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trie
|
TokenByteTrie
|
The underlying |
required |
from_vocab(vocab, **kwargs)
classmethod
Creates an AsyncTokenByteTrie
from a vocabulary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vocab
|
list
|
The vocabulary over which the trie will be defined. |
required |
**kwargs
|
dict
|
Additional arguments passed to the trie constructor |
{}
|
Returns:
Type | Description |
---|---|
AsyncTokenByteTrie
|
The initialized asynchronous trie instance. |
Source code in genlm/bytes/trie.py
weight_sum(ws)
async
Queue a weight_sum
request. Multiple concurrent calls will be automatically batched
together.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ws
|
Tensor
|
Token weights, shape ( |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The calculated mass sums for the given distribution. |
Source code in genlm/bytes/trie.py
weight_max(ws)
async
Queue a weight_max
request. Multiple concurrent calls will be automatically batched
together.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ws
|
Tensor
|
Token weights, shape ( |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The calculated max weights for the given distribution. |
Source code in genlm/bytes/trie.py
start()
Start the background processing task if not already running.
Source code in genlm/bytes/trie.py
cleanup()
async
Async cleanup - preferred method
shutdown()
Stop the background processing task and cleanup resources.
Source code in genlm/bytes/trie.py
Chart
Bases: dict
A specialized dictionary for managing probability distributions.
Extends dict with operations useful for probability distributions and numeric computations, including arithmetic operations, normalization, and visualization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
zero
|
Any
|
Default value for missing keys |
required |
vals
|
tuple
|
Initial (key, value) pairs |
()
|
Source code in genlm/bytes/util.py
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 |
|
project(f)
Apply the function f
to each key; summing when f-transformed keys overlap.