ModelScope Integration¶
fusion_bench.utils.modelscope
¶
load_dataset(name, split='train', platform='hf')
¶
Load a dataset from Hugging Face or ModelScope.
Parameters:
-
platform
(Literal['hf', 'modelscope']
, default:'hf'
) –The platform to load the dataset from.
-
name
(str
) –The name of the dataset.
-
split
(str
, default:'train'
) –The split of the dataset to load (default is "train").
Returns:
-
Dataset
–The loaded dataset.
Source code in fusion_bench/utils/modelscope.py
resolve_file_path(repo_id, filename, repo_type='model', platform='hf', **kwargs)
¶
Resolve and download a specific file from a repository across multiple platforms.
This function downloads a specific file from repositories hosted on various platforms including local paths, Hugging Face Hub, and ModelScope. It handles platform-specific URL prefixes and automatically determines the appropriate download method.
Parameters:
-
repo_id
(str
) –Repository identifier. Can be: - Local directory path (file will be joined with this path if it exists) - Hugging Face model/dataset ID (e.g., "bert-base-uncased") - ModelScope model/dataset ID - URL-prefixed ID (e.g., "hf://model-name", "modelscope://model-name"). The prefix will override the platform argument.
-
filename
(str
) –The specific file to download from the repository.
-
repo_type
(Literal['model', 'dataset']
, default:'model'
) –Type of repository. Defaults to "model". Used for ModelScope platform to determine the correct download function.
-
platform
(Literal['hf', 'huggingface', 'modelscope']
, default:'hf'
) –Platform to download from. Defaults to "hf". Options: - "hf" or "huggingface": Hugging Face Hub - "modelscope": ModelScope platform
-
**kwargs
–Additional arguments passed to the underlying download functions (e.g., cache_dir, force_download, use_auth_token).
Returns:
-
str
(str
) –Local path to the downloaded file.
Raises:
-
ValueError
–If an unsupported repo_type is specified for ModelScope platform.
-
ImportError
–If required dependencies for the specified platform are not installed.
-
FileNotFoundError
–If the file cannot be found or downloaded.
Examples:
>>> # Download config.json from a Hugging Face model
>>> resolve_file_path("bert-base-uncased", "config.json")
"/home/user/.cache/huggingface/hub/models--bert-base-uncased/.../config.json"
>>> # Download from ModelScope
>>> resolve_file_path(
... "damo/nlp_bert_backbone_base_std",
... "pytorch_model.bin",
... platform="modelscope"
... )
"/home/user/.cache/modelscope/hub/.../pytorch_model.bin"
>>> # Local file path
>>> resolve_file_path("/path/to/local/model", "config.json")
"/path/to/local/model/config.json"
>>> # URL-prefixed repository
>>> resolve_file_path("hf://microsoft/DialoGPT-medium", "config.json")
"/home/user/.cache/huggingface/hub/.../config.json"
>>> # Download dataset file from ModelScope
>>> resolve_file_path(
... "DAMO_NLP/jd",
... "train.json",
... repo_type="dataset",
... platform="modelscope"
... )
"/home/user/.cache/modelscope/datasets/.../train.json"
Source code in fusion_bench/utils/modelscope.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
|
resolve_repo_path(repo_id, repo_type='model', platform='hf', **kwargs)
¶
Resolve and download a repository from various platforms to a local path.
This function handles multiple repository sources including local paths, Hugging Face, and ModelScope. It automatically downloads remote repositories to local cache and returns the local path for further use.
Parameters:
-
repo_id
(str
) –Repository identifier. Can be: - Local file/directory path (returned as-is if exists) - Hugging Face model/dataset ID (e.g., "bert-base-uncased") - ModelScope model/dataset ID - URL-prefixed ID (e.g., "hf://model-name", "modelscope://model-name"). The prefix will override the platform argument.
-
repo_type
(str
, default:'model'
) –Type of repository to download. Defaults to "model". Common values include "model" and "dataset".
-
platform
(Literal['hf', 'huggingface', 'modelscope']
, default:'hf'
) –Platform to download from. Defaults to "hf". Options: - "hf" or "huggingface": Hugging Face Hub - "modelscope": ModelScope platform
-
**kwargs
–Additional arguments passed to the underlying download functions.
Returns:
-
str
–Local path to the repository (either existing local path or downloaded cache path).
Raises:
-
FileNotFoundError
–If the repository cannot be found or downloaded from any platform.
-
ValueError
–If an unsupported platform is specified.
-
ImportError
–If required dependencies for the specified platform are not installed.
Examples:
>>> # Local path (returned as-is)
>>> resolve_repo_path("/path/to/local/model")
"/path/to/local/model"
>>> # Hugging Face model
>>> resolve_repo_path("bert-base-uncased")
"/home/user/.cache/huggingface/hub/models--bert-base-uncased/..."
>>> # ModelScope model with explicit platform
>>> resolve_repo_path("damo/nlp_bert_backbone_base_std", platform="modelscope")
"/home/user/.cache/modelscope/hub/damo/nlp_bert_backbone_base_std/..."
>>> # URL-prefixed repository ID
>>> resolve_repo_path("hf://microsoft/DialoGPT-medium")
"/home/user/.cache/huggingface/hub/models--microsoft--DialoGPT-medium/..."
Source code in fusion_bench/utils/modelscope.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|