Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用cupy时内存不足_Python_Cupy - Fatal编程技术网

Python 使用cupy时内存不足

Python 使用cupy时内存不足,python,cupy,Python,Cupy,当我使用cupy处理一些大型阵列时,内存不足错误会出现,但当我检查nvidia smi查看内存使用情况时,它没有达到我的GPU内存限制,我使用的是nvidia geforce RTX 2060,GPU内存为6 GB,下面是我的代码: import cupy as cp mempool = cp.get_default_memory_pool() print(mempool.used_bytes()) # 0 print(mempool.total_bytes())

当我使用cupy处理一些大型阵列时,内存不足错误会出现,但当我检查nvidia smi查看内存使用情况时,它没有达到我的GPU内存限制,我使用的是nvidia geforce RTX 2060,GPU内存为6 GB,下面是我的代码:

import cupy as cp

mempool = cp.get_default_memory_pool()
print(mempool.used_bytes())              # 0
print(mempool.total_bytes())             # 0

a = cp.random.randint(0, 256, (10980, 10980)).astype(cp.uint8)
a = a.ravel()
print(a.nbytes)                          # 120560400
print(mempool.used_bytes())              # 120560640
print(mempool.total_bytes())             # 602803712
# when I finish create this array, the nvidia-smi shows like this
#+-----------------------------------------------------------------------------+
 | NVIDIA-SMI 430.86       Driver Version: 430.86       CUDA Version: 10.2     |
 |-------------------------------+----------------------+----------------------+
 | GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
 | Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
 |===============================+======================+======================|
 |   0  GeForce RTX 2060   WDDM  | 00000000:01:00.0  On |                  N/A |
 | N/A   46C    P8     9W /  N/A |   1280MiB /  6144MiB |      1%      Default |
 +-------------------------------+----------------------+----------------------+

# but then I run this command, and error cames out
s_values, s_idx, s_counts = cp.unique(
    a, return_inverse=True, return_counts=True)
# and the error shows
# cupy.cuda.memory.OutOfMemoryError: out of memory to allocate 964483584 bytes (total 5545867264 bytes)
# the nvidia-smi shows
# +-----------------------------------------------------------------------------+
  | NVIDIA-SMI 430.86       Driver Version: 430.86       CUDA Version: 10.2     |
  |-------------------------------+----------------------+----------------------+
  | GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
  | Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
  |===============================+======================+======================|
  |   0  GeForce RTX 2060   WDDM  | 00000000:01:00.0  On |                  N/A |
  | N/A   45C    P8     9W /  N/A |   5075MiB /  6144MiB |      3%      Default |
  +-------------------------------+----------------------+----------------------+

似乎有足够的空间可供使用,为什么会发生此错误,这是因为我的GPU没有足够的内存,还是因为我的代码错误,或者我没有正确分配内存。

964483584不是比您的
内存池大。602803712的总字节数()


正如评论中所说,你可以分批完成,而不是一次完成整个计算。

这是否意味着我需要一个内存超过9GB的gpu?@ZhouXF似乎是这样,除非你可以分批完成。您也可以参考:以获取一些参考,但我认为这不会有多大帮助。我将拆分阵列并成批处理它,谢谢you@ycx@欢迎,如果答案有助于你解决问题,一定要接受