[diffusion] feat: add ModelScope support (#17924)
This commit is contained in:
@@ -26,12 +26,11 @@ import shutil
|
||||
import time
|
||||
from functools import reduce
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional, cast
|
||||
from typing import Any, Optional, Union, cast
|
||||
|
||||
from diffusers.loaders.lora_base import (
|
||||
_best_guess_weight_name, # watch out for potetential removal from diffusers
|
||||
)
|
||||
from huggingface_hub import snapshot_download
|
||||
from huggingface_hub.errors import (
|
||||
LocalEntryNotFoundError,
|
||||
RepositoryNotFoundError,
|
||||
@@ -45,6 +44,7 @@ from transformers.models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_N
|
||||
from sglang.multimodal_gen.runtime.loader.weight_utils import get_lock
|
||||
from sglang.multimodal_gen.runtime.platforms import current_platform
|
||||
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
||||
from sglang.srt.environ import envs
|
||||
from sglang.utils import is_in_ci
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -467,7 +467,6 @@ def maybe_download_model_index(model_name_or_path: str) -> dict[str, Any]:
|
||||
"""
|
||||
import tempfile
|
||||
|
||||
from huggingface_hub import hf_hub_download
|
||||
from huggingface_hub.errors import EntryNotFoundError
|
||||
|
||||
# If it's a local path, verify it directly
|
||||
@@ -638,9 +637,7 @@ def maybe_download_model(
|
||||
ignore_patterns=["*.onnx", "*.msgpack"],
|
||||
local_dir=local_dir,
|
||||
local_files_only=True,
|
||||
resume_download=True,
|
||||
max_workers=8,
|
||||
etag_timeout=60,
|
||||
)
|
||||
if is_lora or _verify_model_complete(local_path):
|
||||
# CI validation: check all subdirectories for missing shards
|
||||
@@ -709,9 +706,7 @@ def maybe_download_model(
|
||||
ignore_patterns=["*.onnx", "*.msgpack"],
|
||||
allow_patterns=allow_patterns,
|
||||
local_dir=local_dir,
|
||||
resume_download=True,
|
||||
max_workers=8,
|
||||
etag_timeout=120,
|
||||
)
|
||||
|
||||
# Verify downloaded model is complete (skip for LoRA)
|
||||
@@ -725,9 +720,7 @@ def maybe_download_model(
|
||||
repo_id=model_name_or_path,
|
||||
ignore_patterns=["*.onnx", "*.msgpack"],
|
||||
local_dir=local_dir,
|
||||
resume_download=True,
|
||||
max_workers=8,
|
||||
etag_timeout=60,
|
||||
force_download=True,
|
||||
)
|
||||
if not _verify_model_complete(local_path):
|
||||
@@ -775,3 +768,71 @@ def maybe_download_model(
|
||||
raise ValueError(
|
||||
f"Could not find model at {model_name_or_path} and failed to download from HF Hub: {e}"
|
||||
) from e
|
||||
|
||||
|
||||
# Unified download functions with Hugging Face-compatible names
|
||||
def hf_hub_download(
|
||||
repo_id: str,
|
||||
filename: str,
|
||||
local_dir: Optional[Union[str, Path]] = None,
|
||||
**kwargs,
|
||||
) -> str:
|
||||
"""Unified hf_hub_download that supports both Hugging Face Hub and ModelScope."""
|
||||
if envs.SGLANG_USE_MODELSCOPE.get():
|
||||
from modelscope import model_file_download
|
||||
|
||||
return model_file_download(
|
||||
model_id=repo_id,
|
||||
file_path=filename,
|
||||
cache_dir=local_dir,
|
||||
**kwargs,
|
||||
)
|
||||
else:
|
||||
from huggingface_hub import hf_hub_download as _hf_hub_download
|
||||
|
||||
return _hf_hub_download(
|
||||
repo_id=repo_id,
|
||||
filename=filename,
|
||||
local_dir=local_dir,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
def snapshot_download(
|
||||
repo_id: str,
|
||||
local_dir: Optional[Union[str, Path]] = None,
|
||||
ignore_patterns: Optional[Union[list[str], str]] = None,
|
||||
allow_patterns: Optional[Union[list[str], str]] = None,
|
||||
local_files_only: bool = False,
|
||||
max_workers: int = 8,
|
||||
**kwargs,
|
||||
) -> str:
|
||||
"""Unified snapshot_download that supports both Hugging Face Hub and ModelScope."""
|
||||
if envs.SGLANG_USE_MODELSCOPE.get():
|
||||
from modelscope import snapshot_download as _ms_snapshot_download
|
||||
|
||||
ms_kwargs = {
|
||||
"model_id": repo_id,
|
||||
"local_dir": local_dir,
|
||||
"ignore_patterns": ignore_patterns,
|
||||
"allow_patterns": allow_patterns,
|
||||
"local_files_only": local_files_only,
|
||||
"max_workers": max_workers,
|
||||
}
|
||||
ms_kwargs.update(kwargs)
|
||||
return _ms_snapshot_download(**ms_kwargs)
|
||||
else:
|
||||
from huggingface_hub import snapshot_download as _hf_snapshot_download
|
||||
|
||||
hf_kwargs = {
|
||||
"repo_id": repo_id,
|
||||
"local_dir": local_dir,
|
||||
"ignore_patterns": ignore_patterns,
|
||||
"allow_patterns": allow_patterns,
|
||||
"local_files_only": local_files_only,
|
||||
"max_workers": max_workers,
|
||||
"resume_download": True,
|
||||
"etag_timeout": 60,
|
||||
}
|
||||
hf_kwargs.update(kwargs)
|
||||
return _hf_snapshot_download(**hf_kwargs)
|
||||
|
||||
Reference in New Issue
Block a user