trie_state
genlm.bytes.byte_lm.trie_state
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 |