Skip to content

cache packages_distributions #11453

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
merged 5 commits into from
May 1, 2025
Merged

Conversation

vladmandic
Copy link
Contributor

@vladmandic vladmandic commented Apr 29, 2025

pr #11161 refactored _is_package_available,
but it introduced a major performance regression.
diffusers load now takes up to ~5sec while it was taking ~0.5sec earlier.

import time
t0 = time.time()

import torch # preload torch so its not part of diffusers import time
t1 = time.time()
print(f'torch: {t1-t0:.3f}')

import diffusers.utils.import_utils
t2 = time.time()
print(f'utils: {t2-t1:.3f}')

this is due to very expensive call to importlib_metadata.packages_distributions() which is done for every single installed package (each call can be ~0.2sec and it quickly adds up).

this pr simply changes the logic so the call is done once and cached.

result is that diffusers startup is back to 0.5sec as usual.

cc @yiyixuxu @sayakpaul @a-r-r-o-w

Copy link
Member

@a-r-r-o-w a-r-r-o-w left a comment

Choose a reason for hiding this comment

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

Thanks, makes sense!

@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.

Copy link
Collaborator

@yiyixuxu yiyixuxu left a comment

Choose a reason for hiding this comment

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

thanks!

@yiyixuxu yiyixuxu requested a review from DN6 May 1, 2025 06:43
@yiyixuxu
Copy link
Collaborator

yiyixuxu commented May 1, 2025

@DN6 could you take a look here too?

Copy link
Collaborator

@DN6 DN6 left a comment

Choose a reason for hiding this comment

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

Small nit. Could we make package_map and internal variable _package_map. Good to merge after.

@vladmandic
Copy link
Contributor Author

@DN6 can you make the edits as you like? I'm really not sure on exactly what do you want as that introduces second variable that is not accessed anywhere else.

@DN6 DN6 merged commit d0c0239 into huggingface:main May 1, 2025
12 checks passed
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.

5 participants