Skip to content

BLAS memory allocation error in KMeans & kNN & DBSCAN #20539

Closed

Description

@OnlyDeniko

Describe the bug

I have the following error:
BLAS : Program is Terminated. Because you tried to allocate too many memory regions.
When launch code:
KMeans

from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans

X, _ = make_blobs(n_samples=1000000, n_features=20, centers=1000)
model = KMeans(n_clusters=1000, max_iter=50, tol=0.0, n_init=1).fit(X)
model.predict(X)

KNN:

from sklearn.datasets import make_classification
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
import numpy as np

X, y = make_classification(n_samples=20000, n_features=50)

model = KNeighborsClassifier(algorithm='brute', metric='euclidean', n_jobs=-1).fit(X, y)
model.predict(X)

DBSCAN:

from sklearn.datasets import make_blobs
from sklearn.cluster import DBSCAN

X, _ = make_blobs(n_samples=500000, n_features=50, centers=100)

model = DBSCAN(eps=10., n_jobs=-1, min_samples=5, metric='euclidean', algorithm='auto').fit(X)
model.predict(X)

Steps/Code to Reproduce

Environment:

conda create -n bench -c conda-forge python=3.8
conda activate bench
pip install scikit-learn numpy pandas

Platform:

c5.24xlarge AWS EC2 Instance or c5a.24xlarge AWS EC2 Instance
Ubuntu 18.04.5 LTS (Bionic Beaver)

Expected Results

Nothing

Actual Results

BLAS : Program is Terminated. Because you tried to allocate too many memory regions.

Versions

scikit-learn == 0.24.2
numpy == 1.21.0
pandas == 1.3.0
scipy == 1.7.0

Activity

  1. jeremiedbb commented on Jul 16, 2021

    @jeremiedbb
    Member

    Do you have a CPU with more than 64 (or 88 I'm not sure) cores ?

  2. jeremiedbb commented on Jul 16, 2021

    @jeremiedbb
    Member

    I looked and the c5.24xlarge model has 96 cores. As far as I know OpenBLAS can't handle 96 cores. I suggest you set the environment variable OPENBLAS_NUM_THREADS=64.

  3. OnlyDeniko commented on Jul 16, 2021

    @OnlyDeniko
    Author
    export OPENBLAS_NUM_THREADS=64
    

    It is doesn't help

  4. OnlyDeniko commented on Jul 16, 2021

    @OnlyDeniko
    Author

    What changes when I download scikit-learn from the conda-forge channel, for example?

  5. jeremiedbb commented on Jul 19, 2021

    @jeremiedbb
    Member

    Hum then I don't know what to do. Anyway it's not a scikit-learn issue but an OpenBLAS issue. You should file an issue there https://github.com/xianyi/OpenBLAS, they'll be more helpfull than us.

  6. OnlyDeniko commented on Jul 22, 2021

    @OnlyDeniko
    Author

    Could you join the conversation? there are questions about scikit-learn, the answers to which I do not know
    OpenMathLib/OpenBLAS#3321 (comment)

  7. ogrisel commented on Jul 28, 2021

    @ogrisel
    Member

    This will be fixed by MacPython/openblas-libs#64. In the mean time the best workarounds on machines with more than 64 CPUS are:

    • to install numpy and scipy from conda-forge instead of PyPI
    • to use n_jobs=64 instead of n_jobs=-1 or and/set the OMP_NUM_THREADS=64 environment variable depending on the estimator.
  8. ogrisel commented on Aug 20, 2021

    @ogrisel
    Member

    Reopening this because increasing NUM_THREADS=128 is not necessarily the best solution either (see the discussion in MacPython/openblas-libs#64).

    We could workaround this problem in scikit-learn by limiting the number of threads used in the problematic estimators by parsing the MAX_THREADS value in the openblas build config when openblas is used:

    >>> import numpy
    >>> import threadpoolctl
    >>> for ob_module in threadpoolctl._ThreadpoolInfo(user_api="blas").get_modules("internal_api", "openblas").modules:
    ...     print(ob_module._dynlib.openblas_get_config().decode())
    ... 
    OpenBLAS 0.3.12 DYNAMIC_ARCH NO_AFFINITY USE_OPENMP armv8 MAX_THREADS=128

    Credits: OpenMathLib/OpenBLAS#3330 (comment)

    This is a bit hackish and will probably brittle to maintain though...

  9. ogrisel commented on Nov 18, 2021

    @ogrisel
    Member

    This will probably be fixed once the scipy wheel ships OpenBLAS 0.3.18 that includes OpenMathLib/OpenBLAS#3352. Leaving this scikit-learn issue open in the mean time.

  10. thomasjpfan commented on Apr 29, 2022

    @thomasjpfan
    Member

    For reference: SciPy is configured to build with OpenBLAS 0.3.18 on their main branch: https://github.com/scipy/scipy/blob/b74c192aab55422d52589cf0023d9b3bfb5d136b/tools/openblas_support.py#L15 This means OpenBLAS 0.3.18 should at least be shipped with the next major version of SciPy (1.9).

  11. moved this from Todo📬 to Future🤖 in Quansight's scikit-learn Project Boardon May 6, 2022
  12. lesteve commented on Jun 19, 2024

    @lesteve
    Member

    Let's close this one following since this should be fixed in scipy 1.9. We can always reopen if I missed something.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions