trie
AsyncTokenCharacterTrie
An asynchronous wrapper for TokenCharacterTrie
implementations.
This class provides asynchronous access to mass sum calculations, with automatic batching of concurrent requests. It maintains a background task that processes queued requests.
Source code in genlm_backend/trie/async_impl.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 |
|
__init__(trie)
Initialize an AsyncTokenCharacterTrie
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trie
|
TokenCharacterTrie | ParallelTokenCharacterTrie
|
The underlying |
required |
Source code in genlm_backend/trie/async_impl.py
from_vocab(byte_vocab, backend='parallel', **kwargs)
classmethod
Creates an AsyncTokenCharacterTrie
from a byte vocabulary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
byte_vocab
|
list[byte]
|
The byte vocabulary over which the trie will be defined. |
required |
backend
|
str
|
The trie implementation to use - either 'sequential' or 'parallel'. Defaults to 'parallel' which uses GPU acceleration when available. |
'parallel'
|
**kwargs
|
Additional arguments passed to the trie constructor |
{}
|
Returns:
Type | Description |
---|---|
AsyncTokenCharacterTrie
|
The initialized asynchronous trie instance. |
Source code in genlm_backend/trie/async_impl.py
mass_sum(p_llm)
async
Asynchronously computes the mass at each node of the trie.
This method queues the mass calculation to be processed in a background task. Multiple concurrent requests are automatically batched together.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p_llm
|
Tensor
|
Probability distribution over the trie's vocabulary of length |
required |
Returns:
Type | Description |
---|---|
float
|
The calculated mass sum for the given distribution. |
Source code in genlm_backend/trie/async_impl.py
shutdown()
ParallelTokenCharacterTrie
Bases: TokenCharacterTrie
A GPU-optimized version of TokenCharacterTrie
that performs mass_sum
in parallel.
Inherits from TokenCharacterTrie
.
The mass at leaf nodes is propagated to their ancestors through sparse matrix multiplication with a reachability matrix. The reachability matrix is constructed at initialization.
Implementation details:
The reachability matrix M is a num_leafs × num_nodes matrix
where M[i,j] = 1 if:
- leaf_indices[i] == j (self connection) or
- j is an ancestor of leaf_indices[i] in the trie
Example:
Trie: M:
0 [[1, 1, 0, 1],
/ \ [1, 0, 1, 0]]
1 2 (leaf index = 1)
|
3 (leaf index = 0)
The matrix is stored as a sparse tensor in CSR (Compressed Sparse Row) format,
built from COO (Coordinate) format. For example,
rows = [1, 0, 1, 0, 0] (index of leaf node)
cols = [2, 3, 0, 1, 0] (connections)
vals = [1, 1, 1, 1, 1] (connection weights)
When computing masses (batch_size × num_leafs) @ M, each leaf node's mass
flows up to all its ancestors.
Source code in genlm_backend/trie/parallel.py
5 6 7 8 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 |
|
batch_mass_sum(p_llms)
Computes mass sums for a batch of probability distributions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p_llms
|
Tensor
|
Batch of probability distributions over tokens, shape (batch_size × vocab_size). |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Summed masses for each node in the trie, shape (batch_size × num_nodes). |
Source code in genlm_backend/trie/parallel.py
mass_sum(p_llm)
Computes the sum of masses for a single probability distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p_llm
|
Tensor
|
Probability distribution over tokens from the LLM. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Summed masses for each node in the trie. |
Source code in genlm_backend/trie/parallel.py
TokenCharacterTrie
A trie data structure for efficient token-to-character mapping and probability mass computation.
Each node in the trie corresponds to a token prefix. The probability mass computation provides the marginal probability of each prefix under a given distribution over the token vocabulary.
Source code in genlm_backend/trie/base.py
7 8 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 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 |
|
__init__(decode, old_eos=None, new_eos=None)
Initialize a TokenCharacterTrie
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
decode
|
list[bytes]
|
List of byte strings representing the token vocabulary. |
required |
old_eos
|
str | bytes | None
|
The current end-of-sequence token to be replaced. If provided as str, will be encoded to bytes. |
None
|
new_eos
|
str | bytes | None
|
The new end-of-sequence token to use. If provided as str, will be encoded to bytes. |
None
|
Source code in genlm_backend/trie/base.py
batch_mass_sum(p_llms)
Compute probability mass for multiple distributions over tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p_llms
|
list[Tensor | ndarray]
|
Batch of token probability distributions |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Batch of probability mass values of |
Source code in genlm_backend/trie/base.py
mass_sum(p_llm)
Compute probability mass for each node in the trie.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p_llm
|
Tensor | ndarray
|
Token probabilities from language model |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Probability mass values for each node in the trie.
The mass corresponds to the marginal probability under |
Source code in genlm_backend/trie/base.py
visualize(mass=None)
Visualize the trie structure using Graphviz.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mass
|
ndarray | None
|
Optional mass vector to display at each node.
Should be of length |
None
|
Returns:
Type | Description |
---|---|
Digraph
|
The generated graph object |
Source code in genlm_backend/trie/base.py
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 |
|