You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With the latest changes, KMeans is significantly slower on small datasets. The time needed to compute clusters is around ten times longer.
Steps/Code to Reproduce
Times with the following code are:
scikit-lern 0.22: ~0.015
scikit-learn 0.23: ~0.15
import time
import sklearn.cluster
from sklearn import datasets
data = datasets.load_iris()['data']
t = time.time()
sklearn.cluster.KMeans(n_clusters=2).fit(data)
print(time.time() - t)
I also tried on a bigger dataset with shape (300, 25) where clustering with the new version needed 3-4s while before it happened in miliseconds.
Thanks for the report. Can you tell how many cores you have ?
Can you try setting OMP_NUM_THREADS=1 as an env var before launching python ? (I'm not proposing to do it permanently it's just to check if I'm on the right path)
Thank you for fast reaction. I have 4 cores.
I also tried to set OMP_NUM_THREADS=1 and nothing changed regarding the speed - it was same slow than before.
I checked out your PR and installed the package with pip install -e .. It slightly improves the speed but it still much slower than before than me. Did I make anything wrong?
Now times are:
scikit-lern 0.22: ~0.015 s
scikit-learn 0.23: ~0.15 s
scikit-learn #17210: ~0.11 s
I did cleaner measurements and indeed the proposed fix is still ~3x slower. Profiling showed that it's a new helper which takes 90% of the execution time. It's called at each iteration. I'll make a pr to move it outside of the iteration loop.
Out of curiosity, is performance critical for you for problems that take ~10ms to run ?
We use scikit as the Orange dependency (Orange is a graphical tool for data analysis) and on the other problem (data with size (300, 25)) clustering which took milliseconds before (0.1 - 0.5s) takes few seconds now. Most of Orange's users have data with smaller sizes and for them, it is quite a difference. I mean it is not the most critical issue, but if it is possible we would prefer that things would be computed faster.
I opened #17235. It's better but still not as fast as scikit-learn 0.22.
EDIT: the conclusions are wrong, please ignore.
The reason is the overhead of the deprecation of positional args in 0.23 as we can see in this profiling:
Using public functions which are now wrapped in the positional args deprecation context manager in tight loops may have a non negligible overhead. The solution would be to do someting like:
@_deprecate_positional...
def function(x):
return _function(x)
def _function(x):
do the job
and call _function internally. That would apply to all utilities like metrics, validation, ... I don't know if we want to dive into this to improve perf of millisecond problems. What do you think @adrinjalali@rth ?
I take back what I said. I misinterpreted the profile. The overhead of the decorator is actually the very small bars right under inner_f. The 3 large bars are the function itself (euclidean_distances, check_array and check_pairwise here).
As a confirmation, I tried adrin's suggestion and it did not improve
import sklearn.cluster
from sklearn import datasets
data = datasets.load_iris()['data']
# 10000 iteration to have a more reliable profile
km = sklearn.cluster.KMeans(n_clusters=2, max_iter=10000, tol=-1).fit(data)
%snakeviz km.fit(data)
At a glance, it looks like the other parts of inner_f are all the other wrapped functions such as check_pairwise_array and check_array, the overhead of _deprecate_positional_args itself is ~ms in total:
That being said I think we can speed things up here as well.
At a glance, it looks like the other parts of inner_f are all the other wrapped functions such as check_pairwise_array and check_array, the overhead of _deprecate_positional_args itself is ~ms in total
That's exactly what I said in the previous comment :D
Activity
jeremiedbb commentedon May 13, 2020
Thanks for the report. Can you tell how many cores you have ?
Can you try setting
OMP_NUM_THREADS=1as an env var before launching python ? (I'm not proposing to do it permanently it's just to check if I'm on the right path)PrimozGodec commentedon May 13, 2020
Thank you for fast reaction. I have 4 cores.
I also tried to set
OMP_NUM_THREADS=1and nothing changed regarding the speed - it was same slow than before.jeremiedbb commentedon May 14, 2020
I can reproduce the slowdown on my laptop and the changes I made in #17210 solve the issue for me. We are going to merge it and it will be available it the nightly builds. Here are the instructions to install it: https://scikit-learn.org/stable/developers/advanced_installation.html#installing-nightly-builds. We'd be very interested if you could check if it fixes your issue (you'd have to wait a day after we merge it)
PrimozGodec commentedon May 14, 2020
I checked out your PR and installed the package with
pip install -e .. It slightly improves the speed but it still much slower than before than me. Did I make anything wrong?Now times are:
scikit-lern 0.22: ~0.015 s
scikit-learn 0.23: ~0.15 s
scikit-learn #17210: ~0.11 s
jeremiedbb commentedon May 14, 2020
I did cleaner measurements and indeed the proposed fix is still ~3x slower. Profiling showed that it's a new helper which takes 90% of the execution time. It's called at each iteration. I'll make a pr to move it outside of the iteration loop.
Out of curiosity, is performance critical for you for problems that take ~10ms to run ?
PrimozGodec commentedon May 15, 2020
@jeremiedbb thank you. That would be great.
We use scikit as the Orange dependency (Orange is a graphical tool for data analysis) and on the other problem (data with size (300, 25)) clustering which took milliseconds before (0.1 - 0.5s) takes few seconds now. Most of Orange's users have data with smaller sizes and for them, it is quite a difference. I mean it is not the most critical issue, but if it is possible we would prefer that things would be computed faster.
jeremiedbb commentedon May 15, 2020
I opened #17235. It's better but still not as fast as scikit-learn 0.22.
EDIT: the conclusions are wrong, please ignore.
The reason is the overhead of the deprecation of positional args in 0.23 as we can see in this profiling:

Using public functions which are now wrapped in the positional args deprecation context manager in tight loops may have a non negligible overhead. The solution would be to do someting like:
and call
_functioninternally. That would apply to all utilities like metrics, validation, ... I don't know if we want to dive into this to improve perf of millisecond problems. What do you think @adrinjalali @rth ?adrinjalali commentedon May 15, 2020
Another alternative is to disable the wrapping with a context manager. For instance:
Would this improve the overhead?
WDYT @thomasjpfan?
thomasjpfan commentedon May 15, 2020
This could work. I have another idea I want to try out ;)
@jeremiedbb Can you provide the code snippet you used to profile?
jeremiedbb commentedon May 15, 2020
I take back what I said. I misinterpreted the profile. The overhead of the decorator is actually the very small bars right under
inner_f. The 3 large bars are the function itself (euclidean_distances, check_array and check_pairwise here).As a confirmation, I tried adrin's suggestion and it did not improve
@thomasjpfan here's what I use to profile:
in this specific case:
thomasjpfan commentedon May 15, 2020
At a glance, it looks like the other parts of
inner_fare all the other wrapped functions such ascheck_pairwise_arrayandcheck_array, the overhead of_deprecate_positional_argsitself is ~ms in total:That being said I think we can speed things up here as well.
jeremiedbb commentedon May 15, 2020
That's exactly what I said in the previous comment :D
jeremiedbb commentedon May 15, 2020
I think I managed to fix the issue now in #17235. @PrimozGodec would you mind checking this out to see if you recover the performances of 0.22 ?
PrimozGodec commentedon May 18, 2020
@jeremiedbb thank you for your help. I tested the PR and it works now normally.
jeremiedbb commentedon May 18, 2020
Thanks @PrimozGodec ! Closing.