|
| 1 | +import logging |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | +from typing import Optional |
| 5 | + |
| 6 | +from huggingface_hub import hf_hub_download as _hf_hub_download |
| 7 | +from huggingface_hub import snapshot_download as _snapshot_download |
| 8 | +from modelscope import snapshot_download as _ms_snapshot_download |
| 9 | +from modelscope.hub.file_download import model_file_download as _ms_model_file_download |
| 10 | + |
| 11 | +from parallax.utils.weight_filter_utils import ( |
| 12 | + determine_needed_weight_files_for_download, |
| 13 | +) |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | +_USE_MODELSCOPE_ENV = "USE_MODELSCOPE" |
| 17 | + |
| 18 | +__all__ = [ |
| 19 | + "download_model_file", |
| 20 | + "download_model_snapshot", |
| 21 | + "selective_model_download", |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +def download_model_snapshot( |
| 26 | + repo_id: str, |
| 27 | + allow_patterns: Optional[list[str] | str] = None, |
| 28 | + ignore_patterns: Optional[list[str] | str] = None, |
| 29 | + local_dir: Optional[str | Path] = None, |
| 30 | + local_files_only: bool = False, |
| 31 | +) -> Path: |
| 32 | + if _use_modelscope(): |
| 33 | + return Path( |
| 34 | + _ms_snapshot_download( |
| 35 | + model_id=repo_id, |
| 36 | + allow_patterns=allow_patterns, |
| 37 | + ignore_patterns=ignore_patterns, |
| 38 | + local_dir=str(local_dir) if local_dir is not None else None, |
| 39 | + local_files_only=local_files_only, |
| 40 | + ) |
| 41 | + ) |
| 42 | + |
| 43 | + return Path( |
| 44 | + _snapshot_download( |
| 45 | + repo_id=repo_id, |
| 46 | + allow_patterns=allow_patterns, |
| 47 | + ignore_patterns=ignore_patterns, |
| 48 | + local_dir=local_dir, |
| 49 | + local_files_only=local_files_only, |
| 50 | + ) |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def download_model_file( |
| 55 | + repo_id: str, |
| 56 | + filename: str, |
| 57 | + local_files_only: bool = False, |
| 58 | +) -> Path: |
| 59 | + if _use_modelscope(): |
| 60 | + return Path( |
| 61 | + _ms_model_file_download( |
| 62 | + model_id=repo_id, |
| 63 | + file_path=filename, |
| 64 | + local_files_only=local_files_only, |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + return Path( |
| 69 | + _hf_hub_download( |
| 70 | + repo_id=repo_id, |
| 71 | + filename=filename, |
| 72 | + local_files_only=local_files_only, |
| 73 | + ) |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def selective_model_download( |
| 78 | + repo_id: str, |
| 79 | + start_layer: Optional[int] = None, |
| 80 | + end_layer: Optional[int] = None, |
| 81 | + local_files_only: bool = False, |
| 82 | +) -> Path: |
| 83 | + local_path = Path(repo_id) |
| 84 | + if local_path.exists(): |
| 85 | + logger.debug(f"Using local model path: {local_path}") |
| 86 | + return local_path |
| 87 | + |
| 88 | + logger.debug(f"Downloading model metadata for {repo_id}") |
| 89 | + model_path = download_model_snapshot( |
| 90 | + repo_id=repo_id, |
| 91 | + ignore_patterns=_EXCLUDE_WEIGHT_PATTERNS, |
| 92 | + local_files_only=local_files_only, |
| 93 | + ) |
| 94 | + logger.debug(f"Downloaded model metadata to {model_path}") |
| 95 | + |
| 96 | + if start_layer is not None and end_layer is not None: |
| 97 | + logger.debug(f"Determining required weight files for layers [{start_layer}, {end_layer})") |
| 98 | + |
| 99 | + needed_weight_files = determine_needed_weight_files_for_download( |
| 100 | + model_path=model_path, |
| 101 | + start_layer=start_layer, |
| 102 | + end_layer=end_layer, |
| 103 | + ) |
| 104 | + |
| 105 | + if not needed_weight_files: |
| 106 | + logger.debug("Could not determine specific weight files, downloading all") |
| 107 | + download_model_snapshot(repo_id=repo_id, local_files_only=local_files_only) |
| 108 | + else: |
| 109 | + # Step 3: Download only the needed weight files |
| 110 | + logger.info(f"Downloading {len(needed_weight_files)} weight files") |
| 111 | + |
| 112 | + for weight_file in needed_weight_files: |
| 113 | + # Check if file already exists in local cache before downloading |
| 114 | + weight_file_path = model_path / weight_file |
| 115 | + if weight_file_path.exists(): |
| 116 | + continue |
| 117 | + |
| 118 | + logger.debug(f"Downloading {weight_file}") |
| 119 | + try: |
| 120 | + download_model_file( |
| 121 | + repo_id=repo_id, |
| 122 | + filename=weight_file, |
| 123 | + local_files_only=local_files_only, |
| 124 | + ) |
| 125 | + except Exception as e: |
| 126 | + logger.error(f"Failed to download {weight_file} for {repo_id}: {e}") |
| 127 | + logger.error( |
| 128 | + "This node cannot reach Hugging Face Hub to download weight files. " |
| 129 | + "Please check network connectivity or pre-download the model." |
| 130 | + ) |
| 131 | + raise |
| 132 | + |
| 133 | + logger.debug(f"Downloaded weight files for layers [{start_layer}, {end_layer})") |
| 134 | + else: |
| 135 | + logger.debug("No layer range specified, downloading all model files") |
| 136 | + download_model_snapshot(repo_id=repo_id, local_files_only=local_files_only) |
| 137 | + |
| 138 | + return model_path |
| 139 | + |
| 140 | + |
| 141 | +_EXCLUDE_WEIGHT_PATTERNS = [ |
| 142 | + "*.safetensors", |
| 143 | + "*.bin", |
| 144 | + "*.pt", |
| 145 | + "*.pth", |
| 146 | + "pytorch_model*.bin", |
| 147 | + "model*.safetensors", |
| 148 | + "weight*.safetensors", |
| 149 | +] |
| 150 | + |
| 151 | + |
| 152 | +def _use_modelscope() -> bool: |
| 153 | + return _USE_MODELSCOPE_ENV in os.environ |
0 commit comments