Repository navigation
Conversation
|
@cakedev0 Relevant to your interests? |
|
Happy to follow! Thanks for the cc 👍 Not sure I can have an opinion on introducing a new parallelism API without more context about joblib & free-threaded. But I can talk with Loic & Olivier today to try getting a better view of the context. |
My first reaction before considering an alternative to joblib is: is it possible to reduce joblib's overhead ? |
|
@jeremiedbb, in #34747 (comment) Itamar wrote (a while ago):
Unsure if it still stands |
but then the overhead stays for all other joblib users. If it's actually possible to reduce joblib's overhead I think it would be a lot more valuable to do that. I understand the simplicity tradeoff though. And we could actually include this in the mean time to have sklearn benefit from the speed up right away rather than waiting to bump joblib min version. But I think that both should be started jointly. |
Overhead numbersSome approximate numbers for context. If you run 1000 noop functions on 8 cores, you're measuring pure overhead. Here's the runtime for different thread pools, run with Python 3.14 free-threaded:
If you do the " Optimizing joblibI did indeed look into making joblib faster. My first idea was a fast path bypass, where it just switched to a faster mechanism in restricted cases. It proved harder than I expected. I'm sure it's technically possible if I put more time into it, but joblib is complicated. It has batch dispatching of tasks, and different return_as types, and timeouts, and pluggable backends, and an API approach that I honestly think was a mistake. Most of this wasn't a problem until now. With process-based workers, you don't want to use lots of tasks anyway, so high overhead gets amortized over a small number of long-running chunks of work and so is less of an issue. So my takeways were:
Of course, since |
|
Thanks for the links! So My benchmarks suggest that The motivation for using an abstraction layer here instead of
|
|
Another example where this can make a real difference is #4769. Some benchmarks I shared there:
|
|
I think it is sound and safe to use the standard library of Python. And this PR shows nice improvements for some use cases. |
I don’t think relying on the Python Standard Library is advanturous. If any, it is sound and safe. |
Co-authored-by: Arthur Lacote <[email protected]>
|
@scikit-learn/core-devs Are there any concerns left? If not, I intend to merge in about a week. |
|
I would like to first:
I will do both next week when I return from travels. |
|
@lorentzenchr let's wait for more people to have a look before merging. I'd prefer to have a good consensus and confidence on this one before merging. So one it's merged we can quickly start using it in many places. |
|
I'd like us to try and move towards making things simpler and more consistent for users. For example, right now you have If you add free threading to the mix it seems like things will not get simpler for users. I don't know if adding "yet another way" to do things in parallel is a step towards improving the situation for users or not. In particular because at least some of the motivation for this PR reads as (simplified): I disagree with how complex joblib is and if we do less we can make things go faster. I don't think we want to grow more ways of "make your code run faster and use more of your hardware". Today it is already hard for users to understand and do the right thing. There is also maintenance cost to having several ways of doing things. I've not spent enough time on this topic to figure out: what are the reasons for the current amount of user facing complexity? What can we do about reducing it? What of that work is work in scikit-learn? Basically, some kind of holistic view of making life better for users, letting them use more of their hardware and get speed ups because of it. Right now it feels like we started from "how can we make scikit-learn faster?" which in my opinion is not the first question to ask/answer. The approach in this PR might be the way to go, but let's discuss it and illustrate why it is (from the point of view of making life better for (average) users). What are the trade offs and compromises? |
|
As of the next release of joblib, oversaturation problems are (mostly) going to be solved, hopefully. Probably will be some follow up work, but the core problem will be solved. First, Second, (This is the extra work I mentioned that needs to be done in this PR to match joblib once new joblib is out; essentially an extra 2-3 lines that will integrate the infrastructure I've built in joblib for this.) Insofar as oversaturation was a motivation to not use threads, or oversaturation was a motivation not to enable parallelism by default, those problems will be (mostly) solved. Relevant PRs:
And there was also some work on the threadpoolctl side. |
|
I am assuming there will still be some performance issues left in Assuming there are still performance issues with
Of course, if it turns out after trying some time that 2. and 3. are too complicated and not worth the effort, there is a world where we can revisit this. Footnotes
|
I think it's already a bit the conclusion @itamarst reached, see: joblib/joblib#1830 (comment) (and preceding comments) |
Yes. The work on oversaturation in joblib is orthogonal to this PR, which is about the overhead from joblib.Parallel and all its abstractions and features. |
|
Given the skepticism I'm hearing, I'm going to put this on hold and see how far I can get speeding up |
|
Let‘s summarize:
1. OversubscriptionThis one is mostly solved with a new joblib release (in the future). In scikit-learn, we rely on joblib for detecting "number of threads to use". 2. Overhead to deal with threadsThis is what this PR is about. It matters for users when many small jobs are send to threads. For instance, HGBT (just for illustration, not applicable for this PR) calls roughly Way forwardI interpret the questions not as skepticism but as interest in this topic. It is correct that an overview with coclusions and a plan of work was missing (what issues are for). I hope it is clearer now (for me it is clearer, not 100% but better than before). |
|
Thanks for the summary @lorentzenchr ! |
|
I think it's useful to have joblib be faster, so certainly can do both that and this PR at the same time. (The joblib PR will I think demonstrate why I want to to bypass joblib when possible, huge number of features and backwards compatibility edge cases most of which are irrelevant to scikit-learn.) But also: since this PR is a different API, in cases where you want configurability of backend (i.e. supporting both threading and Loky) you'd have to have two different code paths. Which is not great. So doing both is useful in that:
|
|
Thanks @lorentzenchr and @itamarst for your summaries, it's very helpful! I think the idea of having both joblib and this API makes a lot of sense! IIUC, jolib has three main features:
Those features are geared towards outer-parallelism and they are great for that, but they cost overhead. For now, if you want to do some simple inner-parallelism in scikit-learn, you are forced to either:
Having the option for some simple & low-overhead Python threads inner-parallelism would be very useful. I think there are plenty of places where we don't do inner parallelism because we lack such a tool. Conclusion: I'm strongly in favor of introducing a new API that is not joblib. But it's a big thing so we should take our time and try to get it right. Some requirements/notes for getting this new API right:
Some candidates where I'd like to use this new API:
|
|
Thanks for the summaries. For the short term, I think I would rather put the effort on improving the joblib threading backend as much as we can before deciding to introduce an alternative Python threading API. |
As you are not the first one with such an opinion, may I ask for the reasons? (addressed to everybody who shares this opinion) |
I would rather keep the parallelization story in scikit-learn not too complex. Currently we already have the following mechanisms:
If we had an extra option (mostly as a potential alternative to (b) and sometimes (a)), this will make it a bit harder to make decisions when reviewing PRs as of which backend is the best for a particular piece of code and it will make it even more complex to explain to users how scikit-learn parallelism works and how public API options and environment variables impact the expected scalability behavior of their workloads, especially when parallel operations are nested together. Also, if we can reduce the overhead of the threading backend of joblib, this could benefit other projects rather than just scikit-learn. |
I would propose to replace this by a threads-only API simpler and faster than
|
|
Note that some overhead of the joblib threading backend is being optimizes away by @itamarst the joblib/joblib#1899 |
joblib.Parallelhas significant performance overhead.sklearn.utils.parallel.Parallelhas even more. That means parallelism becomes pointless in some cases, because the overhead of using these APIs drowns out the benefits of parallelism.This PR introduces a new parallelism API with lower overhead, and switches the IsolationForest prediction algorithm to use it instead of
Parallel()as a demonstration of the benefits.Background
#28622 introduced parallelism for IsolationForest prediction. However, it did not enable parallelism by default, because for smaller samples the overhead made the parallel version slower.
Speed-up of
IsolationForestenabled by this PRWith this PR, the speed of predicting large number of samples is essentially the same (so I omitted this from the plots below because it obscures the improvements on small numbers of samples). However, because of lower overhead:
Here's a plot of before and after using this API, generated with
bench_plot_isolation_forest_predict.py(with the 50,000 sample case disabled; for that value there's no benefit in the new API):Question for reviewers
Since parallelism is now useful in almost all cases, and less harmful when it's not, it is worth turning it on by default?
AI: None
Future work
ThreadPoolExecutorfor a faster thread pool (e.g. https://github.com/Quansight/spinningjenny/) can reduce parallelism overhead even more. See numbers in PERF Faster text processing with parallelism #34747 for an example.