Skip to main content
 首页 » 编程设计

python之在Theano中启用CNMeM但 'cuDNN not available'有什么影响

2025年04月02日11qq号

我有以下基于 Theano example 的代码:

from theano import function, config, shared, sandbox 
import theano.tensor as T 
import numpy 
import time 
 
vlen = 10 * 30 * 768  # 10 x #cores x # threads per core 
iters = 1000 
 
rng = numpy.random.RandomState(22) 
x = shared(numpy.asarray(rng.rand(vlen), config.floatX)) 
f = function([], T.exp(x)) 
print(f.maker.fgraph.toposort()) 
t0 = time.time() 
for i in range(iters): 
    r = f() 
t1 = time.time() 
print("Looping %d times took %f seconds" % (iters, t1 - t0)) 
print("Result is %s" % (r,)) 
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]): 
    print('Used the cpu') 
else: 
    print('Used the gpu') 

现在当我用两种模式测试代码时:

GPU 模式,我明白了:

$ THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python gpu.py 
Using gpu device 0: Tesla C2075 (CNMeM is enabled with initial size: 95.0% of memory, cuDNN not available) 
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)] 
Looping 1000 times took 0.475526 seconds 
Result is [ 1.23178029  1.61879349  1.52278066 ...,  2.20771813  2.29967761 
  1.62323296] 
Used the gpu 

CPU 模式,我明白了:

$ THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python gpu.py 
[Elemwise{exp,no_inplace}(<TensorType(float32, vector)>)] 
Looping 1000 times took 5.221368 seconds 
Result is [ 1.23178029  1.61879337  1.52278066 ...,  2.20771813  2.29967761 
  1.62323284] 
Used the cpu 

注意两件事,GPU 确实比 CPU 快(0.47 秒对 5 秒)。但同时在 GPU 上我收到 cuDNN 不可用消息。

我的问题是这样的。没有 cuDNN 有什么影响?有害吗?

请您参考如下方法:

如果您没有使用 cuDNN ,您的代码就不会使用 GPU 的所有功能。 GPU 先于 CPU 的好处是 GPU 有很多真正的核心(从 700 到 4000),普通 CPU 从 1 到 8。

但 GPU 内核只能进行原始计算。如果你不使用 cuDNN ,其他标准库进行计算,或者可能(我不完全知道只使用 GPU 内存并使用简单的 CPU 进行计算)。

CuDNN 是一个 GPU 加速的基元库。 这意味着如果您开始制作深度神经网络应用程序,它可能不会那么快。

请阅读CuDNN

注意:因为我写的GPU核心只能进行原始计算,如果你选择使用GPU,但是使用了GPU不支持的功能,theano会暂时将应用程序切换到CPU。(需要时间来制作它)