代码之家  ›  专栏  ›  技术社区  ›  Zach Dwiel

如何计算Theano中的GPU内存使用量?

  •  6
  • Zach Dwiel  · 技术社区  · 9 年前

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

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

    2 回复  |  直到 9 年前
        1
  •  7
  •   Community CDub    7 年前

    假设您知道要存储在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内存。但是,请注意 memory fragmentation 可能导致的问题 MemoryError 意外地

        2
  •  2
  •   Will Brian Hodge    9 年前

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

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

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