llm
AsyncVirtualLM
Bases: AsyncLM
Async language model using vLLM v1 with global logits processor.
This implementation uses vLLM v1's in-process mode with a global logits processor to efficiently capture full vocabulary log probabilities.
Source code in genlm/backend/llm/vllm.py
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 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 | |
__init__(llm_engine, logprobs_capture, cache_size=0, cache_opts=None, batch_size=20, timeout=0.02)
Initialize an AsyncVirtualLM instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm_engine
|
LLM
|
The vLLM engine instance. |
required |
logprobs_capture
|
GlobalLogprobsCapture
|
The global logprobs capture processor. |
required |
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
|
batch_size
|
int
|
Maximum queries to process in one batch during auto-batching. Defaults to 20. |
20
|
timeout
|
float
|
Seconds to wait after the first queued query before processing the current batch. The batch also fires immediately when |
0.02
|
Note
The cache stores the log probabilities for previously seen token sequences to avoid redundant requests. KV caching is handled internally by the vLLM engine.
batch_next_token_logprobs_sync bypasses this cache and always re-evaluates; the other three logprobs methods consult it.
Source code in genlm/backend/llm/vllm.py
from_name(model_name, engine_opts=None, **kwargs)
classmethod
Create a AsyncVirtualLM instance from a model name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Name of the model to load. |
required |
engine_opts
|
dict
|
Additional options to pass to the |
None
|
**kwargs
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncVirtualLM
|
An |
Source code in genlm/backend/llm/vllm.py
underlying_model
property
Access the underlying model for advanced use cases.
clear_lora()
add_new_lora(lora_path, lora_name='lora_1')
Load a LoRA adapter into the base model by creating a unique id for it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lora_path
|
str
|
Path to the adapter weights directory or identifier in HuggingFace's model hub. |
required |
lora_name
|
str
|
Name to assign to the loaded adapter. |
'lora_1'
|
Notes
This does not activate the adapter immediately. Call set_lora() to enable the adapter.
Source code in genlm/backend/llm/vllm.py
hash_to_int(value)
Generates a deterministic unique id for a LoRA adapter from its name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The name of the LoRA adapter to hash. |
required |
Returns:
| Type | Description |
|---|---|
|
An integer ID corresponding to the LoRA adapter, in the range [1, 2^31 - 1]. |
Source code in genlm/backend/llm/vllm.py
set_lora(lora_path, lora_name='lora_1')
Configure a LoRA adapter request for the vLLM engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lora_path
|
str
|
Path to the adapter weights directory or identifier in HuggingFace's model hub. |
required |
lora_name
|
str
|
Identifier name to associate with this LoRA adapter within vLLM. |
'lora_1'
|
lora_id
|
int
|
Globally unique ID for the adapter. |
required |
Source code in genlm/backend/llm/vllm.py
next_token_logprobs(token_ids)
async
Request log probabilities of next token asynchronously with auto-batching.
Concurrent calls to this method are automatically batched into a single
LLM.generate() call for efficiency. 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 |
|---|---|---|
result |
Tensor
|
Normalized log probability tensor. |
Source code in genlm/backend/llm/vllm.py
reset_async_queries()
Clear any pending queries from the queue.
Use this method when an exception prevented an inference algorithm from executing to completion.
Source code in genlm/backend/llm/vllm.py
next_token_logprobs_sync(token_ids)
Request log probabilities of next token synchronously.
Does not support auto-batching. For batched sync calls, use
batch_next_token_logprobs_sync instead.
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/vllm.py
batch_next_token_logprobs_sync(token_ids_list)
Request log probabilities of next tokens in a batch synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids_list
|
list[list[int]]
|
A list of token ID lists, each representing a prompt to the language model. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A tensor of normalized log probability tensors, one for each prompt in the input list. |
Note
This method does not consult the output cache (unlike the async batch path,
which delegates to the cached next_token_logprobs). Every prompt is
re-evaluated.
Source code in genlm/backend/llm/vllm.py
clear_cache()
cleanup()
__del__()
sample(prompt_token_ids, max_tokens, eos_token_ids, temperature=1.0, seed=None)
async
Sample from the language model.
Concurrent calls are auto-batched into a single LLM.generate()
so vLLM continuous-batches the decode steps. Use with await.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt_token_ids
|
list[int]
|
The token IDs of the prompt. |
required |
eos_token_ids
|
list[int]
|
The token IDs of the end-of-sequence tokens. |
required |
temperature
|
float
|
The temperature to use to rescale the logits. Defaults to 1.0. |
1.0
|
max_tokens
|
int
|
The maximum number of tokens to generate. |
required |
seed
|
int
|
The seed for the random number generator. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
list[int]
|
The sampled token IDs. |
Source code in genlm/backend/llm/vllm.py
AsyncTransformer
Bases: AsyncLM
Asynchronous wrapper around a HuggingFace causal language model with caching support.
This class provides an asynchronous interface to HuggingFace language models with automatic batching and caching (output and KV) for improved efficiency.
Source code in genlm/backend/llm/hf.py
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 | |
from_name(model_id, bitsandbytes_opts=None, hf_opts=None, **kwargs)
classmethod
Create an AsyncTransformer instance from a pretrained HuggingFace model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
Model identifier in HuggingFace's model hub. |
required |
bitsandbytes_opts
|
dict
|
Additional configuration options for bitsandbytes quantization. Defaults to None. |
None
|
hf_opts
|
dict
|
Additional configuration options for loading the HuggingFace model. Defaults to None. |
None
|
**kwargs
|
Additional arguments passed to the |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncTransformer
|
An initialized |
Source code in genlm/backend/llm/hf.py
__init__(hf_model, hf_tokenizer, batch_size=20, timeout=0.02)
Initialize an AsyncTransformer instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hf_model
|
A HuggingFace CausalLM model instance. |
required | |
hf_tokenizer
|
A HuggingFace Tokenizer. |
required | |
batch_size
|
int
|
Maximum queries to process in one batch during auto-batching. Defaults to 20. |
20
|
timeout
|
float
|
Seconds to wait since last query before processing current batch. Defaults to 0.02. |
0.02
|
Source code in genlm/backend/llm/hf.py
clear_cache()
clear_kv_cache()
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.
cache_kv(prompt_tokens)
Cache the key and value vectors for a prompt. Future queries that have this prompt as a prefix will only run the LLM on new tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt_tokens
|
list[int]
|
token ids for the prompt to cache. |
required |
Source code in genlm/backend/llm/hf.py
add_new_lora(lora_path, lora_name='lora_1')
Load a LoRA adapter into the base model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lora_path
|
str
|
Path to the adapter weights directory or identifier in HuggingFace's model hub. |
required |
lora_name
|
str
|
Name to assign to the loaded adapter. |
'lora_1'
|
Notes
This does not activate the adapter immediately. Call set_lora() to enable the adapter.
Source code in genlm/backend/llm/hf.py
set_lora(lora_path=None, lora_name='lora_1')
Activate a previously loaded LoRA adapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lora_name
|
str
|
Name of the LoRA adapter to activate. |
'lora_1'
|
Source code in genlm/backend/llm/hf.py
clear_lora()
batch_evaluate_queries()
Process a batch of queued language model queries.
This method is called internally when the batch_size has been met or the timeout has expired.
Source code in genlm/backend/llm/hf.py
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 | |
add_query(query, future, past)
Add a query to be evaluated in the next batch.
This method is called internally when a next_token_logprobs request is made.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
list[int]
|
Token IDs representing the query prompt |
required |
future
|
Future
|
Future to store the result in |
required |
past
|
list[tuple[Tensor]] | None
|
Past key/value states from previous evaluation, or None if this is a new query |
required |
Source code in genlm/backend/llm/hf.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 |
|
Source code in genlm/backend/llm/hf.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/hf.py
next_token_logprobs_sync(token_ids)
Request log probabilities of next token. Not asynchronous, and does not support auto-batching.
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 with the language model's log (normalized) probabilities for the next token following the prompt. |
Source code in genlm/backend/llm/hf.py
next_token_logprobs_uncached(token_ids)
Request log probabilities of next token. No KV or output caching, and does not support auto-batching.
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 with the language model's log (normalized) probabilities for the next token following the prompt. |
Source code in genlm/backend/llm/hf.py
AsyncLM
Bases: ABC
Abstract base class for asynchronous language models.
This class provides an interface for language models that can generate token probabilities asynchronously. It handles tokenization and vocabulary management.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer
|
A Hugging Face tokenizer instance compatible with the language model |
required |
Source code in genlm/backend/llm/base.py
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 | |
next_token_logprobs(token_ids)
abstractmethod
async
Request log probabilities of next token asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids
|
list[int]
|
A list of token IDs representing the prompt. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Normalized log probability tensor. |
Source code in genlm/backend/llm/base.py
next_token_logprobs_sync(token_ids)
abstractmethod
Request log probabilities of next token synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids
|
list[int]
|
A list of token IDs representing the prompt. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Normalized log probability tensor. |
Source code in genlm/backend/llm/base.py
batch_next_token_logprobs(token_ids_list)
async
Batch request log probabilities for multiple token sequences asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids_list
|
list[list[int]]
|
A list of token ID lists. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A tensor of log probability tensors. |
Source code in genlm/backend/llm/base.py
batch_next_token_logprobs_sync(token_ids_list)
Batch request log probabilities for multiple token sequences synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids_list
|
list[list[int]]
|
A list of token ID lists. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A tensor of log probability tensors. |
Source code in genlm/backend/llm/base.py
add_new_lora(lora_path, lora_name)
Load a LoRA adapter into the base model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lora_path
|
str
|
Path to the adapter weights directory or identifier in HuggingFace's model hub. |
required |
lora_name
|
str
|
Name to assign to the loaded adapter. |
required |
Source code in genlm/backend/llm/base.py
set_lora(lora_path, lora_name)
Activate a previously loaded LoRA adapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lora_name
|
str
|
Name of the LoRA adapter to activate. |
required |
Source code in genlm/backend/llm/base.py
clear_lora()
clear_cache()
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. |
required |
eos_token_ids
|
list[int]
|
The token IDs of the end-of-sequence tokens. |
required |
temperature
|
float
|
The temperature to use to rescale the logits. Defaults to 1.0. |
1.0
|
max_tokens
|
int
|
The maximum number of tokens to generate. |
required |
seed
|
int
|
The seed for the random number generator. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
list[int]
|
The sampled token IDs. |
Source code in genlm/backend/llm/base.py
batch_sample(prompt_token_ids_list, max_tokens, eos_token_ids, temperature=1.0, seed=None)
async
Batch sample from the language model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt_token_ids_list
|
list[list[int]]
|
The token IDs of the prompts. |
required |
max_tokens
|
int
|
The maximum number of tokens to generate. |
required |
eos_token_ids
|
list[int]
|
The token IDs of the end-of-sequence token. |
required |
temperature
|
float
|
The temperature to use for the logits. |
1.0
|
seed
|
int
|
The seed for the random number generator. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
list[list[int]]
|
The sampled token IDs. |
Source code in genlm/backend/llm/base.py
MockAsyncLM
Bases: AsyncLM
Mock implementation of AsyncLM used for testing.
Source code in genlm/backend/llm/base.py
__init__(tokenizer)
Initialize a MockAsyncLM instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer
|
Hugging Face tokenizer instance |
required |
from_name(model_name, **kwargs)
classmethod
Create a MockAsyncLM instance over the vocabulary of the model's tokenizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Name of pretrained model to load tokenizer from |
required |
**kwargs
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
MockAsyncLM
|
|
Source code in genlm/backend/llm/base.py
next_token_logprobs(token_ids)
async
Get next token log probabilities asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids
|
list[int]
|
Input token IDs. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Normalized log probability tensor. |
Source code in genlm/backend/llm/base.py
next_token_logprobs_sync(token_ids)
Get next token log probabilities synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids
|
list[int]
|
Input token IDs. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Normalized log probability tensor. |
Source code in genlm/backend/llm/base.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 | |
__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. |
Source code in genlm/backend/llm/mlx.py
AsyncSGLTransformer
Bases: AsyncLM
Asynchronous wrapper around a SGLang inference engine.
This class provides an asynchronous interface to SGLang inference engine with automatic batching and caching. It extends AsyncLM to provide efficient batched inference.
The model automatically batches concurrent requests and uses a cache to store computed log probabilities for reuse.
Source code in genlm/backend/llm/sgl.py
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 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 | |
__init__(sgl_model, cache_size=0, cache_opts=None)
Initialize an AsyncSGLTransformer instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sgl_model
|
The SGLang inference engine instance. |
required | |
cache_size
|
int
|
Maximum number of log probabilities to keep in memory. |
0
|
cache_opts
|
dict
|
Additional configuration options for the cache. |
None
|
Source code in genlm/backend/llm/sgl.py
from_name(model_id, engine_opts=None, gpu_id=0, **kwargs)
classmethod
Create an AsyncSGLTransformer instance from a model name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
The name of the model to load. |
required |
engine_opts
|
dict
|
Additional configuration options for the SGLang inference engine. |
None
|
gpu_id
|
int
|
The GPU ID to use for the inference engine. |
0
|
**kwargs
|
Additional arguments passed to the |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncSGLTransformer
|
An initialized |
Source code in genlm/backend/llm/sgl.py
clear_cache()
clear_kv_cache()
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.
Source code in genlm/backend/llm/sgl.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/sgl.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/sgl.py
batch_next_token_logprobs_sync(token_ids_list)
Request log probabilities of next tokens in a batch synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids_list
|
list[list[int]]
|
A list of token ID lists, each representing a prompt. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A tensor of normalized log probability tensors. |
Source code in genlm/backend/llm/sgl.py
load_model_by_name(name, backend=None, llm_opts=None)
Load a language model by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Hugging Face model name (e.g. "gpt2", "meta-llama/Llama-3.2-1B-Instruct") |
required |
backend
|
str
|
Backend to use for inference. Can be "vllm", "hf", "mlx", "sgl", or "mock". If None, defaults to "vllm" if CUDA is available, otherwise "hf". |
None
|
llm_opts
|
dict
|
Additional options to pass to the backend constructor. See AsyncVirtualLM and AsyncTransformer documentation for details. |
None
|
Returns:
| Type | Description |
|---|---|
AsyncLM
|
An asynchronous language model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an invalid backend is specified. |