Skip to content

ENH Adds lazy loading to all modules - #26440

Closed
thomasjpfan wants to merge 11 commits into
scikit-learn:mainfrom
thomasjpfan:lazy_import
Closed

thomasjpfan wants to merge 11 commits into
scikit-learn:mainfrom
thomasjpfan:lazy_import

Conversation

@thomasjpfan

@thomasjpfan thomasjpfan commented May 26, 2023 •

Copy link
Copy Markdown
Member

This PR implements lazy loading as described in SPEC 1. I see two benefits of lazy loading:

  1. It allows all public methods to be imported from the top sklearn level:
import sklearn

X, y = sklearn.datasets.make_regression()
  1. The initial import time for scikit-learn is faster:

main

%%time
import sklearn
# CPU times: user 625 ms, sys: 88.9 ms, total: 714 ms
# Wall time: 315 ms

PR

%%time
import sklearn
# CPU times: user 2.39 ms, sys: 2.39 ms, total: 4.78 ms
# Wall time: 3.37 ms

The downside is that there is some complexity with changing the __init__.py to using the lazy_loader API.

Background Info

According to NEP 52 NumPy is planning to move to lazy loading and SciPy already has lazy loading.

@betatim

betatim commented May 26, 2023

Copy link
Copy Markdown
Member

I am kinda -0 on this. Mostly because I am not sure that it solves a problem people have/complain about, and the lazy loader (by construction?) uses magic to make the lazy loading work.

Import speed (point 2): what kind of applications can we think of where this is a limiting/annoying factor? From my personal POV I don't have things that bother me in this regard, but N=1 data, etc.

The flat namespace argument (point 1) is kinda nice but I also feel that the SPEC leaves out that you can write:

import sklearn.ensemble
import sklearn.model_selection
# etc

and get the "nested but flat" namespace benefit. For me the decision between preferring import sklearn.ensemble; sklearn.ensemble.RandomForestClassifier and from sklearn import ensemble as ens; ens.RandomForestClassifier (to mirror the linalg example from the SPEC) is more of a case by case one. Sometimes there is a namespace clash, sometimes having to type sklearn.ensemble... makes lines very long.

Some additional reading material (that I've not yet read): https://peps.python.org/pep-0690/ This proposed lazy loading for Python in general. I hope that it contains something to learn about why it got rejected.

@jeremiedbb

jeremiedbb commented May 26, 2023 •

Copy link
Copy Markdown
Member

Import speed (point 2): what kind of applications can we think of where this is a limiting/annoying factor? From my personal POV I don't have things that bother me in this regard, but N=1 data, etc.

I can imagine that we could expect significant benefits in a multiprocessing context, because you repeat the import in each subprocess.

@glemaitre

Copy link
Copy Markdown
Member

With import sklearn and being able to call any class/function, do we improve some kind of introspection/autocompletion in some IDEs?

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've implemented this for another library, and I'm a very happy customer.

Comment thread maint_tools/vendor_lazy_loader.sh Resolved

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No blockers for me.

Comment thread sklearn/calibration.py
Comment on lines +65 to +69
__all__ = ["CalibratedClassifierCV", "calibration_curve"]


def __dir__():
return __all__

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we should move all our implementations from root, like this one, pipeline.py, etc, to a subfolder (probably different PR)

from ..utils.extmath import randomized_svd
from ._lda import LatentDirichletAllocation

# randomized_svd is importable but is defined in utils.extmath

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should we move it?

@jeremiedbb

jeremiedbb commented Jun 14, 2023 •

Copy link
Copy Markdown
Member

Why do we need to vendor lazy_loader ? can't we make it a dependency ?

@github-actions

github-actions Bot commented Jun 23, 2023 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: af95c47. Link to the linter CI: here

@thomasjpfan

Copy link
Copy Markdown
Member Author

Why do we need to vendor lazy_loader ? can't we make it a dependency ?

Given how conservative we are with adding new dependencies, I decided to vendor the lazy_loader library. For me, given how important loading is, I think it's slightly better to vendor it.

With that in mind, I updated this PR to only vendor the single lazy_loader function we used. This greatly reduces the vendored code.

@thomasjpfan

Copy link
Copy Markdown
Member Author

@rth I just saw your EuroPython slides, do you think using this PR's lazy import mechanism will help with load times on WASM?

@adrinjalali

Copy link
Copy Markdown
Member

I think it was better as it was before you removed all extra vendored code. I rather have something which can be trivially updated, rather something which will diverge from upstream. libsvm and liblinear still haunt me for how much we've diverged and haven't upgraded them to match upstream.

@thomasjpfan

Copy link
Copy Markdown
Member Author

I think it was better as it was before you removed all extra vendored code.

Okay, I reverted back to the vendoring the whole file from upstream.

@adrinjalali

Copy link
Copy Markdown
Member

@scikit-learn/core-devs it'd be really nice to have this merged.

@lorentzenchr

Copy link
Copy Markdown
Member

I‘m +-0 on this. As a user, I might like it. I hope we find a 2nd reviewer.

@jjerphan

Copy link
Copy Markdown
Member

±0 for me as well.

@adrinjalali

Copy link
Copy Markdown
Member

Since there are no rejections here, can we can a second review to have it merged?

@betatim

betatim commented Aug 28, 2023

Copy link
Copy Markdown
Member

I am kinda -0 on this. Mostly because I am not sure that it solves a problem people have/complain about, and the lazy loader (by construction?) uses magic to make the lazy loading work.

The only benefit of this that I've seen is Jeremie's comment about faster execution in a multi-processing context. Has someone looked into this?

Are there other things where this would solve a user problem?

@adrinjalali

Copy link
Copy Markdown
Member

@betatim this to me clearly gives the advantage of faster load and less memory footprint. What are your reasons for being against it?

@GaelVaroquaux

GaelVaroquaux commented Aug 29, 2023 via email

Copy link
Copy Markdown
Member

@betatim

betatim commented Aug 29, 2023 •

Copy link
Copy Markdown
Member

My reason to be against it is that the lazy loader (by construction?) uses magic to make the lazy loading work. This is a negative for me. I also don't feel/see the pain point that this change fixes. This is also a negative for me. In the sum it leaves me thinking that the lazy loader is a technological marvel that I admire, but have no use for.

I have not spent the time to investigate how exactly the lazy loader works, how exactly the import system works and what the potential pitfalls are. Years of using Python make me pretty confident that there is a reason why the (C)Python import system works the way it does. If there was low hanging fruit it would have been picked.

Combined with the fact that we already have "too much to do" it makes me think that spending effort on debugging, understanding and fixing (by definition obscure) issues related to the lazy loader isn't a good deal.

Starting from that point of being against it I am open to the idea that there are some large benefits. However, so far I've not seen anyone show that they exist. At least I read Jeremie's comment more as idle speculation over a drink, than the result of measuring the speed up for a typical user.

@jeremiedbb

Copy link
Copy Markdown
Member

At least I read Jeremie's comment more as ideal speculation over a drink, than the result of measuring the speed up for a typical user.

That's exactly what it is: just a guess but no measurment :)

@jeremiedbb

Copy link
Copy Markdown
Member

I can see another benefit but a rather small one and only from the maintenance point of view, not for the users. It would allow to avoid some cycling import for which we currently need to add local imports as a workaround.

@adrinjalali

Copy link
Copy Markdown
Member

Combined with the fact that we already have "too much to do" it makes me think that spending effort on debugging, understanding and fixing (by definition obscure) issues related to the lazy loader isn't a good deal.

I have implemented this also for another library and it's not that magical. It's pretty neat, and compared to the status quo, this makes imports really significantly faster and lighter.

@GaelVaroquaux

GaelVaroquaux commented Aug 29, 2023 via email

Copy link
Copy Markdown
Member

@thomasjpfan

Copy link
Copy Markdown
Member Author

I agree that performance will not be a big win here. I also agree with Tim's comment: #26440 (comment) about the maintainability of lazy loading.

The biggest benefit I see is the improve usability for those who are not familiar with Python. Currently, the following breaks on main:

import sklearn

X, y = sklearn.datasets.make_regression()

while lazy loading allows the above to work.

With the comment from #26440 (comment) about things breaking in strange ways, I am overall -0.25 on this PR, so I am closing this PR.

@adrinjalali

Copy link
Copy Markdown
Member

A version of this has been used by scipy at this point, and there hasn't really been any issues so far as I know. Could we revisit this?

@thomasjpfan

Copy link
Copy Markdown
Member Author

SciPy has a simpler approach that only lazy imports from the top level:

https://github.com/scipy/scipy/blob/fd2a23c46ccd0d66b78feeb5432519af2570b17a/scipy/__init__.py#L134-L143

I'm okay with going with that approach here too.

@adrinjalali

Copy link
Copy Markdown
Member

That sounds like an improvement to what we have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

8 participants