Skip to content

Commit f9c3ee3

Browse files
Isotr0pyyangw-dev
authored andcommitted
[Misc] Improve model redirect to accept json dictionary (vllm-project#16119)
Signed-off-by: Isotr0py <[email protected]> Signed-off-by: Yang Wang <[email protected]>
1 parent 2c3ce84 commit f9c3ee3

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

vllm/envs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,11 @@ def maybe_convert_int(value: Optional[str]) -> Optional[int]:
665665
lambda: os.environ.get("VLLM_CI_USE_S3", "0") == "1",
666666

667667
# Use model_redirect to redirect the model name to a local folder.
668+
# `model_redirect` can be a json file mapping the model between
669+
# repo_id and local folder:
670+
# {"meta-llama/Llama-3.2-1B": "/tmp/Llama-3.2-1B"}
671+
# or a space separated values table file:
672+
# meta-llama/Llama-3.2-1B /tmp/Llama-3.2-1B
668673
"VLLM_MODEL_REDIRECT_PATH":
669674
lambda: os.environ.get("VLLM_MODEL_REDIRECT_PATH", None),
670675

vllm/transformers_utils/utils.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3+
import json
34
from functools import cache
45
from os import PathLike
56
from pathlib import Path
@@ -51,6 +52,26 @@ def modelscope_list_repo_files(
5152
return files
5253

5354

55+
def _maybe_json_dict(path: Union[str, PathLike]) -> dict[str, str]:
56+
with open(path) as f:
57+
try:
58+
return json.loads(f.read())
59+
except Exception:
60+
return dict[str, str]()
61+
62+
63+
def _maybe_space_split_dict(path: Union[str, PathLike]) -> dict[str, str]:
64+
parsed_dict = dict[str, str]()
65+
with open(path) as f:
66+
for line in f.readlines():
67+
try:
68+
model_name, redirect_name = line.strip().split()
69+
parsed_dict[model_name] = redirect_name
70+
except Exception:
71+
pass
72+
return parsed_dict
73+
74+
5475
@cache
5576
def maybe_model_redirect(model: str) -> str:
5677
"""
@@ -68,16 +89,10 @@ def maybe_model_redirect(model: str) -> str:
6889
if not Path(model_redirect_path).exists():
6990
return model
7091

71-
with open(model_redirect_path) as f:
72-
for line in f.readlines():
73-
try:
74-
model_name, redirect_name = line.split("\t")
75-
if model == model_name:
76-
redirect_name = redirect_name.strip()
77-
logger.info("model redirect: [ %s ] -> [ %s ]", model,
78-
redirect_name)
79-
return redirect_name
80-
except Exception:
81-
pass
92+
redirect_dict = (_maybe_json_dict(model_redirect_path)
93+
or _maybe_space_split_dict(model_redirect_path))
94+
if (redirect_model := redirect_dict.get(model)):
95+
logger.info("model redirect: [ %s ] -> [ %s ]", model, redirect_model)
96+
return redirect_model
8297

8398
return model

0 commit comments

Comments
 (0)