Skip to content

Just importing numpy makes python process commit 3+GB of memory #19145

Open

Description

@Ark-kun

Related to vscode-restructuredtext/snooty-parser#24 and networkx/networkx#4857

image

I expect import numpy to commit close to 0 memory.

Reproducing code example:

import numpy as np

Error message:

  File "C:\Users\Ark\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\__init__.py", line 153, in <module>
    from . import ma
  File "C:\Users\Ark\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\ma\__init__.py", line 42, in <module>
    from . import core
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 724, in exec_module
  File "<frozen importlib._bootstrap_external>", line 818, in get_code
  File "<frozen importlib._bootstrap_external>", line 917, in get_data
MemoryError

NumPy/Python version information:

1.19.5 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]

Activity

  1. charris commented on May 31, 2021

    @charris
    Member

    How many cores do you have?

  2. charris commented on Jun 1, 2021

    @charris
    Member

    I ask because OpenBLAS pre-allocates memory for each possible thread. The amount should be smaller in 1.20.x, you can also limit the number of threads with the environmental variable OMP_NUM_THREADS=8 or a more general app like threadpoolctl.

  3. bashtage commented on Jun 1, 2021

    @bashtage
    Contributor

    Or you can use MKL provided by conda or another packager, which does not have this issue.

  4. Ark-kun commented on Jun 2, 2021

    @Ark-kun
    Author

    How many cores do you have?

    I have 32 cores. AMD Ryzen 5950x. Finally managed to get it after so many months.

    I ask because OpenBLAS pre-allocates memory for each possible thread.

    Interesting. The multiprocessing module makes this quadratic as it creates a new process per core.

    OpenBLAS pre-allocates memory for each possible thread.

    Do you think this should be done more lazily, not on import? Is this controlled by OpenBLAS or numpy?

  5. bashtage commented on Jun 2, 2021

    @bashtage
    Contributor

    Interesting. The multiprocessing module makes this quadratic as it creates a new process per core.

    When using multiprocessing I usually export OPENBLAS_NUM_THREADS=1 to both limit memory and to avoid multithreaded code from competing for CPU cores.

    Do you think this should be done more lazily, not on import? Is this controlled by OpenBLAS or numpy?

    It is done by OpenBLAS. They have tried hard to minimize this, so I doubt there is a lot for them to easily do.

  6. rgommers commented on Feb 18, 2022

    @rgommers
    Member

    This is probably useful to document better. We do mention using threadpoolctl to control threading in https://numpy.org/devdocs/reference/routines.linalg.html, however we should explain this behavior and mention also the differences between MKL and OpenBLAS, and the relevant environment variables like OPENBLAS_NUM_THREADS.

  7. mattip commented on Feb 18, 2022

    @mattip
    Member

    OpenBLAS is now more aware of machines with large numbers of cores. @Ark-kun could you try with the latest version and see if the problem still exists?

  8. jakakonda commented on Sep 6, 2023

    @jakakonda

    The issue is still present I do not believe it's any better. I Initially reported as #24650 as a dup of this.

    The commit memory (not allocated) still jumps from few MB to 4.3 GB (PC has 6 physical cores, 12 threads). Another 4/8, It is reproducible on multiple machines (different Win10 versions on Intel and AMD). Based on the experimenting with env variable each threads commits approximately 350 MB of memory.

    This doesn't happen with numpy version from pip nor MKL conda-forge package.
    If I simply dump pip package it in the same env (not healthy but in light of testing) the issue is resolved. But other libraries can cause the same issue (tested only with Spyder).

    Reproduce the code example:

    Win10 x64, different version and conda using used latest, 3 years old and something in between.
    Tested on Intel and AMD machines.

    # Both numpy and python latest
    conda create -n nptest python=3.11.4
    conda activate nptest # activate new env
    conda install conda-forge::numpy=1.25.2 "blas=*=openblas"
    import numpy as np

    Commit memory increased by 4 G.

    ### Runtime information:
    
    [{'numpy_version': '1.25.2',
      'python': '3.11.4 | packaged by Anaconda, Inc. | (main, Jul  5 2023, '
                '13:47:18) [MSC v.1916 64 bit (AMD64)]',
      'uname': uname_result(system='Windows', node='TORNADO', release='10', version='10.0.19045', machine='AMD64')},
     {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                          'found': ['SSSE3',
                                    'SSE41',
                                    'POPCNT',
                                    'SSE42',
                                    'AVX',
                                    'F16C',
                                    'FMA3',
                                    'AVX2'],
                          'not_found': ['AVX512F',
                                        'AVX512CD',
                                        'AVX512_SKX',
                                        'AVX512_CLX',
                                        'AVX512_CNL',
                                        'AVX512_ICL']}}]
    
    # packages in environment
    #
    # Name                    Version                   Build  Channel
    blas                      2.118                  openblas    conda-forge
    blas-devel                3.9.0           18_win64_openblas    conda-forge
    bzip2                     1.0.8                he774522_0
    ca-certificates           2023.7.22            h56e8100_0    conda-forge
    libblas                   3.9.0           18_win64_openblas    conda-forge
    libcblas                  3.9.0           18_win64_openblas    conda-forge
    libffi                    3.4.4                hd77b12b_0
    libflang                  5.0.0           h6538335_20180525    conda-forge
    liblapack                 3.9.0           18_win64_openblas    conda-forge
    liblapacke                3.9.0           18_win64_openblas    conda-forge
    libopenblas               0.3.24          pthreads_hc140b1d_0    conda-forge
    llvm-meta                 5.0.0                         0    conda-forge
    m2w64-gcc-libgfortran     5.3.0                         6    conda-forge
    m2w64-gcc-libs            5.3.0                         7    conda-forge
    m2w64-gcc-libs-core       5.3.0                         7    conda-forge
    m2w64-gmp                 6.1.0                         2    conda-forge
    m2w64-libwinpthread-git   5.0.0.4634.697f757               2    conda-forge
    msys2-conda-epoch         20160418                      1    conda-forge
    numpy                     1.25.2          py311h0b4df5a_0    conda-forge
    openblas                  0.3.24          pthreads_h3721920_0    conda-forge
    openmp                    5.0.0                    vc14_1    conda-forge
    openssl                   3.1.2                hcfcfb64_0    conda-forge
    pip                       23.2.1          py311haa95532_0
    python                    3.11.4               he1021f5_0
    python_abi                3.11                    2_cp311    conda-forge
    setuptools                68.0.0          py311haa95532_0
    sqlite                    3.41.2               h2bbff1b_0
    tk                        8.6.12               h2bbff1b_0
    tzdata                    2023c                h04d1e81_0
    ucrt                      10.0.22621.0         h57928b3_0    conda-forge
    vc                        14.2                 h21ff451_1
    vc14_runtime              14.36.32532         hfdfe4a8_17    conda-forge
    vs2015_runtime            14.36.32532         h05e6639_17    conda-forge
    wheel                     0.38.4          py311haa95532_0
    xz                        5.4.2                h8cc25b3_0
    zlib                      1.2.13               h8cc25b3_0
    
  9. mattip commented on Sep 10, 2023

    @mattip
    Member

    Looking at the build flags in the conda-forge feedstock, I see they do not define BUFFERSIZE, where the OpenBLAS used by the pip wheel does define BUFFERSIZE=20. Also, the conda-forge package uses NUM_THREADS=128 vs. NUM_THREADS=24 on windows. But as I understand things, that is a maximum and actual number of theads is based on the number of detected CPUs. Other than that, I don't see a reason why the pip-installed wheel would be different from the conda-forge supplied binary package.

  10. jakakonda commented on Sep 11, 2023

    @jakakonda

    OPENBLAS_NUM_THREADS=1 greatly reduces commit memory and commit varied on different systems (with different number of CPU cores) and it would confirm assumption that the flag doesn't have effect.

    Is there anything I can do to help you pinpoint the issue?
    Another note, if I replaced conda version with pip version. import numpy worked fine. But if I opened Spyder the commit memory still went way up despite the pip version, so there must be something else that triggers this commit.
    I haven't tried Spyder from pip - will try it and let know the results.

  11. jakakonda commented on Sep 11, 2023

    @jakakonda

    With everything installed from pip, the environment behaves as expected, commits several factors less memory than conda-forge version without any env variables. The issue most likely lies in the conda-forge openblas package, but don't know of a way of confirming this.

  12. WoolenWang commented on Oct 26, 2023

    @WoolenWang

    why we don't add a auto env setup(OPENBLAS_NUM_THREADS=1) when we load OPENBLAS, give user an api to increase the threads num after the import?

  13. rgommers commented on Oct 26, 2023

    @rgommers
    Member

    No, we cannot do that. That will be a very large performance regression for the average user, and quite cumbersome to have to do. This memory consumption problems, which affects a way smaller group of users, does not justify that.

  14. bashtage commented on Oct 26, 2023

    @bashtage
    Contributor

    I am also not sure it is accurate any more (at least for Windows). Latest NumPy, nothing else loaded

    image

    and for reference, just Python

    image

    Edit: This is pip, so likely not a problem reading backward.

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