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
{{ message }}
Repository navigation
Just importing numpy makes python process commit 3+GB of memory #19145
I expect import numpy to commit close to 0 memory.
Reproducing code example:
importnumpyasnp
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
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.
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?
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"
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.
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.
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.
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.
Activity
charris commentedon May 31, 2021
How many cores do you have?
charris commentedon Jun 1, 2021
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=8or a more general app like threadpoolctl.bashtage commentedon Jun 1, 2021
Or you can use MKL provided by conda or another packager, which does not have this issue.
Ark-kun commentedon Jun 2, 2021
I have 32 cores. AMD Ryzen 5950x. Finally managed to get it after so many months.
Interesting. The
multiprocessingmodule makes this quadratic as it creates a new process per core.Do you think this should be done more lazily, not on
import? Is this controlled by OpenBLAS or numpy?bashtage commentedon Jun 2, 2021
When using multiprocessing I usually
export OPENBLAS_NUM_THREADS=1to both limit memory and to avoid multithreaded code from competing for CPU cores.It is done by OpenBLAS. They have tried hard to minimize this, so I doubt there is a lot for them to easily do.
rgommers commentedon Feb 18, 2022
This is probably useful to document better. We do mention using
threadpoolctlto 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 likeOPENBLAS_NUM_THREADS.mattip commentedon Feb 18, 2022
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?
jakakonda commentedon Sep 6, 2023
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.
Commit memory increased by 4 G.
mattip commentedon Sep 10, 2023
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 usesNUM_THREADS=128vs.NUM_THREADS=24on 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.jakakonda commentedon Sep 11, 2023
OPENBLAS_NUM_THREADS=1greatly 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 numpyworked 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.
jakakonda commentedon Sep 11, 2023
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.
WoolenWang commentedon Oct 26, 2023
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?
rgommers commentedon Oct 26, 2023
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.
bashtage commentedon Oct 26, 2023
I am also not sure it is accurate any more (at least for Windows). Latest NumPy, nothing else loaded
and for reference, just Python
Edit: This is pip, so likely not a problem reading backward.
Fix high committed memory
Fix high committed memory