fix: Address performance by lazy loading imports inside init files #8706
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.
Related Issues
Proposed Changes:
Solves slow package load times by lazy loading components using importlib. This prevents loading components defined in init file unless they are actually used and thus optimizes the performance avoiding bringing in ML libraries like
torch
ortransformers
for lightweight pipelinesHow did you test it?
Ran existing tests which import these components already
Notes for the reviewer
Couple of points to consider on the approach chosen
importlib
instead offrom
syntax because this has greater flexibility to define imports in a dictionary and use a reusable function.from
syntax would require defining a separate function for each submodulefrom
imports are used in thetype_checking
condition to provide support for IDE type completion. This solves the problem of importlib not helping IDEs with typesexpit
inutils.py
file is specifically not lazy loaded because the file name and function name being the same is causing python namespace issues when usingimportlib
There is another MR on a related problem. So you guys should probably consolidate them together with whatever changes you see fit. I may not be able to contribute to this PR further, so please take it from there and prioritize as it is severely affecting performance without it
#8655
Checklist
fix:
,feat:
,build:
,chore:
,ci:
,docs:
,style:
,refactor:
,perf:
,test:
and added!
in case the PR includes breaking changes.