-
-
Notifications
You must be signed in to change notification settings - Fork 622
Implement Lazy Loading of Submodules for faster import #3732
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
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## develop #3732 +/- ##
===========================================
- Coverage 99.59% 99.56% -0.03%
===========================================
Files 258 258
Lines 20823 20839 +16
===========================================
+ Hits 20738 20749 +11
- Misses 85 90 +5 ☔ View full report in Codecov by Sentry. |
Tried handling wildcard (i.e. |
I think the This might or might not help to construct a function whose output can provide import pkgutil
import pybamm
print([i for i in pkgutil.walk_packages(pybamm.__path__)]) returns paths to modules under pybamm/
You should be able to run this on packages under print([i for i in pkgutil.walk_packages(pybamm.expression_tree.__path__)]) returns For the expression tree
and you can now manipulate this import machinery to describe your own module paths by joining the Note: (there might be a better solution of course, I was just reporting on what I have found by now) |
I was reading up on making imports faster and I think we don't need tor essentially redefine the from .expression_tree.operations.jacobian import Jacobian or from .util import (
get_parameters_filepath,
have_jax,
install_jax,
have_optional_dependency,
is_jax_compatible,
get_git_commit_info,
) will never be slow, but something like this from .models.submodels import (
active_material,
convection,
current_collector,
electrolyte_conductivity,
electrolyte_diffusion,
electrode,
external_circuit,
interface,
oxygen_diffusion,
particle,
porosity,
thermal,
transport_efficiency,
particle_mechanics,
equivalent_circuit_elements,
) has too many things going on in one statement. I figure that it will be just the It might be worth scouting SciPy to see how they manage it (despite being a large monorepo, N.B. For profiling to be effective, we will have to delete the |
Thanks a lot @agriyakhetarpal, i'm looking accordingly to your suggestion into the segregation first and getting back in some time. |
edab7af
to
fd9a4e0
Compare
I might have solved it: for modules that further have sub-modules ( mkinit --lazy_loader --inplace --recursive pybamm # also accepts --noall so as to not add __all__ in the files and I presumably removed the import caches (all rm -rf $(find . -type d -name '__pycache__') But as I understand, this still does not remove all caches – so Outputs from importing PyBaMMWithout
With
This isn't helpful TBH, so I started deleting the entries from the import sys
keys_to_delete = [key for key in sys.modules if 'pybamm' in key]
for key in keys_to_delete:
del sys.modules[key] in addition to removing the P.S. You can consider putting out a testimonial (scientific-python/lazy-loader#50) after we manage to do this |
This does the work, thanks for suggesting @agriyakhetarpal. Without lazy-import :
With lazy-import :
However, there are a number of CI failures raised due to big change which would be fixed iteratively. |
I am also encountering an issue with |
Now as I'm trying to fix CI failures, I have first tried to resolve imports in the Other way around is to fix the imports in code i.e
But this approach is leading to big API change. So I'd be more than happy to have suggestions here & also would like to know if i'm missing any page and can also try something else instead. |
Definitely don't make that API change. Also having to keep the |
Usually I don't think we are using |
One known benefit of lazy-loading is reduced import time and it is also expected to improve performance as Many of the Calculated attributes or attributes that are loaded are using an expensive operation i.e. |
I've tried doing that but it was leading more imports to skip lazy loading in a chain but yes we can try this to reach at point where there are no or minimum failures and least import time. |
d327af2
to
fb660ec
Compare
d013f0a
to
074616e
Compare
Closing this as discussed in the last developer meeting. |
Description
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
Fixes #3490
Type of change
Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.
Key checklist:
$ pre-commit run
(or$ nox -s pre-commit
) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)$ python run-tests.py --all
(or$ nox -s tests
)$ python run-tests.py --doctest
(or$ nox -s doctests
)You can run integration tests, unit tests, and doctests together at once, using
$ python run-tests.py --quick
(or$ nox -s quick
).Further checks: