byte_lm
genlm.bytes.byte_lm
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
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 | |
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
with_mode(mode)
Create a new beam state with specified trie mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
TrieMode
|
Trie mode for the new beam state |
required |
Returns:
| Type | Description |
|---|---|
ByteBeamState
|
New beam state with updated mode |
Source code in genlm/bytes/byte_lm/beam.py
prefill(bs)
async
Prefill the beam on a sequence of bytes.
During prefilling, EOS tokens are treated as normal tokens and don't cause termination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bs
|
bytes
|
Byte sequence to prefill on |
required |
Returns:
| Type | Description |
|---|---|
ByteBeamState
|
New beam state after prefilling |
Source code in genlm/bytes/byte_lm/beam.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
|
eos_tokens
|
list[bytes]
|
List of tokens that should be treated as EOS. When configured, EOS tokens will terminate generation when sampled. Defaults to None |
None
|
heal
|
bool
|
Whether to enable adaptive token healing. Defaults to True |
True
|
heal_max_backoff
|
int
|
Maximum number of bytes to back off when healing. Defaults to None |
None
|
heal_max_splits
|
int
|
Maximum number of intra-suffix commits allowed during a single healing attempt. Defaults to None |
None
|
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
|
mode
|
TrieMode
|
Trie mode to use |
WITH_EOS
|
terminated
|
bool
|
Whether the state is terminated (EOS has been consumed) |
False
|
Source code in genlm/bytes/byte_lm/trie_state.py
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 | |
initial(lm, trie, mode=TrieMode.WITH_EOS)
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 |
mode
|
TrieMode
|
Trie mode to use |
WITH_EOS
|
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 |
with_mode(mode)
Returns a new state with the given mode.
Source code in genlm/bytes/byte_lm/trie_state.py
actions()
get_EOT()
Returns the end-of-token node if available from current position in the trie.
__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 (terminated or EOS) |
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 |