MCPcopy Create free account
hub / github.com/modelscope/modelscope / _patch_pretrained_class

Function _patch_pretrained_class

modelscope/utils/hf_util/patcher.py:158–477  ·  view source on GitHub ↗

Patch all class to download from modelscope Args: wrap: Wrap the class or monkey patch the original class Returns: The classes after patched

(all_imported_modules, wrap=False)

Source from the content-addressed store, hash-verified

156
157
158def _patch_pretrained_class(all_imported_modules, wrap=False):
159 """Patch all class to download from modelscope
160
161 Args:
162 wrap: Wrap the class or monkey patch the original class
163
164 Returns:
165 The classes after patched
166 """
167
168 def get_model_dir(pretrained_model_name_or_path,
169 ignore_file_pattern=None,
170 allow_file_pattern=None,
171 **kwargs):
172 from modelscope import snapshot_download
173 subfolder = kwargs.pop('subfolder', None)
174 file_filter = None
175 if subfolder:
176 file_filter = f'{subfolder}/*'
177 if not os.path.exists(pretrained_model_name_or_path):
178 revision = kwargs.pop('revision', None)
179 if revision is None or revision == 'main':
180 revision = 'master'
181 if file_filter is not None:
182 allow_file_pattern = file_filter
183 local_files_only = kwargs.pop('local_files_only', False)
184 model_dir = snapshot_download(
185 pretrained_model_name_or_path,
186 revision=revision,
187 local_files_only=local_files_only,
188 ignore_file_pattern=ignore_file_pattern,
189 allow_file_pattern=allow_file_pattern)
190 if subfolder:
191 model_dir = os.path.join(model_dir, subfolder)
192 else:
193 model_dir = pretrained_model_name_or_path
194 return model_dir
195
196 def patch_pretrained_model_name_or_path(cls, pretrained_model_name_or_path,
197 *model_args, **kwargs):
198 """Patch all from_pretrained"""
199 model_dir = get_model_dir(pretrained_model_name_or_path,
200 kwargs.pop('ignore_file_pattern', None),
201 kwargs.pop('allow_file_pattern', None),
202 **kwargs)
203 return cls._from_pretrained_origin.__func__(cls, model_dir,
204 *model_args, **kwargs)
205
206 def patch_get_config_dict(cls, pretrained_model_name_or_path, *model_args,
207 **kwargs):
208 """Patch all get_config_dict"""
209 model_dir = get_model_dir(pretrained_model_name_or_path,
210 kwargs.pop('ignore_file_pattern', None),
211 kwargs.pop('allow_file_pattern', None),
212 **kwargs)
213 return cls._get_config_dict_origin.__func__(cls, model_dir,
214 *model_args, **kwargs)
215

Callers 3

try_import_from_hfFunction · 0.90
_get_hf_pipeline_classFunction · 0.90
patch_hubFunction · 0.85

Calls 2

get_wrapped_classFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…