我正在尝试使用内存映射模式在 cupy 中加载一些较大的 .npy 文件,但我一直在运行OutOfMemoryError
.
我认为由于它是在内存映射模式下打开的,因此此操作不应该占用太多内存,因为内存映射实际上并未将整个数组加载到内存中。
我可以用 np.load 加载这些文件就好了,这似乎只发生在 cupy.load 上。我的环境是带有 Tesla K80 GPU 的 Google Colab。它有大约 12 gigs CPU ram、12 gigs GPU ram 和 350 gb 磁盘空间。
这是重现错误的最小示例:
import os
import numpy as np
import cupy
#Create .npy files.
for i in range(4):
numpyMemmap = np.memmap( 'reg.memmap'+str(i), dtype='float32', mode='w+', shape=( 10000000 , 128 ))
np.save( 'reg.memmap'+str(i) , numpyMemmap )
del numpyMemmap
os.remove( 'reg.memmap'+str(i) )
# Check if they load correctly with np.load.
NPYmemmap = []
for i in range(4):
NPYmemmap.append( np.load( 'reg.memmap'+str(i)+'.npy' , mmap_mode = 'r+' ) )
del NPYmemmap
# Eventually results in memory error.
CPYmemmap = []
for i in range(4):
print(i)
CPYmemmap.append( cupy.load( 'reg.memmap'+str(i)+'.npy' , mmap_mode = 'r+' ) )
输出:
0
1
/usr/local/lib/python3.6/dist-packages/cupy/creation/from_data.py:41: UserWarning: Using synchronous transfer as pinned memory (5120000000 bytes) could not be allocated. This generally occurs because of insufficient host memory. The original error was: cudaErrorMemoryAllocation: out of memory
return core.array(obj, dtype, copy, order, subok, ndmin)
2
3
---------------------------------------------------------------------------
OutOfMemoryError Traceback (most recent call last)
<ipython-input-4-b5c849e2adba> in <module>()
2 for i in range(4):
3 print(i)
----> 4 CPYmemmap.append( cupy.load( 'reg.memmap'+str(i)+'.npy' , mmap_mode = 'r+' ) )
1 frames
/usr/local/lib/python3.6/dist-packages/cupy/io/npz.py in load(file, mmap_mode)
47 obj = numpy.load(file, mmap_mode)
48 if isinstance(obj, numpy.ndarray):
---> 49 return cupy.array(obj)
50 elif isinstance(obj, numpy.lib.npyio.NpzFile):
51 return NpzFile(obj)
/usr/local/lib/python3.6/dist-packages/cupy/creation/from_data.py in array(obj, dtype, copy, order, subok, ndmin)
39
40 """
---> 41 return core.array(obj, dtype, copy, order, subok, ndmin)
42
43
cupy/core/core.pyx in cupy.core.core.array()
cupy/core/core.pyx in cupy.core.core.array()
cupy/core/core.pyx in cupy.core.core.ndarray.__init__()
cupy/cuda/memory.pyx in cupy.cuda.memory.alloc()
cupy/cuda/memory.pyx in cupy.cuda.memory.MemoryPool.malloc()
cupy/cuda/memory.pyx in cupy.cuda.memory.MemoryPool.malloc()
cupy/cuda/memory.pyx in cupy.cuda.memory.SingleDeviceMemoryPool.malloc()
cupy/cuda/memory.pyx in cupy.cuda.memory.SingleDeviceMemoryPool._malloc()
cupy/cuda/memory.pyx in cupy.cuda.memory._try_malloc()
OutOfMemoryError: out of memory to allocate 5120000000 bytes (total 20480000000 bytes)
我还想知道这是否可能与 Google Colab 及其环境/GPU 有关。
为方便起见,这里是这个最小代码的 Google Colab 笔记本
https://colab.research.google.com/drive/12uPL-ZnKhGTJifZGVdTN7e8qBRRus4tA