Skip to content

sklearn.cluster.KMeans 0.23 is extra slower compared to 0.22.2 #17230

Closed

Description

@MichalRIcar

Used code:

from sklearn import cluster

for k in range(1,15):
     cluster.KMeans(
           n_clusters   = k,           
           random_state = 42,      
           n_init       = 10,
           max_iter     = 2000,
           algorithm    = 'full',
           init         = 'k-means++'   )

Expected Results

Computation in v0.22.2 was done in 2mins for whole set of explored 15 k

Actual Results

Computation takes more than 20min with exactly same data and setup as before
Also, computation even with k=1 takes very long time → compared to previous version lower k meant much faster computation

Versions

System:
python: 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]
executable: C:\Users\micha\anaconda3\python.exe
machine: Windows-10-10.0.18362-SP0

Python dependencies:
pip: 20.0.2
setuptools: 45.2.0.post20200210
sklearn: 0.23.0
numpy: 1.18.1
scipy: 1.4.1
Cython: 0.29.15
pandas: 1.0.3
matplotlib: 3.1.3
joblib: 0.14.1

Built with OpenMP: True

Activity

  1. jnothman commented on May 15, 2020

    @jnothman
    Member

    Please provide characteristics of your dataset, at least number of samples and features

  2. MichalRIcar commented on May 15, 2020

    @MichalRIcar
    Author
  3. jeremiedbb commented on May 15, 2020

    @jeremiedbb
    Member

    @MichalRIcar I can't reproduce your timings. I generated random data of shape (8950, 61) and on my laptop I have:
    0.22: 43s
    0.23: 32s

    #17235 should be an improvement. With this I get 11s. Could you try this ?

  4. MichalRIcar commented on May 15, 2020

    @MichalRIcar
    Author
  5. jeremiedbb commented on May 15, 2020

    @jeremiedbb
    Member

    It is but I can reproduce the timings reported in #17208 while I can't reproduce yours. That's why I'd be interested to see if the changes we propose to solve #17208 work for you as well.

  6. MichalRIcar commented on May 15, 2020

    @MichalRIcar
    Author
  7. jeremiedbb commented on May 15, 2020

    @jeremiedbb
    Member

    You can attach a text (.txt) file in a github comment.

  8. MichalRIcar commented on May 15, 2020

    @MichalRIcar
    Author
  9. beew commented on May 19, 2020

    @beew

    Same here. I ran an algorithm that uses kmeans on same data, scikit-learn0.22.0 took 21 minutes, 0.22.2 took 20 minutes, scikit-learn 0.23 took 45 minutes.

    Python 3.8.3 on Ubuntu Linux.

  10. jeremiedbb commented on May 19, 2020

    @jeremiedbb
    Member

    @beew, can you give more details, like the number of samples and features ?
    We made some changes a few days ago to improve performance. You can test it by installing the nightly build of scikit-learn:

    pip install --pre --extra-index https://pypi.anaconda.org/scipy-wheels-nightly/simple scikit-learn
    
  11. jeremiedbb commented on May 19, 2020

    @jeremiedbb
    Member

    Could you also paste the output of:

    import sklearn
    from threadpoolctl import threadpool_info
    
    sklearn.show_versions()
    print(threadpool_info())
    

    thanks.

  12. ogrisel commented on May 19, 2020

    @ogrisel
    Member

    You will need to pip install threadpoolctl to execute the above.

  13. jeremiedbb commented on May 19, 2020

    @jeremiedbb
    Member

    threadpoolctl is a dependency of scikit-learn so it's installed when you install scikit-learn

  14. ogrisel commented on May 19, 2020

    @ogrisel
    Member

    Indeed :)

  15. 7 remaining items

  16. MichalRIcar commented on May 25, 2020

    @MichalRIcar
    Author

    btw, I know you will not like this - to mix topics - but quick question, do you plan to move
    from sklearn.experimental import enable_iterative_imputer
    from sklearn.impute import IterativeImputer
    to standard API as now it warns regarding deprecation in 0.24

  17. ogrisel commented on May 25, 2020

    @ogrisel
    Member

    btw, I know you will not like this - to mix topics -

    Please open a dedicated issue for the IterativeImputer issue (if not is open already) along with the code that triggers the warning.

  18. ogrisel commented on May 25, 2020

    @ogrisel
    Member

    I updated all used packages today and newly (0.23.1) seems solved it and actually it is faster than (0.22.2). Thanks!!

    Ok so part of the issue was fixed in #17235. The remainder will be tackled in #17334. Let's close this.

  19. beew commented on May 26, 2020

    @beew

    Sorry, a bit late to the party. Something happened that needed attentions.

    Upgraded scikit-learn 0.23.1 it is indeed faster than 0.22.2.

    With MKL

    
    System:
        python: 3.8.2 (default, May  9 2020, 21:01:52)  [GCC 8.3.0]
    executable: /home/bernard/opt/python38/bin/python3.8
       machine: Linux-4.19.112-custom-x86_64-with-glibc2.2.5
    
    Python dependencies:
              pip: 20.1.1
       setuptools: 41.2.0
          sklearn: 0.23.1
            numpy: 1.18.4
            scipy: 1.4.1
           Cython: 0.29.17
           pandas: 1.0.3
       matplotlib: 3.2.1
           joblib: 0.14.1
    threadpoolctl: 2.0.0
    
    Built with OpenMP: True
    
    print(threadpool_info())
    [{'filepath': '/opt/intel/compilers_and_libraries_2018.2.199/linux/mkl/lib/intel64_lin/libmkl_rt.so',
     'prefix': 'libmkl_rt',
     'user_api': 'blas', 
    'internal_api': 'mkl',
     'version': '2018.0.2',
     'num_threads': 4, 
    'threading_layer': 'tbb'},
     {'filepath': '/home/bernard/opt/python38/lib/python3.8/site-packages/scikit_learn.libs/libgomp-3300acd3.so.1.0.0', 'prefix': 'libgomp', 'user_api': 'openmp', 'internal_api': 'openmp', 'version': None, 'num_threads': 8}]
    
    

    Switch to openblas with update-alternatives

    [{'filepath': '/opt/openblas-mpi/lib/libopenblasp-r0.3.9.so',
     'prefix': 'libopenblas', 
    'user_api': 'blas',
     'internal_api': 'openblas',
     'version': '0.3.9', 
    'num_threads': 8, 'threading_layer': 'openmp'},
     {'filepath': '/usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0',
     'prefix': 'libgomp', 'user_api': 'openmp', 
    'internal_api': 'openmp',
     'version': None, 'num_threads': 8},
     {'filepath': '/opt/intel/compilers_and_libraries_2018.2.199/linux/mkl/lib/intel64_lin/libmkl_rt.so',
     'prefix': 'libmkl_rt', 'user_api': 'blas', 'internal_api': 'mkl', 'version': '2018.0.2', 'num_threads': 4, 'threading_layer': 'tbb'}, 
    {'filepath': '/home/bernard/opt/python38/lib/python3.8/site-packages/scikit_learn.libs/libgomp-3300acd3.so.1.0.0', 'prefix': 'libgomp', 'user_api': 'openmp', 'internal_api': 'openmp', 'version': None, 'num_threads': 8}]
    

    I guess the mkl shows up even when update-alternatives switched to openblas because numpy was compiled with mkl.

  20. jeremiedbb commented on May 26, 2020

    @jeremiedbb
    Member

    scikit-learn does not use the system blas, but the blas shipped with scipy. So you can't switch blas with update alternatives. MKL is shipped when you install from the default channel of conda and openblas is shipped when you install from pip or from the conda-forge channel of conda

  21. beew commented on May 26, 2020

    @beew

    blas, but the blas shipped with scipy. So you can't switch blas with update alternatives. MKL is shipped when you install from the default channel of conda and openblas is shipped when you install from pip or from the conda-forge channel of conda

    I see, I compiled numpy and scipy from source with mkl. However numpy switches blas it links to with update alternatives at run time (I have checked and the speed was different as well), so what if scikit-learn uses numpy?

  22. jeremiedbb commented on May 26, 2020

    @jeremiedbb
    Member

    I read your comment too quickly, I did not understand that you compile numpy and scipy. In that case you should be able to switch blas with update_alternatives indeed.

    Scikit-learn does not ship any blas library so the output of threadpoolctl can only show the blas used by numpy and scipy. Can you reproduce that when only importing numpy or scipy ?

  23. beew commented on May 26, 2020

    @beew

    Sorry, reproduce what?

  24. jeremiedbb commented on May 26, 2020

    @jeremiedbb
    Member

    The call to threadpool_info, before and after you switch blas with update alternatives.

  25. ogrisel commented on May 27, 2020

    @ogrisel
    Member

    So you can't switch blas with update alternatives.

    It depends how scipy was installed. If it comes from the system then it will probably use the system BLAS.

  26. ogrisel commented on May 27, 2020

    @ogrisel
    Member

    And indeed threadpool_info() shows all the BLAS and OpenMP runtimes that have been dynamically linked into your running Python program at the time you call the threadpool_info() function. Those libraries are typically loaded when you import numpy and scipy packages (or Cython code built with OpenMP pragmas).

    So indeed if you built numpy from source against MKL, it's going to show up in the list.

    I you use the master branch of threadpoolctl you can use it from the command line to show the list of linked libs you get after importing a specific Python package:

    python -m threadpoolctl -i numpy
    
    python -m threadpoolctl -i scipy
    
    python -m threadpoolctl -i sklearn
    
  27. beew commented on May 29, 2020

    @beew

    for mkl

    >>> import numpy as np
    
    >>> np.__version__
     '1.18.4'
    >>> np.show_config()
    blas_mkl_info:
        libraries = ['mkl_rt', 'pthread']
        library_dirs = ['/opt/intel/mkl/lib/intel64/']
        define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
        include_dirs = ['/opt/intel/mkl/include']
    blas_opt_info:
        libraries = ['mkl_rt', 'pthread']
        library_dirs = ['/opt/intel/mkl/lib/intel64/']
        define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
        include_dirs = ['/opt/intel/mkl/include']
    lapack_mkl_info:
        libraries = ['mkl_rt', 'pthread']
        library_dirs = ['/opt/intel/mkl/lib/intel64/']
        define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
        include_dirs = ['/opt/intel/mkl/include']
    lapack_opt_info:
        libraries = ['mkl_rt', 'pthread']
        library_dirs = ['/opt/intel/mkl/lib/intel64/']
        define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
        include_dirs = ['/opt/intel/mkl/include']
    
    >>> from threadpoolctl import threadpool_info
    
    >>> print(threadpool_info())
    [{'filepath': '/opt/intel/compilers_and_libraries_2018.2.199/linux/mkl/lib/intel64_lin/libmkl_rt.so', 'prefix': 'libmkl_rt', 'user_api': 'blas', 'internal_api': 'mkl', 'version': '2018.0.2', 'num_threads': 4, 'threading_layer': 'tbb'}]
    

    update-alternatives to openblas

    >>> from threadpoolctl import threadpool_info
    [{'filepath': '/opt/openblas-mpi/lib/libopenblasp-r0.3.9.so', 'prefix': 'libopenblas', 'user_api': 'blas', 'internal_api': 'openblas', 'version': '0.3.9', 'num_threads': 8, 'threading_layer': 'openmp'}, {'filepath': '/usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0', 'prefix': 'libgomp', 'user_api': 'openmp', 'internal_api': 'openmp', 'version': None, 'num_threads': 8}, {'filepath': '/opt/intel/compilers_and_libraries_2018.2.199/linux/mkl/lib/intel64_lin/libmkl_rt.so', 'prefix': 'libmkl_rt', 'user_api': 'blas', 'internal_api': 'mkl', 'version': '2018.0.2', 'num_threads': 4, 'threading_layer': 'tbb'}]
    
  28. jeremiedbb commented on May 29, 2020

    @jeremiedbb
    Member

    I don't understand why mkl is still linked after the switch to openblas. Maybe there are more mkl libs than libmkl_rt to switch, idk. Anyway it's independent of scikit-learn, we could probably get more information by opening an issue to numpy directly.

  29. beew commented on May 29, 2020

    @beew

    I don't understand why mkl is still linked after the switch to openblas. Maybe there are more mkl libs than libmkl_rt to switch, idk. Anyway it's independent of scikit-learn, we could probably get more information by opening an issue to numpy directly.

    I think that is because numpy was compiled with mkl even when it is using openblas. I can see which one is being used with lsof -p | grep mkl or lsof -p | grep blas

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