vllm.utils ¶
Modules:
| Name | Description |
|---|---|
argparse_utils | Argument parsing utilities for vLLM. |
async_utils | Contains helpers related to asynchronous code. |
cache | |
collection_utils | Contains helpers that are applied to collections. |
deep_gemm | Compatibility wrapper for DeepGEMM API changes. |
flashinfer | Compatibility wrapper for FlashInfer API changes. |
func_utils | Contains helpers that are applied to functions. |
gc_utils | |
hashing | |
import_utils | Contains helpers related to importing modules. |
jsontree | Helper functions to work with nested JSON structures. |
math_utils | Math utility functions for vLLM. |
mem_constants | |
mem_utils | |
nccl | |
network_utils | |
platform_utils | |
profiling | |
serial_utils | |
system_utils | |
tensor_schema | |
torch_utils | |
MULTIMODAL_MODEL_MAX_NUM_BATCHED_TOKENS module-attribute ¶
POOLING_MODEL_MAX_NUM_BATCHED_TOKENS module-attribute ¶
_DEPRECATED_MAPPINGS module-attribute ¶
_DEPRECATED_MAPPINGS = {
"cprofile": "profiling",
"cprofile_context": "profiling",
"get_open_port": "network_utils",
}
__all__ module-attribute ¶
__all__ = [
"FlexibleArgumentParser",
"SortedHelpFormatter",
"StoreBoolean",
"cdiv",
"next_power_of_2",
"prev_power_of_2",
"round_down",
"round_up",
]
AtomicCounter ¶
An atomic, thread-safe counter
Source code in vllm/utils/__init__.py
Counter ¶
Device ¶
FlexibleArgumentParser ¶
Bases: ArgumentParser
ArgumentParser that allows both underscore and dash in names.
Source code in vllm/utils/argparse_utils.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 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 | |
_json_tip class-attribute instance-attribute ¶
_json_tip: str = 'When passing JSON CLI arguments, the following sets of arguments are equivalent:\n --json-arg \'{"key1": "value1", "key2": {"key3": "value2"}}\'\n --json-arg.key1 value1 --json-arg.key2.key3 value2\n\nAdditionally, list elements can be passed individually using +:\n --json-arg \'{"key4": ["value3", "value4", "value5"]}\'\n --json-arg.key4+ value3 --json-arg.key4+=\'value4,value5\'\n\n'
_FlexibleArgumentGroup ¶
__init__ ¶
Source code in vllm/utils/argparse_utils.py
_pull_args_from_config ¶
Method to pull arguments specified in the config file into the command-line args variable.
The arguments in config file will be inserted between the argument list.
example:
$: vllm {serve,chat,complete} "facebook/opt-12B" --config config.yaml -tp 2
$: args = [
"serve,chat,complete",
"facebook/opt-12B",
'--config', 'config.yaml',
'-tp', '2'
]
$: args = [
"serve,chat,complete",
"facebook/opt-12B",
'--port', '12323',
'--tensor-parallel-size', '4',
'-tp', '2'
]
Please note how the config args are inserted after the sub command. this way the order of priorities is maintained when these are args parsed by super().
Source code in vllm/utils/argparse_utils.py
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 | |
add_argument ¶
add_argument_group ¶
check_port ¶
Source code in vllm/utils/argparse_utils.py
format_help ¶
Source code in vllm/utils/argparse_utils.py
load_config_file ¶
Loads a yaml file and returns the key value pairs as a flattened list with argparse like pattern
returns: processed_args: list[str] = [ '--port': '12323', '--tensor-parallel-size': '4' ]Source code in vllm/utils/argparse_utils.py
parse_args ¶
Source code in vllm/utils/argparse_utils.py
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 | |
parse_known_args ¶
Source code in vllm/utils/argparse_utils.py
LayerBlockType ¶
SortedHelpFormatter ¶
Bases: ArgumentDefaultsHelpFormatter, RawDescriptionHelpFormatter
SortedHelpFormatter that sorts arguments by their option strings.
Source code in vllm/utils/argparse_utils.py
_split_lines ¶
- Sentences split across lines have their single newlines removed.
- Paragraphs and explicit newlines are split into separate lines.
- Each line is wrapped to the specified width (width of terminal).
Source code in vllm/utils/argparse_utils.py
StoreBoolean ¶
Bases: Action
Source code in vllm/utils/argparse_utils.py
__call__ ¶
Source code in vllm/utils/argparse_utils.py
__dir__ ¶
__getattr__ ¶
Module-level getattr to handle deprecated utilities.
Source code in vllm/utils/__init__.py
_maybe_force_spawn ¶
Check if we need to force the use of the spawn multiprocessing start method.
Source code in vllm/utils/__init__.py
bind_kv_cache ¶
bind_kv_cache(
ctx: dict[str, Any],
kv_cache: list[list[Tensor]],
shared_kv_cache_layers: dict[str, str] | None = None,
) -> None
Source code in vllm/utils/__init__.py
cdiv ¶
check_use_alibi ¶
check_use_alibi(model_config: ModelConfig) -> bool
Source code in vllm/utils/__init__.py
enable_trace_function_call_for_thread ¶
enable_trace_function_call_for_thread(
vllm_config: VllmConfig,
) -> None
Set up function tracing for the current thread, if enabled via the VLLM_TRACE_FUNCTION environment variable
Source code in vllm/utils/__init__.py
get_exception_traceback ¶
get_mp_context ¶
Get a multiprocessing context with a particular method (spawn or fork). By default we follow the value of the VLLM_WORKER_MULTIPROC_METHOD to determine the multiprocessing method (default is fork). However, under certain conditions, we may enforce spawn and override the value of VLLM_WORKER_MULTIPROC_METHOD.
Source code in vllm/utils/__init__.py
import_pynvml ¶
Historical comments:
libnvml.so is the library behind nvidia-smi, and pynvml is a Python wrapper around it. We use it to get GPU status without initializing CUDA context in the current process. Historically, there are two packages that provide pynvml: - nvidia-ml-py (https://pypi.org/project/nvidia-ml-py/): The official wrapper. It is a dependency of vLLM, and is installed when users install vLLM. It provides a Python module named pynvml. - pynvml (https://pypi.org/project/pynvml/): An unofficial wrapper. Prior to version 12.0, it also provides a Python module pynvml, and therefore conflicts with the official one. What's worse, the module is a Python package, and has higher priority than the official one which is a standalone Python file. This causes errors when both of them are installed. Starting from version 12.0, it migrates to a new module named pynvml_utils to avoid the conflict. It is so confusing that many packages in the community use the unofficial one by mistake, and we have to handle this case. For example, nvcr.io/nvidia/pytorch:24.12-py3 uses the unofficial one, and it will cause errors, see the issue https://github.com/vllm-project/vllm/issues/12847 for example. After all the troubles, we decide to copy the official pynvml module to our codebase, and use it directly.
Source code in vllm/utils/__init__.py
init_cached_hf_modules ¶
is_uva_available cached ¶
is_uva_available() -> bool
Check if Unified Virtual Addressing (UVA) is available.
kill_process_tree ¶
kill_process_tree(pid: int)
Kills all descendant processes of the given pid by sending SIGKILL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pid | int | Process ID of the parent process | required |
Source code in vllm/utils/__init__.py
length_from_prompt_token_ids_or_embeds ¶
length_from_prompt_token_ids_or_embeds(
prompt_token_ids: list[int] | None,
prompt_embeds: Tensor | None,
) -> int
Calculate the request length (in number of tokens) give either prompt_token_ids or prompt_embeds.
Source code in vllm/utils/__init__.py
next_power_of_2 ¶
prev_power_of_2 ¶
round_down ¶
round_up ¶
run_method ¶
run_method(
obj: Any,
method: str | bytes | Callable,
args: tuple[Any],
kwargs: dict[str, Any],
) -> Any
Run a method of an object with the given arguments and keyword arguments. If the method is string, it will be converted to a method using getattr. If the method is serialized bytes and will be deserialized using cloudpickle. If the method is a callable, it will be called directly.
Source code in vllm/utils/__init__.py
set_ulimit ¶
Source code in vllm/utils/__init__.py
update_environment_variables ¶
Source code in vllm/utils/__init__.py
warn_for_unimplemented_methods ¶
A replacement for abc.ABC. When we use abc.ABC, subclasses will fail to instantiate if they do not implement all abstract methods. Here, we only require raise NotImplementedError in the base class, and log a warning if the method is not implemented in the subclass.
Source code in vllm/utils/__init__.py
weak_bind ¶
Make an instance method that weakly references its associated instance and no-ops once that instance is collected.