-
Notifications
You must be signed in to change notification settings - Fork 27.3k
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
[Core] Add lazy import structure to imports #26090
Conversation
Also cc @DN6 - this should then also improve importing from
|
The documentation is not available anymore as the PR was closed or merged. |
Failing tests seem unrelated - ready for a review. |
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.
Thank you for updating this! 🙏
* improve import time * Update src/transformers/integrations/__init__.py * sort import
* improve import time * Update src/transformers/integrations/__init__.py * sort import
* improve import time * Update src/transformers/integrations/__init__.py * sort import
What does this PR do?
Importing model classes from
transformers
has become quite slow. This is mainly because we link more and more directories and files tomodeling_utils.py
andgeneration_utils.py
both files which are imported almost by every model (e.g. as soon asPreTrainedModel
is imported).At the moment we're always importing all third party libraries:
which makes import very slow. This PR adds the lazy import structure to the integrations init file so that we only import all third-party libraries when actually needed.
For environments where e.g. tensorflow is installed we now see a significant speed-up (if we make sure to remove
accelerate
before asaccelerate
is really slow to import.To reproduce:
Make sure
accelerate
is not installed, buttensorflow
is installed.Run
time python -c "from transformers import CLIPTextModel"
# or any other model importBefore PR:
After PR:
=> We're getting a 2x speed-up.
We should also make sure that importing accelerate is sped up as it become a utility library for more and more libraries (@muellerzr @SunMarc)