Skip to content

ENH: Improve speed of function expanding LoRA scales #11834

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

Merged

Conversation

BenjaminBossan
Copy link
Member

@BenjaminBossan BenjaminBossan commented Jun 30, 2025

What does this PR do?

Resolves #11816

The following call proved to be a bottleneck when setting a lot of LoRA adapters in diffusers:

weights = scale_expansion_fn(self, weights)

This is because we would repeatedly call unet.state_dict(), even though in the standard case, it is not necessary:

This PR fixes this by deferring this call, so that it is only run when it's necessary, not earlier.

Note: This PR doesn't change the fact that set_adapters becomes slower the more adapters are already loaded, but since it speeds up the whole process by a factor of approximately 10x, set_adapters is much less of a bottleneck.

Before submitting

Resolves huggingface#11816

The following call proved to be a bottleneck when setting a lot of LoRA
adapters in diffusers:

https://github.com/huggingface/diffusers/blob/cdaf84a708eadf17d731657f4be3fa39d09a12c0/src/diffusers/loaders/peft.py#L482

This is because we would repeatedly call unet.state_dict(), even though
in the standard case, it is not necessary:

https://github.com/huggingface/diffusers/blob/cdaf84a708eadf17d731657f4be3fa39d09a12c0/src/diffusers/loaders/unet_loader_utils.py#L55

This PR fixes this by deferring this call, so that it is only run when
it's necessary, not earlier.
@BenjaminBossan BenjaminBossan changed the title Enh improve speed expand lora scales ENH: Improve speed of function expanding LoRA scales Jun 30, 2025
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@BenjaminBossan BenjaminBossan requested a review from sayakpaul June 30, 2025 14:33
Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM!

Concerned tests:

@@ -52,7 +54,7 @@ def _maybe_expand_lora_scales(
weight_for_adapter,
blocks_with_transformer,
transformer_per_block,
unet.state_dict(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a private function, this should be more than okay to break

@sayakpaul sayakpaul merged commit 3b079ec into huggingface:main Jun 30, 2025
11 checks passed
@sayakpaul
Copy link
Member

Thanks for this contribution!

tolgacangoz pushed a commit to tolgacangoz/diffusers that referenced this pull request Jul 5, 2025
* ENH Improve speed of expanding LoRA scales

Resolves huggingface#11816

The following call proved to be a bottleneck when setting a lot of LoRA
adapters in diffusers:

https://github.com/huggingface/diffusers/blob/cdaf84a708eadf17d731657f4be3fa39d09a12c0/src/diffusers/loaders/peft.py#L482

This is because we would repeatedly call unet.state_dict(), even though
in the standard case, it is not necessary:

https://github.com/huggingface/diffusers/blob/cdaf84a708eadf17d731657f4be3fa39d09a12c0/src/diffusers/loaders/unet_loader_utils.py#L55

This PR fixes this by deferring this call, so that it is only run when
it's necessary, not earlier.

* Small fix

---------

Co-authored-by: Sayak Paul <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

set_adapters performance degrades with the number of inactive adapters
3 participants