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

Function _unpatch_pretrained_class

modelscope/utils/hf_util/patcher.py:480–517  ·  view source on GitHub ↗
(all_imported_modules)

Source from the content-addressed store, hash-verified

478
479
480def _unpatch_pretrained_class(all_imported_modules):
481 # The patcher captured `var.from_pretrained` via descriptor access, which
482 # returns a bound method. Re-wrap as classmethod so subclasses still get
483 # `cls` bound to themselves (not to the ancestor that was patched).
484 def _restore(var, attr, origin_attr):
485 origin = getattr(var, origin_attr)
486 if isinstance(origin, MethodType):
487 setattr(var, attr, classmethod(origin.__func__))
488 else:
489 setattr(var, attr, origin)
490 try:
491 delattr(var, origin_attr)
492 except Exception: # noqa
493 pass
494
495 for var in all_imported_modules:
496 if var is None:
497 continue
498
499 try:
500 has_from_pretrained = hasattr(var, 'from_pretrained')
501 has_get_peft_type = hasattr(var, '_get_peft_type')
502 has_get_config_dict = hasattr(var, 'get_config_dict')
503 except: # noqa
504 continue
505 if has_from_pretrained and hasattr(var, '_from_pretrained_origin'):
506 _restore(var, 'from_pretrained', '_from_pretrained_origin')
507 if has_get_peft_type and hasattr(var, '_get_peft_type_origin'):
508 _restore(var, '_get_peft_type', '_get_peft_type_origin')
509 if has_get_config_dict and hasattr(var, '_get_config_dict_origin'):
510 _restore(var, 'get_config_dict', '_get_config_dict_origin')
511
512 from transformers import dynamic_module_utils
513 if hasattr(dynamic_module_utils, 'origin_get_class_from_dynamic_module'):
514 dynamic_module_utils.get_class_from_dynamic_module = dynamic_module_utils.origin_get_class_from_dynamic_module
515 from transformers.models.auto import configuration_auto
516 configuration_auto.get_class_from_dynamic_module = dynamic_module_utils.origin_get_class_from_dynamic_module
517 delattr(dynamic_module_utils, 'origin_get_class_from_dynamic_module')
518
519
520def _patch_kernels():

Callers 1

unpatch_hubFunction · 0.85

Calls 1

_restoreFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…