vllm.v1.sample.logits_processor ¶
Modules:
| Name | Description |
|---|---|
builtin | |
interface | |
state | |
AdapterLogitsProcessor ¶
Bases: LogitsProcessor
Wrapper for per-request logits processors
To wrap a specific per-request logits processor, * Subclass AdapterLogitsProcessor * Implement self.is_argmax_invariant() base-class method * Implement self.new_req_logits_processor(params)
self.__init__(vllm_config, device, is_pin_memory) does not need to be overridden in general. However, to implement custom constructor behavior - especially any logic which operates on or stores vllm_config, device, or is_pin_memory - self.__init__(vllm_config, device, is_pin_memory) must be overridden and the override must call super().__init__(vllm_config, device, is_pin_memory)
Source code in vllm/v1/sample/logits_processor/__init__.py
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 | |
__init__ ¶
__init__(
vllm_config: VllmConfig,
device: device,
is_pin_memory: bool,
)
Subclass must invoke super().__init__(vllm_config, device, is_pin_memory).
Subclass constructor may find it useful to utilize the vllm_config, device and is_pin_memory argument. However regardless of whether these arguments are used, the vLLM logits processor interface requires all three arguments to be present.
Source code in vllm/v1/sample/logits_processor/__init__.py
_new_state ¶
_new_state(
params: SamplingParams,
prompt_ids: list[int] | None,
output_ids: list[int],
) -> partial[Tensor] | None
Return state representation for new request
Returns None if logits processor is not applicable to request
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params | SamplingParams | request sampling params | required |
prompt_ids | list[int] | None | request prompt token ids | required |
output_ids | list[int] | decoded tokens so far for this request | required |
Returns:
| Type | Description |
|---|---|
partial[Tensor] | None | logits processor partial[Tensor] or None |
Source code in vllm/v1/sample/logits_processor/__init__.py
new_req_logits_processor abstractmethod ¶
new_req_logits_processor(
params: SamplingParams,
) -> LogitsProcessor | None
Consume request info; return a per-request logits processor.
Return None if logits processor does not need to be applied to request
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params | SamplingParams | request sampling params | required |
Returns:
| Type | Description |
|---|---|
LogitsProcessor | None | None if logits processor should not be applied to request; otherwise |
LogitsProcessor | None | returns a |
Source code in vllm/v1/sample/logits_processor/__init__.py
BatchUpdate dataclass ¶
Persistent batch state change info for logitsprocs
Source code in vllm/v1/sample/logits_processor/interface.py
BatchUpdateBuilder ¶
Helps track persistent batch state changes and build a batch update data structure for logitsprocs Assumptions: * All information about requests removed from persistent batch during a step is aggregated in self._removed through calls to self.removed_append() at the beginning of a step. This must happen before the first time that self.removed, self.pop_removed() or self.peek_removed() are invoked in a given step * After the first time that self.removed, self.pop_removed() or self.peek_removed() are read in a step, no new removals are registered using self.removed_append() * Elements of self._removed are never directly modified, added or removed (i.e. modification is only via self.removed_append() and self.pop_removed()) Guarantees under above assumptions: * self.removed is always sorted in descending order * self.pop_removed() and self.peek_removed() both return the lowest removed request index in the current step
Source code in vllm/v1/sample/logits_processor/state.py
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 | |
_ensure_removed_sorted ¶
Sort removed request indices in descending order. Idempotent after first call in a given step, until reset.
Source code in vllm/v1/sample/logits_processor/state.py
get_and_reset ¶
get_and_reset(batch_size: int) -> BatchUpdate | None
Generate a logitsprocs batch update data structure and reset internal batch update builder state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size | int | current persistent batch size | required |
Returns:
| Type | Description |
|---|---|
BatchUpdate | None | Frozen logitsprocs batch update instance; |
Source code in vllm/v1/sample/logits_processor/state.py
removed_append ¶
removed_append(index: int) -> None
Register the removal of a request from the persistent batch.
Must not be called after the first time self.removed, self.pop_removed() or self.peek_removed() are invoked.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index | int | request index | required |
Source code in vllm/v1/sample/logits_processor/state.py
LogitBiasLogitsProcessor ¶
Bases: LogitsProcessor
Source code in vllm/v1/sample/logits_processor/builtin.py
LogitsProcessor ¶
Bases: ABC
Source code in vllm/v1/sample/logits_processor/interface.py
apply abstractmethod ¶
Apply LogitsProcessor to batch logits tensor.
The updated tensor must be returned but may be modified in-place.
is_argmax_invariant abstractmethod ¶
is_argmax_invariant() -> bool
True if logits processor has no impact on the argmax computation in greedy sampling. NOTE: may or may not have the same value for all instances of a given LogitsProcessor subclass, depending on subclass implementation.
Source code in vllm/v1/sample/logits_processor/interface.py
update_state abstractmethod ¶
update_state(batch_update: BatchUpdate | None) -> None
Called when there are new output tokens, prior to each forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_update | BatchUpdate | None | Non-None iff there have been changes to the batch makeup. | required |
Source code in vllm/v1/sample/logits_processor/interface.py
validate_params classmethod ¶
validate_params(sampling_params: SamplingParams)
Validate sampling params for this logits processor.
Raise ValueError for invalid ones.
LogitsProcessors ¶
Encapsulates initialized logitsproc objects.
Source code in vllm/v1/sample/logits_processor/state.py
MinPLogitsProcessor ¶
Bases: LogitsProcessor
Source code in vllm/v1/sample/logits_processor/builtin.py
MinTokensLogitsProcessor ¶
Bases: LogitsProcessor
Source code in vllm/v1/sample/logits_processor/builtin.py
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 | |
apply_with_spec_decode ¶
Spec-decode version of apply(). Priority: min_tokens > stop_token_ids / EOS. Example: num_draft_tokens = [2, 3, 1] → logits shape [6, V], cumsum = [0, 2, 5, 6] → request 0 owns rows 0‑1, request 1 rows 2‑4, request 2 row 5.
Source code in vllm/v1/sample/logits_processor/builtin.py
ThinkingTokenBudgetLogitsProcessor ¶
Bases: LogitsProcessor
Limits the number of tokens allowed inside a 'thinking' section.
Source code in vllm/v1/sample/logits_processor/builtin.py
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 | |
_find_last_sequence_index staticmethod ¶
Returns the index of the last occurrence of token_ids in target_list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_list | list[int] | The list of token IDs. | required |
token_ids | list[int] | The sequence of token IDs to find. | required |
Source code in vllm/v1/sample/logits_processor/builtin.py
_init_state_entry ¶
_init_state_entry(
prompt_tok_ids: list[int] | None,
thinking_token_budget: int,
) -> dict[str, Any]
Initializes the tracking state for a given sequence index.
Source code in vllm/v1/sample/logits_processor/builtin.py
_update_think_state ¶
Updates the state based on newly generated output tokens.
Source code in vllm/v1/sample/logits_processor/builtin.py
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 | |
_load_custom_logitsprocs ¶
_load_custom_logitsprocs(
logits_processors: Sequence[str | type[LogitsProcessor]]
| None,
) -> list[type[LogitsProcessor]]
Load all custom logits processors.
- First load all installed logitproc plugins
- Second load custom logitsprocs pass by the user at initialization time
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits_processors | Sequence[str | type[LogitsProcessor]] | None | potentially mixed list of logitproc types and logitproc type fully-qualified names (FQCNs) which need to be loaded | required |
Returns:
| Type | Description |
|---|---|
list[type[LogitsProcessor]] | A list of all loaded logitproc types |
Source code in vllm/v1/sample/logits_processor/__init__.py
_load_logitsprocs_by_fqcns ¶
_load_logitsprocs_by_fqcns(
logits_processors: Sequence[str | type[LogitsProcessor]]
| None,
) -> list[type[LogitsProcessor]]
Load logit processor types, identifying them by fully-qualified class names (FQCNs).
Effectively, a mixed list of logitproc types and FQCN strings is converted into a list of entirely logitproc types, by loading from the FQCNs.
FQCN syntax is
Already-loaded logitproc types must be subclasses of LogitsProcessor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits_processors | Sequence[str | type[LogitsProcessor]] | None | Potentially mixed list of logitsprocs types and FQCN strings for logitproc types | required |
Returns:
| Type | Description |
|---|---|
list[type[LogitsProcessor]] | List of logitproc types |
Source code in vllm/v1/sample/logits_processor/__init__.py
_load_logitsprocs_plugins ¶
_load_logitsprocs_plugins() -> list[type[LogitsProcessor]]
Load all installed logit processor plugins