mlx
Query
dataclass
A query to a language model, waiting to be batched.
Attributes:
| Name | Type | Description |
|---|---|---|
prompt |
list[int]
|
Token IDs representing the input prompt. |
future |
Future
|
Future object to store the result when the query is processed. |
past |
array
|
Past key-value cache states from previous computations. Defaults to None. |
node |
DynamicTokenTrie
|
The cache node where this query should be stored. Defaults to None. |
next_token_index |
int
|
The index in the prompt where new tokens start (after cached prefix). Defaults to None. |
Source code in genlm/backend/llm/mlx.py
AsyncMlxLM
Bases: AsyncLM
Asynchronous MLX-based language model wrapper.
This class provides an async interface to MLX language models with automatic batching, caching, and KV cache management. It extends AsyncLM to provide efficient batched inference with prefix caching.
The model automatically batches concurrent requests and uses a trie-based cache to store computed log probabilities and KV states for reuse.
Source code in genlm/backend/llm/mlx.py
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 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 | |
__init__(mlx_lm_model, tokenizer, batch_size=5, timeout=0.001, prefill_step_size=2048, cache_size=400)
Initialize an AsyncMlxLM instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mlx_lm_model
|
The MLX language model instance. |
required | |
tokenizer
|
The tokenizer for encoding/decoding text. |
required | |
batch_size
|
int
|
Maximum number of queries to batch together. |
5
|
timeout
|
float
|
Maximum time in seconds to wait before processing a batch, even if batch_size is not met. |
0.001
|
prefill_step_size
|
int
|
Number of tokens to process per step during prompt prefilling. |
2048
|
cache_size
|
int
|
Maximum number of KV cache entries to keep in memory. |
400
|
Source code in genlm/backend/llm/mlx.py
from_name(model_name, **kwargs)
classmethod
Create an AsyncMlxLM instance from a model name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Name of the model to load. Can be a Hugging Face model identifier or local path. |
required |
**kwargs
|
Additional arguments passed to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
AsyncMlxLM |
An |
Source code in genlm/backend/llm/mlx.py
clear_cache()
Clear the output cache and MLX device cache.
This method resets the internal token trie cache and clears any cached arrays on the MLX device to free memory.
Source code in genlm/backend/llm/mlx.py
walk_cache(token_ids)
Walk the cache tree to find the deepest node matching a sequence of tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids
|
list[int]
|
Sequence of token IDs to follow in the cache tree |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
A 5-tuple containing: - node: The deepest node in the cache tree that matches the token sequence, irregardless of whether its kv is cached or not - next_token_index: Number of tokens matched from the start of token_ids - past_kvs: Past key/value states concatenated from cached nodes, or None if no cached states were found - kv_node: The cache node where KV states start - kv_next_token_index: Number of tokens matched from the start of token_ids for the KV states |
Source code in genlm/backend/llm/mlx.py
cache_kv(token_ids)
reset_async_queries()
Clear any pending language model queries from the queue. Use this method when an exception prevented an inference algorithm from executing to completion.
add_to_cache(queries, prompt_cache=None, logprobs=None)
Add computed log probabilities and KV states to the cache tree.
Source code in genlm/backend/llm/mlx.py
batch_evaluate_queries()
Process a batch of queued language model queries.
Source code in genlm/backend/llm/mlx.py
add_query(query)
Add a query to be evaluated in the next batch and reset the timeout.
Source code in genlm/backend/llm/mlx.py
next_token_logprobs(token_ids)
async
Request log probabilities of next token. This version is asynchronous because it automatically batches concurrent requests; use with await.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids
|
list[int]
|
a list of token ids, representing a prompt to the language model. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
logprobs |
Tensor
|
a tensor of with the language model's log (normalized) probabilities for the next token following the prompt. |
Source code in genlm/backend/llm/mlx.py
next_token_logprobs_sync(token_ids)
Request log probabilities of next token synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids
|
list[int]
|
A list of token IDs, representing a prompt to the language model. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Normalized log probability tensor. |
Source code in genlm/backend/llm/mlx.py
sample(prompt_token_ids, max_tokens, eos_token_ids, temperature=1.0, seed=None)
async
Sample from the language model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt_token_ids
|
list[int]
|
The token IDs of the prompt to start generation from. |
required |
max_tokens
|
int
|
The maximum number of tokens to generate. |
required |
eos_token_ids
|
list[int]
|
The token IDs that signal end-of-sequence. Generation stops when one of these is sampled. |
required |
temperature
|
float
|
The temperature to use for sampling. Higher values make the distribution more uniform, lower values make it more peaked. Defaults to 1.0. |
1.0
|
seed
|
int
|
The seed for the random number generator. If provided, sets the random seed before sampling. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
list[int]
|
The sampled token IDs. |