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

Function get_wrapped_class

modelscope/utils/hf_util/patcher.py:233–379  ·  view source on GitHub ↗

Get a custom wrapper class for auto classes to download the models from the ModelScope hub Args: module_class (`PreTrainedModel`): The actual module class ignore_file_pattern (`str` or `List`, *optional*, default to `None`): Any file pattern to be ign

(
            module_class: 'PreTrainedModel',
            ignore_file_pattern: Optional[Union[str, List[str]]] = None,
            allow_file_pattern: Optional[Union[str, List[str]]] = None,
            **kwargs)

Source from the content-addressed store, hash-verified

231 return cls._get_peft_type_origin.__func__(cls, model_dir, **kwargs)
232
233 def get_wrapped_class(
234 module_class: 'PreTrainedModel',
235 ignore_file_pattern: Optional[Union[str, List[str]]] = None,
236 allow_file_pattern: Optional[Union[str, List[str]]] = None,
237 **kwargs):
238 """Get a custom wrapper class for auto classes to download the models from the ModelScope hub
239 Args:
240 module_class (`PreTrainedModel`): The actual module class
241 ignore_file_pattern (`str` or `List`, *optional*, default to `None`):
242 Any file pattern to be ignored, like exact file names or file extensions.
243 allow_file_pattern (`str` or `List`, *optional*, default to `None`):
244 Any file pattern to be included, like exact file names or file extensions.
245 Returns:
246 The wrapped class
247 """
248
249 @contextlib.contextmanager
250 def file_pattern_context(kwargs, module_class, cls):
251 if 'allow_file_pattern' not in kwargs:
252 kwargs['allow_file_pattern'] = allow_file_pattern
253 if 'ignore_file_pattern' not in kwargs:
254 kwargs['ignore_file_pattern'] = ignore_file_pattern
255
256 if kwargs.get(
257 'allow_file_pattern') is None and module_class is not None:
258 extra_allow_file_pattern = _decide_allow_file_pattern(
259 module_class.__name__, cls)
260 kwargs['allow_file_pattern'] = extra_allow_file_pattern
261 yield
262 kwargs.pop('ignore_file_pattern', None)
263 kwargs.pop('allow_file_pattern', None)
264
265 def from_pretrained(model, model_id, *model_args, **kwargs):
266
267 with file_pattern_context(kwargs):
268 # model is an instance
269 model_dir = get_model_dir(
270 model_id,
271 module_class=module_class,
272 cls=module_class,
273 **kwargs)
274
275 module_obj = module_class.from_pretrained(model, model_dir,
276 *model_args, **kwargs)
277
278 return module_obj
279
280 class ClassWrapper(module_class):
281
282 @classmethod
283 def from_pretrained(cls, pretrained_model_name_or_path,
284 *model_args, **kwargs):
285 with file_pattern_context(kwargs, module_class, cls):
286 model_dir = get_model_dir(pretrained_model_name_or_path,
287 **kwargs)
288
289 module_obj = module_class.from_pretrained(
290 model_dir, *model_args, **kwargs)

Callers 1

_patch_pretrained_classFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…