Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/diffusers/utils/dynamic_modules_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
"""Utilities to dynamically load objects from the Hub."""

import hashlib
import importlib
import inspect
import json
Expand Down Expand Up @@ -199,6 +200,10 @@ def get_class_in_module(class_name, module_path, force_reload=False):
cached_module: ModuleType | None = sys.modules.get(name)
module_spec = importlib.util.spec_from_file_location(name, location=module_file)

# Hash the module file and all its relative imports to check if we need to reload it
module_files: list[Path] = [module_file] + sorted(map(Path, get_relative_import_files(module_file)))
module_hash: str = hashlib.sha256(b"".join(bytes(f) + f.read_bytes() for f in module_files)).hexdigest()

module: ModuleType
if cached_module is None:
module = importlib.util.module_from_spec(module_spec)
Expand All @@ -207,7 +212,10 @@ def get_class_in_module(class_name, module_path, force_reload=False):
else:
module = cached_module

module_spec.loader.exec_module(module)
# reload in both cases, unless the module is already imported and the hash hits
if getattr(module, "__diffusers_module_hash__", "") != module_hash:
module_spec.loader.exec_module(module)
module.__diffusers_module_hash__ = module_hash

if class_name is None:
return find_pipeline_class(module)
Expand Down
Loading