llms
Utilities for working with language models.
CachedCausalLM
Wrapper around a genlm.backend.llm.AsyncLM.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
AsyncLM
|
The underlying language model (either |
str_vocab |
list[str]
|
List mapping token IDs to their string representations. |
byte_vocab |
list[bytes]
|
List mapping token IDs to their byte representations. |
masks |
Masks
|
Token masks for filtering logits during generation. |
Source code in llamppl/llms.py
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 | |
vocab
property
Legacy accessor for string vocabulary. Prefer using .str_vocab directly for access to the model's string vocabulary.
__init__(model)
Create a CachedCausalLM from an AsyncLM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
AsyncLM
|
an |
required |
Source code in llamppl/llms.py
cache_kv(prompt_tokens)
Cache the key and value vectors for a prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt_tokens
|
list[int]
|
token ids for the prompt to cache. |
required |
Source code in llamppl/llms.py
clear_cache()
Clear the cache of log probabilities and key/value pairs.
For HuggingFace backend: Clears both logprob cache and KV cache.
For vLLM backend: Only clears logprob cache (KV cache is managed internally by vLLM).
Source code in llamppl/llms.py
clear_kv_cache()
Clear any key and value vectors from the cache.
Source code in llamppl/llms.py
from_pretrained(model_id, backend=None, **kwargs)
classmethod
Create a CachedCausalLM from a HuggingFace model name.
This is a convenience method that instantiates the underlying AsyncLM from a HuggingFace model name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
Name or path of the HuggingFace pretrained model to load. |
required |
backend
|
str
|
|
None
|
**kwargs
|
Additional keyword arguments passed to the |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
CachedCausalLM |
The llamppl-compatible interface to the |
Source code in llamppl/llms.py
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 | |
next_token_logprobs(token_ids)
async
Request log probabilities of next token. This version is asynchronous and support auto batching of 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 |
array
|
a numpy array of length |
Source code in llamppl/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, representing a prompt to the language model. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
logprobs |
array
|
a numpy array of length |
Source code in llamppl/llms.py
reset_async_queries()
Clear any pending language model queries from the queue.
Source code in llamppl/llms.py
Masks
Source code in llamppl/llms.py
precompute_token_lengths(lm)
Precompute the length of each token. Special tokens are considered to have length 0.
Source code in llamppl/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 llamppl/llms.py
TokenSequence
A sequence of tokens.
Supports addition (via + or mutating +=) with:
- other
TokenSequenceinstances (concatenation) - individual tokens, represented as integers or
Tokeninstances - 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 llamppl/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
|