vllm.v1.sample.logits_processor ¶
Modules:
Classes:
-
AdapterLogitsProcessor–Wrapper for per-request logits processors
-
BatchUpdate–Persistent batch state change info for logitsprocs
-
BatchUpdateBuilder–Helps track persistent batch state changes and build
-
LogitBiasLogitsProcessor– -
LogitsProcessor– -
LogitsProcessors–Encapsulates initialized logitsproc objects.
-
MinPLogitsProcessor– -
MinTokensLogitsProcessor–
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)
Methods:
-
__init__–Subclass must invoke
-
new_req_logits_processor–Consume request info; return a per-request logits processor.
Source code in vllm/v1/sample/logits_processor/__init__.py
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 | |
__init__(vllm_config, device, is_pin_memory) ¶
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(params, prompt_ids, output_ids) ¶
Return state representation for new request
Returns None if logits processor is not applicable to request
Parameters:
-
(params¶SamplingParams) –request sampling params
-
(prompt_ids¶list[int] | None) –request prompt token ids
-
(output_ids¶list[int]) –decoded tokens so far for this request
Returns:
Source code in vllm/v1/sample/logits_processor/__init__.py
new_req_logits_processor(params) abstractmethod ¶
Consume request info; return a per-request logits processor.
Return None if logits processor does not need to be applied to request
Parameters:
-
(params¶SamplingParams) –request sampling params
Returns:
-
LogitsProcessor | None–None if logits processor should not be applied to request; otherwise
-
LogitsProcessor | None–returns a
RequestLogitsProcessorinstance
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
Methods:
-
get_and_reset–Generate a logitsprocs batch update data structure and reset
-
peek_removed–Return lowest removed request index
-
pop_removed–Pop lowest removed request index
-
removed_append–Register the removal of a request from the persistent batch.
-
reset–Returns True if there were any changes to the batch.
Attributes:
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 | |
removed property ¶
Removed request indices sorted in descending order
_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(batch_size) ¶
Generate a logitsprocs batch update data structure and reset internal batch update builder state.
Parameters:
Returns:
-
BatchUpdate | None–Frozen logitsprocs batch update instance;
Noneif no updates
Source code in vllm/v1/sample/logits_processor/state.py
peek_removed() ¶
pop_removed() ¶
removed_append(index) ¶
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:
Source code in vllm/v1/sample/logits_processor/state.py
reset() ¶
Returns True if there were any changes to the batch.
Source code in vllm/v1/sample/logits_processor/state.py
LogitBiasLogitsProcessor ¶
Bases: LogitsProcessor
Methods:
-
is_argmax_invariant–Logit bias can rebalance token probabilities and change the
Source code in vllm/v1/sample/logits_processor/builtin.py
is_argmax_invariant() ¶
Logit bias can rebalance token probabilities and change the outcome of argmax in greedy sampling.
LogitsProcessor ¶
Bases: ABC
Methods:
-
apply–Apply LogitsProcessor to batch logits tensor.
-
is_argmax_invariant–True if logits processor has no impact on the
-
update_state–Called when there are new output tokens, prior
-
validate_params–Validate sampling params for this logits processor.
Source code in vllm/v1/sample/logits_processor/interface.py
apply(logits) abstractmethod ¶
Apply LogitsProcessor to batch logits tensor.
The updated tensor must be returned but may be modified in-place.
is_argmax_invariant() abstractmethod ¶
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(batch_update) abstractmethod ¶
Called when there are new output tokens, prior to each forward pass.
Parameters:
-
(batch_update¶BatchUpdate | None) –Non-None iff there have been changes to the batch makeup.
Source code in vllm/v1/sample/logits_processor/interface.py
validate_params(sampling_params) classmethod ¶
Validate sampling params for this logits processor.
Raise ValueError for invalid ones.
LogitsProcessors ¶
Encapsulates initialized logitsproc objects.
Attributes:
-
all(Iterator[LogitsProcessor]) –Iterator over all logits processors.
Source code in vllm/v1/sample/logits_processor/state.py
all property ¶
Iterator over all logits processors.
MinPLogitsProcessor ¶
Bases: LogitsProcessor
Methods:
-
is_argmax_invariant–Min-p never impacts greedy sampling
Source code in vllm/v1/sample/logits_processor/builtin.py
MinTokensLogitsProcessor ¶
Bases: LogitsProcessor
Methods:
-
apply_with_spec_decode–Spec-decode version of apply().
-
is_argmax_invariant–By censoring stop tokens, min-tokens can change the outcome
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(logits, num_draft_tokens) ¶
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
is_argmax_invariant() ¶
By censoring stop tokens, min-tokens can change the outcome of the argmax operation in greedy sampling.
_load_custom_logitsprocs(logits_processors) ¶
Load all custom logits processors.
- First load all installed logitproc plugins
- Second load custom logitsprocs pass by the user at initialization time
Parameters:
-
(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
Returns:
-
list[type[LogitsProcessor]]–A list of all loaded logitproc types
Source code in vllm/v1/sample/logits_processor/__init__.py
_load_logitsprocs_by_fqcns(logits_processors) ¶
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:
-
(logits_processors¶Sequence[str | type[LogitsProcessor]] | None) –Potentially mixed list of logitsprocs types and FQCN strings for logitproc types
Returns:
-
list[type[LogitsProcessor]]–List of logitproc types
Source code in vllm/v1/sample/logits_processor/__init__.py
_load_logitsprocs_plugins() ¶
Load all installed logit processor plugins