GPU Memory Usage占满而GPU-Util却为0的调试

最近使用github上的一个开源项目训练基于CNN的翻译模型,使用THEANO_FLAGS='floatX=float32,device=gpu2,lib.cnmem=1' python run_nnet.py -w data/exp1/,运行时报错,打印"The image and the kernel must have the same type. inputs(float64), kerns(float32)"的错误,然后使用THEANO_FLAGS='floatX=float64,device=gpu2,lib.cnmem=1' python run_nnet.py -w data/exp1/,运行成功。但几百个训练数据却需要十几分钟,运行十分缓慢。

使用nvidia-smi -l查看GPU情况,发现GPU memory usage 是满了,而GPU-Util却是0,top命令看CPU却是1600%(16核CPU),这与跑其他任务很不相同(GPU-Util接近100%,CPU不到100%)。看起来是CPU被打满了,而GPU空着,运算完全在CPU上进行。查找原因,Google这个问题,却没有找到什么满足需求的解答,只好回过头来阅读官方文档。

首先使用assert_no_cpu_op=raise:

THEANO_FLAGS="floatX=float64,device=gpu2,force_device=True,mode=FAST_RUN,lib.cnmem=1,assert_no_cpu_op=raise" python run_nnet.py

按照官方文档,如果设置了这个参数,有在CPU上执行的操作,是应该抛异常的。然而实际情况并没有。

仔细阅读官方文档,发现在theano的FAQ文档(refer: http://deeplearning.net/software/theano/faq.html)中说:

“It should be noted that using float32 and int{32, 64} together inside a function would provide float64 as output.

Since the GPU can’t compute this kind of output, it would be preferable not to use those dtypes together.

To help you find where float64 are created, see the warn_float64 Theano flag.”

也就是float64的话,GPU是不能计算的,所以就是CPU计算。进一步的,在使用GPU的文档中(refer: http://deeplearning.net/software/theano/tutorial/using_gpu.html):

"

  • Only computations with float32 data-type can be accelerated. Better support for float64 is expected in upcoming hardware but float64 computations are still relatively slow (Jan 2010).
  • Prefer constructors like matrixvector and scalar to dmatrixdvector and dscalar because the former will give you float32 variables when floatX=float32.
  • Ensure that your output variables have a float32 dtype and not float64. The more float32 variables are in your graph, the more work the GPU can do for you."

所以原因就是代码中有float64的输入。根据文档中建议,可以使用config的warn_float64来帮助寻找float64的输入,所以执行:

THEANO_FLAGS="floatX=float64,device=gpu2,force_device=True,mode=FAST_RUN,lib.cnmem=1,warn_float64=raise" python run_nnet.py -w data/exp1/ 

异常栈如下:

Traceback (most recent call last):
File "run_nnet.py", line 570, in <module>
main()
File "run_nnet.py", line 208, in main
nnet_q.set_input((x_q, x_q_overlap))
File ".../nn_layers.py", line 65, in set_input
self.output = self.output_func(input)
File ".../nn_layers.py", line 89, in output_func
layer.set_input(cur_input)

这个栈信息之反映了在网络set_input的时候有float64,但是float64的变量可是在此之前早就创建好的,所以还是无法定位到问题,这是一个然并卵的参数。

由于代码中的输入几乎都是由numpy生成或者load的,查阅numpy的文档,发现numpy建立数组的操作,如果没有指定dtype,那么默认就是float64,例如numpy.ones, numpy.zero, numpy.random.RandomState.randn等,theano的config(refer: http://deeplearning.net/software/theano/library/config.html)中有一个cast_policy参数,按照文档的说法,当设定floatX=float32,同时设置cast_policy=numpy+floatX时,执行过程中会自动的把numpy产生的数组转换成float32的。于是执行:

THEANO_FLAGS="floatX=float32,device=gpu2,force_device=True,mode=FAST_RUN,lib.cnmem=1,cast_policy=numpy+floatX" python run_nnet.py -w data/exp1/  

结果。。。依然报错:"NotImplementedError: The image and the kernel must have the same type.inputs(float64), kerns(float32)"

这个参数的说明:

" Note that ‘numpy+floatX’ is not currently behaving exactly as planned (it is a work-in-progress), and thus you should consider it as experimental. "

好吧,果然还是实验性质的,有些情况搞不定。

那么最后一招,仔细的检查所有numpy的调用,把所有创建数组的地方都显示的指定dtype=numpy.float32,全部改好后,执行:

THEANO_FLAGS="floatX=float32,device=gpu2,force_device=True,mode=FAST_RUN,lib.cnmem=1" python run_nnet.py -w data/exp1/

成功的把GPU-Util打满,CPU也降到了100%,训练几百条数据的时间一下降到秒杀!

经验总结:

遇到问题阅读官方文档是十分有效的方法,往往常见问题在这些文档中已经说得很明确了,可以帮你明确的了解问题所在,进而找到解决方案。