如何在Theano中计算GPU内存使用率



我正在试验不同的Theano模型,并使用不断增加序列长度的课程。我如何提前预测任何给定序列长度和模型的批量大小,以填充GPU的内存?

更糟糕的是,如果我不小心使用了太多内存,我会收到MemoryError,GPU上的内存没有释放,这需要我重新启动进程以释放内存,并丢失我的网络,然后再尝试新的批量大小。因为这个错误是不可恢复的,所以很难只增加批处理大小,直到出现异常,然后再回退。

假设您知道要存储在GPU上的元素数量,则可以轻松计算存储这些元素所需的内存量。

一个简单的例子:

import numpy as np
import theano.tensor as T
T.config.floatX = 'float32'
dataPoints = np.random.random((5000, 256 * 256)).astype(T.config.floatX)
#float32 data type requires 4 bytes
sizeinGBs = 5000 * 256 * 256 * 4 / 1024. / 1024 / 1024 + (some small over-head constant)
print "Data will need %2f GBs of free memory" % sizeInGB

假设头顶常数为0,则将打印:

>>> Data will need 1.22 GBs of free memory

如果您正在使用NVIDIA显卡,并且在机器上安装了CUDA,那么您可以使用以下代码轻松获得GPU上的可用内存总量:

import theano.sandbox.cuda.basic_ops as sbcuda
import numpy as np
import theano.tensor as T
T.config.floatX = 'float32'
GPUFreeMemoryInBytes = sbcuda.cuda_ndarray.cuda_ndarray.mem_info()[0]
freeGPUMemInGBs = GPUFreeMemoryInBytes/1024./1024/1024
print "Your GPU has %s GBs of free memory" % str(freeGPUMemInGBs)
#An operation is to be executed below
testData = shared(np.random.random((5000, 256 * 256)).astype(T.config.floatX), borrow = True)
print "The tasks above used %s GBs of your GPU memory. The available memory is %s GBs" % (str(freeGPUMemInGBs - GPUFreeMemoryInBytes/1024./1024/1024), str(GPUFreeMemoryInBytes/1024./1024/1024))

然后输出为以下格式(对于我的机器):

>>> Your GPU has 11.2557678223 GBs of free memory
>>> The tasks above used 1.22077941895 GBs of your GPU memory. The available memory is 10.0349884033 GBs

通过监控可用内存量并计算模型/数据的大小,您可以更好地使用GPU内存。但是,请注意内存碎片问题,因为它可能会意外导致MemoryError

Theano似乎没有任何内置的方法来估计模型的内存大小。你最好的选择是创建一个已知大小的模型子集,并使用Theano手册中描述的内存估计技术。

我们还需要考虑我们的对象在GPU中是如何表示的(例如,我们是使用float32还是float64,以及每个对象在GPU内占用了多少字节)。

一旦你可以估计一个小模型的大小,你就可以以合理的精度将这些估计投影到一个大得多的模型的大小。您应该能够编写自己的内存估计函数,该函数可以将许多特征和观测值、张量或图节点作为参数,并返回内存使用量。

最新更新