(all_imported_modules)
| 478 | |
| 479 | |
| 480 | def _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 | |
| 520 | def _patch_kernels(): |
no test coverage detected
searching dependent graphs…