Repository navigation
ENH Adds lazy loading to all modules - #26440
thomasjpfan wants to merge 11 commits into
Conversation
|
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
# etcand get the "nested but flat" namespace benefit. For me the decision between preferring 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. |
I can imagine that we could expect significant benefits in a multiprocessing context, because you repeat the import in each subprocess. |
|
With |
adrinjalali
left a comment
There was a problem hiding this comment.
I've implemented this for another library, and I'm a very happy customer.
| __all__ = ["CalibratedClassifierCV", "calibration_curve"] | ||
|
|
||
|
|
||
| def __dir__(): | ||
| return __all__ |
There was a problem hiding this comment.
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 |
|
Why do we need to vendor |
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. |
|
@rth I just saw your EuroPython slides, do you think using this PR's lazy import mechanism will help with load times on WASM? |
|
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. |
This reverts commit 5616b40.
Okay, I reverted back to the vendoring the whole file from upstream. |
|
@scikit-learn/core-devs it'd be really nice to have this merged. |
|
I‘m +-0 on this. As a user, I might like it. I hope we find a 2nd reviewer. |
|
±0 for me as well. |
|
Since there are no rejections here, can we can a second review to have it merged? |
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? |
|
@betatim this to me clearly gives the advantage of faster load and less memory footprint. What are your reasons for being against it? |
|
In my experience, all the implementations of lazy loading always break some strange use of import.
For instance, on scipy (which I believe uses the specific lazy loading that we are interested in), someone reported breakage on Jep which enables Java processes to run CPython inside the process:
***@***.***/thread/S3FNTVTDCU5YTIIJTKX726ZXQ6YEKKLX/
My view is that the gain (faster loading) is little compared to the cost (strange breakage). If the whole stack adopts this specific lazy loading, I suggest that we go with it. However, I would wait and see to hear more feedack as the corresponding code hits more users (keep in mind that only a fraction of people report problems).
|
|
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. |
That's exactly what it is: just a guess but no measurment :) |
|
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. |
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. |
|
I very much agree with the above message of Tim.
In the long run, if all the libraries of the ecosystem adopt this, we might as well.
But in the short run, my question is: what's the big gain for the user?
For the typical data scientist, import time is not an issue.
To put a model in production (serving answers), import time might be an issue. So, to get better at production, it is probably a desirable thing. However, I suspect that we have most pressing needs to address in our ecosystem.
One thing that might make a difference is for parallel computing under windows. Maybe this lazy loading enables reduced overhead with multiprocessing and thus speedups under windows. If that would be the case, it would be a strong argument to merge it in. But I would like to see it measured first. Loky maintains a reusable pool of workers, and thus probably absorbs the import time.
|
|
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. |
|
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? |
|
SciPy has a simpler approach that only lazy imports from the top level: I'm okay with going with that approach here too. |
|
That sounds like an improvement to what we have. |
This PR implements lazy loading as described in SPEC 1. I see two benefits of lazy loading:
sklearnlevel:main
PR
The downside is that there is some complexity with changing the
__init__.pyto 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.