"vscode:/vscode.git/clone" did not exist on "5b214b50b65c221fe71568ab724a4569d8a01835"
Unverified Commit b6e0b016 authored by Dhruv Nair's avatar Dhruv Nair Committed by GitHub
Browse files

Lazy Import for Diffusers (#4829)



* initial commit

* move modules to import struct

* add dummy objects and _LazyModule

* add lazy import to schedulers

* clean up unused imports

* lazy import on models module

* lazy import for schedulers module

* add lazy import to pipelines module

* lazy import altdiffusion

* lazy import audio diffusion

* lazy import audioldm

* lazy import consistency model

* lazy import controlnet

* lazy import dance diffusion ddim ddpm

* lazy import deepfloyd

* lazy import kandinksy

* lazy imports

* lazy import semantic diffusion

* lazy imports

* lazy import stable diffusion

* move sd output to its own module

* clean up

* lazy import t2iadapter

* lazy import unclip

* lazy import versatile and vq diffsuion

* lazy import vq diffusion

* helper to fetch objects from modules

* lazy import sdxl

* lazy import txt2vid

* lazy import stochastic karras

* fix model imports

* fix bug

* lazy import

* clean up

* clean up

* fixes for tests

* fixes for tests

* clean up

* remove import of torch_utils from utils module

* clean up

* clean up

* fix mistake import statement

* dedicated modules for exporting and loading

* remove testing utils from utils module

* fixes from  merge conflicts

* Update src/diffusers/pipelines/kandinsky2_2/__init__.py

* fix docs

* fix alt diffusion copied from

* fix check dummies

* fix more docs

* remove accelerate import from utils module

* add type checking

* make style

* fix check dummies

* remove torch import from xformers check

* clean up error message

* fixes after upstream merges

* dummy objects fix

* fix tests

* remove unused module import

---------
Co-authored-by: default avatarPatrick von Platen <patrick.v.platen@gmail.com>
parent 88735249
import torch
from diffusers import DPMSolverSDEScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import require_torchsde
from diffusers.utils.testing_utils import require_torchsde, torch_device
from .test_schedulers import SchedulerCommonTest
......
import torch
from diffusers import EulerDiscreteScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import torch_device
from .test_schedulers import SchedulerCommonTest
......
import torch
from diffusers import EulerAncestralDiscreteScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import torch_device
from .test_schedulers import SchedulerCommonTest
......
import torch
from diffusers import HeunDiscreteScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import torch_device
from .test_schedulers import SchedulerCommonTest
......
import torch
from diffusers import KDPM2AncestralDiscreteScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import torch_device
from .test_schedulers import SchedulerCommonTest
......
import torch
from diffusers import KDPM2DiscreteScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import torch_device
from .test_schedulers import SchedulerCommonTest
......
import torch
from diffusers import LMSDiscreteScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import torch_device
from .test_schedulers import SchedulerCommonTest
......
......@@ -40,8 +40,7 @@ from diffusers import (
)
from diffusers.configuration_utils import ConfigMixin, register_to_config
from diffusers.schedulers.scheduling_utils import SchedulerMixin
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import CaptureLogger
from diffusers.utils.testing_utils import CaptureLogger, torch_device
from ..others.test_utils import TOKEN, USER, is_staging_test
......
......@@ -15,7 +15,6 @@
import argparse
import glob
import importlib.util
import os
import re
......@@ -29,15 +28,6 @@ DIFFUSERS_PATH = "src/diffusers"
REPO_PATH = "."
# This is to make sure the diffusers module imported is the one in the repo.
spec = importlib.util.spec_from_file_location(
"diffusers",
os.path.join(DIFFUSERS_PATH, "__init__.py"),
submodule_search_locations=[DIFFUSERS_PATH],
)
diffusers_module = spec.loader.load_module()
def _should_continue(line, indent):
return line.startswith(indent) or len(line) <= 1 or re.search(r"^\s*\)(\s*->.*:|:)\s*$", line) is not None
......
......@@ -71,24 +71,27 @@ def read_init():
# Get to the point we do the actual imports for type checking
line_index = 0
while not lines[line_index].startswith("if TYPE_CHECKING"):
line_index += 1
backend_specific_objects = {}
# Go through the end of the file
while line_index < len(lines):
# If the line contains is_backend_available, we grab all objects associated with the `else` block
backend = find_backend(lines[line_index])
if backend is not None:
while not lines[line_index].startswith("else:"):
while not lines[line_index].startswith(" else:"):
line_index += 1
line_index += 1
objects = []
# Until we unindent, add backend objects to the list
while line_index < len(lines) and len(lines[line_index]) > 1:
while len(lines[line_index]) <= 1 or lines[line_index].startswith(" " * 8):
line = lines[line_index]
single_line_import_search = _re_single_line_import.search(line)
if single_line_import_search is not None:
objects.extend(single_line_import_search.groups()[0].split(", "))
elif line.startswith(" " * 8):
objects.append(line[8:-2])
elif line.startswith(" " * 12):
objects.append(line[12:-2])
line_index += 1
if len(objects) > 0:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment