Skip to content

Commit

Permalink
Do not use deprecated SourceFileLoader.load_module() in dynamic mod…
Browse files Browse the repository at this point in the history
…ule loading (#30370)
  • Loading branch information
XuehaiPan committed Apr 25, 2024
1 parent e60491a commit bc274a2
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/transformers/dynamic_module_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Utilities to dynamically load objects from the Hub."""
import filecmp
import importlib
import importlib.util
import os
import re
import shutil
Expand Down Expand Up @@ -196,9 +197,15 @@ def get_class_in_module(class_name: str, module_path: Union[str, os.PathLike]) -
Returns:
`typing.Type`: The class looked for.
"""
name = os.path.normpath(module_path).replace(".py", "").replace(os.path.sep, ".")
module_path = str(Path(HF_MODULES_CACHE) / module_path)
module = importlib.machinery.SourceFileLoader(name, module_path).load_module()
name = os.path.normpath(module_path).rstrip(".py").replace(os.path.sep, ".")
module_spec = importlib.util.spec_from_file_location(name, location=Path(HF_MODULES_CACHE) / module_path)
module = sys.modules.get(name)
if module is None:
module = importlib.util.module_from_spec(module_spec)
# insert it into sys.modules before any loading begins
sys.modules[name] = module
# reload in both cases
module_spec.loader.exec_module(module)
return getattr(module, class_name)


Expand Down

0 comments on commit bc274a2

Please sign in to comment.