vllm.model_executor.model_loader.weight_utils ¶
Utilities for downloading and initializing model weights.
_natural_sort_key ¶
Natural sort key for filenames with numeric components, such as model-00001-of-00005.safetensors -> ['model-', 1, '-of-', 5, '.safetensors']
Source code in vllm/model_executor/model_loader/weight_utils.py
_prefetch_all_checkpoints ¶
Start prefetching checkpoint files into page cache in a background thread.
Source code in vllm/model_executor/model_loader/weight_utils.py
_prefetch_checkpoint ¶
_prefetch_checkpoint(file_path: str) -> None
Prefetch a checkpoint file into the OS page cache.
Reads the file in 16MB blocks so the kernel caches its pages before workers load the same file.
Source code in vllm/model_executor/model_loader/weight_utils.py
atomic_writer ¶
atomic_writer(
filepath: str | Path,
mode: str = "w",
encoding: str | None = None,
) -> Generator[IO]
Context manager that provides an atomic file writing routine.
The context manager writes to a temporary file and, if successful, atomically replaces the original file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath | str or Path | The path to the file to write. | required |
mode | str | The file mode for the temporary file (e.g., 'w', 'wb'). | 'w' |
encoding | str | The encoding for text mode. | None |
Yields:
| Type | Description |
|---|---|
Generator[IO] | file object: A handle to the temporary file. |
Source code in vllm/model_executor/model_loader/weight_utils.py
composed_weight_loader ¶
Create a weight loader that post-processes the weights after loading
Source code in vllm/model_executor/model_loader/weight_utils.py
convert_pyslice_to_tensor ¶
convert PySafeSlice object from safetensors to torch.Tensor
PySafeSlice object supports indexing, which is done before loading the actual tensor and can reduce the amount of memory being read into the memory. However, it does not support more advanced functionalities like .view() or .t(). Therefore, if we need to modify the loaded tensor with these more complicated operators, we need to convert to tensor first.
Source code in vllm/model_executor/model_loader/weight_utils.py
default_weight_loader ¶
Default weight loader.
Source code in vllm/model_executor/model_loader/weight_utils.py
download_safetensors_index_file_from_hf ¶
download_safetensors_index_file_from_hf(
model_name_or_path: str,
index_file: str,
cache_dir: str | None,
subfolder: str | None = None,
revision: str | None = None,
) -> None
Download hf safetensors index file from Hugging Face Hub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name_or_path | str | The model name or path. | required |
index_file | str | The safetensors index file name | required |
cache_dir | Optional[str] | The cache directory to store the model weights. If None, will use HF defaults. | required |
subfolder | Optional[str] | The subfolder within the model repository to download weights from. | None |
revision | Optional[str] | The revision of the model. | None |
Source code in vllm/model_executor/model_loader/weight_utils.py
download_weights_from_hf ¶
download_weights_from_hf(
model_name_or_path: str,
cache_dir: str | None,
allow_patterns: list[str],
revision: str | None = None,
subfolder: str | None = None,
ignore_patterns: str | list[str] | None = None,
) -> str
Download model weights from Hugging Face Hub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name_or_path | str | The model name or path. | required |
cache_dir | Optional[str] | The cache directory to store the model weights. If None, will use HF defaults. | required |
allow_patterns | list[str] | The allowed patterns for the weight files. Files matched by any of the patterns will be downloaded. | required |
revision | Optional[str] | The revision of the model. | None |
subfolder | Optional[str] | The subfolder within the model repository to download weights from. | None |
ignore_patterns | Optional[Union[str, list[str]]] | The patterns to filter out the weight files. Files matched by any of the patterns will be ignored. | None |
Returns:
| Name | Type | Description |
|---|---|---|
str | str | The path to the downloaded model weights. |
Source code in vllm/model_executor/model_loader/weight_utils.py
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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
enable_hf_transfer ¶
automatically activates hf_transfer
Source code in vllm/model_executor/model_loader/weight_utils.py
enable_xet_high_performance ¶
automatically activates xet high performance mode
fastsafetensors_weights_iterator ¶
fastsafetensors_weights_iterator(
hf_weights_files: list[str], use_tqdm_on_load: bool
) -> Generator[tuple[str, Tensor], None, None]
Iterate over the weights in the model safetensor files using fastsafetensor library.
Source code in vllm/model_executor/model_loader/weight_utils.py
filter_files_not_needed_for_inference ¶
Exclude files that are not needed for inference.
See https://github.com/huggingface/transformers/blob/v4.34.0/src/transformers/trainer.py#L227-L233
Source code in vllm/model_executor/model_loader/weight_utils.py
get_gguf_weight_type_map ¶
Return GGUF mapped weight's name and its quant type
Source code in vllm/model_executor/model_loader/weight_utils.py
gguf_quant_weights_iterator ¶
gguf_quant_weights_iterator(
gguf_file: str, gguf_to_hf_name_map: dict[str, str]
) -> Generator[tuple[str, Tensor], None, None]
Iterate over the quant weights in the model gguf files and convert them to torch tensors. Be careful of the order of yielding weight types and weights data, we have to yield all weight types first before yielding any weights. Otherwise it would cause issue when loading weights with for packed layer with different quant types.
Source code in vllm/model_executor/model_loader/weight_utils.py
initialize_dummy_weights ¶
initialize_dummy_weights(
model: Module,
model_config: ModelConfig,
low: float = -0.001,
high: float = 0.001,
seed: int = 1234,
) -> None
Initialize model weights with random values.
The model weights must be randomly initialized for accurate performance measurements. Additionally, the model weights should not cause NaNs in the forward pass. We empirically found that initializing the weights with values between -1e-3 and 1e-3 works well for most models.
We use per-parameter random seed, so that dummy weights are consistent, even if the model is partitioned across multiple devices. When the seed is fixed, the random values generated by this function only depends on the parameter's number of elements and its data type.
Source code in vllm/model_executor/model_loader/weight_utils.py
instanttensor_weights_iterator ¶
instanttensor_weights_iterator(
hf_weights_files: list[str], use_tqdm_on_load: bool
) -> Generator[tuple[str, Tensor], None, None]
Iterate over the weights in the model safetensor files using instanttensor library.
Source code in vllm/model_executor/model_loader/weight_utils.py
maybe_download_from_modelscope ¶
maybe_download_from_modelscope(
model: str,
revision: str | None = None,
download_dir: str | None = None,
ignore_patterns: str | list[str] | None = None,
allow_patterns: list[str] | str | None = None,
) -> str | None
Download model from ModelScope hub if VLLM_USE_MODELSCOPE is True.
Returns the path to the downloaded model, or None if the model is not downloaded from ModelScope.
Source code in vllm/model_executor/model_loader/weight_utils.py
maybe_remap_kv_scale_name ¶
Remap the name of FP8 k/v_scale parameters.
This function handles the remapping of FP8 k/v_scale parameter names. It detects if the given name ends with a suffix and attempts to remap it to the expected name format in the model. If the remapped name is not found in the params_dict, a warning is printed and None is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name | str | The original loaded checkpoint parameter name. | required |
params_dict | dict | Dictionary containing the model's named parameters. | required |
Returns:
| Name | Type | Description |
|---|---|---|
str | str | None | The remapped parameter name if successful, or the original name if no remapping is needed. |
None | str | None | If the remapped name is not found in params_dict. |
Source code in vllm/model_executor/model_loader/weight_utils.py
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 | |
multi_thread_pt_weights_iterator ¶
multi_thread_pt_weights_iterator(
hf_weights_files: list[str],
use_tqdm_on_load: bool,
pt_load_map_location: str | dict[str, str] = "cpu",
max_workers: int = 4,
) -> Generator[tuple[str, Tensor], None, None]
Multi-Thread iterate over the weights in the model bin/pt files.
Source code in vllm/model_executor/model_loader/weight_utils.py
multi_thread_safetensors_weights_iterator ¶
multi_thread_safetensors_weights_iterator(
hf_weights_files: list[str],
use_tqdm_on_load: bool,
max_workers: int = 4,
) -> Generator[tuple[str, Tensor], None, None]
Multi-Thread iterate over the weights in the model safetensor files.
Source code in vllm/model_executor/model_loader/weight_utils.py
np_cache_weights_iterator ¶
np_cache_weights_iterator(
model_name_or_path: str,
cache_dir: str | None,
hf_folder: str,
hf_weights_files: list[str],
use_tqdm_on_load: bool,
) -> Generator[tuple[str, Tensor], None, None]
Iterate over the weights in the model np files.
Will dump the model weights to numpy files if they are not already dumped.
Source code in vllm/model_executor/model_loader/weight_utils.py
pt_weights_iterator ¶
pt_weights_iterator(
hf_weights_files: list[str],
use_tqdm_on_load: bool,
pt_load_map_location: str | dict[str, str] = "cpu",
) -> Generator[tuple[str, Tensor], None, None]
Iterate over the weights in the model bin/pt files.
Source code in vllm/model_executor/model_loader/weight_utils.py
row_parallel_weight_loader ¶
Load weights that are row-parallelized.
Source code in vllm/model_executor/model_loader/weight_utils.py
runai_safetensors_weights_iterator ¶
runai_safetensors_weights_iterator(
hf_weights_files: list[str],
use_tqdm_on_load: bool,
is_distributed: bool = False,
) -> Generator[tuple[str, Tensor], None, None]
Iterate over the weights in the model safetensor files.
Source code in vllm/model_executor/model_loader/weight_utils.py
safetensors_weights_iterator ¶
safetensors_weights_iterator(
hf_weights_files: list[str],
use_tqdm_on_load: bool,
safetensors_load_strategy: str = "lazy",
local_expert_ids: set[int] | None = None,
) -> Generator[tuple[str, Tensor], None, None]
Iterate over the weights in the model safetensor files.
When local_expert_ids is provided, expert weights not belonging to this rank are skipped before reading from disk, which drastically reduces storage I/O for MoE models under EP.
Source code in vllm/model_executor/model_loader/weight_utils.py
sharded_weight_loader ¶
sharded_weight_loader(shard_axis: int) -> LoaderFunction
Create a weight loader that shards the weights along the given axis