Skip to content

Commit 11dfa31

Browse files
Wauplinclaude
andauthored
Add vLLM library info to Hugging Face Hub requests (vllm-project#43857)
Signed-off-by: Wauplin <lucainp@gmail.com> Signed-off-by: Lucain Pouget <lucain@huggingface.co> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3f6f508 commit 11dfa31

13 files changed

Lines changed: 78 additions & 43 deletions

File tree

tests/lora/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def test_get_adapter_absolute_path_local_existing(mock_abspath, mock_exist):
175175
assert get_adapter_absolute_path(path) == absolute_path
176176

177177

178-
@patch("huggingface_hub.snapshot_download")
178+
@patch("huggingface_hub.HfApi.snapshot_download")
179179
@patch("os.path.exists")
180180
def test_get_adapter_absolute_path_huggingface(mock_exist, mock_snapshot_download):
181181
# Hugging Face model identifier
@@ -186,7 +186,7 @@ def test_get_adapter_absolute_path_huggingface(mock_exist, mock_snapshot_downloa
186186
assert get_adapter_absolute_path(path) == absolute_path
187187

188188

189-
@patch("huggingface_hub.snapshot_download")
189+
@patch("huggingface_hub.HfApi.snapshot_download")
190190
@patch("os.path.exists")
191191
def test_get_adapter_absolute_path_huggingface_error(
192192
mock_exist, mock_snapshot_download

vllm/assets/video.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
import numpy as np
99
import numpy.typing as npt
10-
from huggingface_hub import hf_hub_download
1110
from PIL import Image
1211

1312
from vllm.multimodal.media.audio import load_audio_pyav
13+
from vllm.transformers_utils.repo_utils import hf_api
1414

1515
from .base import get_cache_dir
1616

@@ -27,7 +27,7 @@ def download_video_asset(filename: str) -> str:
2727
video_path = video_directory / filename
2828
video_path_str = str(video_path)
2929
if not video_path.exists():
30-
video_path_str = hf_hub_download(
30+
video_path_str = hf_api().hf_hub_download(
3131
repo_id="raushan-testing-hf/videos-test",
3232
filename=filename,
3333
repo_type="dataset",

vllm/benchmarks/datasets/datasets.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import numpy as np
3333
import pybase64 as base64
34-
from huggingface_hub import snapshot_download
3534
from PIL import Image
3635
from typing_extensions import deprecated
3736

@@ -46,6 +45,7 @@
4645
from vllm.multimodal.audio import get_audio_duration
4746
from vllm.multimodal.image import convert_image_mode
4847
from vllm.tokenizers import TokenizerLike
48+
from vllm.transformers_utils.repo_utils import hf_api
4949
from vllm.utils.argparse_utils import FlexibleArgumentParser
5050
from vllm.utils.import_utils import PlaceholderModule
5151
from vllm.utils.mistral import is_mistral_tokenizer
@@ -3334,7 +3334,10 @@ def __init__(self, **kwargs) -> None:
33343334
self._remote_path_root = (
33353335
f"https://huggingface.co/datasets/{self.hf_name}/resolve/main"
33363336
)
3337-
self._local_path_root = snapshot_download(self.hf_name, repo_type="dataset")
3337+
self._local_path_root = hf_api().snapshot_download(
3338+
self.hf_name,
3339+
repo_type="dataset",
3340+
)
33383341

33393342
def sample(
33403343
self,

vllm/lora/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
from typing import TYPE_CHECKING
66

7-
import huggingface_hub
87
import regex as re
98
from huggingface_hub.utils import HfHubHTTPError, HFValidationError
109
from torch import nn
@@ -37,6 +36,7 @@
3736
from vllm.model_executor.layers.fused_moe import FusedMoE
3837
from vllm.model_executor.layers.linear import LinearBase
3938
from vllm.model_executor.utils import get_moe_expert_mapping, get_packed_modules_mapping
39+
from vllm.transformers_utils.repo_utils import hf_api
4040

4141
if TYPE_CHECKING:
4242
from vllm.model_executor.layers.logits_processor import LogitsProcessor
@@ -340,7 +340,9 @@ def get_adapter_absolute_path(lora_path: str) -> str:
340340
error_log = "Error downloading the ModelScope model"
341341
else:
342342
# Otherwise, we assume the path is a Hugging Face Hub repo.
343-
download_fn = lambda: huggingface_hub.snapshot_download(repo_id=lora_path)
343+
download_fn = lambda: hf_api().snapshot_download(
344+
repo_id=lora_path,
345+
)
344346
download_exceptions = (HfHubHTTPError, HFValidationError)
345347
error_log = "Error downloading the HuggingFace model"
346348

vllm/model_executor/model_loader/bitsandbytes_loader.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import numpy as np
1212
import torch
13-
from huggingface_hub import HfApi
1413
from packaging import version
1514
from torch import nn
1615
from transformers.utils import SAFE_WEIGHTS_INDEX_NAME
@@ -48,6 +47,7 @@
4847
set_weight_attrs,
4948
)
5049
from vllm.platforms import current_platform
50+
from vllm.transformers_utils.repo_utils import hf_api
5151
from vllm.utils.torch_utils import set_default_torch_dtype
5252

5353
logger = init_logger(__name__)
@@ -97,8 +97,7 @@ def _get_weight_files(
9797
if weight_files:
9898
return model_name_or_path, weight_files, pattern
9999
else:
100-
hf_api = HfApi()
101-
repo_files = hf_api.list_repo_files(repo_id=model_name_or_path)
100+
repo_files = hf_api().list_repo_files(repo_id=model_name_or_path)
102101
for pattern in allowed_patterns:
103102
matching_files = fnmatch.filter(repo_files, pattern)
104103
if matching_files:

vllm/model_executor/model_loader/gguf_loader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import regex as re
99
import torch
1010
import torch.nn as nn
11-
from huggingface_hub import hf_hub_download
1211
from transformers import AutoModelForCausalLM, AutoModelForImageTextToText
1312

1413
from vllm.config import ModelConfig, VllmConfig
@@ -27,6 +26,7 @@
2726
gguf_quant_weights_iterator_multi,
2827
)
2928
from vllm.transformers_utils.gguf_utils import detect_gguf_multimodal
29+
from vllm.transformers_utils.repo_utils import hf_api
3030
from vllm.utils.torch_utils import set_default_torch_dtype
3131

3232
if TYPE_CHECKING:
@@ -57,7 +57,7 @@ def _prepare_weights(self, model_config: ModelConfig):
5757
# repo id/filename.gguf
5858
if "/" in model_name_or_path and model_name_or_path.endswith(".gguf"):
5959
repo_id, filename = model_name_or_path.rsplit("/", 1)
60-
return hf_hub_download(
60+
return hf_api().hf_hub_download(
6161
repo_id=repo_id,
6262
filename=filename,
6363
revision=model_config.revision,

vllm/model_executor/model_loader/tensorizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import regex as re
1818
import torch
19-
from huggingface_hub import snapshot_download
2019
from torch import nn
2120
from torch.utils._python_dispatch import TorchDispatchMode
2221
from transformers import PretrainedConfig
@@ -26,6 +25,7 @@
2625
from vllm.logger import init_logger
2726
from vllm.model_executor.layers.vocab_parallel_embedding import VocabParallelEmbedding
2827
from vllm.platforms import current_platform
28+
from vllm.transformers_utils.repo_utils import hf_api
2929
from vllm.utils.argparse_utils import FlexibleArgumentParser
3030
from vllm.utils.import_utils import PlaceholderModule
3131

@@ -629,7 +629,7 @@ def serialize_extra_artifacts(
629629
)
630630

631631
with tempfile.TemporaryDirectory() as tmpdir:
632-
snapshot_download(
632+
hf_api().snapshot_download(
633633
served_model_name,
634634
local_dir=tmpdir,
635635
ignore_patterns=[

vllm/model_executor/model_loader/weight_utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import numpy as np
2424
import regex as re
2525
import torch
26-
from huggingface_hub import HfFileSystem, hf_hub_download, snapshot_download
2726
from safetensors.torch import load, load_file, safe_open, save_file
2827
from tqdm.auto import tqdm
2928
from transformers.utils import SAFE_WEIGHTS_INDEX_NAME
@@ -46,6 +45,7 @@
4645
)
4746
from vllm.platforms import current_platform
4847
from vllm.tracing import instrument
48+
from vllm.transformers_utils.repo_utils import hf_api, hf_fs
4949
from vllm.utils.import_utils import PlaceholderModule
5050

5151
try:
@@ -373,7 +373,7 @@ def get_quant_config(
373373
if not is_local:
374374
# Download the config files.
375375
with get_lock(model_config.model, load_config.download_dir):
376-
hf_folder = snapshot_download(
376+
hf_folder = hf_api().snapshot_download(
377377
model_config.model,
378378
revision=model_config.revision,
379379
allow_patterns="*.json",
@@ -431,7 +431,7 @@ def get_sparse_attention_config(
431431
if not is_local:
432432
# Download the config files.
433433
with get_lock(model_name_or_path, load_config.download_dir):
434-
hf_folder = snapshot_download(
434+
hf_folder = hf_api().snapshot_download(
435435
model_name_or_path,
436436
revision=model_config.revision,
437437
allow_patterns="*.json",
@@ -534,7 +534,7 @@ def download_weights_from_hf(
534534
# Attempt to reduce allow_patterns to a single pattern
535535
# so we only have to call snapshot_download once.
536536
try:
537-
fs = HfFileSystem()
537+
fs = hf_fs()
538538
file_list = fs.ls(
539539
os.path.join(model_name_or_path, subfolder or ""),
540540
detail=False,
@@ -546,7 +546,7 @@ def download_weights_from_hf(
546546
# unnecessary files (e.g., from subdirectories like "original/").
547547
index_file = f"{model_name_or_path}/{SAFE_WEIGHTS_INDEX_NAME}"
548548
if "*.safetensors" in allow_patterns and index_file in file_list:
549-
index_path = hf_hub_download(
549+
index_path = hf_api().hf_hub_download(
550550
repo_id=model_name_or_path,
551551
filename=SAFE_WEIGHTS_INDEX_NAME,
552552
cache_dir=cache_dir,
@@ -582,7 +582,7 @@ def download_weights_from_hf(
582582
with get_lock(model_name_or_path, cache_dir):
583583
start_time = time.perf_counter()
584584
for allow_pattern in allow_patterns:
585-
hf_folder = snapshot_download(
585+
hf_folder = hf_api().snapshot_download(
586586
model_name_or_path,
587587
allow_patterns=allow_pattern,
588588
ignore_patterns=ignore_patterns,
@@ -631,7 +631,7 @@ def download_safetensors_index_file_from_hf(
631631
with get_lock(model_name_or_path, cache_dir):
632632
try:
633633
# Download the safetensors index file.
634-
hf_hub_download(
634+
hf_api().hf_hub_download(
635635
repo_id=model_name_or_path,
636636
filename=index_file,
637637
cache_dir=cache_dir,

vllm/plugins/lora_resolvers/hf_hub_resolver.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
import asyncio
44
import os
55

6-
from huggingface_hub import HfApi, snapshot_download
7-
86
import vllm.envs as envs
97
from vllm.logger import init_logger
108
from vllm.lora.request import LoRARequest
119
from vllm.lora.resolver import LoRAResolverRegistry
1210
from vllm.plugins.lora_resolvers.filesystem_resolver import FilesystemResolver
11+
from vllm.transformers_utils.repo_utils import hf_api
1312

1413
logger = init_logger(__name__)
1514

@@ -49,7 +48,7 @@ async def resolve_lora(
4948
return None
5049

5150
repo_path = await asyncio.to_thread(
52-
snapshot_download,
51+
hf_api().snapshot_download,
5352
repo_id=maybe_repo,
5453
allow_patterns=f"{maybe_subpath}/*" if maybe_subpath != "." else "*",
5554
)
@@ -110,7 +109,10 @@ async def _get_adapter_dirs(self, repo_name: str) -> set[str]:
110109
Args:
111110
repo_name: Name of the HF hub repo to inspect.
112111
"""
113-
repo_files = await asyncio.to_thread(HfApi().list_repo_files, repo_id=repo_name)
112+
repo_files = await asyncio.to_thread(
113+
hf_api().list_repo_files,
114+
repo_id=repo_name,
115+
)
114116
adapter_dirs = {
115117
os.path.dirname(name)
116118
for name in repo_files

vllm/tokenizers/grok2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pathlib import Path
99
from typing import Any, Literal, overload
1010

11-
from huggingface_hub import hf_hub_download
1211
from huggingface_hub.utils import (
1312
EntryNotFoundError,
1413
HfHubHTTPError,
@@ -20,6 +19,7 @@
2019

2120
from vllm.entrypoints.chat_utils import ChatCompletionMessageParam
2221
from vllm.logger import init_logger
22+
from vllm.transformers_utils.repo_utils import hf_api
2323

2424
from .protocol import TokenizerLike
2525

@@ -70,7 +70,7 @@ def _maybe_load_tokenizer_config(
7070
return {}
7171

7272
try:
73-
config_file = hf_hub_download(
73+
config_file = hf_api().hf_hub_download(
7474
repo_id=repo_id,
7575
filename="tokenizer_config.json",
7676
revision=revision,
@@ -208,7 +208,7 @@ def from_pretrained(
208208
repo_id = None
209209
else:
210210
vocab_file = Path(
211-
hf_hub_download(
211+
hf_api().hf_hub_download(
212212
repo_id=str(path_or_repo_id),
213213
filename="tokenizer.tok.json",
214214
revision=revision,

0 commit comments

Comments
 (0)