llms
Utilities for working with HuggingFace language models, including caching and auto-batching.
CachedCausalLM
Wrapper around a HuggingFace causal language model, with support for caching.
Attributes:
Name | Type | Description |
---|---|---|
model |
the underlying HuggingFace model. |
|
tokenizer |
the underlying HuggingFace tokenizer. |
|
device |
str
|
the PyTorch device identifier (e.g. "cpu" or "cuda:0") on which the model is loaded. |
cache |
TokenTrie
|
the cache of previously evaluated log probabilities and key/value vectors. |
vocab |
list[str]
|
a list mapping token ids to strings. |
batch_size |
int
|
when auto-batching, maximum number of queries to process in one batch. |
timeout |
float
|
number of seconds to wait since last query before processing the current batch of queries, even if not full. |
Source code in hfppl/llms.py
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 |
|
__init__(hf_model, hf_tokenizer, batch_size=20)
Create a CachedCausalLM
from a loaded HuggingFace model and tokenizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hf_model |
a HuggingFace |
required | |
hf_tokenizer |
a HuggingFace |
required | |
batch_size |
int
|
when auto-batching, maximum number of queries to process in one batch. |
20
|
Source code in hfppl/llms.py
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 hfppl/llms.py
clear_cache()
clear_kv_cache()
from_pretrained(model_id, auth_token=False, load_in_8bit=True)
classmethod
Create a CachedCausalLM
from a pretrained HuggingFace model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_id |
str
|
the string identifier of the model in HuggingFace's model library. |
required |
auth_token |
str
|
a HuggingFace API key. Only necessary if using private models, e.g. Meta's Llama models, which require authorization. |
False
|
load_in_8bit |
bool
|
whether to use the |
True
|
Returns:
Name | Type | Description |
---|---|---|
model |
CachedCausalLM
|
the LLaMPPL-compatible interface to the HuggingFace model. |
Source code in hfppl/llms.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 starting with |
required |
Returns:
Name | Type | Description |
---|---|---|
logprobs |
array
|
a numpy array of |
Source code in hfppl/llms.py
next_token_logprobs_unbatched(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 starting with |
required |
Returns:
Name | Type | Description |
---|---|---|
logprobs |
array
|
a numpy array of |
Source code in hfppl/llms.py
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.
Masks
Source code in hfppl/llms.py
precompute_token_length_masks(lm)
Precompute masks for tokens of different lengths.
Each mask is a set of token ids that are of the given length or shorter.
Source code in hfppl/llms.py
Query
A query to a language model, waiting to be batched.
Source code in hfppl/llms.py
Token
Class representing a token.
Attributes:
Name | Type | Description |
---|---|---|
lm |
CachedCausalLM
|
the language model for which this is a Token. |
token_id |
int
|
the integer token id (an index into the vocabulary). |
token_str |
str
|
a string, which the token represents—equal to |
Source code in hfppl/llms.py
TokenSequence
A sequence of tokens.
Supports addition (via +
or mutating +=
) with:
- other
TokenSequence
instances (concatenation) - individual tokens, represented as integers or
Token
instances - strings, which are tokenized by
lm.tokenizer
Attributes:
Name | Type | Description |
---|---|---|
lm |
CachedCausalLM
|
the language model whose vocabulary the tokens come from. |
seq |
list[Token]
|
the sequence of tokens. |
Source code in hfppl/llms.py
__init__(lm, seq=None)
Create a TokenSequence
from a language model and a sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lm |
CachedCausalLM
|
the language model whose vocabulary the tokens come from. |
required |
seq |
str | list[int]
|
the sequence of token ids, or a string which will be automatically tokenized. Defaults to the singleton sequence containing a bos token. |
None
|
Source code in hfppl/llms.py
TokenTrie
Class used internally to cache language model results.