Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Support LinFusion. It accelerates diffusion models by replacing all the self-attention layers in a diffusion UNet with distilled Generalized Linear Attention layers. The distilled model is linear-complexity and highly compatible with existing diffusion plugins like ControlNet, IP-Adapter, LoRA, etc. The acceleration can be dramatic at high resolution. Strategical pipelines for high-resolution generation can be found in the original codebase.
You can use it with only 1 additional line:
import torch from diffusers import StableDiffusionPipeline repo_id = "stabilityai/stable-diffusion-2-1" pipe = StableDiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.bfloat16, variant="fp16").to("cuda") + pipe.load_linfusion(pipeline_name_or_path=repo_id) image = pipe("a photo of an astronaut on a moon").images[0]
Currently,
stable-diffusion-v1-5/stable-diffusion-v1-5
,stabilityai/stable-diffusion-2-1
,stabilityai/stable-diffusion-xl-base-1.0
, models finetuned from them, and pipelines based on them are supported. If therepo_id
is different from them, e.g., when using a fine-tuned model from the community, you need to specifypipeline_name_or_path
explicitly to the model it is based on. Otherwise, this argument is optional and LinFusion will read it from the current pipeline. Alternatively, you can also specify the argumentpretrained_model_name_or_path_or_dict
to load LinFusion from other sources. You can also unload it withpipe.unload_linfusion()
when unnecessary.Accordingly, we also update the doc under
docs/source/en/optimization/linfusion.md
for a specific example.Thanks for your efforts in reviewing this pull request in advance! We are open to any changes to make sure LinFusion can best fit the current diffusers library!
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.