-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Lazy Import for Diffusers #4829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I'd actually do one big PR here and we can try to merge it quickly to not run into merge conflicts :-) We should measure the speed-up of importing things once this is merged and also try to clean up many of these imports inside functions in the diffusers/src/diffusers/loaders.py Line 274 in a76f2ad
|
@patrickvonplaten The import speed should be improved. Results on my machine with all backends + accelerate installed.
Could you verify you're seeing is similar numbers after the changes? |
Ok I think I found one last package that slows down the import which is |
@patrickvonplaten changed it so that we use |
@DN6 could you take a look at the final failing tests or do you need help here? |
Think we also need to integrate the changes from the just merged Wuerstchen PR |
@patrickvonplaten Seeing some strange behaviour in the When running python -m pytest -n 2 --max-worker-restart=0 --dist=loadfile -s -v -k "not Flax and not Onnx" tests/models tests/schedulers All tests pass The failures occur when If you run this test with the model/scheduler test, you get a few failures in the scheduler tests.
Any thoughts on what's happening? I can't seem to figure it out. |
# 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() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@patrickvonplaten This section is causing the failing fast tests for models and schedulers. diffusers_module
doesn't appear to be used anywhere. So I removed the snippet.
LMK if there's something I've missed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works!
Alright, great job - let's get this one in to avoid any more merge conflicts. |
for name, value in _dummy_objects.items(): | ||
setattr(sys.modules[__name__], name, value) | ||
for name, value in _additional_imports.items(): | ||
setattr(sys.modules[__name__], name, value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need the "If TYPE_CHECKING" branch here
@DN6, Looks like |
* 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: Patrick von Platen <[email protected]>
* 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: Patrick von Platen <[email protected]>
What does this PR do?
Adds lazy import functionality to Diffusers, similar to what exists in Transformers
Benchmark:
Initially tested import speed up by running
time python -c "import diffusers"
with all backends (torch, transformers, flax, onnxruntime) installed.With Lazy Import
Without Lazy Import
This PR:
__init__.py
files of a module to their own dedicated files. e.g.StableDiffusionPipelineOutput
is now in apipeline_output.py
file within thestable_diffusion
module. This follows the same format astransformers
and also keeps__init__.py
reserved for import related code.TODO:
utils/check_dummies
behaviour and the corresponding test is broken because of the new import structure. This is the last thing that needs to be addressed before merging.Fixes # (issue)
#4260
Before submitting
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.