Skip to content

KMeans singnificantly slower on 0.23 #17208

Closed

Description

@PrimozGodec

Describe the bug

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.

Expected Results

Clusters would be computed as fast as before.

Versions

System:
    python: 3.7.6 | packaged by conda-forge | (default, Jan  7 2020, 22:05:27)  [Clang 9.0.1 ]
executable: /Users/primoz/miniconda3/envs/orange/bin/python
   machine: Darwin-19.0.0-x86_64-i386-64bit
Python dependencies:
       pip: 20.1
setuptools: 46.1.3
   sklearn: 0.23.0
     numpy: 1.18.4
     scipy: 1.4.1
    Cython: None
    pandas: 1.0.3
matplotlib: 3.2.1
    joblib: 0.14.1
Built with OpenMP: True

Activity

  1. jeremiedbb commented on May 13, 2020

    @jeremiedbb
    Member

    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)

  2. PrimozGodec commented on May 13, 2020

    @PrimozGodec
    Author

    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.

  3. jeremiedbb commented on May 14, 2020

    @jeremiedbb
    Member

    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)

  4. PrimozGodec commented on May 14, 2020

    @PrimozGodec
    Author

    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

  5. jeremiedbb commented on May 14, 2020

    @jeremiedbb
    Member

    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 ?

  6. PrimozGodec commented on May 15, 2020

    @PrimozGodec
    Author

    @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.

  7. jeremiedbb commented on May 15, 2020

    @jeremiedbb
    Member

    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:
    prof2

    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 ?

  8. adrinjalali commented on May 15, 2020

    @adrinjalali
    Member

    Another alternative is to disable the wrapping with a context manager. For instance:

    def _deprecate_positional_args(f):
        if not get_config(wrap_positional):
            return f
        ...
    
    with config_context(wrap_positional=False):
        public_method(...)

    Would this improve the overhead?

    WDYT @thomasjpfan?

  9. thomasjpfan commented on May 15, 2020

    @thomasjpfan
    Member

    WDYT @thomasjpfan?

    This could work. I have another idea I want to try out ;)

    @jeremiedbb Can you provide the code snippet you used to profile?

  10. jeremiedbb commented on May 15, 2020

    @jeremiedbb
    Member

    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:

    pip install snakeviz
    
    ipython
    > %load_ext snakeviz
    > %snakeviz my_func(x)
    

    in this specific case:

    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)
    
  11. thomasjpfan commented on May 15, 2020

    @thomasjpfan
    Member

    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:

    82044449-acc01700-96ad-11ea-9c6e-f6ff2f1f922e

    That being said I think we can speed things up here as well.

  12. jeremiedbb commented on May 15, 2020

    @jeremiedbb
    Member

    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

  13. jeremiedbb commented on May 15, 2020

    @jeremiedbb
    Member

    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 ?

  14. PrimozGodec commented on May 18, 2020

    @PrimozGodec
    Author

    @jeremiedbb thank you for your help. I tested the PR and it works now normally.

  15. jeremiedbb commented on May 18, 2020

    @jeremiedbb
    Member

    Thanks @PrimozGodec ! Closing.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions