mlx
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, continues the live KV cache when a batch extends the previous one, and optionally caches computed log probabilities for reuse.
Source code in genlm/backend/llm/mlx.py
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 | |
__init__(mlx_lm_model, tokenizer, timeout=0.0, prefill_step_size=2048, cache_size=0, cache_opts=None)
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 | |
timeout
|
float
|
Seconds to wait once for late concurrent requests before processing a batch. Defaults to 0. |
0.0
|
prefill_step_size
|
int
|
Number of tokens to process per step during prompt prefilling. |
2048
|
cache_size
|
int
|
Maximum size of the output cache. If 0, caching is disabled. Defaults to 0. |
0
|
cache_opts
|
dict
|
Additional options to pass to the
|
None
|
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
add_new_lora(lora_path, lora_name='lora_1')
Register the adapter at lora_path under lora_name.
Re-registering a name rebinds it. Every adapter on a model must share the layout the first one wrapped it with.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lora_path
|
str
|
Directory holding |
required |
lora_name
|
str
|
Name to select the adapter by. |
'lora_1'
|
Source code in genlm/backend/llm/mlx.py
remove_lora(lora_name)
Unregister lora_name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lora_name
|
str
|
Name of the adapter to remove. |
required |
clear_cache()
Clear the output cache, the live KV rows, and the MLX device cache.
next_token_logprobs(token_ids, lora_name=None)
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 |
lora_name
|
str
|
Name of the LoRA adapter to use. Defaults to None (the base model). |
None
|
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, lora_name=None)
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 |
lora_name
|
str
|
Name of the LoRA adapter to use. Defaults to None (the base model). |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Normalized log probability tensor. |
Source code in genlm/backend/llm/mlx.py
batch_next_token_logprobs_sync(token_ids_list, lora_name=None)
Next-token log-probs for each sequence, in one batched forward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids_list
|
list[list[int]]
|
Prompts' token ids. |
required |
lora_name
|
str
|
Name of the LoRA adapter to use. Defaults to None (the base model). |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in genlm/backend/llm/mlx.py
sample(prompt_token_ids, max_tokens, eos_token_ids, temperature=1.0, seed=None, lora_name=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
|
lora_name
|
str
|
Name of the LoRA adapter to use. Defaults to None (the base model). |
None
|
Returns:
| Type | Description |
|---|---|
list[int]
|
The sampled token IDs. |